diff --git a/db/model/user_public_platoon_double_network_setting.go b/db/model/user_public_platoon_double_network_setting.go index af3c862..94eff2a 100644 --- a/db/model/user_public_platoon_double_network_setting.go +++ b/db/model/user_public_platoon_double_network_setting.go @@ -5,14 +5,15 @@ import ( ) type UserPublicPlatoonDoubleNetworkSetting struct { - Id int `json:"id" xorm:"not null pk autoincr INT(11)"` - IsOpen int `json:"is_open" xorm:"not null default 0 comment('是否开启(0:关闭 1:开启)') TINYINT(1)"` - SeveralTimes int `json:"several_times" xorm:"not null default 0 comment('几乘') TINYINT(3)"` - SeveralRows int `json:"several_rows" xorm:"not null default 0 comment('几排') TINYINT(3)"` - OriginatorUid int `json:"originator_uid" xorm:"not null default -1 comment('创始人uid') INT(11)"` - SettlementDate string `json:"settlement_date" xorm:"not null default '0000-00-00 00:00:00' comment('结算日期') CHAR(50)"` - CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` - Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` - 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)"` + IsOpen int `json:"is_open" xorm:"not null default 0 comment('是否开启(0:关闭 1:开启)') TINYINT(1)"` + SeveralTimes int `json:"several_times" xorm:"not null default 0 comment('几乘') TINYINT(3)"` + SeveralRows int `json:"several_rows" xorm:"not null default 0 comment('几排') TINYINT(3)"` + OriginatorUid int `json:"originator_uid" xorm:"not null default -1 comment('创始人uid') INT(11)"` + LastSettlementDate string `json:"last_settlement_date" xorm:"not null default '0000-00-00' comment('上一次结算日期') CHAR(50)"` + SettlementDate string `json:"settlement_date" xorm:"not null default '0000-00-00' comment('结算日期') CHAR(50)"` + CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` + Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` + 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"` } diff --git a/rule/public_platoon_double_network_relate_commission.go b/rule/public_platoon_double_network_relate_commission.go index af81d17..87f0369 100644 --- a/rule/public_platoon_double_network_relate_commission.go +++ b/rule/public_platoon_double_network_relate_commission.go @@ -6,6 +6,7 @@ import ( "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/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" zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" "errors" "fmt" @@ -16,6 +17,14 @@ import ( "xorm.io/xorm" ) +func InitForResetPublicPlatoonDoubleNetwork(redisAddr string) (err error) { + if redisAddr != "" { + cache.NewRedis(redisAddr) + } + _, err = cache.SelectDb(md.RedisDataBase) + return +} + /* EstimateUserPosition 预估用户位置 total 排名总数 @@ -166,8 +175,23 @@ func GetUserRankAmount(engine *xorm.Engine, rank string) (amount string, err err return } +const ResetPublicPlatoonDoubleNetworkPessimismLockKey = "reset_public_platoon_double_network_lock_key" +const PessimismLResetPublicPlatoonDoubleNetworkPessimismLockValue = "running" + func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err error) { fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!dbName!!!!!!!!!!!!!!!!!!!!", dbName) + + //TODO::增加“悲观锁”防止串行 + getString, _ := cache.GetString(ResetPublicPlatoonDoubleNetworkPessimismLockKey) + //if err != nil { + // return err + //} + if getString == PessimismLResetPublicPlatoonDoubleNetworkPessimismLockValue { + fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") + return errors.New("上一次结算未执行完") + } + cache.SetEx(ResetPublicPlatoonDoubleNetworkPessimismLockKey, PessimismLResetPublicPlatoonDoubleNetworkPessimismLockValue, 3600*8) //8小时 + //1、查找 `user_public_platoon_double_network_setting` 基础设置 now := time.Now() userPublicPlatoonDoubleNetworkSetting, err := db.UserPublicPlatoonDoubleNetworkSettingGetOneByParams(engine, map[string]interface{}{ @@ -222,6 +246,29 @@ func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err er return err } + sql5 := "DROP TABLE `user_public_platoon_double_network_user_coin_record_1`" //删除备份表 + sql6 := "CREATE TABLE `user_public_platoon_double_network_user_coin_record_1` LIKE `user_public_platoon_double_network_user_coin_record`" //复制表结构 + sql7 := "INSERT INTO `user_public_platoon_double_network_user_coin_record_1` SELECT * FROM `user_public_platoon_double_network_user_coin_record`" //复制表数据 + + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql5) + if err != nil { + _ = session.Rollback() + return err + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql6) + if err != nil { + _ = session.Rollback() + return err + } + time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000))) + _, err = db.ExecuteOriginalSqlBySession(engine.NewSession(), sql7) + if err != nil { + _ = session.Rollback() + return err + } + //2、先插入创始人 //2.1 查找 `user_public_platoon_setting` 基础设置 userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ @@ -251,7 +298,7 @@ func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err er _ = session.Rollback() return err } - err = session.Begin() + err = session.Commit() if err != nil { _ = session.Rollback() return err @@ -264,6 +311,10 @@ func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err er return errors.New("当前无排名数据") } for _, v := range nativeString { + if v["uid"] == zhios_order_relate_utils.IntToStr(userPublicPlatoonSetting.OriginatorUid) { + //TODO::创始人不需要重排 + continue + } //3.1 加入公排 _, err1 := AddPublicPlatoonRelateCommission(engine, []*md.AddPublicPlatoonRelateCommissionReq{ { @@ -275,16 +326,36 @@ func ResetPublicPlatoonDoubleNetwork(engine *xorm.Engine, dbName string) (err er 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"]) + var tmpSql = fmt.Sprintf("UPDATE user_public_platoon_double_network_user_coin_record 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 中的结算时间 + //4、修改 user_public_platoon_relation 中的 wait_for_settlement_date 待结算时间 + sql8 := "SELECT * FROM `user_public_platoon_relation_1`" + nativeString1, _ := db.QueryNativeString(engine, sql8) + if len(nativeString1) >= 0 { + var oldWaitForSettlementDateMap = map[string]string{} + for _, v := range nativeString1 { + oldWaitForSettlementDateMap[v["uid"]] = v["wait_for_settlement_date"] + } + sql9 := "SELECT * FROM `user_public_platoon_relation`" + nativeString2, _ := db.QueryNativeString(engine, sql9) + for _, v := range nativeString2 { + if oldWaitForSettlementDateMap[v["uid"]] != "" { + tmpSql := fmt.Sprintf("update `user_public_platoon_relation` set wait_for_settlement_date = '%s' where uid = %s", oldWaitForSettlementDateMap[v["uid"]], v["uid"]) + db.ExecuteOriginalSql(engine, tmpSql) + time.Sleep(time.Millisecond * time.Duration(rand.Intn(100))) + } + } + } + + //5、修改 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") + userPublicPlatoonDoubleNetworkSetting.LastSettlementDate = now.Format("2006-01-02") + updateAffected, err := db.UserPublicPlatoonDoubleNetworkSettingUpdate(engine, userPublicPlatoonDoubleNetworkSetting.Id, userPublicPlatoonDoubleNetworkSetting, "settlement_date", "last_settlement_date") if err != nil { return err }