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) }