From f24b7bd14af4080c027225b058d33e958f4d1f36 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 6 Feb 2024 22:02:03 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=85=AC=E6=8E=92=E5=8F=8C=E7=BD=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db.go | 26 +++- db/db_user_public_platoon_relation.go | 9 ++ ...platoon_double_network_user_coin_record.go | 19 ++- ...latoon_double_network_relate_commission.go | 118 ++++++++++++++++-- 4 files changed, 152 insertions(+), 20 deletions(-) diff --git a/db/db.go b/db/db.go index 4fb8e39..3fc215a 100644 --- a/db/db.go +++ b/db/db.go @@ -1,6 +1,10 @@ package db -import "xorm.io/xorm" +import ( + zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" + "database/sql" + "xorm.io/xorm" +) func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) { results, err := Db.SQL(sql, args...).QueryString() @@ -16,3 +20,23 @@ func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, err row, err := session.InsertOne(model) return row, err } + +// ExecuteOriginalSqlBySession 执行原生sql +func ExecuteOriginalSqlBySession(session *xorm.Session, sql string) (sql.Result, error) { + result, err := session.Exec(sql) + if err != nil { + _ = zhios_order_relate_logx.Warn(err) + return nil, err + } + return result, nil +} + +// ExecuteOriginalSql 执行原生sql +func ExecuteOriginalSql(Db *xorm.Engine, sql string) (sql.Result, error) { + result, err := Db.Exec(sql) + if err != nil { + _ = zhios_order_relate_logx.Warn(err) + return nil, err + } + return result, nil +} diff --git a/db/db_user_public_platoon_relation.go b/db/db_user_public_platoon_relation.go index 208d7d5..e8becdf 100644 --- a/db/db_user_public_platoon_relation.go +++ b/db/db_user_public_platoon_relation.go @@ -29,6 +29,15 @@ func UserPublicPlatoonRelationInsert(Db *xorm.Engine, UserPublicPlatoonRelation return UserPublicPlatoonRelation.Id, nil } +// UserPublicPlatoonRelationInsertBySession 插入单条数据 +func UserPublicPlatoonRelationInsertBySession(session *xorm.Session, UserPublicPlatoonRelation *model.UserPublicPlatoonRelation) (int, error) { + _, err := session.InsertOne(UserPublicPlatoonRelation) + if err != nil { + return 0, err + } + return UserPublicPlatoonRelation.Id, nil +} + // BatchAddUserPublicPlatoonRelations 批量新增数据 func BatchAddUserPublicPlatoonRelations(Db *xorm.Engine, UserPublicPlatoonRelationData []*model.UserPublicPlatoonRelation) (int64, error) { affected, err := Db.Insert(UserPublicPlatoonRelationData) diff --git a/db/model/user_public_platoon_double_network_user_coin_record.go b/db/model/user_public_platoon_double_network_user_coin_record.go index 4e4ce4f..a2ec6ba 100644 --- a/db/model/user_public_platoon_double_network_user_coin_record.go +++ b/db/model/user_public_platoon_double_network_user_coin_record.go @@ -1,15 +1,12 @@ package model -import ( - "time" -) - type UserPublicPlatoonDoubleNetworkUserCoinRecord struct { - Id int `json:"id" xorm:"not null pk autoincr INT(11)"` - Uid int `json:"uid" xorm:"not null default 0 comment('uid') INT(11)"` - LastAmount string `json:"last_amount" xorm:"not null default 0.0000 comment('上次金额') DECIMAL(10,4)"` - Amount string `json:"amount" xorm:"not null default 0.0000 comment('当前金额') DECIMAL(10,4)"` - CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` - CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` - UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid int `json:"uid" xorm:"not null default 0 comment('uid') INT(11)"` + RecommendUid int `json:"recommend_uid" xorm:"not null default 0 comment('推荐人uid') INT(11)"` + LastAmount string `json:"last_amount" xorm:"not null default 0.0000 comment('上次金额') DECIMAL(10,4)"` + Amount string `json:"amount" xorm:"not null default 0.0000 comment('当前金额') DECIMAL(10,4)"` + CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` + CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` + UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` } diff --git a/rule/public_platoon_double_network_relate_commission.go b/rule/public_platoon_double_network_relate_commission.go index 06b91f4..460d285 100644 --- a/rule/public_platoon_double_network_relate_commission.go +++ b/rule/public_platoon_double_network_relate_commission.go @@ -2,9 +2,14 @@ package rule import ( "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" zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" + zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "errors" + "fmt" "github.com/shopspring/decimal" + "math/rand" "strconv" "time" "xorm.io/xorm" @@ -93,6 +98,7 @@ func GetUserRankAmount(engine *xorm.Engine, rank string) (amount string, err err } func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err error) { + fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!dbName!!!!!!!!!!!!!!!!!!!!", dbName) //1、查找 `user_public_platoon_double_network_setting` 基础设置 now := time.Now() userPublicPlatoonDoubleNetworkSetting, err := db.UserPublicPlatoonDoubleNetworkSettingGetOneByParams(engine, map[string]interface{}{ @@ -109,17 +115,113 @@ func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err er return errors.New("非重置日期!") } - //2、计算排名数据 - sql := "SELECT id, uid, amount, @rank := @rank + 1 AS rank FROM `user_public_platoon_double_network_user_coin_record`, (SELECT @rank:=0) r ORDER BY amount DESC;" + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + + session.Begin() + //2、进行数据清理 + sql1 := "DROP TABLE `user_public_platoon_relation_1`" //删除备份表 + sql2 := "CREATE TABLE `user_public_platoon_relation_1` LIKE `user_public_platoon_relation`" //复制表结构 + sql3 := "INSERT INTO `user_public_platoon_relation_1` SELECT * FROM `user_public_platoon_relation`" //复制表数据 + sql4 := "TRUNCATE TABLE `user_public_platoon_relation`;" //截断表 + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql1) + if err != nil { + _ = session.Rollback() + fmt.Println("err___SQl1", err.Error()) + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql2) + if err != nil { + _ = session.Rollback() + return err + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql3) + if err != nil { + _ = session.Rollback() + return err + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql4) + if err != nil { + _ = session.Rollback() + return err + } + + //2、先插入创始人 + //2.1 查找 `user_public_platoon_setting` 基础设置 + userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + _ = session.Rollback() + return err + } + //2.2 插入数据 + _, err = db.UserPublicPlatoonRelationInsertBySession(session, &model.UserPublicPlatoonRelation{ + Uid: userPublicPlatoonSetting.OriginatorUid, + FatherUid: "", + Pid: 0, + RecommendUid: 0, + Level: 1, + Position: 1, + UniqueIdentifier: "0-" + zhios_order_relate_utils.IntToStr(userPublicPlatoonDoubleNetworkSetting.OriginatorUid) + "-1-1", + ReturnCommissionNum: 0, + JoinAt: now, + WaitForSettlementDate: now.AddDate(0, 0, userPublicPlatoonSetting.SettleDay).Format("2006-01-02"), + CreateAt: now, + UpdateAt: now, + }) + if err != nil { + _ = session.Rollback() + return err + } + err = session.Begin() + if err != nil { + _ = session.Rollback() + return err + } + + //3、计算排名数据 + sql := "SELECT id, uid,recommend_uid, amount, @rank := @rank + 1 AS rank FROM `user_public_platoon_double_network_user_coin_record`, (SELECT @rank:=0) r ORDER BY amount DESC;" nativeString, _ := db.QueryNativeString(engine, sql) if len(nativeString) <= 0 { return errors.New("当前无排名数据") } - //for _, v := range nativeString { - // //if rank == v["rank"] { - // // amount = v["amount"] - // // break - // //} - //} + for _, v := range nativeString { + //3.1 加入公排 + _, err1 := AddPublicPlatoonRelateCommission(engine, []*md.AddPublicPlatoonRelateCommissionReq{ + { + Uid: v["uid"], + RecommendUid: v["recommend_uid"], + }, + }) + if err1 != nil { + return err1 + } + //3.2将`user_public_platoon_double_network_user_coin_record` 中 amount 置0 + var tmpSql = fmt.Sprintf("UPDATE table_name SET amount = 0 WHERE id=%s", v["id"]) + _, err2 := db.ExecuteOriginalSql(engine, tmpSql) + if err2 != nil { + return err2 + } + } + + //4、修改 user_public_platoon_double_network_setting 中的结算时间 + userPublicPlatoonDoubleNetworkSetting.SettlementDate = now.AddDate(0, 1, 0).Format("2006-01-02") + updateAffected, err := db.UserPublicPlatoonDoubleNetworkSettingUpdate(engine, userPublicPlatoonDoubleNetworkSetting.Id, userPublicPlatoonDoubleNetworkSetting, "settlement_date") + if err != nil { + return err + } + if updateAffected <= 0 { + fmt.Println("updateAffected:::::::::", updateAffected) + return errors.New("更新结算时间失败") + } return }