package agent import ( "context" "ycc-server/app/main/model" "ycc-server/common/ctxdata" "ycc-server/common/xerr" "github.com/pkg/errors" "ycc-server/app/main/api/internal/svc" "ycc-server/app/main/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetLastWithdrawalInfoLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetLastWithdrawalInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLastWithdrawalInfoLogic { return &GetLastWithdrawalInfoLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetLastWithdrawalInfoLogic) GetLastWithdrawalInfo(req *types.GetLastWithdrawalInfoReq) (resp *types.GetLastWithdrawalInfoResp, err error) { userID, err := ctxdata.GetUidFromCtx(l.ctx) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err) } // 1. 获取代理信息 agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID) if err != nil { if errors.Is(err, model.ErrNotFound) { return nil, errors.Wrapf(xerr.NewErrMsg("您不是代理"), "") } return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err) } // 2. 验证提现方式 if req.WithdrawalType != 1 && req.WithdrawalType != 2 { return nil, errors.Wrapf(xerr.NewErrMsg("提现方式无效"), "") } // 3. 查询该代理最近一次该类型的提现记录 // 注意:FindAll 方法会自动添加 del_state = ? 条件,所以这里不需要手动添加 builder := l.svcCtx.AgentWithdrawalModel.SelectBuilder(). Where("agent_id = ? AND withdrawal_type = ?", agent.Id, req.WithdrawalType). OrderBy("create_time DESC"). Limit(1) withdrawals, err := l.svcCtx.AgentWithdrawalModel.FindAll(l.ctx, builder, "") if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询上次提现记录失败, %v", err) } // 4. 如果没有找到记录,返回空信息 if len(withdrawals) == 0 { return &types.GetLastWithdrawalInfoResp{ WithdrawalType: req.WithdrawalType, PayeeAccount: "", PayeeName: "", BankCardNo: "", BankName: "", }, nil } // 5. 组装响应 lastWithdrawal := withdrawals[0] resp = &types.GetLastWithdrawalInfoResp{ WithdrawalType: lastWithdrawal.WithdrawalType, PayeeAccount: lastWithdrawal.PayeeAccount, PayeeName: lastWithdrawal.PayeeName, BankCardNo: "", BankName: "", } // 如果是银行卡提现,填充银行卡信息 if lastWithdrawal.WithdrawalType == 2 { if lastWithdrawal.BankCardNo.Valid { resp.BankCardNo = lastWithdrawal.BankCardNo.String } if lastWithdrawal.BankName.Valid { resp.BankName = lastWithdrawal.BankName.String } } return resp, nil }