2026-01-04 16:10:47 +08:00
|
|
|
|
package admin_agent
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
|
|
"tydata-server/app/main/api/internal/svc"
|
|
|
|
|
|
"tydata-server/app/main/api/internal/types"
|
|
|
|
|
|
"tydata-server/app/main/model"
|
|
|
|
|
|
"tydata-server/common/xerr"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type AdminUpdateAgentWalletBalanceLogic struct {
|
|
|
|
|
|
logx.Logger
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewAdminUpdateAgentWalletBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUpdateAgentWalletBalanceLogic {
|
|
|
|
|
|
return &AdminUpdateAgentWalletBalanceLogic{
|
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *AdminUpdateAgentWalletBalanceLogic) AdminUpdateAgentWalletBalance(req *types.AdminUpdateAgentWalletBalanceReq) (resp *types.AdminUpdateAgentWalletBalanceResp, err error) {
|
|
|
|
|
|
// 参数校验
|
|
|
|
|
|
if req.AgentId <= 0 {
|
|
|
|
|
|
return nil, xerr.NewErrMsg("代理ID无效")
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Amount == 0 {
|
|
|
|
|
|
return nil, xerr.NewErrMsg("修改金额不能为0")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询代理钱包信息
|
|
|
|
|
|
wallet, err := l.svcCtx.AgentWalletModel.FindOneByAgentId(l.ctx, req.AgentId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
|
|
|
|
return nil, xerr.NewErrMsg("代理钱包不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 计算新余额
|
|
|
|
|
|
newBalance := wallet.Balance + req.Amount
|
|
|
|
|
|
|
|
|
|
|
|
// 校验余额不能为负数
|
|
|
|
|
|
if newBalance < 0 {
|
|
|
|
|
|
return nil, xerr.NewErrMsg(fmt.Sprintf("操作后余额不能为负数,当前余额: %.2f,操作金额: %.2f", wallet.Balance, req.Amount))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新余额
|
|
|
|
|
|
updateErr := l.svcCtx.AgentWalletModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
|
2026-01-07 16:50:42 +08:00
|
|
|
|
// 记录变动前的余额
|
|
|
|
|
|
balanceBefore := wallet.Balance
|
|
|
|
|
|
frozenBalanceBefore := wallet.FrozenBalance
|
|
|
|
|
|
|
2026-01-04 16:10:47 +08:00
|
|
|
|
// 使用版本号更新
|
|
|
|
|
|
wallet.Balance = newBalance
|
|
|
|
|
|
err := l.svcCtx.AgentWalletModel.UpdateWithVersion(transCtx, session, wallet)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-07 16:50:42 +08:00
|
|
|
|
// 创建钱包交易流水记录(手动调整)
|
|
|
|
|
|
remark := fmt.Sprintf("管理员手动调整余额,金额: %.2f", req.Amount)
|
|
|
|
|
|
transErr := l.svcCtx.AgentService.CreateWalletTransaction(
|
|
|
|
|
|
transCtx,
|
|
|
|
|
|
session,
|
|
|
|
|
|
req.AgentId,
|
|
|
|
|
|
model.WalletTransactionTypeAdjust,
|
|
|
|
|
|
req.Amount, // 变动金额(正数表示增加,负数表示减少)
|
|
|
|
|
|
balanceBefore, // 变动前余额
|
|
|
|
|
|
wallet.Balance, // 变动后余额
|
|
|
|
|
|
frozenBalanceBefore, // 变动前冻结余额
|
|
|
|
|
|
wallet.FrozenBalance, // 变动后冻结余额(保持不变)
|
|
|
|
|
|
"", // 关联交易ID(无关联)
|
|
|
|
|
|
0, // 关联用户ID(无关联)
|
|
|
|
|
|
remark, // 备注
|
|
|
|
|
|
)
|
|
|
|
|
|
if transErr != nil {
|
|
|
|
|
|
l.Logger.Errorf("创建代理钱包流水记录失败: %+v", transErr)
|
|
|
|
|
|
return transErr
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 16:10:47 +08:00
|
|
|
|
l.Logger.Infof("代理钱包余额变更 - AgentId: %d, 原余额: %.2f, 变更金额: %.2f, 新余额: %.2f",
|
2026-01-07 16:50:42 +08:00
|
|
|
|
req.AgentId, balanceBefore, req.Amount, wallet.Balance)
|
2026-01-04 16:10:47 +08:00
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
if updateErr != nil {
|
|
|
|
|
|
l.Logger.Errorf("更新代理钱包余额失败: %+v", updateErr)
|
|
|
|
|
|
return nil, xerr.NewErrMsg("更新余额失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resp = &types.AdminUpdateAgentWalletBalanceResp{
|
|
|
|
|
|
Success: true,
|
|
|
|
|
|
Balance: newBalance,
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|