- 修复 health.go handler 直接操作响应的架构违规问题 - 为 model 字段添加 GORM comment 标签(account_role、base、role_permission) - 为 handler、service、store 包添加包级文档注释 - 清理 customer service 和 personal_customer handler 中注释掉的代码 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
201 lines
5.3 KiB
Go
201 lines
5.3 KiB
Go
// Package shop 提供店铺管理的业务逻辑服务
|
|
// 包含店铺创建、查询、更新、删除等功能
|
|
package shop
|
|
|
|
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 {
|
|
shopStore *postgres.ShopStore
|
|
}
|
|
|
|
// New 创建店铺服务
|
|
func New(shopStore *postgres.ShopStore) *Service {
|
|
return &Service{
|
|
shopStore: shopStore,
|
|
}
|
|
}
|
|
|
|
// Create 创建店铺
|
|
func (s *Service) Create(ctx context.Context, req *model.CreateShopRequest) (*model.Shop, error) {
|
|
// 获取当前用户 ID
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
// 检查店铺编号唯一性
|
|
if req.ShopCode != "" {
|
|
existing, err := s.shopStore.GetByCode(ctx, req.ShopCode)
|
|
if err == nil && existing != nil {
|
|
return nil, errors.New(errors.CodeShopCodeExists, "店铺编号已存在")
|
|
}
|
|
}
|
|
|
|
// 计算层级
|
|
level := 1
|
|
if req.ParentID != nil {
|
|
// 验证上级店铺存在
|
|
parent, err := s.shopStore.GetByID(ctx, *req.ParentID)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeInvalidParentID, "上级店铺不存在或无效")
|
|
}
|
|
|
|
// 计算新店铺的层级
|
|
level = parent.Level + 1
|
|
|
|
// 校验层级不超过最大值
|
|
if level > constants.MaxShopLevel {
|
|
return nil, errors.New(errors.CodeShopLevelExceeded, "店铺层级不能超过 7 级")
|
|
}
|
|
}
|
|
|
|
// 创建店铺
|
|
shop := &model.Shop{
|
|
ShopName: req.ShopName,
|
|
ShopCode: req.ShopCode,
|
|
ParentID: req.ParentID,
|
|
Level: level,
|
|
ContactName: req.ContactName,
|
|
ContactPhone: req.ContactPhone,
|
|
Province: req.Province,
|
|
City: req.City,
|
|
District: req.District,
|
|
Address: req.Address,
|
|
Status: constants.StatusEnabled,
|
|
}
|
|
shop.Creator = currentUserID
|
|
shop.Updater = currentUserID
|
|
|
|
if err := s.shopStore.Create(ctx, shop); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return shop, nil
|
|
}
|
|
|
|
// Update 更新店铺信息
|
|
func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateShopRequest) (*model.Shop, error) {
|
|
// 获取当前用户 ID
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
// 查询店铺
|
|
shop, err := s.shopStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
|
}
|
|
|
|
// 检查店铺编号唯一性(如果修改了编号)
|
|
if req.ShopCode != nil && *req.ShopCode != shop.ShopCode {
|
|
existing, err := s.shopStore.GetByCode(ctx, *req.ShopCode)
|
|
if err == nil && existing != nil && existing.ID != id {
|
|
return nil, errors.New(errors.CodeShopCodeExists, "店铺编号已存在")
|
|
}
|
|
shop.ShopCode = *req.ShopCode
|
|
}
|
|
|
|
// 更新字段
|
|
if req.ShopName != nil {
|
|
shop.ShopName = *req.ShopName
|
|
}
|
|
if req.ContactName != nil {
|
|
shop.ContactName = *req.ContactName
|
|
}
|
|
if req.ContactPhone != nil {
|
|
shop.ContactPhone = *req.ContactPhone
|
|
}
|
|
if req.Province != nil {
|
|
shop.Province = *req.Province
|
|
}
|
|
if req.City != nil {
|
|
shop.City = *req.City
|
|
}
|
|
if req.District != nil {
|
|
shop.District = *req.District
|
|
}
|
|
if req.Address != nil {
|
|
shop.Address = *req.Address
|
|
}
|
|
|
|
shop.Updater = currentUserID
|
|
|
|
if err := s.shopStore.Update(ctx, shop); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return shop, nil
|
|
}
|
|
|
|
// Disable 禁用店铺
|
|
func (s *Service) Disable(ctx context.Context, id uint) error {
|
|
// 获取当前用户 ID
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
// 查询店铺
|
|
shop, err := s.shopStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return errors.New(errors.CodeShopNotFound, "店铺不存在")
|
|
}
|
|
|
|
// 更新状态
|
|
shop.Status = constants.StatusDisabled
|
|
shop.Updater = currentUserID
|
|
|
|
return s.shopStore.Update(ctx, shop)
|
|
}
|
|
|
|
// Enable 启用店铺
|
|
func (s *Service) Enable(ctx context.Context, id uint) error {
|
|
// 获取当前用户 ID
|
|
currentUserID := middleware.GetUserIDFromContext(ctx)
|
|
if currentUserID == 0 {
|
|
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
|
}
|
|
|
|
// 查询店铺
|
|
shop, err := s.shopStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return errors.New(errors.CodeShopNotFound, "店铺不存在")
|
|
}
|
|
|
|
// 更新状态
|
|
shop.Status = constants.StatusEnabled
|
|
shop.Updater = currentUserID
|
|
|
|
return s.shopStore.Update(ctx, shop)
|
|
}
|
|
|
|
// GetByID 获取店铺详情
|
|
func (s *Service) GetByID(ctx context.Context, id uint) (*model.Shop, error) {
|
|
shop, err := s.shopStore.GetByID(ctx, id)
|
|
if err != nil {
|
|
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
|
|
}
|
|
return shop, nil
|
|
}
|
|
|
|
// List 查询店铺列表
|
|
func (s *Service) List(ctx context.Context, opts *store.QueryOptions, filters map[string]interface{}) ([]*model.Shop, int64, error) {
|
|
return s.shopStore.List(ctx, opts, filters)
|
|
}
|
|
|
|
// GetSubordinateShopIDs 获取下级店铺 ID 列表(包含自己)
|
|
func (s *Service) GetSubordinateShopIDs(ctx context.Context, shopID uint) ([]uint, error) {
|
|
return s.shopStore.GetSubordinateShopIDs(ctx, shopID)
|
|
}
|