|
|
@@ -0,0 +1,128 @@ |
|
|
|
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" |
|
|
|
"strconv" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
// EggRestoreDataConsume 恢复返还用户流水 |
|
|
|
func EggRestoreDataConsume(queue md.MqQueue) { |
|
|
|
fmt.Println(">>>>>>>>>>>>EggRestoreDataConsume>>>>>>>>>>>>") |
|
|
|
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 = handleEggRestoreDataConsume(res.Body) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("EggRestoreDataConsume_ERR:::::", err.Error()) |
|
|
|
utils2.FilePutContents("EggRestoreDataConsume_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 handleEggRestoreDataConsume(msgData []byte) error { |
|
|
|
time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒 |
|
|
|
// 1.解析mq中queue的数据结构体 |
|
|
|
var msg *md2.EggRoutKeyForRestoreRecordData |
|
|
|
err := json.Unmarshal(msgData, &msg) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
settingDb := implement.NewEggEnergyBasicSettingDb(db.Db) |
|
|
|
setting, err := settingDb.EggEnergyBasicSettingGetOne() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//1、分布式锁阻拦 |
|
|
|
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() // 释放锁 |
|
|
|
} |
|
|
|
|
|
|
|
session := db.Db.NewSession() |
|
|
|
defer session.Close() |
|
|
|
session.Begin() |
|
|
|
var personalDel model.UserVirtualCoinFlow |
|
|
|
has1, err := session.Table("user_virtual_coin_flow"). |
|
|
|
Where("transfer_type = ?", int(enum.EggEnergyMaliciousAccountFlashingDeductionRecoverPersonal)). |
|
|
|
And("uid = ?", msg.Uid).Get(&personalDel) |
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
if has1 { |
|
|
|
err = rule.DealUserVirtualCoinMinus(session, md3.DealUserVirtualCoinReq{ |
|
|
|
Kind: "sub", |
|
|
|
Title: enum.EggEnergyMaliciousAccountFlashingDeductionRestorePersonal.String(), |
|
|
|
TransferType: int(enum.EggEnergyMaliciousAccountFlashingDeductionRestorePersonal), |
|
|
|
CoinId: setting.PersonEggEnergyCoinId, |
|
|
|
Uid: msg.Uid, |
|
|
|
Amount: utils2.StrToFloat64(personalDel.Amount), |
|
|
|
}) |
|
|
|
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("handleEggRestoreDataConsume_succeed ====>", msg.Uid) |
|
|
|
return nil |
|
|
|
} |