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_one_circles_public_platoon_free_punish_with_user.go b/db/db_one_circles_public_platoon_free_punish_with_user.go index 7249cb6..e0139a3 100644 --- a/db/db_one_circles_public_platoon_free_punish_with_user.go +++ b/db/db_one_circles_public_platoon_free_punish_with_user.go @@ -12,8 +12,35 @@ func FindAllOneCirclesPublicPlatoonFreePunishWithUser(Db *xorm.Engine) (resp map if err != nil { return nil, zhios_order_relate_logx.Error(err) } + resp = map[int]*model.OneCirclesPublicPlatoonFreePunishWithUser{} for _, v := range m { resp[v.Uid] = &v } return } + +// OneCirclesPublicPlatoonFreePunishWithUserInsert 插入单条数据 +func OneCirclesPublicPlatoonFreePunishWithUserInsert(Db *xorm.Engine, oneCirclesPublicPlatoonFreePunishWithUser *model.OneCirclesPublicPlatoonFreePunishWithUser) (int, error) { + _, err := Db.InsertOne(oneCirclesPublicPlatoonFreePunishWithUser) + if err != nil { + return 0, err + } + return oneCirclesPublicPlatoonFreePunishWithUser.Id, nil +} + +// OneCirclesPublicPlatoonFreePunishWithUserUpdate 更新记录 +func OneCirclesPublicPlatoonFreePunishWithUserUpdate(Db *xorm.Engine, id interface{}, oneCirclesPublicPlatoonFreePunishWithUser *model.OneCirclesPublicPlatoonFreePunishWithUser, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(oneCirclesPublicPlatoonFreePunishWithUser) + } else { + affected, err = Db.Where("id=?", id).Update(oneCirclesPublicPlatoonFreePunishWithUser) + } + if err != nil { + return 0, err + } + return affected, nil +} diff --git a/db/db_one_circles_public_platoon_records_punish_with_user.go b/db/db_one_circles_public_platoon_records_punish_with_user.go new file mode 100644 index 0000000..78067ac --- /dev/null +++ b/db/db_one_circles_public_platoon_records_punish_with_user.go @@ -0,0 +1,46 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" + zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" + "xorm.io/xorm" +) + +func FindAllOneCirclesPublicPlatoonRecordsPunishWithUser(Db *xorm.Engine) (resp map[int]*model.OneCirclesPublicPlatoonRecordsPunishWithUser, err error) { + var m []model.OneCirclesPublicPlatoonRecordsPunishWithUser + err = Db.Where("1=1").Find(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + resp = map[int]*model.OneCirclesPublicPlatoonRecordsPunishWithUser{} + for _, v := range m { + resp[v.Uid] = &v + } + return +} + +// OneCirclesPublicPlatoonRecordsPunishWithUserInsert 插入单条数据 +func OneCirclesPublicPlatoonRecordsPunishWithUserInsert(Db *xorm.Engine, oneCirclesPublicPlatoonRecordsPunishWithUser *model.OneCirclesPublicPlatoonRecordsPunishWithUser) (int, error) { + _, err := Db.InsertOne(oneCirclesPublicPlatoonRecordsPunishWithUser) + if err != nil { + return 0, err + } + return oneCirclesPublicPlatoonRecordsPunishWithUser.Id, nil +} + +// OneCirclesPublicPlatoonRecordsPunishWithUserUpdate 更新记录 +func OneCirclesPublicPlatoonRecordsPunishWithUserUpdate(Db *xorm.Engine, id interface{}, oneCirclesPublicPlatoonRecordsPunishWithUser *model.OneCirclesPublicPlatoonRecordsPunishWithUser, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(oneCirclesPublicPlatoonRecordsPunishWithUser) + } else { + affected, err = Db.Where("id=?", id).Update(oneCirclesPublicPlatoonRecordsPunishWithUser) + } + if err != nil { + return 0, err + } + return affected, 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/one_circles_public_platoon_records_punish_with_user.go b/db/model/one_circles_public_platoon_records_punish_with_user.go new file mode 100644 index 0000000..2f673d3 --- /dev/null +++ b/db/model/one_circles_public_platoon_records_punish_with_user.go @@ -0,0 +1,7 @@ +package model + +type OneCirclesPublicPlatoonRecordsPunishWithUser struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Uid int `json:"uid" xorm:"not null default 0 comment('用户id') INT(11)"` + Date string `json:"date" xorm:"default '0000-00-00' comment('处罚日期') CHAR(50)"` +} 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/one_circles/one_circles_public_platoon_user_relation_commission.go b/rule/one_circles/one_circles_public_platoon_user_relation_commission.go index d9a5196..d84f586 100644 --- a/rule/one_circles/one_circles_public_platoon_user_relation_commission.go +++ b/rule/one_circles/one_circles_public_platoon_user_relation_commission.go @@ -857,11 +857,38 @@ func OneCirclesDealUserPublicPlatoonPunish(engine *xorm.Engine, masterId string) var list []model.OneCirclesGreenEnergySignIn err = engine.Where("start_time >= ?", startDate).And("uid = ?", user.Uid).Find(&list) if len(list) <= 0 && mapOneCirclesPublicPlatoonFreePunishWithUser[user.Uid] == nil { + var oneCirclesPublicPlatoonRecordsPunishWithUser model.OneCirclesPublicPlatoonRecordsPunishWithUser + has, err1 := engine.Where("uid = ?", user.Uid).Get(&oneCirclesPublicPlatoonRecordsPunishWithUser) + if err1 != nil { + return err1 + } + oneCirclesPublicPlatoonRecordsPunishWithUserDate, _ := time.ParseInLocation("2006-01-02", oneCirclesPublicPlatoonRecordsPunishWithUser.Date, time.Local) + if has && now.AddDate(0, 0, -7).After(oneCirclesPublicPlatoonRecordsPunishWithUserDate) { + //TODO::不进行重复处罚 + continue + } //进行处罚 err, _ = OneCirclesDealCommonWealthPunish(engine, user.Uid, "公排处罚") if err != nil { return err } + + //添加 one_circles_public_platoon_records_punish_with_user 记录 + if has { + oneCirclesPublicPlatoonRecordsPunishWithUser.Date = now.Format("2006-01-02") + _, err2 := db.OneCirclesPublicPlatoonRecordsPunishWithUserUpdate(engine, oneCirclesPublicPlatoonRecordsPunishWithUser.Id, &oneCirclesPublicPlatoonRecordsPunishWithUser, "date") + if err2 != nil { + return err2 + } + } else { + _, err2 := db.OneCirclesPublicPlatoonRecordsPunishWithUserInsert(engine, &model.OneCirclesPublicPlatoonRecordsPunishWithUser{ + Uid: user.Uid, + Date: now.Format("2006-01-02"), + }) + if err2 != nil { + return err2 + } + } } } page++ 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 }