diff --git a/app/db/db_un_freeze_user.go b/app/db/db_un_freeze_user.go new file mode 100644 index 0000000..37c6274 --- /dev/null +++ b/app/db/db_un_freeze_user.go @@ -0,0 +1,15 @@ +package db + +import ( + "applet/app/db/model" + "xorm.io/xorm" +) + +func GetUnFreezeUser(eg *xorm.Engine, id string) *model.UnFreezeUser { + var data model.UnFreezeUser + get, err := eg.Where("id=?", id).Get(&data) + if get == false || err != nil { + return nil + } + return &data +} diff --git a/app/db/model/un_freeze_user.go b/app/db/model/un_freeze_user.go new file mode 100644 index 0000000..022fdb3 --- /dev/null +++ b/app/db/model/un_freeze_user.go @@ -0,0 +1,19 @@ +package model + +import ( + "time" +) + +type UnFreezeUser struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid int `json:"uid" xorm:"default 0 INT(11)"` + Time int `json:"time" xorm:"default 0 INT(11)"` + NextTime int `json:"next_time" xorm:"INT(11)"` + CoinId int `json:"coin_id" xorm:"default 0 INT(11)"` + Money string `json:"money" xorm:"default 0.00 DECIMAL(20,2)"` + IsUse int `json:"is_use" xorm:"default 0 INT(1)"` + Day int `json:"day" xorm:"default 0 INT(11)"` + IsTest int `json:"is_test" xorm:"default 0 INT(1)"` + UpdateTime time.Time `json:"update_time" xorm:"DATETIME"` + CreateTime time.Time `json:"create_time" xorm:"DATETIME"` +} diff --git a/consume/init.go b/consume/init.go index a6c4300..994fbfd 100644 --- a/consume/init.go +++ b/consume/init.go @@ -60,7 +60,8 @@ func initConsumes() { jobs[consumeMd.ZhiosExpressOrderFail] = ZhiosExpressOrderFail jobs[consumeMd.ZhiosWithdrawReward] = ZhiosWithdrawReward jobs[consumeMd.ZhiosTaskTotal] = ZhiosTaskTotal - //jobs[consumeMd.ZhiosUserProfileInviteCode] = ZhiosUserProfileInviteCode + jobs[consumeMd.ZhiosAutoUnFreeze] = ZhiosAutoUnFreeze + ////jobs[consumeMd.ZhiosUserProfileInviteCode] = ZhiosUserProfileInviteCode // diff --git a/consume/md/consume_key.go b/consume/md/consume_key.go index 84cafb4..5edf3d6 100644 --- a/consume/md/consume_key.go +++ b/consume/md/consume_key.go @@ -299,6 +299,15 @@ var RabbitMqQueueKeyList = []*MqQueue{ BindKey: "", ConsumeFunName: "ZhiosUserProfileInviteCode", }, + { + ExchangeName: "zhios.user_profile.exchange", + Name: "zhios_auto_un_freeze", + Type: DirectQueueType, + IsPersistent: false, + RoutKey: "auto_un_freeze", + BindKey: "", + ConsumeFunName: "ZhiosAutoUnFreeze", + }, { ExchangeName: "zhios.user_valid.exchange", Name: "zhios_user_valid", @@ -594,5 +603,6 @@ const ( FlexibleEmploymentWithdrawForPupiaoConsumeFunName = "FlexibleEmploymentWithdrawForPupiaoConsume" ZhiosTaskTotal = "zhiosTaskTotal" ZhiosUserProfileInviteCode = "ZhiosUserProfileInviteCode" + ZhiosAutoUnFreeze = "ZhiosAutoUnFreeze" InstallmentPaymentAutoRepaidConsumeFunName = "InstallmentPaymentAutoRepaidConsume" ) diff --git a/consume/md/md.go b/consume/md/md.go index 6af4330..7af48c8 100644 --- a/consume/md/md.go +++ b/consume/md/md.go @@ -11,6 +11,10 @@ type ZhiosUserProfile struct { Uid string `json:"uid"` Mid string `json:"mid"` } +type ZhiosAutoUnFreezes struct { + Id string `json:"id"` + Mid string `json:"mid"` +} type AcquisitionCfg struct { Id string `json:"id"` Status string `json:"status"` diff --git a/consume/zhios_user_auto_un_freeze.go b/consume/zhios_user_auto_un_freeze.go new file mode 100644 index 0000000..bf13a8d --- /dev/null +++ b/consume/zhios_user_auto_un_freeze.go @@ -0,0 +1,174 @@ +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" + "encoding/json" + "errors" + "fmt" + "github.com/streadway/amqp" + "time" + "xorm.io/xorm" +) + +func ZhiosAutoUnFreeze(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(1) + 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 = AutoUnFreeze(res.Body) + //_ = res.Reject(false) + if err == nil { + _ = res.Ack(true) + } + } else { + panic(errors.New("error getting message")) + } + } + fmt.Println("get msg done") +} + +func AutoUnFreeze(msg []byte) error { + //1、解析canal采集至mq中queue的数据结构体 + var canalMsg *md.ZhiosAutoUnFreezes + 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 + } + tmp := db.GetUnFreezeUser(eg, canalMsg.Id) + if tmp == nil { + return nil + } + if utils.StrToFloat64(tmp.Money) == 0 { + return nil + } + sess := eg.NewSession() + defer sess.Close() + sess.Begin() + if tmp.CoinId == 0 { + err = finDoing(sess, tmp) + } else { + err = coinDoing(sess, tmp) + } + if err != nil { + sess.Rollback() + return nil + } + sess.Commit() + return nil +} +func finDoing(sess *xorm.Session, data *model.UnFreezeUser) error { + + user, _ := db.UserProfileFindByIDSess(sess, data.Uid) + if user == nil { + return nil + } + if utils.StrToFloat64(user.FinInvalid) <= 0 { + return nil + } + money := data.Money + if utils.StrToFloat64(user.FinInvalid) < utils.StrToFloat64(data.Money) { + money = user.FinInvalid + } + beforeAmount := user.FinValid + user.FinInvalid = utils.Float64ToStrByPrec(utils.StrToFloat64(user.FinInvalid)-utils.StrToFloat64(money), 8) + user.FinValid = utils.Float64ToStrByPrec(utils.StrToFloat64(user.FinValid)+utils.StrToFloat64(money), 8) + update, err := sess.Where("uid=?", user.Uid).Cols("fin_valid,fin_invalid").Update(user) + if update == 0 || err != nil { + return errors.New("失败") + } + var flowData = model.FinUserFlow{ + Uid: user.Uid, + Type: 0, + Amount: money, + BeforeAmount: beforeAmount, + AfterAmount: user.FinValid, + OrdType: "auto_freeze", + OrdId: "", + OrdTitle: "自动解冻金额", + OrdAction: 30, + OrdTime: int(time.Now().Unix()), + State: 2, + CreateAt: time.Now(), + UpdateAt: time.Now(), + } + insert, err := sess.Insert(&flowData) + if insert == 0 || err != nil { + return errors.New("失败") + } + return nil +} +func coinDoing(sess *xorm.Session, data *model.UnFreezeUser) error { + user, _ := db.UserVirtualAmountFindById(sess, data.Uid, data.CoinId) + if user == nil { + return nil + } + if utils.StrToFloat64(user.FreezeAmount) <= 0 { + return nil + } + money := data.Money + if utils.StrToFloat64(user.FreezeAmount) < utils.StrToFloat64(data.Money) { + money = user.FreezeAmount + } + beforeAmount := user.Amount + beforeFreezeAmount := user.FreezeAmount + user.FreezeAmount = utils.Float64ToStrByPrec(utils.StrToFloat64(user.FreezeAmount)-utils.StrToFloat64(money), 8) + user.Amount = utils.Float64ToStrByPrec(utils.StrToFloat64(user.Amount)+utils.StrToFloat64(money), 8) + update, err := sess.Where("uid=?", user.Uid).Cols("freeze_amount,amount").Update(user) + if update == 0 || err != nil { + return errors.New("失败") + } + var flowData = model.UserVirtualCoinFlow{ + Uid: user.Uid, + CoinId: data.CoinId, + Direction: 1, + Title: "自动解冻", + OrdId: "", + Date: "", + Amout: money, + BeforeAmout: beforeAmount, + AfterAmout: user.Amount, + CreateTime: time.Now(), + TransferType: 200, + FreezeBeforeAmout: beforeFreezeAmount, + FreezeAfterAmout: user.FreezeAmount, + } + insert, err := sess.Insert(&flowData) + if insert == 0 || err != nil { + return errors.New("失败") + } + return nil +}