package svc import ( "applet/app/md" "applet/app/utils/cache" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" zhios_order_relate_utils "code.fnuoos.com/zhimeng/model.git/utils" zhios_order_relate_logx "code.fnuoos.com/zhimeng/model.git/utils/logx" "errors" "fmt" "github.com/shopspring/decimal" "strconv" "time" "xorm.io/xorm" ) // DealAgentAmount 处理给代理余额 func DealAgentAmount(session *xorm.Session, req md.DealAgentAmount) (err error) { if req.Amount < 0 { req.Amount = 0 } //1、分布式锁阻拦 requestIdPrefix := fmt.Sprintf(md.DealAgentAmountRequestIdPrefix, req.Mid, req.AgentId) cb, err := HandleBalanceDistributedLockForAgent(req.Mid, strconv.Itoa(req.AgentId), requestIdPrefix) if err != nil { return err } if cb != nil { defer cb() // 释放锁 } //2、计算&&组装数据 now := time.Now() userAmount, err := GetAgentAmount(session, req.Mid, req.AgentId, true) if err != nil { return err } userAmountValue := decimal.NewFromFloat(zhios_order_relate_utils.StrToFloat64(userAmount)) amountValue := decimal.NewFromFloat(req.Amount).RoundFloor(4) var finUserFlow = model.FinAgentFlow{ AgentId: req.AgentId, Type: req.Type, Amount: amountValue.String(), BeforeAmount: userAmount, OrdId: req.OrdId, Kind: req.Kind, Memo: req.Memo, CreateAt: now.Format("2006-01-02 15:04:05"), UpdateAt: now.Format("2006-01-02 15:04:05"), } if req.Type == md.FinAgentFlowDirectionIncome { finUserFlow.AfterAmount = userAmountValue.Add(amountValue).RoundFloor(4).String() } else if req.Type == md.FinAgentFlowDirectionExpenditure { finUserFlow.AfterAmount = userAmountValue.Sub(amountValue).RoundFloor(4).String() } else { err = errors.New("错误的kind类型") return err } if zhios_order_relate_utils.StrToFloat64(finUserFlow.AfterAmount) < 0 { zhios_order_relate_utils.FilePutContents("agent_amount_not", zhios_order_relate_utils.SerializeStr(map[string]interface{}{ "agent_id": finUserFlow.AgentId, "amount": finUserFlow.Amount, "before_amount": finUserFlow.BeforeAmount, "memo": finUserFlow.Memo, "mid": req.Mid, })) return errors.New("代理余额不足") } //3、插入 `fin_user_flow` 记录 affected, err := session.Insert(&finUserFlow) if affected == 0 || err != nil { _ = zhios_order_relate_logx.Warn(err) return err } //4、修改 `user_profile`的fin_valid值 && 及缓存 err = SetCacheAgentAmount(session, req.Mid, finUserFlow.AfterAmount, req.AgentId, true) if err != nil { return err } return nil } // GetAgentAmount 获取代理余额 func GetAgentAmount(session *xorm.Session, masterId string, agentId int, isForceUpdate bool) (amount string, err error) { if isForceUpdate { agentListDb := implement.NewAgentListDb(db.Db) agent, err1 := agentListDb.GetAgentListBySession(session, agentId) if err1 != nil { return amount, err1 } if agent == nil { amount = "0" } else { amount = agent.Amount } //将获取到的余额值缓存至redis _ = SetCacheAgentAmount(session, masterId, amount, agentId, false) } else { redisKey := fmt.Sprintf(md.AgentAmountRedisKey, masterId, agentId) amount, err = cache.GetString(redisKey) if err != nil && err.Error() != "redigo: nil returned" { return } agentListDb := implement.NewAgentListDb(db.Db) agent, err1 := agentListDb.GetAgentListBySession(session, agentId) if err1 != nil { return amount, err1 } if agent == nil { amount = "0" } else { amount = agent.Amount } //将获取到的余额值缓存至redis _ = SetCacheAgentAmount(session, masterId, amount, agentId, false) } return amount, nil } // SetCacheAgentAmount 设置缓存的用户余额 func SetCacheAgentAmount(session *xorm.Session, masterId, amount string, agentId int, isUpdateDb bool) error { redisKey := fmt.Sprintf(md.AgentAmountRedisKey, masterId, agentId) if isUpdateDb { _, err := session.Where("agent_id=?", agentId).Update(model.AgentList{ AgentId: agentId, Amount: amount, }) if err != nil { return err } } //TODO::默认缓存1小时 (先调整为 2 min) _, err := cache.SetEx(redisKey, zhios_order_relate_utils.StrToFloat64(amount), 60*0.5) if err != nil { return err } return nil }