diff --git a/enum/fin_user_flow.go b/enum/fin_user_flow.go index b9d4660..e008ec1 100644 --- a/enum/fin_user_flow.go +++ b/enum/fin_user_flow.go @@ -20,6 +20,8 @@ func FinUserFlowOrderActionString(kind int) string { return "subsidy" case md.InstallmentPaymentAutoRepaidForFinUserFlow: return "installment_payment_auto_repaid" + case md.HoneyDividendForRewardForFinUserFlow: + return "honey_dividend_for_reward" default: return "unknown" } diff --git a/go.mod b/go.mod index f3528e3..e507f77 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ go 1.15 require ( code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 - code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240805085726-a0042fa1ed3c + code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240812063422-6380c884a38b github.com/gin-gonic/gin v1.9.1 github.com/go-redis/redis v6.15.9+incompatible github.com/gomodule/redigo v1.8.9 diff --git a/go.sum b/go.sum index 5116672..402c313 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT 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.5 h1:dqvWJqlJi0WXCwTxbWPLvSOsKPjP+iEDBVgLcAl9nOE= code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5/go.mod h1:TTcCnFn/LhBGapnutpezlW+GXkLRNPMWkziOoCsXQqY= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240701102131-0408d7ee8572 h1:HncIr54hrhKSgOpq/KQiOiMdf4895JpxIUk56UyAsMY= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240701102131-0408d7ee8572/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240723073901-849ec3e356f3 h1:YQhrYavdlE5gUpQ6tm7029ONJVyy9IcSw4W1vU52+3E= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240723073901-849ec3e356f3/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240805064206-7176a3f52675 h1:CHFIwllR+HxDvhOsrW2+h/riCpAclOVbPh5xm63530w= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240805064206-7176a3f52675/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240805074152-3ee9fa9e2938/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= -code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240805085726-a0042fa1ed3c/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= +code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240812063422-6380c884a38b h1:RIDin5L2JusKXSZrWu9ThMtf5QSAHdlws/ukGPY83MI= +code.fnuoos.com/go_rely_warehouse/zyos_model.git v0.0.4-0.20240812063422-6380c884a38b/go.mod h1:nT2x13YFgrS3tS1fDyUR6q/GNIK+hPw7bdzZXz99SM0= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s= diff --git a/md/block_star_chain.go b/md/block_star_chain.go index 189949e..1e3fd32 100644 --- a/md/block_star_chain.go +++ b/md/block_star_chain.go @@ -79,6 +79,8 @@ const ( GreenCoinDoubleChainExchangeByCoin1 = "绿色积分双链兑换账户余额-减少绿色积分" GreenCoinDoubleChainExchangeByCoin2 = "绿色积分双链兑换账户余额-减少贡献积分" HappyOrchardGiveIntegral = "开心农场-赠送积分" + + HoneyDividendForDestroy = "蜂蜜分红-销毁蜂蜜" ) const ( @@ -156,6 +158,7 @@ const ( GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow = 170 //绿色积分双链兑换账户余额-减少绿色积分 GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow = 171 //绿色积分双链兑换账户余额-减少贡献积分 HappyOrchardGiveIntegralForUserVirtualCoinFlow = 172 //开心农场-赠送积分 + HoneyDividendForDestroyForUserVirtualCoinFlow = 173 //蜂蜜分红-销毁蜂蜜 ) diff --git a/md/fin_user_flow.go b/md/fin_user_flow.go index 88dad8a..5890a29 100644 --- a/md/fin_user_flow.go +++ b/md/fin_user_flow.go @@ -20,6 +20,7 @@ const ( BalanceExchangeForGreenEnergyTitleForFinUserFlow = "账户余额兑换" GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow = "绿色积分双链兑换账户余额" InstallmentPaymentAutoRepaidTitleForFinUserFlow = "分期付-自动扣款" + HoneyDividendForReward = "蜂蜜分红-分红奖励" ) const ( @@ -32,6 +33,7 @@ const ( BalanceExchangeForGreenEnergyForFinUserFlow = 113 // 账户余额兑换 GreenCoinDoubleChainExchangeForBalanceForFinUserFlow = 114 // 绿色积分双链兑换账户余额 InstallmentPaymentAutoRepaidForFinUserFlow = 116 // 分期付-自动扣款 + HoneyDividendForRewardForFinUserFlow = 117 // 分期付-自动扣款 ) const DealUserAmountRequestIdPrefix = "%s:deal_user_amount:%d" diff --git a/md/mq.go b/md/mq.go index 00711f5..6b6f08f 100644 --- a/md/mq.go +++ b/md/mq.go @@ -36,3 +36,18 @@ type OneCirclesStructForStarLevelDividends struct { Uid int `json:"uid"` SignDividend float64 `json:"sign_dividend"` } + +const HoneyDividendExchange = "honey.dividend" + +type HoneyDividendForUserDividend struct { + RecordsId int64 `json:"records_id"` + MasterId string `json:"master_id"` + Uid int `json:"uid"` + Amount string `json:"amount"` + DestroyHoneyValues string `json:"destroy_honey_values"` + CoinId int `json:"coin_id"` +} + +const ( + HoneyDividendRoutKeyForUserDividend = "user_dividend" // 签到 +) diff --git a/rule/honey_dividend/dividend.go b/rule/honey_dividend/dividend.go new file mode 100644 index 0000000..e4296b4 --- /dev/null +++ b/rule/honey_dividend/dividend.go @@ -0,0 +1,159 @@ +package service_award_dividend + +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/enum" + md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/svc" + zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" + "code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/implement" + "code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/models" + "errors" + "fmt" + "github.com/shopspring/decimal" + "math/rand" + "time" + "xorm.io/xorm" +) + +func Init(redisAddr string) (err error) { + if redisAddr != "" { + cache.NewRedis(redisAddr) + } + _, err = cache.SelectDb(md2.RedisDataBase) + return +} + +func CollectDividendData(eg *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) { + honeyDividendBasicDb := implement.NewHoneyDividendBasicDb(eg) + base, _ := honeyDividendBasicDb.GetHoneyDividendBasic() + if base.IsOpen != 1 { + err = errors.New("蜂蜜分红未开启!") + return + } + + //1、统计当前`现有蜂蜜数量` + var sql1 = fmt.Sprintf("SELECT SUM(amount) AS total FROM `user_virtual_amount` WHERE coin_id = %d", base.CoinId) + results1, _ := db.QueryNativeString(eg, sql1) + nowHoneys := results1[0]["total"] //当前蜂蜜总量 + + //2、统计当前`可以分红`的用户数量 + var sql2 = fmt.Sprintf("SELECT COUNT(*)AS total FROM `user_virtual_amount` WHERE coin_id = %d and amount >= %s", base.CoinId, base.DestroyHoneyValues) + results2, _ := db.QueryNativeString(eg, sql2) + nowUsers := results2[0]["total"] //当前用户总量 + + //3、更新基础设置配置 + base.NowHoneyValues = nowHoneys + _, err = honeyDividendBasicDb.UpdateHoneyDividendBasic(base, "now_honey_values") + if err != nil { + return + } + + //4、计算数据 + honeyDividendsRecords := new(models.HoneyDividendsRecords) + has, err := eg.Where("state =?", 0).Desc("id").Get(&honeyDividendsRecords) + if err != nil { + return + } + if has == false { + fmt.Println(">>>>>>>>>>>当前没有需要分红的记录") + return nil + } + honeyDividendsRecordsAmountValue, _ := decimal.NewFromString(honeyDividendsRecords.Amount) + nowHoneyValue, _ := decimal.NewFromString(nowHoneys) + unitPrice := honeyDividendsRecordsAmountValue.Div(nowHoneyValue) + + //5、进行分红(推入mq处理) + var page = 1 + var pageSize = 100 + for { + var list []model.UserVirtualAmount + err = eg.Where("coin_id = ? and amount >= ?", base.CoinId, base.DestroyHoneyValues).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 { + tmpValue, _ := decimal.NewFromString(v.Amount) + amount := unitPrice.Mul(tmpValue).String() + //TODO::推入rabbitmq 异步处理 + ch.Publish(md2.HoneyDividendExchange, md2.HoneyDividendForUserDividend{ + RecordsId: honeyDividendsRecords.Id, + MasterId: masterId, + Uid: v.Uid, + Amount: amount, + DestroyHoneyValues: base.DestroyHoneyValues, + CoinId: base.CoinId, + }, md2.HoneyDividendRoutKeyForUserDividend) + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + } + + //6、修改分红记录数据 + honeyDividendsRecords.TotalHoneyValues = nowHoneys + honeyDividendsRecords.UserNums = zhios_order_relate_utils.StrToInt(nowUsers) + honeyDividendsRecords.State = 1 + honeyDividendsRecordsDb := implement.NewHoneyDividendsRecordsDb(eg) + _, err = honeyDividendsRecordsDb.UpdateHoneyDividendsRecords(honeyDividendsRecords, "total_honey_values", "user_nums", "state") + if err != nil { + return err + } + + return +} + +func DealDividendReward(session *xorm.Session, data md2.HoneyDividendForUserDividend) error { + //1、进行蜂蜜销毁 + err := rule.DealUserCoin(session, md2.DealUserCoinReq{ + Kind: "sub", + Mid: data.MasterId, + Title: md2.HoneyDividendForDestroy, + TransferType: md2.HoneyDividendForDestroyForUserVirtualCoinFlow, + OrdId: "", + CoinId: data.CoinId, + Uid: data.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(data.DestroyHoneyValues), + }) + if err != nil { + return err + } + + //2、给用户加上余额 + orderType := enum.FinUserFlowOrderActionString(md2.HoneyDividendForRewardForFinUserFlow) + err = svc.DealUserAmount(session, md2.DealUserAmount{ + Kind: "add", + Mid: data.MasterId, + Title: md2.HoneyDividendForReward, + OrderType: orderType, + OrdAction: md2.HoneyDividendForRewardForFinUserFlow, + OrdId: zhios_order_relate_utils.Int64ToStr(data.RecordsId), + Uid: data.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(data.Amount), + Num: "", + }) + if err != nil { + return err + } + + //3、进行数据记录 + honeyDividendsRecordsWithFlowDb := implement.NewHoneyDividendsRecordsWithFlowDb(nil) + _, err = honeyDividendsRecordsWithFlowDb.HoneyDividendsRecordsWithFlowInsert(session, &models.HoneyDividendsRecordsWithFlow{ + RecordsId: int(data.RecordsId), + Uid: data.Uid, + Amount: data.Amount, + CreateAt: time.Now().Format("2006-01-02 15:04:05"), + UpdateAt: time.Now().Format("2006-01-02 15:04:05"), + }) + if err != nil { + return err + } + return nil +}