feat: 业务逻辑补全 — 佣金待审记录、C端订单重构、支付抽象、富友支付、卡设备状态联动
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
F-1: 佣金链断裂时创建 status=99 零额待审记录,新增修正接口 POST /commission-records/:id/resolve F-4: C端订单查询从 Handler 迁移至 Service 层,移除 SkipPermissionCtx J-1: 富友支付 JSAPI/MiniApp 预下单实现,回调补全签名验证 J-2: 平台钱包代购支持(buyerType 为空时使用资产所属代理钱包) J-3: 套餐激活后自动更新卡/设备 status=3,最后套餐过期后更新 status=4 支付抽象: 引入 PaymentProvider 接口 + 微信/富友适配器,CreateOrder 支持多支付渠道 修复: 设备/IoT卡响应 DTO 移除 omitempty,空值字段返回 null 而非省略
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
"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"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -19,6 +21,8 @@ type Service struct {
|
||||
agentWalletStore *postgres.AgentWalletStore
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore
|
||||
commissionRecordStore *postgres.CommissionRecordStore
|
||||
db *gorm.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -27,6 +31,8 @@ func New(
|
||||
agentWalletStore *postgres.AgentWalletStore,
|
||||
commissionWithdrawalReqStore *postgres.CommissionWithdrawalRequestStore,
|
||||
commissionRecordStore *postgres.CommissionRecordStore,
|
||||
db *gorm.DB,
|
||||
logger *zap.Logger,
|
||||
) *Service {
|
||||
return &Service{
|
||||
shopStore: shopStore,
|
||||
@@ -34,6 +40,8 @@ func New(
|
||||
agentWalletStore: agentWalletStore,
|
||||
commissionWithdrawalReqStore: commissionWithdrawalReqStore,
|
||||
commissionRecordStore: commissionRecordStore,
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -425,3 +433,77 @@ func contains(s, substr string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ResolveCommissionRecord 修正待审佣金记录(status=99)
|
||||
// release: 填入金额并入账到代理佣金钱包
|
||||
// invalidate: 标记为已失效
|
||||
func (s *Service) ResolveCommissionRecord(ctx context.Context, recordID uint, req *dto.CommissionRecordResolveRequest) error {
|
||||
record, err := s.commissionRecordStore.GetByID(ctx, recordID)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "佣金记录不存在")
|
||||
}
|
||||
|
||||
if record.Status != constants.CommissionStatusPendingReview {
|
||||
return errors.New(errors.CodeInvalidParam, "该记录不是待修正状态")
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
resolveRemark := record.Remark
|
||||
if req.Remark != "" {
|
||||
resolveRemark += " | 处理备注: " + req.Remark
|
||||
}
|
||||
|
||||
if req.Action == "invalidate" {
|
||||
return s.commissionRecordStore.UpdateByID(ctx, nil, recordID, map[string]any{
|
||||
"status": model.CommissionStatusInvalid,
|
||||
"remark": resolveRemark,
|
||||
})
|
||||
}
|
||||
|
||||
// release 入账
|
||||
if req.Amount == nil || *req.Amount <= 0 {
|
||||
return errors.New(errors.CodeInvalidParam, "入账操作必须指定金额")
|
||||
}
|
||||
amount := *req.Amount
|
||||
|
||||
wallet, err := s.agentWalletStore.GetCommissionWallet(ctx, record.ShopID)
|
||||
if err != nil {
|
||||
return errors.Wrap(errors.CodeNotFound, err, "店铺佣金钱包不存在")
|
||||
}
|
||||
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 更新佣金记录
|
||||
if err := s.commissionRecordStore.UpdateByID(ctx, tx, recordID, map[string]any{
|
||||
"status": model.CommissionStatusReleased,
|
||||
"amount": amount,
|
||||
"released_at": now,
|
||||
"remark": resolveRemark,
|
||||
}); err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新佣金记录失败")
|
||||
}
|
||||
|
||||
// 入账到佣金钱包(乐观锁)
|
||||
balanceBefore := wallet.Balance
|
||||
result := tx.Model(&model.AgentWallet{}).
|
||||
Where("id = ? AND version = ?", wallet.ID, wallet.Version).
|
||||
Updates(map[string]any{
|
||||
"balance": gorm.Expr("balance + ?", amount),
|
||||
"version": gorm.Expr("version + 1"),
|
||||
})
|
||||
if result.Error != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, result.Error, "更新佣金钱包余额失败")
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return errors.New(errors.CodeInternalError, "佣金钱包版本冲突,请重试")
|
||||
}
|
||||
|
||||
// 回写入账后余额
|
||||
if err := s.commissionRecordStore.UpdateByID(ctx, tx, recordID, map[string]any{
|
||||
"balance_after": balanceBefore + amount,
|
||||
}); err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "更新入账后余额失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user