Files
junhong_cmp_fiber/internal/service/enterprise/service.go
huang a36e4a79c0 实现用户和组织模型(店铺、企业、个人客户)
核心功能:
- 实现 7 级店铺层级体系(Shop 模型 + 层级校验)
- 实现企业管理模型(Enterprise 模型)
- 实现个人客户管理模型(PersonalCustomer 模型)
- 重构 Account 模型关联关系(基于 EnterpriseID 而非 ParentID)
- 完整的 Store 层和 Service 层实现
- 递归查询下级店铺功能(含 Redis 缓存)
- 全面的单元测试覆盖(Shop/Enterprise/PersonalCustomer Store + Shop Service)

技术要点:
- 显式指定所有 GORM 模型的数据库字段名(column: 标签)
- 统一的字段命名规范(数据库用 snake_case,Go 用 PascalCase)
- 完整的中文字段注释和业务逻辑说明
- 100% 测试覆盖(20+ 测试用例全部通过)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 18:02:46 +08:00

187 lines
5.4 KiB
Go

package enterprise
import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"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"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
)
// Service 企业业务服务
type Service struct {
enterpriseStore *postgres.EnterpriseStore
shopStore *postgres.ShopStore
}
// New 创建企业服务
func New(enterpriseStore *postgres.EnterpriseStore, shopStore *postgres.ShopStore) *Service {
return &Service{
enterpriseStore: enterpriseStore,
shopStore: shopStore,
}
}
// Create 创建企业
func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseRequest) (*model.Enterprise, error) {
// 获取当前用户 ID
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
// 检查企业编号唯一性
if req.EnterpriseCode != "" {
existing, err := s.enterpriseStore.GetByCode(ctx, req.EnterpriseCode)
if err == nil && existing != nil {
return nil, errors.New(errors.CodeEnterpriseCodeExists, "企业编号已存在")
}
}
// 验证归属店铺存在(如果提供)
if req.OwnerShopID != nil {
_, err := s.shopStore.GetByID(ctx, *req.OwnerShopID)
if err != nil {
return nil, errors.New(errors.CodeShopNotFound, "归属店铺不存在或无效")
}
}
// 创建企业
enterprise := &model.Enterprise{
EnterpriseName: req.EnterpriseName,
EnterpriseCode: req.EnterpriseCode,
OwnerShopID: req.OwnerShopID,
LegalPerson: req.LegalPerson,
ContactName: req.ContactName,
ContactPhone: req.ContactPhone,
BusinessLicense: req.BusinessLicense,
Province: req.Province,
City: req.City,
District: req.District,
Address: req.Address,
Status: constants.StatusEnabled,
}
enterprise.Creator = currentUserID
enterprise.Updater = currentUserID
if err := s.enterpriseStore.Create(ctx, enterprise); err != nil {
return nil, err
}
return enterprise, nil
}
// Update 更新企业信息
func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateEnterpriseRequest) (*model.Enterprise, error) {
// 获取当前用户 ID
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
// 查询企业
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
// 检查企业编号唯一性(如果修改了编号)
if req.EnterpriseCode != nil && *req.EnterpriseCode != enterprise.EnterpriseCode {
existing, err := s.enterpriseStore.GetByCode(ctx, *req.EnterpriseCode)
if err == nil && existing != nil && existing.ID != id {
return nil, errors.New(errors.CodeEnterpriseCodeExists, "企业编号已存在")
}
enterprise.EnterpriseCode = *req.EnterpriseCode
}
// 更新字段
if req.EnterpriseName != nil {
enterprise.EnterpriseName = *req.EnterpriseName
}
if req.LegalPerson != nil {
enterprise.LegalPerson = *req.LegalPerson
}
if req.ContactName != nil {
enterprise.ContactName = *req.ContactName
}
if req.ContactPhone != nil {
enterprise.ContactPhone = *req.ContactPhone
}
if req.BusinessLicense != nil {
enterprise.BusinessLicense = *req.BusinessLicense
}
if req.Province != nil {
enterprise.Province = *req.Province
}
if req.City != nil {
enterprise.City = *req.City
}
if req.District != nil {
enterprise.District = *req.District
}
if req.Address != nil {
enterprise.Address = *req.Address
}
enterprise.Updater = currentUserID
if err := s.enterpriseStore.Update(ctx, enterprise); err != nil {
return nil, err
}
return enterprise, nil
}
// Disable 禁用企业
func (s *Service) Disable(ctx context.Context, id uint) error {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return errors.New(errors.CodeUnauthorized, "未授权访问")
}
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
if err != nil {
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
enterprise.Status = constants.StatusDisabled
enterprise.Updater = currentUserID
return s.enterpriseStore.Update(ctx, enterprise)
}
// Enable 启用企业
func (s *Service) Enable(ctx context.Context, id uint) error {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return errors.New(errors.CodeUnauthorized, "未授权访问")
}
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
if err != nil {
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
enterprise.Status = constants.StatusEnabled
enterprise.Updater = currentUserID
return s.enterpriseStore.Update(ctx, enterprise)
}
// GetByID 获取企业详情
func (s *Service) GetByID(ctx context.Context, id uint) (*model.Enterprise, error) {
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
return enterprise, nil
}
// List 查询企业列表
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Enterprise, int64, error) {
return s.enterpriseStore.List(ctx, opts, filters)
}