feat: 实现账号与佣金管理模块
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:
2026-01-21 18:20:44 +08:00
parent 1489abe668
commit 91c9bbfeb8
89 changed files with 11958 additions and 159 deletions

View File

@@ -0,0 +1,420 @@
package commission_withdrawal
import (
"context"
"encoding/json"
"fmt"
"time"
"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"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
shopStore *postgres.ShopStore
accountStore *postgres.AccountStore
walletStore *postgres.WalletStore
walletTransactionStore *postgres.WalletTransactionStore
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore
}
func New(
db *gorm.DB,
shopStore *postgres.ShopStore,
accountStore *postgres.AccountStore,
walletStore *postgres.WalletStore,
walletTransactionStore *postgres.WalletTransactionStore,
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore,
) *Service {
return &Service{
db: db,
shopStore: shopStore,
accountStore: accountStore,
walletStore: walletStore,
walletTransactionStore: walletTransactionStore,
commissionWithdrawalReqStore: commissionWithdrawalReqStore,
}
}
func (s *Service) ListWithdrawalRequests(ctx context.Context, req *model.WithdrawalRequestListReq) (*model.WithdrawalRequestPageResult, error) {
opts := &store.QueryOptions{
Page: req.Page,
PageSize: req.PageSize,
OrderBy: "created_at DESC",
}
if opts.Page == 0 {
opts.Page = 1
}
if opts.PageSize == 0 {
opts.PageSize = constants.DefaultPageSize
}
filters := &postgres.WithdrawalRequestListFilters{
WithdrawalNo: req.WithdrawalNo,
Status: req.Status,
}
if req.StartTime != "" {
t, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
if err == nil {
filters.StartTime = &t
}
}
if req.EndTime != "" {
t, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
if err == nil {
filters.EndTime = &t
}
}
requests, total, err := s.commissionWithdrawalReqStore.List(ctx, opts, filters)
if err != nil {
return nil, fmt.Errorf("查询提现申请列表失败: %w", err)
}
shopIDs := make([]uint, 0)
applicantIDs := make([]uint, 0)
processorIDs := make([]uint, 0)
for _, r := range requests {
if r.ShopID > 0 {
shopIDs = append(shopIDs, r.ShopID)
}
if r.ApplicantID > 0 {
applicantIDs = append(applicantIDs, r.ApplicantID)
}
if r.ProcessorID > 0 {
processorIDs = append(processorIDs, r.ProcessorID)
}
}
shopMap := make(map[uint]*model.Shop)
for _, id := range shopIDs {
shop, err := s.shopStore.GetByID(ctx, id)
if err == nil {
shopMap[id] = shop
}
}
applicantMap := make(map[uint]string)
processorMap := make(map[uint]string)
if len(applicantIDs) > 0 {
accounts, _ := s.accountStore.GetByIDs(ctx, applicantIDs)
for _, acc := range accounts {
applicantMap[acc.ID] = acc.Username
}
}
if len(processorIDs) > 0 {
accounts, _ := s.accountStore.GetByIDs(ctx, processorIDs)
for _, acc := range accounts {
processorMap[acc.ID] = acc.Username
}
}
items := make([]model.WithdrawalRequestItem, 0, len(requests))
for _, r := range requests {
shop := shopMap[r.ShopID]
shopName := ""
shopHierarchy := ""
if shop != nil {
shopName = shop.ShopName
shopHierarchy = s.buildShopHierarchyPath(ctx, shop)
if req.ShopName != "" && !containsSubstring(shopName, req.ShopName) {
total--
continue
}
}
item := s.buildWithdrawalRequestItem(r, shopName, shopHierarchy, applicantMap, processorMap)
items = append(items, item)
}
return &model.WithdrawalRequestPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func (s *Service) Approve(ctx context.Context, id uint, req *model.ApproveWithdrawalReq) (*model.WithdrawalApprovalResp, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
withdrawal, err := s.commissionWithdrawalReqStore.GetByID(ctx, id)
if err != nil {
return nil, errors.New(errors.CodeNotFound, "提现申请不存在")
}
if withdrawal.Status != constants.WithdrawalStatusPending {
return nil, errors.New(errors.CodeInvalidStatus, "申请状态不允许此操作")
}
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, withdrawal.ShopID)
if err != nil {
return nil, errors.New(errors.CodeNotFound, "店铺佣金钱包不存在")
}
amount := withdrawal.Amount
if req.Amount != nil {
amount = *req.Amount
}
if wallet.FrozenBalance < amount {
return nil, errors.New(errors.CodeInsufficientBalance, "钱包冻结余额不足")
}
now := time.Now()
err = s.db.Transaction(func(tx *gorm.DB) error {
if err := s.walletStore.DeductFrozenBalanceWithTx(ctx, tx, wallet.ID, amount); err != nil {
return fmt.Errorf("扣除冻结余额失败: %w", err)
}
refType := "withdrawal"
refID := withdrawal.ID
transaction := &model.WalletTransaction{
WalletID: wallet.ID,
UserID: currentUserID,
TransactionType: "withdrawal",
Amount: -amount,
BalanceBefore: wallet.Balance,
BalanceAfter: wallet.Balance,
Status: 1,
ReferenceType: &refType,
ReferenceID: &refID,
Creator: currentUserID,
}
if err := s.walletTransactionStore.CreateWithTx(ctx, tx, transaction); err != nil {
return fmt.Errorf("创建交易流水失败: %w", err)
}
updates := map[string]interface{}{
"status": constants.WithdrawalStatusApproved,
"processor_id": currentUserID,
"processed_at": now,
"payment_type": req.PaymentType,
"remark": req.Remark,
}
if req.Amount != nil {
feeRate := withdrawal.FeeRate
fee := amount * feeRate / 10000
actualAmount := amount - fee
updates["amount"] = amount
updates["fee"] = fee
updates["actual_amount"] = actualAmount
}
if req.WithdrawalMethod != nil {
updates["withdrawal_method"] = *req.WithdrawalMethod
}
if req.AccountName != nil || req.AccountNumber != nil {
accountInfo := make(map[string]interface{})
if withdrawal.AccountInfo != nil {
_ = json.Unmarshal(withdrawal.AccountInfo, &accountInfo)
}
if req.AccountName != nil {
accountInfo["account_name"] = *req.AccountName
}
if req.AccountNumber != nil {
accountInfo["account_number"] = *req.AccountNumber
}
infoBytes, _ := json.Marshal(accountInfo)
updates["account_info"] = infoBytes
}
if err := s.commissionWithdrawalReqStore.UpdateStatusWithTx(ctx, tx, id, updates); err != nil {
return fmt.Errorf("更新提现申请状态失败: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return &model.WithdrawalApprovalResp{
ID: withdrawal.ID,
WithdrawalNo: withdrawal.WithdrawalNo,
Status: constants.WithdrawalStatusApproved,
StatusName: "已通过",
ProcessedAt: now.Format("2006-01-02 15:04:05"),
}, nil
}
func (s *Service) Reject(ctx context.Context, id uint, req *model.RejectWithdrawalReq) (*model.WithdrawalApprovalResp, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
withdrawal, err := s.commissionWithdrawalReqStore.GetByID(ctx, id)
if err != nil {
return nil, errors.New(errors.CodeNotFound, "提现申请不存在")
}
if withdrawal.Status != constants.WithdrawalStatusPending {
return nil, errors.New(errors.CodeInvalidStatus, "申请状态不允许此操作")
}
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, withdrawal.ShopID)
if err != nil {
return nil, errors.New(errors.CodeNotFound, "店铺佣金钱包不存在")
}
now := time.Now()
err = s.db.Transaction(func(tx *gorm.DB) error {
if err := s.walletStore.UnfreezeBalanceWithTx(ctx, tx, wallet.ID, withdrawal.Amount); err != nil {
return fmt.Errorf("解冻余额失败: %w", err)
}
refType := "withdrawal"
refID := withdrawal.ID
transaction := &model.WalletTransaction{
WalletID: wallet.ID,
UserID: currentUserID,
TransactionType: "refund",
Amount: withdrawal.Amount,
BalanceBefore: wallet.Balance,
BalanceAfter: wallet.Balance + withdrawal.Amount,
Status: 1,
ReferenceType: &refType,
ReferenceID: &refID,
Creator: currentUserID,
}
if err := s.walletTransactionStore.CreateWithTx(ctx, tx, transaction); err != nil {
return fmt.Errorf("创建交易流水失败: %w", err)
}
updates := map[string]interface{}{
"status": constants.WithdrawalStatusRejected,
"processor_id": currentUserID,
"processed_at": now,
"reject_reason": req.Remark,
"remark": req.Remark,
}
if err := s.commissionWithdrawalReqStore.UpdateStatusWithTx(ctx, tx, id, updates); err != nil {
return fmt.Errorf("更新提现申请状态失败: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return &model.WithdrawalApprovalResp{
ID: withdrawal.ID,
WithdrawalNo: withdrawal.WithdrawalNo,
Status: constants.WithdrawalStatusRejected,
StatusName: "已拒绝",
ProcessedAt: now.Format("2006-01-02 15:04:05"),
}, nil
}
func (s *Service) buildShopHierarchyPath(ctx context.Context, shop *model.Shop) string {
if shop == nil {
return ""
}
path := shop.ShopName
current := shop
depth := 0
for current.ParentID != nil && depth < 2 {
parent, err := s.shopStore.GetByID(ctx, *current.ParentID)
if err != nil {
break
}
path = parent.ShopName + "_" + path
current = parent
depth++
}
return path
}
func (s *Service) buildWithdrawalRequestItem(r *model.CommissionWithdrawalRequest, shopName, shopHierarchy string, applicantMap, processorMap map[uint]string) model.WithdrawalRequestItem {
var processorID *uint
if r.ProcessorID > 0 {
processorID = &r.ProcessorID
}
var accountName, accountNumber, bankName string
if len(r.AccountInfo) > 0 {
var info map[string]interface{}
if err := json.Unmarshal(r.AccountInfo, &info); err == nil {
if v, ok := info["account_name"].(string); ok {
accountName = v
}
if v, ok := info["account_number"].(string); ok {
accountNumber = v
}
if v, ok := info["bank_name"].(string); ok {
bankName = v
}
}
}
var processedAt string
if r.ProcessedAt != nil {
processedAt = r.ProcessedAt.Format("2006-01-02 15:04:05")
}
return model.WithdrawalRequestItem{
ID: r.ID,
WithdrawalNo: r.WithdrawalNo,
Amount: r.Amount,
FeeRate: r.FeeRate,
Fee: r.Fee,
ActualAmount: r.ActualAmount,
Status: r.Status,
StatusName: getWithdrawalStatusName(r.Status),
ShopID: r.ShopID,
ShopName: shopName,
ShopHierarchy: shopHierarchy,
ApplicantID: r.ApplicantID,
ApplicantName: applicantMap[r.ApplicantID],
ProcessorID: processorID,
ProcessorName: processorMap[r.ProcessorID],
WithdrawalMethod: r.WithdrawalMethod,
PaymentType: r.PaymentType,
AccountName: accountName,
AccountNumber: accountNumber,
BankName: bankName,
RejectReason: r.RejectReason,
Remark: r.Remark,
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
ProcessedAt: processedAt,
}
}
func getWithdrawalStatusName(status int) string {
switch status {
case constants.WithdrawalStatusPending:
return "待审核"
case constants.WithdrawalStatusApproved:
return "已通过"
case constants.WithdrawalStatusRejected:
return "已拒绝"
case constants.WithdrawalStatusPaid:
return "已到账"
default:
return "未知"
}
}
func containsSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}

View File

@@ -0,0 +1,161 @@
package commission_withdrawal_setting
import (
"context"
"fmt"
"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"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
accountStore *postgres.AccountStore
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore
}
func New(
db *gorm.DB,
accountStore *postgres.AccountStore,
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore,
) *Service {
return &Service{
db: db,
accountStore: accountStore,
commissionWithdrawalSettingStore: commissionWithdrawalSettingStore,
}
}
func (s *Service) Create(ctx context.Context, req *model.CreateWithdrawalSettingReq) (*model.WithdrawalSettingItem, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
setting := &model.CommissionWithdrawalSetting{
DailyWithdrawalLimit: req.DailyWithdrawalLimit,
MinWithdrawalAmount: req.MinWithdrawalAmount,
FeeRate: req.FeeRate,
IsActive: true,
}
setting.Creator = currentUserID
setting.Updater = currentUserID
err := s.db.Transaction(func(tx *gorm.DB) error {
if err := s.commissionWithdrawalSettingStore.DeactivateCurrentWithTx(ctx, tx); err != nil {
return fmt.Errorf("失效旧配置失败: %w", err)
}
if err := s.commissionWithdrawalSettingStore.CreateWithTx(ctx, tx, setting); err != nil {
return fmt.Errorf("创建配置失败: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
creatorName := ""
if creator, err := s.accountStore.GetByID(ctx, currentUserID); err == nil {
creatorName = creator.Username
}
return &model.WithdrawalSettingItem{
ID: setting.ID,
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
MinWithdrawalAmount: setting.MinWithdrawalAmount,
FeeRate: setting.FeeRate,
ArrivalDays: setting.ArrivalDays,
IsActive: setting.IsActive,
CreatorID: setting.Creator,
CreatorName: creatorName,
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}
func (s *Service) List(ctx context.Context, req *model.WithdrawalSettingListReq) (*model.WithdrawalSettingPageResult, 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
}
settings, total, err := s.commissionWithdrawalSettingStore.List(ctx, opts)
if err != nil {
return nil, fmt.Errorf("查询配置列表失败: %w", err)
}
creatorIDs := make([]uint, 0)
for _, setting := range settings {
if setting.Creator > 0 {
creatorIDs = append(creatorIDs, setting.Creator)
}
}
creatorMap := make(map[uint]string)
if len(creatorIDs) > 0 {
accounts, _ := s.accountStore.GetByIDs(ctx, creatorIDs)
for _, acc := range accounts {
creatorMap[acc.ID] = acc.Username
}
}
items := make([]model.WithdrawalSettingItem, 0, len(settings))
for _, setting := range settings {
items = append(items, model.WithdrawalSettingItem{
ID: setting.ID,
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
MinWithdrawalAmount: setting.MinWithdrawalAmount,
FeeRate: setting.FeeRate,
ArrivalDays: setting.ArrivalDays,
IsActive: setting.IsActive,
CreatorID: setting.Creator,
CreatorName: creatorMap[setting.Creator],
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
return &model.WithdrawalSettingPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func (s *Service) GetCurrent(ctx context.Context) (*model.WithdrawalSettingItem, error) {
setting, err := s.commissionWithdrawalSettingStore.GetCurrent(ctx)
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, errors.New(errors.CodeNotFound, "暂无生效的提现配置")
}
return nil, fmt.Errorf("查询当前配置失败: %w", err)
}
creatorName := ""
if creator, err := s.accountStore.GetByID(ctx, setting.Creator); err == nil {
creatorName = creator.Username
}
return &model.WithdrawalSettingItem{
ID: setting.ID,
DailyWithdrawalLimit: setting.DailyWithdrawalLimit,
MinWithdrawalAmount: setting.MinWithdrawalAmount,
FeeRate: setting.FeeRate,
ArrivalDays: setting.ArrivalDays,
IsActive: setting.IsActive,
CreatorID: setting.Creator,
CreatorName: creatorName,
CreatedAt: setting.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}

View File

@@ -0,0 +1,328 @@
package customer_account
import (
"context"
"fmt"
"github.com/break/junhong_cmp_fiber/internal/model"
"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"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
accountStore *postgres.AccountStore
shopStore *postgres.ShopStore
enterpriseStore *postgres.EnterpriseStore
}
func New(
db *gorm.DB,
accountStore *postgres.AccountStore,
shopStore *postgres.ShopStore,
enterpriseStore *postgres.EnterpriseStore,
) *Service {
return &Service{
db: db,
accountStore: accountStore,
shopStore: shopStore,
enterpriseStore: enterpriseStore,
}
}
func (s *Service) List(ctx context.Context, req *model.CustomerAccountListReq) (*model.CustomerAccountPageResult, error) {
page := req.Page
pageSize := req.PageSize
if page == 0 {
page = 1
}
if pageSize == 0 {
pageSize = constants.DefaultPageSize
}
query := s.db.WithContext(ctx).Model(&model.Account{}).
Where("user_type IN ?", []int{constants.UserTypeAgent, constants.UserTypeEnterprise})
if req.Username != "" {
query = query.Where("username LIKE ?", "%"+req.Username+"%")
}
if req.Phone != "" {
query = query.Where("phone LIKE ?", "%"+req.Phone+"%")
}
if req.UserType != nil {
query = query.Where("user_type = ?", *req.UserType)
}
if req.ShopID != nil {
query = query.Where("shop_id = ?", *req.ShopID)
}
if req.EnterpriseID != nil {
query = query.Where("enterprise_id = ?", *req.EnterpriseID)
}
if req.Status != nil {
query = query.Where("status = ?", *req.Status)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, fmt.Errorf("统计账号数量失败: %w", err)
}
var accounts []model.Account
offset := (page - 1) * pageSize
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&accounts).Error; err != nil {
return nil, fmt.Errorf("查询账号列表失败: %w", err)
}
shopIDs := make([]uint, 0)
enterpriseIDs := make([]uint, 0)
for _, acc := range accounts {
if acc.ShopID != nil {
shopIDs = append(shopIDs, *acc.ShopID)
}
if acc.EnterpriseID != nil {
enterpriseIDs = append(enterpriseIDs, *acc.EnterpriseID)
}
}
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
}
}
enterpriseMap := make(map[uint]string)
if len(enterpriseIDs) > 0 {
var enterprises []model.Enterprise
s.db.WithContext(ctx).Where("id IN ?", enterpriseIDs).Find(&enterprises)
for _, ent := range enterprises {
enterpriseMap[ent.ID] = ent.EnterpriseName
}
}
items := make([]model.CustomerAccountItem, 0, len(accounts))
for _, acc := range accounts {
shopName := ""
if acc.ShopID != nil {
shopName = shopMap[*acc.ShopID]
}
enterpriseName := ""
if acc.EnterpriseID != nil {
enterpriseName = enterpriseMap[*acc.EnterpriseID]
}
items = append(items, model.CustomerAccountItem{
ID: acc.ID,
Username: acc.Username,
Phone: acc.Phone,
UserType: acc.UserType,
UserTypeName: getUserTypeName(acc.UserType),
ShopID: acc.ShopID,
ShopName: shopName,
EnterpriseID: acc.EnterpriseID,
EnterpriseName: enterpriseName,
Status: acc.Status,
StatusName: getStatusName(acc.Status),
CreatedAt: acc.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
return &model.CustomerAccountPageResult{
Items: items,
Total: total,
Page: page,
Size: pageSize,
}, nil
}
func (s *Service) Create(ctx context.Context, req *model.CreateCustomerAccountReq) (*model.CustomerAccountItem, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
_, err := s.shopStore.GetByID(ctx, req.ShopID)
if err != nil {
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
}
existingAccount, _ := s.accountStore.GetByPhone(ctx, req.Phone)
if existingAccount != nil {
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
return nil, fmt.Errorf("密码加密失败: %w", err)
}
account := &model.Account{
Username: req.Username,
Phone: req.Phone,
Password: string(hashedPassword),
UserType: constants.UserTypeAgent,
ShopID: &req.ShopID,
Status: constants.StatusEnabled,
}
account.Creator = currentUserID
account.Updater = currentUserID
if err := s.db.WithContext(ctx).Create(account).Error; err != nil {
return nil, fmt.Errorf("创建账号失败: %w", err)
}
shop, _ := s.shopStore.GetByID(ctx, req.ShopID)
shopName := ""
if shop != nil {
shopName = shop.ShopName
}
return &model.CustomerAccountItem{
ID: account.ID,
Username: account.Username,
Phone: account.Phone,
UserType: account.UserType,
UserTypeName: getUserTypeName(account.UserType),
ShopID: account.ShopID,
ShopName: shopName,
Status: account.Status,
StatusName: getStatusName(account.Status),
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}
func (s *Service) Update(ctx context.Context, id uint, req *model.UpdateCustomerAccountReq) (*model.CustomerAccountItem, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
account, err := s.accountStore.GetByID(ctx, id)
if err != nil {
return nil, errors.New(errors.CodeAccountNotFound, "账号不存在")
}
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
return nil, errors.New(errors.CodeForbidden, "无权限操作此账号")
}
if req.Username != nil {
account.Username = *req.Username
}
if req.Phone != nil {
if *req.Phone != account.Phone {
existingAccount, _ := s.accountStore.GetByPhone(ctx, *req.Phone)
if existingAccount != nil && existingAccount.ID != id {
return nil, errors.New(errors.CodePhoneExists, "手机号已被使用")
}
account.Phone = *req.Phone
}
}
account.Updater = currentUserID
if err := s.db.WithContext(ctx).Save(account).Error; err != nil {
return nil, fmt.Errorf("更新账号失败: %w", err)
}
shopName := ""
if account.ShopID != nil {
if shop, _ := s.shopStore.GetByID(ctx, *account.ShopID); shop != nil {
shopName = shop.ShopName
}
}
enterpriseName := ""
if account.EnterpriseID != nil {
if ent, _ := s.enterpriseStore.GetByID(ctx, *account.EnterpriseID); ent != nil {
enterpriseName = ent.EnterpriseName
}
}
return &model.CustomerAccountItem{
ID: account.ID,
Username: account.Username,
Phone: account.Phone,
UserType: account.UserType,
UserTypeName: getUserTypeName(account.UserType),
ShopID: account.ShopID,
ShopName: shopName,
EnterpriseID: account.EnterpriseID,
EnterpriseName: enterpriseName,
Status: account.Status,
StatusName: getStatusName(account.Status),
CreatedAt: account.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}
func (s *Service) UpdatePassword(ctx context.Context, id uint, password string) error {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return errors.New(errors.CodeUnauthorized, "未授权访问")
}
account, err := s.accountStore.GetByID(ctx, id)
if err != nil {
return errors.New(errors.CodeAccountNotFound, "账号不存在")
}
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
return errors.New(errors.CodeForbidden, "无权限操作此账号")
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return fmt.Errorf("密码加密失败: %w", err)
}
return s.db.WithContext(ctx).Model(&model.Account{}).
Where("id = ?", id).
Updates(map[string]interface{}{
"password": string(hashedPassword),
"updater": currentUserID,
}).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, "未授权访问")
}
account, err := s.accountStore.GetByID(ctx, id)
if err != nil {
return errors.New(errors.CodeAccountNotFound, "账号不存在")
}
if account.UserType != constants.UserTypeAgent && account.UserType != constants.UserTypeEnterprise {
return errors.New(errors.CodeForbidden, "无权限操作此账号")
}
return s.db.WithContext(ctx).Model(&model.Account{}).
Where("id = ?", id).
Updates(map[string]interface{}{
"status": status,
"updater": currentUserID,
}).Error
}
func getUserTypeName(userType int) string {
switch userType {
case constants.UserTypeAgent:
return "代理账号"
case constants.UserTypeEnterprise:
return "企业账号"
default:
return "未知"
}
}
func getStatusName(status int) string {
if status == constants.StatusEnabled {
return "启用"
}
return "禁用"
}

View File

@@ -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 "禁用"
}

View File

@@ -0,0 +1,440 @@
package enterprise_card
import (
"context"
"fmt"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"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"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
enterpriseStore *postgres.EnterpriseStore
enterpriseCardAuthStore *postgres.EnterpriseCardAuthorizationStore
}
func New(
db *gorm.DB,
enterpriseStore *postgres.EnterpriseStore,
enterpriseCardAuthStore *postgres.EnterpriseCardAuthorizationStore,
) *Service {
return &Service{
db: db,
enterpriseStore: enterpriseStore,
enterpriseCardAuthStore: enterpriseCardAuthStore,
}
}
func (s *Service) AllocateCardsPreview(ctx context.Context, enterpriseID uint, req *model.AllocateCardsPreviewReq) (*model.AllocateCardsPreviewResp, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
var iotCards []model.IotCard
if err := s.db.WithContext(ctx).Where("iccid IN ?", req.ICCIDs).Find(&iotCards).Error; err != nil {
return nil, fmt.Errorf("查询卡信息失败: %w", err)
}
cardMap := make(map[string]*model.IotCard)
cardIDMap := make(map[uint]*model.IotCard)
for i := range iotCards {
cardMap[iotCards[i].ICCID] = &iotCards[i]
cardIDMap[iotCards[i].ID] = &iotCards[i]
}
cardIDs := make([]uint, 0, len(iotCards))
for _, card := range iotCards {
cardIDs = append(cardIDs, card.ID)
}
var bindings []model.DeviceSimBinding
if len(cardIDs) > 0 {
s.db.WithContext(ctx).Where("iot_card_id IN ? AND bind_status = 1", cardIDs).Find(&bindings)
}
cardToDevice := make(map[uint]uint)
deviceCards := make(map[uint][]uint)
for _, binding := range bindings {
cardToDevice[binding.IotCardID] = binding.DeviceID
deviceCards[binding.DeviceID] = append(deviceCards[binding.DeviceID], binding.IotCardID)
}
deviceIDs := make([]uint, 0, len(deviceCards))
for deviceID := range deviceCards {
deviceIDs = append(deviceIDs, deviceID)
}
var devices []model.Device
deviceMap := make(map[uint]*model.Device)
if len(deviceIDs) > 0 {
s.db.WithContext(ctx).Where("id IN ?", deviceIDs).Find(&devices)
for i := range devices {
deviceMap[devices[i].ID] = &devices[i]
}
}
resp := &model.AllocateCardsPreviewResp{
StandaloneCards: make([]model.StandaloneCard, 0),
DeviceBundles: make([]model.DeviceBundle, 0),
FailedItems: make([]model.FailedItem, 0),
}
processedDevices := make(map[uint]bool)
for _, iccid := range req.ICCIDs {
card, exists := cardMap[iccid]
if !exists {
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
ICCID: iccid,
Reason: "卡不存在",
})
continue
}
deviceID, hasDevice := cardToDevice[card.ID]
if !hasDevice {
resp.StandaloneCards = append(resp.StandaloneCards, model.StandaloneCard{
ICCID: card.ICCID,
IotCardID: card.ID,
MSISDN: card.MSISDN,
CarrierID: card.CarrierID,
StatusName: getCardStatusName(card.Status),
})
} else {
if processedDevices[deviceID] {
continue
}
processedDevices[deviceID] = true
device := deviceMap[deviceID]
if device == nil {
continue
}
bundleCardIDs := deviceCards[deviceID]
bundle := model.DeviceBundle{
DeviceID: deviceID,
DeviceNo: device.DeviceNo,
BundleCards: make([]model.DeviceBundleCard, 0),
}
for _, bundleCardID := range bundleCardIDs {
bundleCard := cardIDMap[bundleCardID]
if bundleCard == nil {
continue
}
if bundleCard.ID == card.ID {
bundle.TriggerCard = model.DeviceBundleCard{
ICCID: bundleCard.ICCID,
IotCardID: bundleCard.ID,
MSISDN: bundleCard.MSISDN,
}
} else {
bundle.BundleCards = append(bundle.BundleCards, model.DeviceBundleCard{
ICCID: bundleCard.ICCID,
IotCardID: bundleCard.ID,
MSISDN: bundleCard.MSISDN,
})
}
}
resp.DeviceBundles = append(resp.DeviceBundles, bundle)
}
}
deviceCardCount := 0
for _, bundle := range resp.DeviceBundles {
deviceCardCount += 1 + len(bundle.BundleCards)
}
resp.Summary = model.AllocatePreviewSummary{
StandaloneCardCount: len(resp.StandaloneCards),
DeviceCount: len(resp.DeviceBundles),
DeviceCardCount: deviceCardCount,
TotalCardCount: len(resp.StandaloneCards) + deviceCardCount,
FailedCount: len(resp.FailedItems),
}
return resp, nil
}
func (s *Service) AllocateCards(ctx context.Context, enterpriseID uint, req *model.AllocateCardsReq) (*model.AllocateCardsResp, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
currentShopID := middleware.GetShopIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
preview, err := s.AllocateCardsPreview(ctx, enterpriseID, &model.AllocateCardsPreviewReq{ICCIDs: req.ICCIDs})
if err != nil {
return nil, err
}
if len(preview.DeviceBundles) > 0 && !req.ConfirmDeviceBundles {
return nil, errors.New(errors.CodeInvalidParam, "存在设备包,请确认整体授权设备下所有卡")
}
resp := &model.AllocateCardsResp{
FailedItems: preview.FailedItems,
FailCount: len(preview.FailedItems),
AllocatedDevices: make([]model.AllocatedDevice, 0),
}
cardIDsToAllocate := make([]uint, 0)
for _, card := range preview.StandaloneCards {
cardIDsToAllocate = append(cardIDsToAllocate, card.IotCardID)
}
for _, bundle := range preview.DeviceBundles {
cardIDsToAllocate = append(cardIDsToAllocate, bundle.TriggerCard.IotCardID)
for _, card := range bundle.BundleCards {
cardIDsToAllocate = append(cardIDsToAllocate, card.IotCardID)
}
iccids := []string{bundle.TriggerCard.ICCID}
for _, card := range bundle.BundleCards {
iccids = append(iccids, card.ICCID)
}
resp.AllocatedDevices = append(resp.AllocatedDevices, model.AllocatedDevice{
DeviceID: bundle.DeviceID,
DeviceNo: bundle.DeviceNo,
CardCount: 1 + len(bundle.BundleCards),
ICCIDs: iccids,
})
}
existingAuths, err := s.enterpriseCardAuthStore.GetActiveAuthsByCardIDs(ctx, enterpriseID, cardIDsToAllocate)
if err != nil {
return nil, fmt.Errorf("查询已有授权失败: %w", err)
}
now := time.Now()
auths := make([]*model.EnterpriseCardAuthorization, 0)
for _, cardID := range cardIDsToAllocate {
if existingAuths[cardID] {
continue
}
auths = append(auths, &model.EnterpriseCardAuthorization{
EnterpriseID: enterpriseID,
IotCardID: cardID,
ShopID: currentShopID,
AuthorizedBy: currentUserID,
AuthorizedAt: &now,
Status: 1,
})
}
if len(auths) > 0 {
if err := s.enterpriseCardAuthStore.BatchCreate(ctx, auths); err != nil {
return nil, fmt.Errorf("创建授权记录失败: %w", err)
}
}
resp.SuccessCount = len(cardIDsToAllocate)
return resp, nil
}
func (s *Service) RecallCards(ctx context.Context, enterpriseID uint, req *model.RecallCardsReq) (*model.RecallCardsResp, error) {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
}
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
var iotCards []model.IotCard
if err := s.db.WithContext(ctx).Where("iccid IN ?", req.ICCIDs).Find(&iotCards).Error; err != nil {
return nil, fmt.Errorf("查询卡信息失败: %w", err)
}
cardMap := make(map[string]*model.IotCard)
cardIDMap := make(map[uint]*model.IotCard)
cardIDs := make([]uint, 0, len(iotCards))
for i := range iotCards {
cardMap[iotCards[i].ICCID] = &iotCards[i]
cardIDMap[iotCards[i].ID] = &iotCards[i]
cardIDs = append(cardIDs, iotCards[i].ID)
}
existingAuths, err := s.enterpriseCardAuthStore.GetActiveAuthsByCardIDs(ctx, enterpriseID, cardIDs)
if err != nil {
return nil, fmt.Errorf("查询已有授权失败: %w", err)
}
resp := &model.RecallCardsResp{
FailedItems: make([]model.FailedItem, 0),
RecalledDevices: make([]model.RecalledDevice, 0),
}
cardIDsToRecall := make([]uint, 0)
for _, iccid := range req.ICCIDs {
card, exists := cardMap[iccid]
if !exists {
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
ICCID: iccid,
Reason: "卡不存在",
})
continue
}
if !existingAuths[card.ID] {
resp.FailedItems = append(resp.FailedItems, model.FailedItem{
ICCID: iccid,
Reason: "该卡未授权给此企业",
})
continue
}
cardIDsToRecall = append(cardIDsToRecall, card.ID)
}
if len(cardIDsToRecall) > 0 {
if err := s.enterpriseCardAuthStore.BatchUpdateStatus(ctx, enterpriseID, cardIDsToRecall, 0); err != nil {
return nil, fmt.Errorf("回收授权失败: %w", err)
}
}
resp.SuccessCount = len(cardIDsToRecall)
resp.FailCount = len(resp.FailedItems)
return resp, nil
}
func (s *Service) ListCards(ctx context.Context, enterpriseID uint, req *model.EnterpriseCardListReq) (*model.EnterpriseCardPageResult, error) {
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
if err != nil {
return nil, errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
cardIDs, err := s.enterpriseCardAuthStore.ListCardIDsByEnterprise(ctx, enterpriseID)
if err != nil {
return nil, fmt.Errorf("查询授权卡ID失败: %w", err)
}
if len(cardIDs) == 0 {
return &model.EnterpriseCardPageResult{
Items: make([]model.EnterpriseCardItem, 0),
Total: 0,
Page: req.Page,
Size: req.PageSize,
}, nil
}
page := req.Page
pageSize := req.PageSize
if page == 0 {
page = 1
}
if pageSize == 0 {
pageSize = constants.DefaultPageSize
}
query := s.db.WithContext(ctx).Model(&model.IotCard{}).Where("id IN ?", cardIDs)
if req.Status != nil {
query = query.Where("status = ?", *req.Status)
}
if req.CarrierID != nil {
query = query.Where("carrier_id = ?", *req.CarrierID)
}
if req.ICCID != "" {
query = query.Where("iccid LIKE ?", "%"+req.ICCID+"%")
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, fmt.Errorf("统计卡数量失败: %w", err)
}
var cards []model.IotCard
offset := (page - 1) * pageSize
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&cards).Error; err != nil {
return nil, fmt.Errorf("查询卡列表失败: %w", err)
}
items := make([]model.EnterpriseCardItem, 0, len(cards))
for _, card := range cards {
items = append(items, model.EnterpriseCardItem{
ID: card.ID,
ICCID: card.ICCID,
MSISDN: card.MSISDN,
CarrierID: card.CarrierID,
Status: card.Status,
StatusName: getCardStatusName(card.Status),
NetworkStatus: card.NetworkStatus,
NetworkStatusName: getNetworkStatusName(card.NetworkStatus),
})
}
return &model.EnterpriseCardPageResult{
Items: items,
Total: total,
Page: page,
Size: pageSize,
}, nil
}
func (s *Service) SuspendCard(ctx context.Context, enterpriseID, cardID uint) error {
return s.updateCardNetworkStatus(ctx, enterpriseID, cardID, 0)
}
func (s *Service) ResumeCard(ctx context.Context, enterpriseID, cardID uint) error {
return s.updateCardNetworkStatus(ctx, enterpriseID, cardID, 1)
}
func (s *Service) updateCardNetworkStatus(ctx context.Context, enterpriseID, cardID uint, networkStatus int) error {
currentUserID := middleware.GetUserIDFromContext(ctx)
if currentUserID == 0 {
return errors.New(errors.CodeUnauthorized, "未授权访问")
}
_, err := s.enterpriseStore.GetByID(ctx, enterpriseID)
if err != nil {
return errors.New(errors.CodeEnterpriseNotFound, "企业不存在")
}
auth, err := s.enterpriseCardAuthStore.GetByEnterpriseAndCard(ctx, enterpriseID, cardID)
if err != nil || auth.Status != 1 {
return errors.New(errors.CodeForbidden, "无权限操作此卡")
}
return s.db.WithContext(ctx).Model(&model.IotCard{}).
Where("id = ?", cardID).
Update("network_status", networkStatus).Error
}
func getCardStatusName(status int) string {
switch status {
case 1:
return "在库"
case 2:
return "已分销"
case 3:
return "已激活"
case 4:
return "已停用"
default:
return "未知"
}
}
func getNetworkStatusName(status int) string {
if status == 1 {
return "开机"
}
return "停机"
}

View File

@@ -0,0 +1,416 @@
package my_commission
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"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"
"gorm.io/gorm"
)
type Service struct {
db *gorm.DB
shopStore *postgres.ShopStore
walletStore *postgres.WalletStore
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore
commissionRecordStore *postgres.CommissionRecordStore
walletTransactionStore *postgres.WalletTransactionStore
}
func New(
db *gorm.DB,
shopStore *postgres.ShopStore,
walletStore *postgres.WalletStore,
commissionWithdrawalRequestStore *postgres.CommissionWithdrawalRequestStore,
commissionWithdrawalSettingStore *postgres.CommissionWithdrawalSettingStore,
commissionRecordStore *postgres.CommissionRecordStore,
walletTransactionStore *postgres.WalletTransactionStore,
) *Service {
return &Service{
db: db,
shopStore: shopStore,
walletStore: walletStore,
commissionWithdrawalRequestStore: commissionWithdrawalRequestStore,
commissionWithdrawalSettingStore: commissionWithdrawalSettingStore,
commissionRecordStore: commissionRecordStore,
walletTransactionStore: walletTransactionStore,
}
}
// GetCommissionSummary 获取我的佣金概览
func (s *Service) GetCommissionSummary(ctx context.Context) (*model.MyCommissionSummaryResp, error) {
userType := middleware.GetUserTypeFromContext(ctx)
if userType != constants.UserTypeAgent {
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
}
shopID := middleware.GetShopIDFromContext(ctx)
if shopID == 0 {
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
}
shop, err := s.shopStore.GetByID(ctx, shopID)
if err != nil {
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
}
// 使用 GetShopCommissionWallet 获取店铺佣金钱包
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
if err != nil {
// 钱包不存在时返回空数据
return &model.MyCommissionSummaryResp{
ShopID: shopID,
ShopName: shop.ShopName,
}, nil
}
// 计算累计佣金(当前余额 + 冻结余额 + 已提现金额)
// 由于 Wallet 模型没有 TotalIncome、TotalWithdrawn 字段,
// 我们需要从 WalletTransaction 表计算或简化处理
var totalWithdrawn int64
s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
Where("shop_id = ? AND status IN ?", shopID, []int{2, 4}). // 已通过或已到账
Select("COALESCE(SUM(actual_amount), 0)").Scan(&totalWithdrawn)
totalCommission := wallet.Balance + wallet.FrozenBalance + totalWithdrawn
return &model.MyCommissionSummaryResp{
ShopID: shopID,
ShopName: shop.ShopName,
TotalCommission: totalCommission,
WithdrawnCommission: totalWithdrawn,
UnwithdrawCommission: wallet.Balance + wallet.FrozenBalance,
FrozenCommission: wallet.FrozenBalance,
WithdrawingCommission: wallet.FrozenBalance, // 提现中的金额等于冻结金额
AvailableCommission: wallet.Balance,
}, nil
}
// CreateWithdrawalRequest 发起提现申请
func (s *Service) CreateWithdrawalRequest(ctx context.Context, req *model.CreateMyWithdrawalReq) (*model.CreateMyWithdrawalResp, error) {
userType := middleware.GetUserTypeFromContext(ctx)
if userType != constants.UserTypeAgent {
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
}
shopID := middleware.GetShopIDFromContext(ctx)
currentUserID := middleware.GetUserIDFromContext(ctx)
if shopID == 0 || currentUserID == 0 {
return nil, errors.New(errors.CodeForbidden, "无法获取用户信息")
}
// 获取提现配置
setting, err := s.commissionWithdrawalSettingStore.GetCurrent(ctx)
if err != nil {
return nil, errors.New(errors.CodeInvalidParam, "暂未开放提现功能")
}
// 验证最低提现金额
if req.Amount < setting.MinWithdrawalAmount {
return nil, errors.New(errors.CodeInvalidParam, fmt.Sprintf("提现金额不能低于 %.2f 元", float64(setting.MinWithdrawalAmount)/100))
}
// 获取钱包
wallet, err := s.walletStore.GetShopCommissionWallet(ctx, shopID)
if err != nil {
return nil, errors.New(errors.CodeInsufficientBalance, "钱包不存在")
}
// 验证余额
if req.Amount > wallet.Balance {
return nil, errors.New(errors.CodeInsufficientBalance, "可提现余额不足")
}
// 验证今日提现次数
today := time.Now().Format("2006-01-02")
todayStart := today + " 00:00:00"
todayEnd := today + " 23:59:59"
var todayCount int64
s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
Where("shop_id = ? AND created_at >= ? AND created_at <= ?", shopID, todayStart, todayEnd).
Count(&todayCount)
if int(todayCount) >= setting.DailyWithdrawalLimit {
return nil, errors.New(errors.CodeInvalidParam, "今日提现次数已达上限")
}
// 计算手续费
fee := req.Amount * setting.FeeRate / 10000
actualAmount := req.Amount - fee
// 生成提现单号
withdrawalNo := generateWithdrawalNo()
// 构建账户信息 JSON
accountInfo := map[string]string{
"account_name": req.AccountName,
"account_number": req.AccountNumber,
}
accountInfoJSON, _ := json.Marshal(accountInfo)
var withdrawalRequest *model.CommissionWithdrawalRequest
err = s.db.Transaction(func(tx *gorm.DB) error {
// 冻结余额
if err := tx.WithContext(ctx).Model(&model.Wallet{}).
Where("id = ? AND balance >= ?", wallet.ID, req.Amount).
Updates(map[string]interface{}{
"balance": gorm.Expr("balance - ?", req.Amount),
"frozen_balance": gorm.Expr("frozen_balance + ?", req.Amount),
}).Error; err != nil {
return fmt.Errorf("冻结余额失败: %w", err)
}
// 创建提现申请
withdrawalRequest = &model.CommissionWithdrawalRequest{
WithdrawalNo: withdrawalNo,
ShopID: shopID,
ApplicantID: currentUserID,
Amount: req.Amount,
FeeRate: setting.FeeRate,
Fee: fee,
ActualAmount: actualAmount,
WithdrawalMethod: req.WithdrawalMethod,
AccountInfo: accountInfoJSON,
Status: 1, // 待审核
}
withdrawalRequest.Creator = currentUserID
withdrawalRequest.Updater = currentUserID
if err := tx.WithContext(ctx).Create(withdrawalRequest).Error; err != nil {
return fmt.Errorf("创建提现申请失败: %w", err)
}
// 创建钱包流水记录
remark := fmt.Sprintf("提现冻结,单号:%s", withdrawalNo)
refType := constants.ReferenceTypeWithdrawal
transaction := &model.WalletTransaction{
WalletID: wallet.ID,
UserID: currentUserID,
TransactionType: constants.TransactionTypeWithdrawal,
Amount: -req.Amount, // 冻结为负数
BalanceBefore: wallet.Balance,
BalanceAfter: wallet.Balance - req.Amount,
Status: constants.TransactionStatusProcessing, // 处理中
ReferenceType: &refType,
ReferenceID: &withdrawalRequest.ID,
Remark: &remark,
Creator: currentUserID,
}
if err := tx.WithContext(ctx).Create(transaction).Error; err != nil {
return fmt.Errorf("创建钱包流水失败: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
return &model.CreateMyWithdrawalResp{
ID: withdrawalRequest.ID,
WithdrawalNo: withdrawalRequest.WithdrawalNo,
Amount: withdrawalRequest.Amount,
FeeRate: withdrawalRequest.FeeRate,
Fee: withdrawalRequest.Fee,
ActualAmount: withdrawalRequest.ActualAmount,
Status: withdrawalRequest.Status,
StatusName: getWithdrawalStatusName(withdrawalRequest.Status),
CreatedAt: withdrawalRequest.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}
// ListMyWithdrawalRequests 查询我的提现记录
func (s *Service) ListMyWithdrawalRequests(ctx context.Context, req *model.MyWithdrawalListReq) (*model.WithdrawalRequestPageResult, error) {
userType := middleware.GetUserTypeFromContext(ctx)
if userType != constants.UserTypeAgent {
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
}
shopID := middleware.GetShopIDFromContext(ctx)
if shopID == 0 {
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
}
page := req.Page
pageSize := req.PageSize
if page == 0 {
page = 1
}
if pageSize == 0 {
pageSize = constants.DefaultPageSize
}
query := s.db.WithContext(ctx).Model(&model.CommissionWithdrawalRequest{}).
Where("shop_id = ?", shopID)
if req.Status != nil {
query = query.Where("status = ?", *req.Status)
}
if req.StartTime != "" {
query = query.Where("created_at >= ?", req.StartTime)
}
if req.EndTime != "" {
query = query.Where("created_at <= ?", req.EndTime)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, fmt.Errorf("统计提现记录失败: %w", err)
}
var requests []model.CommissionWithdrawalRequest
offset := (page - 1) * pageSize
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&requests).Error; err != nil {
return nil, fmt.Errorf("查询提现记录失败: %w", err)
}
items := make([]model.WithdrawalRequestItem, 0, len(requests))
for _, r := range requests {
// 解析账户信息
accountName, accountNumber := parseAccountInfo(r.AccountInfo)
items = append(items, model.WithdrawalRequestItem{
ID: r.ID,
WithdrawalNo: r.WithdrawalNo,
ShopID: r.ShopID,
Amount: r.Amount,
FeeRate: r.FeeRate,
Fee: r.Fee,
ActualAmount: r.ActualAmount,
WithdrawalMethod: r.WithdrawalMethod,
AccountName: accountName,
AccountNumber: accountNumber,
Status: r.Status,
StatusName: getWithdrawalStatusName(r.Status),
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
return &model.WithdrawalRequestPageResult{
Items: items,
Total: total,
Page: page,
Size: pageSize,
}, nil
}
// ListMyCommissionRecords 查询我的佣金明细
func (s *Service) ListMyCommissionRecords(ctx context.Context, req *model.MyCommissionRecordListReq) (*model.MyCommissionRecordPageResult, error) {
userType := middleware.GetUserTypeFromContext(ctx)
if userType != constants.UserTypeAgent {
return nil, errors.New(errors.CodeForbidden, "仅代理商用户可访问")
}
shopID := middleware.GetShopIDFromContext(ctx)
if shopID == 0 {
return nil, errors.New(errors.CodeForbidden, "无法获取店铺信息")
}
page := req.Page
pageSize := req.PageSize
if page == 0 {
page = 1
}
if pageSize == 0 {
pageSize = constants.DefaultPageSize
}
query := s.db.WithContext(ctx).Model(&model.CommissionRecord{}).
Where("shop_id = ?", shopID)
if req.CommissionType != nil {
query = query.Where("commission_type = ?", *req.CommissionType)
}
var total int64
if err := query.Count(&total).Error; err != nil {
return nil, fmt.Errorf("统计佣金记录失败: %w", err)
}
var records []model.CommissionRecord
offset := (page - 1) * pageSize
if err := query.Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&records).Error; err != nil {
return nil, fmt.Errorf("查询佣金记录失败: %w", err)
}
items := make([]model.MyCommissionRecordItem, 0, len(records))
for _, r := range records {
items = append(items, model.MyCommissionRecordItem{
ID: r.ID,
ShopID: r.ShopID,
OrderID: r.OrderID,
CommissionType: r.CommissionType,
Amount: r.Amount,
Status: r.Status,
StatusName: getCommissionStatusName(r.Status),
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
})
}
return &model.MyCommissionRecordPageResult{
Items: items,
Total: total,
Page: page,
Size: pageSize,
}, nil
}
// generateWithdrawalNo 生成提现单号
func generateWithdrawalNo() string {
now := time.Now()
return fmt.Sprintf("W%s%04d", now.Format("20060102150405"), rand.Intn(10000))
}
// getWithdrawalStatusName 获取提现状态名称
func getWithdrawalStatusName(status int) string {
switch status {
case 1:
return "待审核"
case 2:
return "已通过"
case 3:
return "已拒绝"
case 4:
return "已到账"
default:
return "未知"
}
}
// getCommissionStatusName 获取佣金状态名称
func getCommissionStatusName(status int) string {
switch status {
case 1:
return "已冻结"
case 2:
return "解冻中"
case 3:
return "已发放"
case 4:
return "已失效"
default:
return "未知"
}
}
// parseAccountInfo 解析账户信息 JSON
func parseAccountInfo(data []byte) (accountName, accountNumber string) {
if len(data) == 0 {
return "", ""
}
var info map[string]string
if err := json.Unmarshal(data, &info); err != nil {
return "", ""
}
return info["account_name"], info["account_number"]
}

View File

@@ -0,0 +1,427 @@
package shop_commission
import (
"context"
"encoding/json"
"fmt"
"time"
"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"
)
type Service struct {
shopStore *postgres.ShopStore
accountStore *postgres.AccountStore
walletStore *postgres.WalletStore
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore
commissionRecordStore *postgres.CommissionRecordStore
}
func New(
shopStore *postgres.ShopStore,
accountStore *postgres.AccountStore,
walletStore *postgres.WalletStore,
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore,
commissionRecordStore *postgres.CommissionRecordStore,
) *Service {
return &Service{
shopStore: shopStore,
accountStore: accountStore,
walletStore: walletStore,
commissionWithdrawalReqStore: commissionWithdrawalReqStore,
commissionRecordStore: commissionRecordStore,
}
}
func (s *Service) ListShopCommissionSummary(ctx context.Context, req *model.ShopCommissionSummaryListReq) (*model.ShopCommissionSummaryPageResult, error) {
opts := &store.QueryOptions{
Page: req.Page,
PageSize: req.PageSize,
OrderBy: "created_at DESC",
}
if opts.Page == 0 {
opts.Page = 1
}
if opts.PageSize == 0 {
opts.PageSize = constants.DefaultPageSize
}
filters := make(map[string]interface{})
if req.ShopName != "" {
filters["shop_name"] = req.ShopName
}
shops, total, err := s.shopStore.List(ctx, opts, filters)
if err != nil {
return nil, fmt.Errorf("查询店铺列表失败: %w", err)
}
if len(shops) == 0 {
return &model.ShopCommissionSummaryPageResult{
Items: []model.ShopCommissionSummaryItem{},
Total: 0,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
shopIDs := make([]uint, 0, len(shops))
for _, shop := range shops {
shopIDs = append(shopIDs, shop.ID)
}
walletSummaries, err := s.walletStore.GetShopCommissionSummaryBatch(ctx, shopIDs)
if err != nil {
return nil, fmt.Errorf("查询店铺钱包汇总失败: %w", err)
}
withdrawnAmounts, err := s.commissionWithdrawalReqStore.SumAmountByShopIDsAndStatus(ctx, shopIDs, constants.WithdrawalStatusApproved)
if err != nil {
return nil, fmt.Errorf("查询已提现金额失败: %w", err)
}
withdrawingAmounts, err := s.commissionWithdrawalReqStore.SumAmountByShopIDsAndStatus(ctx, shopIDs, constants.WithdrawalStatusPending)
if err != nil {
return nil, fmt.Errorf("查询提现中金额失败: %w", err)
}
primaryAccounts, err := s.accountStore.GetPrimaryAccountsByShopIDs(ctx, shopIDs)
if err != nil {
return nil, fmt.Errorf("查询主账号失败: %w", err)
}
accountMap := make(map[uint]*model.Account)
for _, acc := range primaryAccounts {
if acc.ShopID != nil {
accountMap[*acc.ShopID] = acc
}
}
items := make([]model.ShopCommissionSummaryItem, 0, len(shops))
for _, shop := range shops {
if req.Username != "" {
acc := accountMap[shop.ID]
if acc == nil || !containsSubstring(acc.Username, req.Username) {
total--
continue
}
}
item := s.buildCommissionSummaryItem(shop, walletSummaries[shop.ID], withdrawnAmounts[shop.ID], withdrawingAmounts[shop.ID], accountMap[shop.ID])
items = append(items, item)
}
return &model.ShopCommissionSummaryPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func (s *Service) buildCommissionSummaryItem(shop *model.Shop, walletSummary *postgres.ShopCommissionSummary, withdrawnAmount, withdrawingAmount int64, account *model.Account) model.ShopCommissionSummaryItem {
var balance, frozenBalance int64
if walletSummary != nil {
balance = walletSummary.Balance
frozenBalance = walletSummary.FrozenBalance
}
totalCommission := balance + frozenBalance + withdrawnAmount
unwithdrawCommission := totalCommission - withdrawnAmount
availableCommission := balance - withdrawingAmount
if availableCommission < 0 {
availableCommission = 0
}
var username, phone string
if account != nil {
username = account.Username
phone = account.Phone
}
return model.ShopCommissionSummaryItem{
ShopID: shop.ID,
ShopName: shop.ShopName,
ShopCode: shop.ShopCode,
Username: username,
Phone: phone,
TotalCommission: totalCommission,
WithdrawnCommission: withdrawnAmount,
UnwithdrawCommission: unwithdrawCommission,
FrozenCommission: frozenBalance,
WithdrawingCommission: withdrawingAmount,
AvailableCommission: availableCommission,
CreatedAt: shop.CreatedAt.Format("2006-01-02 15:04:05"),
}
}
func (s *Service) ListShopWithdrawalRequests(ctx context.Context, shopID uint, req *model.ShopWithdrawalRequestListReq) (*model.ShopWithdrawalRequestPageResult, error) {
_, err := s.shopStore.GetByID(ctx, shopID)
if err != nil {
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
}
opts := &store.QueryOptions{
Page: req.Page,
PageSize: req.PageSize,
OrderBy: "created_at DESC",
}
if opts.Page == 0 {
opts.Page = 1
}
if opts.PageSize == 0 {
opts.PageSize = constants.DefaultPageSize
}
filters := &postgres.WithdrawalRequestListFilters{
ShopID: shopID,
WithdrawalNo: req.WithdrawalNo,
}
if req.StartTime != "" {
t, err := time.Parse("2006-01-02 15:04:05", req.StartTime)
if err == nil {
filters.StartTime = &t
}
}
if req.EndTime != "" {
t, err := time.Parse("2006-01-02 15:04:05", req.EndTime)
if err == nil {
filters.EndTime = &t
}
}
requests, total, err := s.commissionWithdrawalReqStore.ListByShopID(ctx, opts, filters)
if err != nil {
return nil, fmt.Errorf("查询提现记录失败: %w", err)
}
shop, _ := s.shopStore.GetByID(ctx, shopID)
shopHierarchy := s.buildShopHierarchyPath(ctx, shop)
applicantIDs := make([]uint, 0)
processorIDs := make([]uint, 0)
for _, r := range requests {
if r.ApplicantID > 0 {
applicantIDs = append(applicantIDs, r.ApplicantID)
}
if r.ProcessorID > 0 {
processorIDs = append(processorIDs, r.ProcessorID)
}
}
applicantMap := make(map[uint]string)
processorMap := make(map[uint]string)
if len(applicantIDs) > 0 {
accounts, _ := s.accountStore.GetByIDs(ctx, applicantIDs)
for _, acc := range accounts {
applicantMap[acc.ID] = acc.Username
}
}
if len(processorIDs) > 0 {
accounts, _ := s.accountStore.GetByIDs(ctx, processorIDs)
for _, acc := range accounts {
processorMap[acc.ID] = acc.Username
}
}
items := make([]model.ShopWithdrawalRequestItem, 0, len(requests))
for _, r := range requests {
item := s.buildWithdrawalRequestItem(r, shop.ShopName, shopHierarchy, applicantMap, processorMap)
items = append(items, item)
}
return &model.ShopWithdrawalRequestPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func (s *Service) buildWithdrawalRequestItem(r *model.CommissionWithdrawalRequest, shopName, shopHierarchy string, applicantMap, processorMap map[uint]string) model.ShopWithdrawalRequestItem {
var processorID *uint
if r.ProcessorID > 0 {
processorID = &r.ProcessorID
}
var accountName, accountNumber, bankName string
if len(r.AccountInfo) > 0 {
var info map[string]interface{}
if err := json.Unmarshal(r.AccountInfo, &info); err == nil {
if v, ok := info["account_name"].(string); ok {
accountName = v
}
if v, ok := info["account_number"].(string); ok {
accountNumber = v
}
if v, ok := info["bank_name"].(string); ok {
bankName = v
}
}
}
var processedAt, paidAt string
if r.ProcessedAt != nil {
processedAt = r.ProcessedAt.Format("2006-01-02 15:04:05")
}
if r.PaidAt != nil {
paidAt = r.PaidAt.Format("2006-01-02 15:04:05")
}
return model.ShopWithdrawalRequestItem{
ID: r.ID,
WithdrawalNo: r.WithdrawalNo,
Amount: r.Amount,
FeeRate: r.FeeRate,
Fee: r.Fee,
ActualAmount: r.ActualAmount,
Status: r.Status,
StatusName: getWithdrawalStatusName(r.Status),
ShopID: r.ShopID,
ShopName: shopName,
ShopHierarchy: shopHierarchy,
ApplicantID: r.ApplicantID,
ApplicantName: applicantMap[r.ApplicantID],
ProcessorID: processorID,
ProcessorName: processorMap[r.ProcessorID],
WithdrawalMethod: r.WithdrawalMethod,
PaymentType: r.PaymentType,
AccountName: accountName,
AccountNumber: accountNumber,
BankName: bankName,
RejectReason: r.RejectReason,
Remark: r.Remark,
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
ProcessedAt: processedAt,
PaidAt: paidAt,
}
}
func (s *Service) buildShopHierarchyPath(ctx context.Context, shop *model.Shop) string {
if shop == nil {
return ""
}
path := shop.ShopName
current := shop
depth := 0
for current.ParentID != nil && depth < 2 {
parent, err := s.shopStore.GetByID(ctx, *current.ParentID)
if err != nil {
break
}
path = parent.ShopName + "_" + path
current = parent
depth++
}
return path
}
func (s *Service) ListShopCommissionRecords(ctx context.Context, shopID uint, req *model.ShopCommissionRecordListReq) (*model.ShopCommissionRecordPageResult, error) {
_, err := s.shopStore.GetByID(ctx, shopID)
if err != nil {
return nil, errors.New(errors.CodeShopNotFound, "店铺不存在")
}
opts := &store.QueryOptions{
Page: req.Page,
PageSize: req.PageSize,
OrderBy: "created_at DESC",
}
if opts.Page == 0 {
opts.Page = 1
}
if opts.PageSize == 0 {
opts.PageSize = constants.DefaultPageSize
}
filters := &postgres.CommissionRecordListFilters{
ShopID: shopID,
CommissionType: req.CommissionType,
ICCID: req.ICCID,
DeviceNo: req.DeviceNo,
OrderNo: req.OrderNo,
}
records, total, err := s.commissionRecordStore.ListByShopID(ctx, opts, filters)
if err != nil {
return nil, fmt.Errorf("查询佣金明细失败: %w", err)
}
items := make([]model.ShopCommissionRecordItem, 0, len(records))
for _, r := range records {
item := model.ShopCommissionRecordItem{
ID: r.ID,
Amount: r.Amount,
BalanceAfter: r.BalanceAfter,
CommissionType: r.CommissionType,
Status: r.Status,
StatusName: getCommissionStatusName(r.Status),
OrderID: r.OrderID,
OrderNo: "",
DeviceNo: "",
ICCID: "",
OrderCreatedAt: "",
CreatedAt: r.CreatedAt.Format("2006-01-02 15:04:05"),
}
items = append(items, item)
}
return &model.ShopCommissionRecordPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func getWithdrawalStatusName(status int) string {
switch status {
case constants.WithdrawalStatusPending:
return "待审核"
case constants.WithdrawalStatusApproved:
return "已通过"
case constants.WithdrawalStatusRejected:
return "已拒绝"
case constants.WithdrawalStatusPaid:
return "已到账"
default:
return "未知"
}
}
func getCommissionStatusName(status int) string {
switch status {
case constants.CommissionStatusFrozen:
return "已冻结"
case constants.CommissionStatusUnfreezing:
return "解冻中"
case constants.CommissionStatusReleased:
return "已发放"
case constants.CommissionStatusInvalid:
return "已失效"
default:
return "未知"
}
}
func containsSubstring(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 || (len(s) > 0 && len(substr) > 0 && contains(s, substr)))
}
func contains(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}