Files
junhong_cmp_fiber/internal/service/commission_withdrawal/service.go
huang 409a68d60b
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 5m45s
feat: OpenAPI 契约对齐与框架优化
主要变更:
1. OpenAPI 文档契约对齐
   - 统一错误响应字段名为 msg(非 message)
   - 规范 envelope 响应结构(code, msg, data, timestamp)
   - 个人客户路由纳入文档体系(使用 Register 机制)
   - 新增 BuildDocHandlers() 统一管理 handler 构造
   - 确保文档生成的幂等性

2. Service 层错误处理统一
   - 全面替换 fmt.Errorf 为 errors.New/Wrap
   - 统一错误码使用规范
   - Handler 层参数校验不泄露底层细节
   - 新增错误码验证集成测试

3. 代码质量提升
   - 删除未使用的 Task handler 和路由
   - 新增代码规范检查脚本(check-service-errors.sh)
   - 新增注释路径一致性检查(check-comment-paths.sh)
   - 更新 API 文档生成指南

4. OpenSpec 归档
   - 归档 openapi-contract-alignment 变更(63 tasks)
   - 归档 service-error-unify-core 变更
   - 归档 service-error-unify-support 变更
   - 归档 code-cleanup-docs-update 变更
   - 归档 handler-validation-security 变更
   - 同步 delta specs 到主规范文件

影响范围:
- pkg/openapi: 新增 handlers.go,优化 generator.go
- internal/service/*: 48 个 service 文件错误处理统一
- internal/handler/admin: 优化参数校验错误提示
- internal/routes: 个人客户路由改造,删除 task 路由
- scripts: 新增 3 个代码检查脚本
- docs: 更新 OpenAPI 文档(15750+ 行)
- openspec/specs: 同步 3 个主规范文件

破坏性变更:无
向后兼容:是
2026-01-30 11:40:36 +08:00

421 lines
12 KiB
Go

package commission_withdrawal
import (
"context"
"encoding/json"
"time"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
"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 *dto.WithdrawalRequestListReq) (*dto.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, errors.Wrap(errors.CodeInternalError, 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([]dto.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 &dto.WithdrawalRequestPageResult{
Items: items,
Total: total,
Page: opts.Page,
Size: opts.PageSize,
}, nil
}
func (s *Service) Approve(ctx context.Context, id uint, req *dto.ApproveWithdrawalReq) (*dto.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 errors.Wrap(errors.CodeInternalError, 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 errors.Wrap(errors.CodeInternalError, 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 errors.Wrap(errors.CodeInternalError, err, "更新提现申请状态失败")
}
return nil
})
if err != nil {
return nil, err
}
return &dto.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 *dto.RejectWithdrawalReq) (*dto.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 errors.Wrap(errors.CodeInternalError, 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 errors.Wrap(errors.CodeInternalError, 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 errors.Wrap(errors.CodeInternalError, err, "更新提现申请状态失败")
}
return nil
})
if err != nil {
return nil, err
}
return &dto.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) dto.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 dto.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
}