|
- package consume
-
- import (
- "applet/app/cfg"
- "applet/app/db"
- utils2 "applet/app/utils"
- "applet/app/utils/logx"
- "applet/consume/md"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- zhios_order_relate_utils "code.fnuoos.com/EggPlanet/egg_models.git/utils"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
- md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/streadway/amqp"
- "math"
- "strconv"
- "time"
- )
-
- // EggRecoverDataConsume 返还用户流水
- func EggRecoverDataConsume(queue md.MqQueue) {
- fmt.Println(">>>>>>>>>>>>EggPublishDataConsume>>>>>>>>>>>>")
- 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(1)
- delivery := ch.Consume(queue.Name, false)
-
- egg_system_rules.Init(cfg.RedisAddr)
- var res amqp.Delivery
- var ok bool
- for {
- res, ok = <-delivery
- if ok == true {
- err = handleEggRecoverDataConsume(res.Body)
- if err != nil {
- fmt.Println("EggRecoverDataConsume_ERR:::::", err.Error())
- utils2.FilePutContents("EggRecoverDataConsume_ERR", utils2.SerializeStr(map[string]interface{}{
- "body": res.Body,
- "err": err.Error(),
- }))
- }
- //_ = res.Reject(false)
- err = res.Ack(true)
- fmt.Println("err ::: ", err)
- } else {
- panic(errors.New("error getting message"))
- }
- }
- }
-
- func handleEggRecoverDataConsume(msgData []byte) error {
- time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
- // 1.解析mq中queue的数据结构体
- var msg *md2.EggRoutKeyForRecoverRecordData
- err := json.Unmarshal(msgData, &msg)
- if err != nil {
- return err
- }
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- return err
- }
-
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- var personalDel []model.UserVirtualCoinFlow
- var teamDel []model.UserVirtualCoinFlow
- err = session.Table("user_virtual_coin_flow").
- In("transfer_type", 17, 19, 21).
- Where("uid = ?", msg.Uid).
- And("coin_id = ?", setting.PersonEggEnergyCoinId).
- Find(&personalDel)
- if err != nil {
- session.Rollback()
- return err
- }
- var personalAmount float64
- for _, flow := range personalDel {
- if flow.Direction == 1 {
- personalAmount += utils2.StrToFloat64(flow.Amount)
- }
- if flow.Direction == 2 {
- personalAmount -= utils2.StrToFloat64(flow.Amount)
- }
- }
-
- err = session.Table("user_virtual_coin_flow").
- In("transfer_type", 18, 20).
- Where("uid = ?", msg.Uid).
- And("coin_id = ?", setting.TeamEggEnergyCoinId).
- Find(&teamDel)
- if err != nil {
- session.Rollback()
- return err
- }
- var teamAmount float64
- for _, flow := range teamDel {
- if flow.Direction == 1 {
- teamAmount += utils2.StrToFloat64(flow.Amount)
- }
- if flow.Direction == 2 {
- teamAmount -= utils2.StrToFloat64(flow.Amount)
- }
- }
-
- if personalAmount != 0 {
- requestIdPrefix1 := fmt.Sprintf(md2.DealUserCoinRequestIdPrefix, setting.PersonEggEnergyCoinId, msg.Uid)
- cb1, err := svc.HandleDistributedLock(zhios_order_relate_utils.Int64ToStr(msg.Uid), strconv.Itoa(setting.PersonEggEnergyCoinId), requestIdPrefix1)
- if err != nil {
- return err
- }
- if cb1 != nil {
- defer cb1() // 释放锁
- }
- var kind string
- if personalAmount > 0 {
- kind = "sub"
- } else {
- kind = "add"
- }
- err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{
- Kind: kind,
- Title: enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal.String(),
- TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal),
- CoinId: setting.PersonEggEnergyCoinId,
- Uid: msg.Uid,
- Amount: math.Abs(personalAmount - 0),
- })
- if err != nil {
- session.Rollback()
- return err
- }
- }
-
- if teamAmount != 0 {
- //1、分布式锁阻拦
- requestIdPrefix2 := fmt.Sprintf(md2.DealUserCoinRequestIdPrefix, setting.TeamEggEnergyCoinId, msg.Uid)
- cb2, err := svc.HandleDistributedLock(zhios_order_relate_utils.Int64ToStr(msg.Uid), strconv.Itoa(setting.TeamEggEnergyCoinId), requestIdPrefix2)
- if err != nil {
- return err
- }
- if cb2 != nil {
- defer cb2() // 释放锁
- }
- var kind string
- if teamAmount > 0 {
- kind = "sub"
- } else {
- kind = "add"
- }
- err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{
- Kind: kind,
- Title: enum.EggEnergyMaliciousAccountFlashingDeductionRecoverTeam.String(),
- TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverTeam),
- CoinId: setting.TeamEggEnergyCoinId,
- Uid: msg.Uid,
- Amount: math.Abs(teamAmount - 0),
- })
- if err != nil {
- session.Rollback()
- return err
- }
- }
-
- _, err = session.Table("punishment_record").
- Where("uid = ?", msg.Uid).Cols("complete").
- Update(model.PunishmentRecord{
- Complete: 1,
- })
- if err != nil {
- return err
- }
-
- session.Commit()
- fmt.Println("handleEggRecoverDataConsume_succeed ====>", msg.Uid)
- return nil
- }
|