feat: 实现账号与佣金管理模块
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m35s
新增功能: - 店铺佣金查询:店铺佣金统计、店铺佣金记录列表、店铺提现记录 - 佣金提现审批:提现申请列表、审批通过、审批拒绝 - 提现配置管理:配置列表、新增配置、获取当前生效配置 - 企业管理:企业列表、创建、更新、删除、获取详情 - 企业卡授权:授权列表、批量授权、批量取消授权、统计 - 客户账号管理:账号列表、创建、更新状态、重置密码 - 我的佣金:佣金统计、佣金记录、提现申请、提现记录 数据库变更: - 扩展 tb_commission_withdrawal_request 新增提现单号等字段 - 扩展 tb_account 新增 is_primary 字段 - 扩展 tb_commission_record 新增 shop_id、balance_after - 扩展 tb_commission_withdrawal_setting 新增每日提现次数限制 - 扩展 tb_iot_card、tb_device 新增 shop_id 冗余字段 - 新建 tb_enterprise_card_authorization 企业卡授权表 - 新建 tb_asset_allocation_record 资产分配记录表 - 数据迁移:owner_type 枚举值 agent 统一为 shop 测试: - 新增 7 个单元测试文件覆盖各服务 - 修复集成测试 Redis 依赖问题
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
// Package enterprise 提供企业管理的业务逻辑服务
|
||||
// 包含企业创建、查询、更新、删除等功能
|
||||
package enterprise
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
@@ -11,39 +10,44 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/middleware"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Service 企业业务服务
|
||||
type Service struct {
|
||||
db *gorm.DB
|
||||
enterpriseStore *postgres.EnterpriseStore
|
||||
shopStore *postgres.ShopStore
|
||||
accountStore *postgres.AccountStore
|
||||
}
|
||||
|
||||
// New 创建企业服务
|
||||
func New(enterpriseStore *postgres.EnterpriseStore, shopStore *postgres.ShopStore) *Service {
|
||||
func New(db *gorm.DB, enterpriseStore *postgres.EnterpriseStore, shopStore *postgres.ShopStore, accountStore *postgres.AccountStore) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
enterpriseStore: enterpriseStore,
|
||||
shopStore: shopStore,
|
||||
accountStore: accountStore,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建企业
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseRequest) (*model.Enterprise, error) {
|
||||
// 获取当前用户 ID
|
||||
func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseReq) (*model.CreateEnterpriseResp, error) {
|
||||
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 {
|
||||
existing, _ := s.enterpriseStore.GetByCode(ctx, req.EnterpriseCode)
|
||||
if existing != nil {
|
||||
return nil, errors.New(errors.CodeEnterpriseCodeExists, "企业编号已存在")
|
||||
}
|
||||
}
|
||||
|
||||
// 验证归属店铺存在(如果提供)
|
||||
existingAccount, _ := s.accountStore.GetByPhone(ctx, req.LoginPhone)
|
||||
if existingAccount != nil {
|
||||
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
|
||||
}
|
||||
|
||||
if req.OwnerShopID != nil {
|
||||
_, err := s.shopStore.GetByID(ctx, *req.OwnerShopID)
|
||||
if err != nil {
|
||||
@@ -51,29 +55,87 @@ func (s *Service) Create(ctx context.Context, req *model.CreateEnterpriseRequest
|
||||
}
|
||||
}
|
||||
|
||||
// 创建企业
|
||||
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,
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
enterprise.Creator = currentUserID
|
||||
enterprise.Updater = currentUserID
|
||||
|
||||
if err := s.enterpriseStore.Create(ctx, enterprise); err != nil {
|
||||
var enterprise *model.Enterprise
|
||||
var account *model.Account
|
||||
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
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 := tx.WithContext(ctx).Create(enterprise).Error; err != nil {
|
||||
return fmt.Errorf("创建企业失败: %w", err)
|
||||
}
|
||||
|
||||
account = &model.Account{
|
||||
Username: req.EnterpriseName,
|
||||
Phone: req.LoginPhone,
|
||||
Password: string(hashedPassword),
|
||||
UserType: constants.UserTypeEnterprise,
|
||||
EnterpriseID: &enterprise.ID,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
account.Creator = currentUserID
|
||||
account.Updater = currentUserID
|
||||
|
||||
if err := tx.WithContext(ctx).Create(account).Error; err != nil {
|
||||
return fmt.Errorf("创建企业账号失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return enterprise, nil
|
||||
ownerShopName := ""
|
||||
if enterprise.OwnerShopID != nil {
|
||||
if shop, err := s.shopStore.GetByID(ctx, *enterprise.OwnerShopID); err == nil {
|
||||
ownerShopName = shop.ShopName
|
||||
}
|
||||
}
|
||||
|
||||
return &model.CreateEnterpriseResp{
|
||||
Enterprise: model.EnterpriseItem{
|
||||
ID: enterprise.ID,
|
||||
EnterpriseName: enterprise.EnterpriseName,
|
||||
EnterpriseCode: enterprise.EnterpriseCode,
|
||||
OwnerShopID: enterprise.OwnerShopID,
|
||||
OwnerShopName: ownerShopName,
|
||||
LegalPerson: enterprise.LegalPerson,
|
||||
ContactName: enterprise.ContactName,
|
||||
ContactPhone: enterprise.ContactPhone,
|
||||
LoginPhone: req.LoginPhone,
|
||||
BusinessLicense: enterprise.BusinessLicense,
|
||||
Province: enterprise.Province,
|
||||
City: enterprise.City,
|
||||
District: enterprise.District,
|
||||
Address: enterprise.Address,
|
||||
Status: enterprise.Status,
|
||||
StatusName: getStatusName(enterprise.Status),
|
||||
CreatedAt: enterprise.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
AccountID: account.ID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新企业信息
|
||||
@@ -137,8 +199,7 @@ func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateEnterpri
|
||||
return enterprise, nil
|
||||
}
|
||||
|
||||
// Disable 禁用企业
|
||||
func (s *Service) Disable(ctx context.Context, id uint) error {
|
||||
func (s *Service) UpdateStatus(ctx context.Context, id uint, status int) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
@@ -149,31 +210,50 @@ func (s *Service) Disable(ctx context.Context, id uint) error {
|
||||
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
enterprise.Status = constants.StatusDisabled
|
||||
enterprise.Updater = currentUserID
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
enterprise.Status = status
|
||||
enterprise.Updater = currentUserID
|
||||
if err := tx.WithContext(ctx).Save(enterprise).Error; err != nil {
|
||||
return fmt.Errorf("更新企业状态失败: %w", err)
|
||||
}
|
||||
|
||||
return s.enterpriseStore.Update(ctx, enterprise)
|
||||
if err := tx.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("enterprise_id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"status": status,
|
||||
"updater": currentUserID,
|
||||
}).Error; err != nil {
|
||||
return fmt.Errorf("同步更新企业账号状态失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Enable 启用企业
|
||||
func (s *Service) Enable(ctx context.Context, id uint) error {
|
||||
func (s *Service) UpdatePassword(ctx context.Context, id uint, password string) error {
|
||||
currentUserID := middleware.GetUserIDFromContext(ctx)
|
||||
if currentUserID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
_, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
|
||||
}
|
||||
|
||||
enterprise.Status = constants.StatusEnabled
|
||||
enterprise.Updater = currentUserID
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
|
||||
return s.enterpriseStore.Update(ctx, enterprise)
|
||||
return s.db.WithContext(ctx).Model(&model.Account{}).
|
||||
Where("enterprise_id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
"password": string(hashedPassword),
|
||||
"updater": currentUserID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// GetByID 获取企业详情
|
||||
func (s *Service) GetByID(ctx context.Context, id uint) (*model.Enterprise, error) {
|
||||
enterprise, err := s.enterpriseStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -182,7 +262,104 @@ func (s *Service) GetByID(ctx context.Context, id uint) (*model.Enterprise, erro
|
||||
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)
|
||||
func (s *Service) List(ctx context.Context, req *model.EnterpriseListReq) (*model.EnterprisePageResult, error) {
|
||||
opts := &store.QueryOptions{
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
if opts.Page == 0 {
|
||||
opts.Page = 1
|
||||
}
|
||||
if opts.PageSize == 0 {
|
||||
opts.PageSize = constants.DefaultPageSize
|
||||
}
|
||||
|
||||
filters := make(map[string]interface{})
|
||||
if req.EnterpriseName != "" {
|
||||
filters["enterprise_name"] = req.EnterpriseName
|
||||
}
|
||||
if req.ContactPhone != "" {
|
||||
filters["contact_phone"] = req.ContactPhone
|
||||
}
|
||||
if req.OwnerShopID != nil {
|
||||
filters["owner_shop_id"] = *req.OwnerShopID
|
||||
}
|
||||
if req.Status != nil {
|
||||
filters["status"] = *req.Status
|
||||
}
|
||||
|
||||
enterprises, total, err := s.enterpriseStore.List(ctx, opts, filters)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询企业列表失败: %w", err)
|
||||
}
|
||||
|
||||
enterpriseIDs := make([]uint, 0, len(enterprises))
|
||||
shopIDs := make([]uint, 0)
|
||||
for _, e := range enterprises {
|
||||
enterpriseIDs = append(enterpriseIDs, e.ID)
|
||||
if e.OwnerShopID != nil {
|
||||
shopIDs = append(shopIDs, *e.OwnerShopID)
|
||||
}
|
||||
}
|
||||
|
||||
accountMap := make(map[uint]string)
|
||||
if len(enterpriseIDs) > 0 {
|
||||
var accounts []model.Account
|
||||
s.db.WithContext(ctx).Where("enterprise_id IN ?", enterpriseIDs).Find(&accounts)
|
||||
for _, acc := range accounts {
|
||||
if acc.EnterpriseID != nil {
|
||||
accountMap[*acc.EnterpriseID] = acc.Phone
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := make(map[uint]string)
|
||||
if len(shopIDs) > 0 {
|
||||
var shops []model.Shop
|
||||
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
|
||||
for _, shop := range shops {
|
||||
shopMap[shop.ID] = shop.ShopName
|
||||
}
|
||||
}
|
||||
|
||||
items := make([]model.EnterpriseItem, 0, len(enterprises))
|
||||
for _, e := range enterprises {
|
||||
ownerShopName := ""
|
||||
if e.OwnerShopID != nil {
|
||||
ownerShopName = shopMap[*e.OwnerShopID]
|
||||
}
|
||||
items = append(items, model.EnterpriseItem{
|
||||
ID: e.ID,
|
||||
EnterpriseName: e.EnterpriseName,
|
||||
EnterpriseCode: e.EnterpriseCode,
|
||||
OwnerShopID: e.OwnerShopID,
|
||||
OwnerShopName: ownerShopName,
|
||||
LegalPerson: e.LegalPerson,
|
||||
ContactName: e.ContactName,
|
||||
ContactPhone: e.ContactPhone,
|
||||
LoginPhone: accountMap[e.ID],
|
||||
BusinessLicense: e.BusinessLicense,
|
||||
Province: e.Province,
|
||||
City: e.City,
|
||||
District: e.District,
|
||||
Address: e.Address,
|
||||
Status: e.Status,
|
||||
StatusName: getStatusName(e.Status),
|
||||
CreatedAt: e.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return &model.EnterprisePageResult{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: opts.Page,
|
||||
Size: opts.PageSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func getStatusName(status int) string {
|
||||
if status == constants.StatusEnabled {
|
||||
return "启用"
|
||||
}
|
||||
return "禁用"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user