|
|
|
@@ -60,57 +60,6 @@ func (s *AgentServiceV1) List(ctx context.Context, req *v1.AgentListReq) *utils.
|
|
|
|
|
return utils.SuccessWithTotal[[]v1.AgentListRes](res, total)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *AgentServiceV1) Add(ctx context.Context, req *v1.AgentAddReq) *utils.Response[any] {
|
|
|
|
|
// 参数校验
|
|
|
|
|
currentUser := utils.GetUserInfoFromCtx(ctx)
|
|
|
|
|
agent := currentUser.QueryAgent().OnlyX(ctx)
|
|
|
|
|
if agent.Level >= req.Level {
|
|
|
|
|
return utils.FailParam("下级代理级别不能高于当前代理级别")
|
|
|
|
|
}
|
|
|
|
|
if agent.Rate.Compare(req.Rate) <= 0 {
|
|
|
|
|
return utils.FailParam("下级代理费率比例不能低于当前代理比例")
|
|
|
|
|
}
|
|
|
|
|
tx, err := db.Global.Tx(ctx)
|
|
|
|
|
utils.OkOrPanic(err, func(err error) error {
|
|
|
|
|
return oops.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
With("创建事务失败").
|
|
|
|
|
Wrap(err)
|
|
|
|
|
})
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
user := tx.SysUser.Query().Where(sysuser.IDEQ(currentUser.ID)).OnlyX(ctx)
|
|
|
|
|
user.Balance = utils.CalculateBalance(user.Balance, req.Rate, req.Balance, req.Rate)
|
|
|
|
|
if user.Balance.LessThan(decimal.NewFromInt(0)) {
|
|
|
|
|
return utils.FailParam("余额不足")
|
|
|
|
|
}
|
|
|
|
|
// 扣除余额
|
|
|
|
|
_, err = tx.SysUser.UpdateOne(user).SetBalance(user.Balance).Save(ctx)
|
|
|
|
|
utils.OkOrPanic(err, func(err error) error {
|
|
|
|
|
return oops.
|
|
|
|
|
WithContext(ctx).
|
|
|
|
|
With("更新用户余额失败").
|
|
|
|
|
Wrap(err)
|
|
|
|
|
})
|
|
|
|
|
// 创建用户
|
|
|
|
|
tmp := tx.SysUser.Create().
|
|
|
|
|
SetUsername(req.Username).
|
|
|
|
|
SetPassword(utils.PasswordBcryptHash(req.Password)).
|
|
|
|
|
SetEmail(req.Email).
|
|
|
|
|
SetParent(user)
|
|
|
|
|
save := tmp.SaveX(ctx)
|
|
|
|
|
// 关联角色
|
|
|
|
|
role := tx.SysRole.Query().Where(sysrole.NameEQ(cst.KeyRoleAgent)).AllX(ctx)
|
|
|
|
|
tx.SysUser.UpdateOne(save).AddRoles(role...).ExecX(ctx)
|
|
|
|
|
// 创建代理
|
|
|
|
|
tx.SysAgent.Create().
|
|
|
|
|
SetLevel(req.Level).
|
|
|
|
|
SetRate(req.Rate).
|
|
|
|
|
SaveX(ctx)
|
|
|
|
|
|
|
|
|
|
err = tx.Commit()
|
|
|
|
|
return utils.SuccessWithMsg("")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *AgentServiceV1) Ban(ctx context.Context, req *v1.AgentBanReq) *utils.Response[any] {
|
|
|
|
|
user := db.Global.SysUser.Query().Where(sysuser.IDEQ(req.ID)).OnlyX(ctx)
|
|
|
|
|
if !utils.IsAdminFromCtx(ctx) {
|
|
|
|
@@ -130,13 +79,13 @@ func (s *AgentServiceV1) Recharge(ctx context.Context, req *v1.AgentRechargeReq)
|
|
|
|
|
// 参数校验
|
|
|
|
|
user := db.Global.SysUser.Query().Where(sysuser.IDEQ(req.ID)).WithAgent().OnlyX(ctx)
|
|
|
|
|
if !utils.IsAdminFromCtx(ctx) {
|
|
|
|
|
currentUser := db.Global.SysUser.Query().Where(sysuser.ID(utils.GetUserInfoFromCtx(ctx).ID)).WithAgent().WithRoles().OnlyX(ctx)
|
|
|
|
|
currentUser := db.Global.SysUser.Query().Where(sysuser.ID(utils.GetUserInfoFromCtx(ctx).ID)).WithAgent().OnlyX(ctx)
|
|
|
|
|
// 判断是否是自己的下级代理
|
|
|
|
|
parent := user.QueryParent().WithAgent().FirstX(ctx)
|
|
|
|
|
if parent == nil || parent.ID != currentUser.ID {
|
|
|
|
|
return utils.FailParam("非法的下级代理参数")
|
|
|
|
|
}
|
|
|
|
|
needBalance := utils.CalculateBalance(parent.Balance, parent.Edges.Agent.Rate, req.Amount, user.Edges.Agent.Rate)
|
|
|
|
|
needBalance := utils.NeedBalance(parent.Edges.Agent.Rate, req.Amount, user.Edges.Agent.Rate)
|
|
|
|
|
balance.BalanceService.ChangeUserBalance(ctx, parent.ID, needBalance.Neg())
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|