From f2576800fb0f24f810aabc30b6c2f05c0bcc62dd Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Thu, 20 Jun 2024 18:09:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/db/db_comm_order_ralate.go | 23 ++ app/db/model/comm_ord_list_relate.go | 15 ++ app/svc/svc_comm_deal_commission.go | 282 ++++++++++++++++++++++++ app/svc/svc_deal_commission.go | 10 +- consume/init.go | 2 + consume/md/consume_key.go | 10 + consume/md/md.go | 1 + consume/zhios_relate_reward_exchange.go | 93 ++++++++ 8 files changed, 434 insertions(+), 2 deletions(-) create mode 100644 app/db/db_comm_order_ralate.go create mode 100644 app/db/model/comm_ord_list_relate.go create mode 100644 app/svc/svc_comm_deal_commission.go create mode 100644 consume/zhios_relate_reward_exchange.go diff --git a/app/db/db_comm_order_ralate.go b/app/db/db_comm_order_ralate.go new file mode 100644 index 0000000..fa19898 --- /dev/null +++ b/app/db/db_comm_order_ralate.go @@ -0,0 +1,23 @@ +package db + +import ( + "applet/app/db/model" + "xorm.io/xorm" +) + +func CommOrderRelateListByOid(Db *xorm.Engine, oid int64, pvd string) ([]*model.CommOrdListRelate, error) { + var ol []*model.CommOrdListRelate + err := Db.Where("oid=? and pvd=?", oid, pvd).Find(&ol) + if err != nil { + return nil, err + } + return ol, nil +} +func CommOrderRelateListByOidSess(sess *xorm.Session, oid int64, pvd string) ([]*model.CommOrdListRelate, error) { + var ol []*model.CommOrdListRelate + err := sess.Where("oid=? and pvd=?", oid, pvd).Find(&ol) + if err != nil { + return nil, err + } + return ol, nil +} diff --git a/app/db/model/comm_ord_list_relate.go b/app/db/model/comm_ord_list_relate.go new file mode 100644 index 0000000..ee2edb4 --- /dev/null +++ b/app/db/model/comm_ord_list_relate.go @@ -0,0 +1,15 @@ +package model + +type CommOrdListRelate struct { + Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"` + Oid int64 `json:"oid" xorm:"not null default 0 comment('订单号') index unique(IDX_ORD) BIGINT(20)"` + Uid int `json:"uid" xorm:"not null default 0 comment('用户ID') unique(IDX_ORD) index INT(10)"` + Amount float64 `json:"amount" xorm:"not null default 0.00 comment('金额') FLOAT(10,2)"` + Pvd string `json:"pvd" xorm:"not null default '' comment('供应商taobao,jd,pdd,vip,suning,kaola') index VARCHAR(8)"` + Info string `json:"info" xorm:"not null comment('备注') TEXT"` + CreateAt int `json:"create_at" xorm:"not null default 0 comment('订单创建时间') index INT(10)"` + Level int `json:"level" xorm:"not null default 0 comment('0自购 1直推 大于1:间推') INT(10)"` + Mode string `json:"mode" xorm:"default '' comment('分佣方案类型') VARCHAR(255)"` + AdditionalSubsidy string `json:"additional_subsidy" xorm:"default 0.000000 comment('额外补贴 酒庄模式才有效') DECIMAL(16,6)"` + ExtendType int `json:"extend_type" xorm:"default 0 unique(IDX_ORD) comment('0普通 1超级推荐人 2团长 3团长上级超级推荐人 4团长担保用户') INT(11)"` +} diff --git a/app/svc/svc_comm_deal_commission.go b/app/svc/svc_comm_deal_commission.go new file mode 100644 index 0000000..8fa1692 --- /dev/null +++ b/app/svc/svc_comm_deal_commission.go @@ -0,0 +1,282 @@ +package svc + +import ( + db2 "applet/app/db" + "applet/app/db/model" + "applet/app/utils" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/lib/comm_plan" + 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" + svc2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/svc" + "fmt" + "github.com/jinzhu/copier" + "time" + "xorm.io/xorm" +) + +func CommGetLvUser(engine *xorm.Engine, CommissionParam md2.CommissionFirstParam, oid int64, masterId string, mapData map[string]string) { + commArr := rule.GetComm(engine) + plan, commission, virtualCoinMoneyRate := svc2.GetAllPlan(engine, masterId) + var CommissionParam1 md2.CommissionParam + comm := CommissionParam.CommissionParam + err2 := copier.Copy(&CommissionParam1, &comm) + fmt.Println(err2) + _, _, _, _, lvUser, err := svc2.GetRewardCommission(engine, &CommissionParam1, false, CommissionParam.Uid, CommissionParam.Provider, masterId, true, mapData, commArr, plan, commission, virtualCoinMoneyRate) + if err != nil { + return + } + if lvUser == nil { + return + } + + CommCommOrderRelateInsert(engine, oid, CommissionParam.Provider, int(time.Now().Unix()), lvUser, mapData) + //sql := `SELECT id from user_virtual_coin_flow where ord_id='%d'` + //sql = fmt.Sprintf(sql, oid) + //nativeString, _ := db2.QueryNativeString(engine, sql) + //has := false + //if len(nativeString) > 0 && utils.StrToInt(nativeString[0]["id"]) > 0 { + // has = true + //} + //sql1 := `SELECT id from fin_user_flow where ord_id='%d'` + //sql1 = fmt.Sprintf(sql1, oid) + //nativeString1, _ := db2.QueryNativeString(engine, sql1) + //has1 := false + //if len(nativeString1) > 0 && utils.StrToInt(nativeString1[0]["id"]) > 0 { + // has1 = true + //} + //fmt.Println("===========任务4=============", masterId, CommissionParam.Uid, oid, has, has1) + //if has == false && has1 == false { + CommSettleDone(engine, CommissionParam.Provider, oid, masterId, mapData) + //} +} + +// 分佣表插入获取到的数据 +func CommCommOrderRelateInsert(eg *xorm.Engine, oid int64, pvd string, createTime int, lvUser *comm_plan.LvUser, mapData map[string]string) { + level := 0 + oldLevel := 0 + fmt.Println(lvUser) + profit := utils.FloatFormat(lvUser.Profit+lvUser.SubsidyFee, 6) + oldLvUser := lvUser + data := []*model.CommOrdListRelate{ + { + Oid: oid, + Uid: lvUser.Uid, + Amount: profit, + Pvd: pvd, + CreateAt: createTime, + Level: level, + }, + } + if mapData["reward_type"] == "2" { + data = make([]*model.CommOrdListRelate, 0) + } + mode := mapData["mode"] + for lvUser.ParentUser != nil { + lvUser = lvUser.ParentUser + if lvUser.Uid == 0 { + continue + } + fmt.Println(lvUser) + level = level + 1 + profit = utils.FloatFormat(lvUser.Profit+lvUser.SubsidyFee, 6) + var additionalSubsidy float64 = 0 + if utils.InArr(mode, []string{"lv_winery", "public_platoon"}) { + profit = utils.FloatFormat(lvUser.Profit, 6) + additionalSubsidy = lvUser.SubsidyFee + } + data = append(data, &model.CommOrdListRelate{ + Oid: oid, + Uid: lvUser.Uid, + Amount: profit, + Pvd: pvd, + CreateAt: createTime, + Level: level, + Mode: mode, + AdditionalSubsidy: utils.Float64ToStrByPrec(additionalSubsidy, 9), + ExtendType: lvUser.ExtendType, + }) + } + for _, v := range data { + if utils.StrToInt(mapData["coin_id_type"]) > 0 { + v.Amount = 0 + v.Info = "任务没佣金设置,不返佣金" + } + } + err1 := db2.DbInsertBatch(eg, data) + if err1 != nil { + return + } else { + // 插入虚拟币数据 + vcrData := CommCombineVirtualCoinRelateData(oldLvUser, oid, pvd, oldLevel, mapData) + err2 := db2.DbInsertBatch(eg, vcrData) + for _, item := range vcrData { + fmt.Println(item) + } + if err2 != nil { + return + } + } +} +func CommSettleDone(eg *xorm.Engine, pvd string, oid int64, masterId string, mapData map[string]string) bool { + fmt.Println("=======================任务=============", masterId, oid, pvd) + ol, err := db2.CommOrderRelateListByOid(eg, oid, pvd) + if err != nil { + return false + } + // 查询虚拟币 virtual_coin_relate 表 + vcrList, err := db2.GetVirtualCoinRelateListWithOrdId(eg, oid, pvd) + if err != nil { + return false + } + session := eg.NewSession() + defer session.Close() + if err := session.Begin(); err != nil { + return false + } + + set, _ := db2.SysCfgGetOne(eg, "app_name_cn") + var appName = "" + if set != nil { + appName = set.Val + } + for _, item := range ol { + if mapData["reward_type"] == "2" && item.Level == 0 { + continue + } + //佣金不为空 + if item.Amount > 0 { + //公排 + if utils.InArr(item.Mode, []string{"lv_commission_public_platoon", "lv_price_public_platoon"}) && item.ExtendType == 5 { + fmt.Println("======================555") + bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, 0, item.Amount) + fmt.Println("======================666", bools) + if bools == false { + _ = session.Rollback() + return false + } + continue + } + _ = CommCommSettleMoney(session, item, appName, masterId, mapData) + } + if utils.StrToFloat64(item.AdditionalSubsidy) > 0 { + if item.Mode == "public_platoon" { //公排 + bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, 0, utils.StrToFloat64(item.AdditionalSubsidy)) + if bools == false { + _ = session.Rollback() + return false + } + } + } + } + // 虚拟币相关操作 + for _, item := range vcrList { + if mapData["reward_type"] == "2" && item.Level == 0 { + continue + } + if utils.StrToFloat64(item.Amount) > 0 { + if utils.InArr(item.Mode, []string{"lv_commission_public_platoon", "lv_price_public_platoon"}) && item.ExtendType == 5 { + fmt.Println("======================555") + bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, item.CoinId, utils.StrToFloat64(item.Amount)) + fmt.Println("======================666", bools) + if bools == false { + _ = session.Rollback() + return false + } + continue + } + _ = CommSettleVirtualCoin(session, item, mapData) + } + //公排 + if item.Mode == "public_platoon" && utils.StrToFloat64(item.AdditionalSubsidy) > 0 { + bools := PublicPlatoonSettle(session, "共富奖励", mapData["title"], utils.Int64ToStr(oid), item.Uid, item.CoinId, utils.StrToFloat64(item.AdditionalSubsidy)) + if bools == false { + _ = session.Rollback() + return false + } + } + } + // 提交事务 + err = session.Commit() + if err != nil { + return false + } + return true +} +func CommCommSettleMoney(session *xorm.Session, item *model.CommOrdListRelate, appName, masterId string, mapData map[string]string) bool { + now := time.Now() + beforeAmount := "0" + afterAmount := "0" + var affected int64 = 0 + userProfile, err := db2.UserProfileFindByIdWithSession(session, item.Uid) + if userProfile == nil { + } + if err != nil || userProfile == nil { + _ = session.Rollback() + return false + } + + // 获取余额更新锁 + cb, err := HandleBalanceDistributedLock(masterId, utils.IntToStr(item.Uid), "mall_settle_order") + if err != nil { + fmt.Println(err.Error()) + _ = session.Rollback() + return false + } + // 释放锁 + if cb != nil { + defer cb() + } + // 开始写入流水 + var orderAction int + if item.Level == 0 { + orderAction = 10 // 自购 + } else { + orderAction = 11 // 推广 + } + var ItemTitle = mapData["title"] + "奖励" + if item.Level > 0 { + ItemTitle = mapData["title"] + "团队奖励" + } + finUserFlow := model.FinUserFlow{ + Type: 0, + Uid: item.Uid, + Amount: utils.Float64ToStrByPrec(item.Amount, 8), + BeforeAmount: beforeAmount, + AfterAmount: afterAmount, + OrdType: item.Pvd, + OrdId: utils.AnyToString(item.Oid), + OrdAction: orderAction, + OrdDetail: "", + State: 2, + OtherId: item.Id, + OrdTitle: ItemTitle, + OrdTime: int(now.Unix()), + CreateAt: now, + UpdateAt: now, + } + // 更新用户余额 + finUserFlow.BeforeAmount = userProfile.FinValid + userProfile.FinValid = utils.Float64ToStrByPrec(utils.AnyToFloat64(userProfile.FinValid)+utils.AnyToFloat64(item.Amount), 8) + userProfile.FinTotal = userProfile.FinTotal + utils.StrToFloat32(utils.Float64ToStrByPrec(item.Amount, 8)) + affected, err = db2.UserProfileUpdateWithSession(session, item.Uid, userProfile) + finUserFlow.AfterAmount = userProfile.FinValid + has, errs := db2.InsertCommWithSession( + session, &finUserFlow) + if affected == 0 || err != nil || errs != nil || has == 0 { + _ = session.Rollback() + return false + } + if utils.InArr(item.Mode, []string{"public_platoon", "lv_commission_public_platoon", "lv_price_public_platoon"}) { //公排的加一条流水 + kind := 4 + title := "自购奖" + if item.Level == 1 { + kind = 5 + title = "直推奖" + } + bools := PublicPlatoonAddRecordWithSession(session, title, utils.Int64ToStr(item.Oid), item.Uid, 0, kind, 0, item.Amount, beforeAmount) + if bools == false { + return false + } + } + return true +} diff --git a/app/svc/svc_deal_commission.go b/app/svc/svc_deal_commission.go index 64c3f17..5766f3f 100644 --- a/app/svc/svc_deal_commission.go +++ b/app/svc/svc_deal_commission.go @@ -259,7 +259,7 @@ func SettleDone(eg *xorm.Engine, pvd string, oid int64, masterId string, mapData } continue } - _ = CommSettleVirtualCoin(session, item) + _ = CommSettleVirtualCoin(session, item, map[string]string{}) } //公排 if item.Mode == "public_platoon" && utils.StrToFloat64(item.AdditionalSubsidy) > 0 { @@ -362,7 +362,7 @@ func CommSettleMoney(session *xorm.Session, item *model.TaskOrdListRelate, appNa return true } -func CommSettleVirtualCoin(session *xorm.Session, virtualCoinRelateItem *model.VirtualCoinRelate) bool { +func CommSettleVirtualCoin(session *xorm.Session, virtualCoinRelateItem *model.VirtualCoinRelate, mapData map[string]string) bool { var ( beforeAmount = "0" @@ -407,11 +407,17 @@ func CommSettleVirtualCoin(session *xorm.Session, virtualCoinRelateItem *model.V if virtualCoinRelateItem.Pvd == "adset_video" { title = "看视频奖励" } + if mapData["title"] != "" { + title = mapData["title"] + "奖励" + } if virtualCoinRelateItem.Level > 0 { title = "团队任务奖励" if virtualCoinRelateItem.Pvd == "adset_video" { title = "团队看视频奖励" } + if mapData["title"] != "" { + title = mapData["title"] + "团队奖励" + } } // 用户虚拟币流水表新增记录 var userVirtualCoinFlow = model.UserVirtualCoinFlow{ diff --git a/consume/init.go b/consume/init.go index 55ba141..51cadda 100644 --- a/consume/init.go +++ b/consume/init.go @@ -96,6 +96,8 @@ func initConsumes() { jobs[consumeMd.ZhiosTaskVideoRewardConsumeFunName] = ZhiosTaskVideoRewardExchange //视频分佣 jobs[consumeMd.ZhiosNewVideoRewardConsumeFunName] = ZhiosNewVideoRewardExchange //短视频奖励 + jobs[consumeMd.ZhiosRelateRewardConsumeFunName] = ZhiosRelateRewardExchange //分佣结算 + } func Run() { diff --git a/consume/md/consume_key.go b/consume/md/consume_key.go index 4f82f62..2ce12b5 100644 --- a/consume/md/consume_key.go +++ b/consume/md/consume_key.go @@ -461,6 +461,15 @@ var RabbitMqQueueKeyList = []*MqQueue{ BindKey: "", ConsumeFunName: "ZhiosTaskRewardExchange", }, + { + ExchangeName: "zhios.relate_reward.exchange", + Name: "zhios_relate_reward", + Type: FanOutQueueType, + IsPersistent: false, + RoutKey: "relate_reward", + BindKey: "", + ConsumeFunName: "ZhiosRelateRewardExchange", + }, { ExchangeName: "zhios.task_video_reward.exchange", Name: "zhios_task_video_reward", @@ -553,6 +562,7 @@ const ( CancalUserRelateConsumeFunName = "CancalUserRelateConsume" CancalUserIntegralExchangeConsumeFunName = "CancalUserIntegralExchange" ZhiosTaskRewardConsumeFunName = "ZhiosTaskRewardExchange" + ZhiosRelateRewardConsumeFunName = "ZhiosRelateRewardExchange" ZhiosTaskVideoRewardConsumeFunName = "ZhiosTaskVideoRewardExchange" ZhiosNewVideoRewardConsumeFunName = "ZhiosNewVideoRewardExchange" CanalOneOrengeUserVirtualCcoinFlowFunName = "CanalOneOrengeUserVirtualCoinFlowConsume" diff --git a/consume/md/md.go b/consume/md/md.go index fe6dec7..12cbbf1 100644 --- a/consume/md/md.go +++ b/consume/md/md.go @@ -20,6 +20,7 @@ type ZhiosTaskReward struct { Oid string `json:"oid"` RewardType string `json:"reward_type"` PlanType string `json:"plan_type"` + Provider string `json:"provider"` } type AcquisitionCfg struct { Id string `json:"id"` diff --git a/consume/zhios_relate_reward_exchange.go b/consume/zhios_relate_reward_exchange.go new file mode 100644 index 0000000..f476dd0 --- /dev/null +++ b/consume/zhios_relate_reward_exchange.go @@ -0,0 +1,93 @@ +package consume + +import ( + "applet/app/db" + "applet/app/svc" + "applet/app/utils" + "applet/app/utils/logx" + "applet/consume/md" + "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" + md3 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" + svc2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/svc" + "encoding/json" + "errors" + "fmt" + "github.com/streadway/amqp" +) + +// +func ZhiosRelateRewardExchange(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 = handleZhiosRelateRewardExchange(res.Body) + //_ = res.Reject(false) + fmt.Println(err) + _ = res.Ack(true) + } else { + panic(errors.New("error getting message")) + } + } + fmt.Println("get msg done") +} + +func handleZhiosRelateRewardExchange(msg []byte) error { + //1、解析canal采集至mq中queue的数据结构体 + var canalMsg *md.ZhiosTaskReward + 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 + } + uid := canalMsg.Uid + //计算佣金 + var CommissionParam md3.CommissionFirstParam + CommissionParam.CommissionParam.Commission = canalMsg.Reward + CommissionParam.Uid = uid + CommissionParam.Provider = canalMsg.Provider + + plans, commission, virtualCoinMoneyRate := svc2.GetAllPlan(eg, mid) + cfgs, err := svc2.GetPlanCfg(eg, canalMsg.Provider, mid, plans, commission, virtualCoinMoneyRate, &CommissionParam.CommissionParam) + if err != nil || cfgs == nil { + return nil + } + title := canalMsg.Title + var mapData = map[string]string{ + "coin_id_type": canalMsg.CoinIdType, + "mode": cfgs.Mode, + "title": title, + "device_model": canalMsg.DeviceModel, + "reward_type": canalMsg.RewardType, + } + svc.CommGetLvUser(eg, CommissionParam, utils.StrToInt64(canalMsg.Oid), mid, mapData) + return nil +}