All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m22s
- 移动 17 个 DTO 文件到 internal/model/dto/ 目录 - 更新所有 DTO 文件的 package 声明从 model 改为 dto - 更新所有引用文件的 import 和类型引用 - Handler 层:admin 和 h5 所有处理器 - Service 层:所有业务服务 - Routes 层:所有路由定义 - Tests 层:单元测试和集成测试 - 清理未使用的 import 语句 - 验证:项目构建成功,测试编译通过,LSP 无错误
127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
// Package customer 提供客户管理的业务逻辑服务
|
|
// 包含客户信息管理、客户查询等功能
|
|
package customer
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
"github.com/break/junhong_cmp_fiber/internal/store"
|
|
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
)
|
|
|
|
// Service 个人客户业务服务
|
|
type Service struct {
|
|
customerStore *postgres.PersonalCustomerStore
|
|
}
|
|
|
|
// New 创建个人客户服务
|
|
func New(customerStore *postgres.PersonalCustomerStore) *Service {
|
|
return &Service{
|
|
customerStore: customerStore,
|
|
}
|
|
}
|
|
|
|
// Create 创建个人客户
|
|
func (s *Service) Create(ctx context.Context, req *dto.CreatePersonalCustomerRequest) (*model.PersonalCustomer, error) {
|
|
// 检查手机号唯一性
|
|
if req.Phone != "" {
|
|
existing, err := s.customerStore.GetByPhone(ctx, req.Phone)
|
|
if err == nil && existing != nil {
|
|
return nil, errors.New(errors.CodeCustomerPhoneExists, "手机号已存在")
|
|
}
|
|
}
|
|
|
|
// 创建个人客户
|
|
// 注意:根据新的数据模型,手机号应该存储在 PersonalCustomerPhone 表中
|
|
// 这里暂时先创建客户记录,手机号的存储后续通过 PersonalCustomerPhoneStore 实现
|
|
customer := &model.PersonalCustomer{
|
|
Nickname: req.Nickname,
|
|
AvatarURL: req.AvatarURL,
|
|
WxOpenID: req.WxOpenID,
|
|
WxUnionID: req.WxUnionID,
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
|
|
if err := s.customerStore.Create(ctx, customer); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO: 创建 PersonalCustomerPhone 记录,需要通过 PersonalCustomerPhoneStore 创建手机号关联
|
|
|
|
return customer, nil
|
|
}
|
|
|
|
// Update 更新个人客户信息
|
|
func (s *Service) Update(ctx context.Context, id uint, req *dto.UpdatePersonalCustomerRequest) (*model.PersonalCustomer, error) {
|
|
// 查询客户
|
|
customer, err := s.customerStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
|
|
}
|
|
|
|
// TODO: 手机号的更新逻辑需要通过 PersonalCustomerPhoneStore 更新或创建手机号记录
|
|
|
|
// 更新字段
|
|
if req.Nickname != nil {
|
|
customer.Nickname = *req.Nickname
|
|
}
|
|
if req.AvatarURL != nil {
|
|
customer.AvatarURL = *req.AvatarURL
|
|
}
|
|
|
|
if err := s.customerStore.Update(ctx, customer); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return customer, nil
|
|
}
|
|
|
|
// BindWeChat 绑定微信信息
|
|
func (s *Service) BindWeChat(ctx context.Context, id uint, wxOpenID, wxUnionID string) error {
|
|
customer, err := s.customerStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
|
|
}
|
|
|
|
customer.WxOpenID = wxOpenID
|
|
customer.WxUnionID = wxUnionID
|
|
|
|
return s.customerStore.Update(ctx, customer)
|
|
}
|
|
|
|
// GetByID 获取个人客户详情
|
|
func (s *Service) GetByID(ctx context.Context, id uint) (*model.PersonalCustomer, error) {
|
|
customer, err := s.customerStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
|
|
}
|
|
return customer, nil
|
|
}
|
|
|
|
// GetByPhone 根据手机号获取个人客户
|
|
func (s *Service) GetByPhone(ctx context.Context, phone string) (*model.PersonalCustomer, error) {
|
|
customer, err := s.customerStore.GetByPhone(ctx, phone)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
|
|
}
|
|
return customer, nil
|
|
}
|
|
|
|
// GetByWxOpenID 根据微信 OpenID 获取个人客户
|
|
func (s *Service) GetByWxOpenID(ctx context.Context, wxOpenID string) (*model.PersonalCustomer, error) {
|
|
customer, err := s.customerStore.GetByWxOpenID(ctx, wxOpenID)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeCustomerNotFound, "个人客户不存在")
|
|
}
|
|
return customer, nil
|
|
}
|
|
|
|
// List 查询个人客户列表
|
|
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.PersonalCustomer, int64, error) {
|
|
return s.customerStore.List(ctx, opts, filters)
|
|
}
|