@@ -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
}