|
- package consume
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/utils"
- "applet/app/utils/logx"
- "applet/consume/md"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/streadway/amqp"
- "github.com/tidwall/gjson"
- "time"
- "xorm.io/xorm"
- )
-
- func ZhiosWithdrawReward(queue md.MqQueue) {
- fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>")
- ch, err := rabbit.Cfg.Pool.GetChannel()
- if err != nil {
- logx.Error(err)
- return
- }
- defer ch.Release()
- //1、将自己绑定到交换机上
- ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
- //2、取出数据进行消费
- ch.Qos(20)
- delivery := ch.Consume(queue.Name, false)
-
- var res amqp.Delivery
- var ok bool
- for {
- res, ok = <-delivery
- if ok == true {
- //fmt.Println(string(res.Body))
- fmt.Println(">>>>>>>>>>>>>>>>CanalOrderConsume<<<<<<<<<<<<<<<<<<<<<<<<<")
- err = handleZhiosWithdrawReward(res.Body)
- //_ = res.Reject(false)
- if err == nil {
- _ = res.Ack(true)
- }
- } else {
- panic(errors.New("error getting message"))
- }
- }
- fmt.Println("get msg done")
- }
-
- func handleZhiosWithdrawReward(msg []byte) error {
- //1、解析canal采集至mq中queue的数据结构体
- var canalMsg *md.ZhiosWithdraw
- fmt.Println(string(msg))
- var tmpString string
- err := json.Unmarshal(msg, &tmpString)
- if err != nil {
- fmt.Println(err.Error())
- return err
- }
- fmt.Println(tmpString)
- err = json.Unmarshal([]byte(tmpString), &canalMsg)
- if err != nil {
- return err
- }
- mid := canalMsg.Mid
- eg := db.DBs[mid]
- if eg == nil {
- return nil
- }
- //判断用户是什么等级
- var apply model.FinWithdrawApply
- get, err := eg.Where("id=?", canalMsg.Id).Get(&apply)
- if get == false || err != nil {
- return nil
- }
- if apply.Uid == 0 {
- return nil
- }
- //
- withdrawSetting := db.SysCfgGetWithDb(eg, mid, "withdraw_setting")
- withdrawFirstBili := gjson.Get(withdrawSetting, "withdrawFirstBili").String()
- publicWithdrawTeamBili := db.SysCfgGetWithDb(eg, mid, "public_withdraw_team_bili")
- publicWithdrawTeamCount := db.SysCfgGetWithDb(eg, mid, "public_withdraw_team_count")
- if utils.StrToFloat64(withdrawFirstBili) > 0 {
- level := 0
- all, _ := db.UserLevlEgAll(eg)
- for k, v := range all {
- if k == 0 {
- level = v.Id
- }
- }
- //分给粉丝
- levelUser := db.UserFindByLevel(eg, level)
- if len(levelUser) == 0 {
- return nil
- }
- ids := make([]int64, 0)
- for _, v := range levelUser {
- ids = append(ids, int64(v.Uid))
- }
- money := utils.FloatFormat(utils.StrToFloat64(apply.Amount)*(utils.StrToFloat64(withdrawFirstBili)/100)/float64(len(levelUser)), 6)
- comm(eg, canalMsg.Id, ids, money, "全网提现分红", "withdraw_reward", "92")
- }
- if utils.StrToFloat64(publicWithdrawTeamBili) > 0 && utils.StrToFloat64(publicWithdrawTeamCount) > 0 {
- //分给粉丝
- user, _ := rule.FindRandUser(eg, utils.StrToInt(publicWithdrawTeamCount))
- money := utils.FloatFormat(utils.StrToFloat64(apply.Amount)*(utils.StrToFloat64(publicWithdrawTeamBili)/100)/float64(len(user)), 6)
- comm(eg, canalMsg.Id, user, money, "公排团队提现分红", "withdraw_team_reward", "93")
- }
- return nil
- }
- func comm(eg *xorm.Engine, id string, levelUser []int64, money float64, title, types, ordAction string) {
- for _, v := range levelUser {
- profile, err := db.UserProfileFindByID(eg, v)
- if err != nil || profile == nil {
- continue
- }
- oldAmount := profile.FinValid
- profile.FinValid = utils.Float64ToStrByPrec(utils.StrToFloat64(profile.FinValid)+money, 6)
- eg.Where("uid=?", profile.Uid).Update(profile)
- var flow = model.FinUserFlow{
- Uid: int(v),
- Type: 0,
- Amount: utils.Float64ToStrByPrec(money, 6),
- BeforeAmount: oldAmount,
- AfterAmount: profile.FinValid,
- OrdType: types,
- OrdId: id,
- OrdTitle: title,
- OrdAction: utils.StrToInt(ordAction),
- OrdTime: int(time.Now().Unix()),
- State: 2,
- CreateAt: time.Now(),
- UpdateAt: time.Now(),
- }
- eg.Insert(&flow)
- }
-
- }
|