diff --git a/go.mod b/go.mod index 834649e..2959593 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git go 1.15 require ( + code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.3 // indirect github.com/go-redis/redis v6.15.9+incompatible github.com/gomodule/redigo v1.8.9 github.com/jinzhu/copier v0.3.5 diff --git a/go.sum b/go.sum index 1b25d2a..4010616 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.3 h1:SPp5AswPmkDO2ML6WwGlzhIuls+/1dUfU40iOeH0dh4= +code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.3/go.mod h1:TTcCnFn/LhBGapnutpezlW+GXkLRNPMWkziOoCsXQqY= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU= gitee.com/travelliu/dm v1.8.11192/go.mod h1:DHTzyhCrM843x9VdKVbZ+GKXGRbKM2sJ4LxihRxShkE= @@ -339,6 +341,8 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3 github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= +github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= +github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/md/mq.go b/md/mq.go new file mode 100644 index 0000000..b6f49a0 --- /dev/null +++ b/md/mq.go @@ -0,0 +1,13 @@ +package md + +const OneCirclesExchange = "one.circles" + +const ( + OneCirclesRoutKeyForSignIn = "sign_in" // 签到 +) + +type OneCirclesStructForSignIn struct { + MasterId string `json:"master_id"` + Uid int `json:"uid"` + Id int64 `json:"id"` +} diff --git a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go index a358d95..961ef1a 100644 --- a/rule/one_circles/one_circles_sign_in_green_energy_settlement.go +++ b/rule/one_circles/one_circles_sign_in_green_energy_settlement.go @@ -1,6 +1,7 @@ package one_circles import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" @@ -12,12 +13,55 @@ import ( "errors" "fmt" "github.com/shopspring/decimal" + "math/rand" "time" "xorm.io/xorm" ) // SettlementSignInGreenEnergy 计算签到得到绿色能量 -func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string) (err error) { +func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { + defer ch.Release() + //1、查找 `one_circles_green_energy_basic_setting` 基础设置 + oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return + } + if oneCirclesGreenEnergyBasicSetting == nil { + return errors.New("开关未开!!") + } + //3、统计签到结束的用户数据 + now := time.Now() + var page = 1 + var pageSize = 100 + for { + var list []model.OneCirclesGreenEnergySignIn + err = engine.Where("end_time <= ?", now.Format("2006-01-02 15:04:05")).And("is_completed =?", 0).Limit(pageSize, (page-1)*pageSize).Asc("id").Find(&list) + if err != nil { + fmt.Println("err:::::1111", err) + return + } + if len(list) <= 0 { + break + } + + for _, v := range list { + //TODO::推入rabbitmq 异步处理 + ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForSignIn{ + MasterId: masterId, + Uid: v.Uid, + Id: v.Id, + }, md.OneCirclesRoutKeyForSignIn) + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + } + + return +} + +func HandleSettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, id int64, uid int) (err error) { //1、查找 `one_circles_green_energy_basic_setting` 基础设置 oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{ "key": "is_open", @@ -49,15 +93,6 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string) (err erro rewardValue := zhios_order_relate_utils.Float64ToStrPrec8(zhios_order_relate_utils.StrToFloat64(reward) * float64(oneRoundDuration) * 60 * 60) //3、统计签到结束的用户数据 - now := time.Now() - startTime := now.Add(-time.Hour * time.Duration(oneRoundDuration)).Format("2006-01-02 15:04:05") - var list []model.OneCirclesGreenEnergySignIn - err = engine.Where("start_time <= ?", startTime).And("is_completed =?", 0).Find(&list) - if err != nil { - fmt.Println("err:::::1111", err) - return - } - session := engine.NewSession() defer func() { session.Close() @@ -68,68 +103,66 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string) (err erro session.Begin() var reduceTotalGreenEnergy float64 - for _, v := range list { - //4.2给相应的用户加上个人的绿色积分(可用数量) - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "add", - Mid: masterId, - Title: md.OneCirclesGreenEnergySignInSettlementPersonalReward, - TransferType: md.OneCirclesGreenEnergySignInSettlementPersonalRewardForUserVirtualCoinFlow, - OrdId: "", - CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId, - Uid: v.Uid, - Amount: zhios_order_relate_utils.StrToFloat64(rewardValue), - }) - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::2222", err) - return err - } - reduceTotalGreenEnergy += zhios_order_relate_utils.StrToFloat64(rewardValue) + //4.2给相应的用户加上个人的绿色积分(可用数量) + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "add", + Mid: masterId, + Title: md.OneCirclesGreenEnergySignInSettlementPersonalReward, + TransferType: md.OneCirclesGreenEnergySignInSettlementPersonalRewardForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId, + Uid: uid, + Amount: zhios_order_relate_utils.StrToFloat64(rewardValue), + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::2222", err) + return err + } + reduceTotalGreenEnergy += zhios_order_relate_utils.StrToFloat64(rewardValue) - //4.2给相应的上级用户加上团队奖励的绿色积分(结算数量) - relates, err1 := db.DbsUserRelate(engine, v.Uid, 0) - if err1 != nil { - _ = session.Rollback() - fmt.Println("err:::::3333", err1) - return err1 - } - var parentRewardValue = zhios_order_relate_utils.StrToFloat64(rewardValue) - if relates != nil { - for _, relate := range *relates { - if parentRewardValue <= rewardEndValue { - break - } - parentRewardValue = parentRewardValue * rewardDecrement - //TODO::判断是否活跃 - var m model.OneCirclesGreenEnergySignIn - has, err3333333 := engine.Where("uid =?", relate.ParentUid).And("start_time >=?", time.Now().Format("2006-01-02")+" 00:00:00").Get(&m) - if err3333333 != nil { - _ = session.Rollback() - fmt.Println("err:::::3333333", err3333333) - return err3333333 - } - if !has { - //不活跃不需要奖励 - continue - } - err = DealUserCoin(session, md.DealUserCoinReq{ - Kind: "add", - Mid: masterId, - Title: md.OneCirclesGreenEnergySignInSettlementTeamReward, - TransferType: md.OneCirclesGreenEnergySignInSettlementTeamRewardForUserVirtualCoinFlow, - OrdId: "", - CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, - Uid: relate.ParentUid, - Amount: parentRewardValue, - }) - if err != nil { - _ = session.Rollback() - fmt.Println("err:::::44444", err) - return err - } - reduceTotalGreenEnergy += parentRewardValue + //4.2给相应的上级用户加上团队奖励的绿色积分(结算数量) + relates, err1 := db.DbsUserRelate(engine, uid, 0) + if err1 != nil { + _ = session.Rollback() + fmt.Println("err:::::3333", err1) + return err1 + } + var parentRewardValue = zhios_order_relate_utils.StrToFloat64(rewardValue) + if relates != nil { + for _, relate := range *relates { + if parentRewardValue <= rewardEndValue { + break } + parentRewardValue = parentRewardValue * rewardDecrement + //TODO::判断是否活跃 + var m model.OneCirclesGreenEnergySignIn + has, err3333333 := engine.Where("uid =?", relate.ParentUid).And("start_time >=?", time.Now().Format("2006-01-02")+" 00:00:00").Get(&m) + if err3333333 != nil { + _ = session.Rollback() + fmt.Println("err:::::3333333", err3333333) + return err3333333 + } + if !has { + //不活跃不需要奖励 + continue + } + err = DealUserCoin(session, md.DealUserCoinReq{ + Kind: "add", + Mid: masterId, + Title: md.OneCirclesGreenEnergySignInSettlementTeamReward, + TransferType: md.OneCirclesGreenEnergySignInSettlementTeamRewardForUserVirtualCoinFlow, + OrdId: "", + CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId, + Uid: relate.ParentUid, + Amount: parentRewardValue, + }) + if err != nil { + _ = session.Rollback() + fmt.Println("err:::::44444", err) + return err + } + reduceTotalGreenEnergy += parentRewardValue } } @@ -144,18 +177,21 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string) (err erro } //6、更新 `one_circles_green_energy_sign_in` 中的 is_completed 状态 - _, err = engine.Where("start_time <= ?", startTime).And("is_completed =?", 0).Update(&model.OneCirclesGreenEnergySignIn{ + _, err = engine.Where("id = ?", id).Update(&model.OneCirclesGreenEnergySignIn{ IsCompleted: 1, }) if err != nil { + _ = session.Rollback() fmt.Println("err:::::1111", err) return } + err = session.Commit() if err != nil { _ = session.Rollback() return errors.New("事务提交失败") } + return }