From 01406bf788b3d654df9a8f1eff7ef1f4c3885eb6 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Tue, 6 Feb 2024 16:49:01 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=B8=80=E4=B8=AA=E5=9C=88=E5=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...er_public_platoon_system_punish_records.go | 15 ++ ...er_public_platoon_system_punish_records.go | 18 ++ ...public_platoon_user_relation_commission.go | 165 +++++++++++++++++- 3 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 db/db_one_circles_user_public_platoon_system_punish_records.go create mode 100644 db/model/one_circles_user_public_platoon_system_punish_records.go diff --git a/db/db_one_circles_user_public_platoon_system_punish_records.go b/db/db_one_circles_user_public_platoon_system_punish_records.go new file mode 100644 index 0000000..3700a13 --- /dev/null +++ b/db/db_one_circles_user_public_platoon_system_punish_records.go @@ -0,0 +1,15 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" + "xorm.io/xorm" +) + +// OneCirclesUserPublicPlatoonSystemPunishRecordsInsert 插入单条数据 +func OneCirclesUserPublicPlatoonSystemPunishRecordsInsert(session *xorm.Session, OneCirclesUserPublicPlatoonSystemPunishRecords *model.OneCirclesUserPublicPlatoonSystemPunishRecords) (int64, error) { + _, err := session.InsertOne(OneCirclesUserPublicPlatoonSystemPunishRecords) + if err != nil { + return 0, err + } + return OneCirclesUserPublicPlatoonSystemPunishRecords.Id, nil +} diff --git a/db/model/one_circles_user_public_platoon_system_punish_records.go b/db/model/one_circles_user_public_platoon_system_punish_records.go new file mode 100644 index 0000000..71ff6d0 --- /dev/null +++ b/db/model/one_circles_user_public_platoon_system_punish_records.go @@ -0,0 +1,18 @@ +package model + +import ( + "time" +) + +type OneCirclesUserPublicPlatoonSystemPunishRecords struct { + Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"` + Uid int `json:"uid" xorm:"not null comment('用户id') index INT(11)"` + OldPostion int `json:"old_postion" xorm:"not null default 0 comment('位置(旧)') INT(11)"` + NewPostion int `json:"new_postion" xorm:"not null default 0 comment('位置(新)') INT(11)"` + Date string `json:"date" xorm:"not null default '' comment('日期(0000-00)') VARCHAR(50)"` + Title string `json:"title" xorm:"not null default '' comment('标题') VARCHAR(100)"` + Reason string `json:"reason" xorm:"not null default '' comment('原因') VARCHAR(255)"` + Type int `json:"type" xorm:"not null default 1 comment('类型(1:大公排 2:小公排)') TINYINT(1)"` + 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/one_circles/one_circles_public_platoon_user_relation_commission.go b/rule/one_circles/one_circles_public_platoon_user_relation_commission.go index 5da51d6..626d01d 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 @@ -5,6 +5,8 @@ import ( "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" + "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" "math" @@ -255,7 +257,7 @@ func OneCirclesPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, oneCirc } - //2、查找对应pid的 user_public_platoon_relation 记录 + //2、查找对应pid的 one_circles_public_platoon_user_relation 记录 var m1 model.OneCirclesPublicPlatoonUserRelation if has, err := engine.Where("uid=?", fatherUid).Get(&m1); err != nil || has == false { return oneCirclesPublicPlatoonUserRelation, err @@ -273,7 +275,7 @@ func OneCirclesPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, oneCirc } } - // 3、插入 user_public_platoon_relation 记录 + // 3、插入 one_circles_public_platoon_user_relation 记录 now := time.Now() oneCirclesPublicPlatoonUserRelation.Uid = uid oneCirclesPublicPlatoonUserRelation.FatherUid = fatherUidStr @@ -704,3 +706,162 @@ func smallMakeSearchLevel(position *int, rows float64, times *float64) (level in return smallMakeSearchLevel(position, rows, times) } } + +// OneCirclesDealCommonWealthPunish 处理公排处罚 +func OneCirclesDealCommonWealthPunish(engine *xorm.Engine, uid int, reason string) (err error, resp []map[string]string) { + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + + //1、查找 `user_public_platoon_setting` 基础设置 + oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + _ = session.Rollback() + return + } + + //2、查询出 `one_circles_public_platoon_user_relation` 中相关记录 && 将该记录的uid置为 -1 + params, err := db.UserSmallPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ + "key": "uid", + "value": uid, + }) + if err != nil { + _ = session.Rollback() + return + } + if params == nil { + err = errors.New("未查询到公排关系记录") + _ = session.Rollback() + } + + //TODO::判断是否为推荐用户 + if params.RecommendUid == 0 { + params.Uid = params.Uid - 2147483648 + } else { + params.Uid = params.Uid - 2147483648 + } + + for n := 1; n <= 9; n++ { + str := "father_uid" + strconv.Itoa(n) + sql := fmt.Sprintf("UPDATE `user_small_public_platoon_relation` SET %s=%s where %s=%s", str, strconv.Itoa(params.Uid), str, strconv.Itoa(uid)) + fmt.Println(">>>>>>>sql>>>>>>>", sql) + _, err = session.Exec(sql) + if err != nil { + _ = session.Rollback() + return + } + } + + updateAffected, err := db.UserSmallPublicPlatoonRelationUpdate(session, params.Id, params) + if err != nil { + _ = session.Rollback() + return + } + if updateAffected == 0 { + err = errors.New("更新 one_circles_public_platoon_user_relation 记录失败") + _ = session.Rollback() + return + } + + //3、新增一条 `one_circles_public_platoon_user_relation` 记录 + params.Uid = uid + res, err := OneCirclesPublicPlatoon(engine, params.Uid, params.RecommendUid, *oneCirclesPublicPlatoonBasicSetting) + if err != nil { + _ = session.Rollback() + return + } + + //4、新增一条 `one_circles_user_public_platoon_system_punish_records` 记录 + now := time.Now() + insertAffected, err := db.OneCirclesUserPublicPlatoonSystemPunishRecordsInsert(session, &model.OneCirclesUserPublicPlatoonSystemPunishRecords{ + Uid: params.Uid, + OldPostion: params.Position, + NewPostion: res.Position, + Date: now.AddDate(0, 0, 30).Format("2006-01-02"), + Title: "共富收益-系统处罚记录", + Reason: reason, + Type: 2, + CreateAt: now, + UpdateAt: now, + }) + if err != nil { + _ = session.Rollback() + return + } + if insertAffected == 0 { + err = errors.New("新增 one_circles_user_public_platoon_system_punish_records 记录失败") + _ = session.Rollback() + return + } + + err = session.Commit() + return +} + +const OneCirclesDealUserPublicPlatoonPunishLockKey = "one_circles_deal_user_public_platoon_lock_key" + +// OneCirclesDealUserPublicPlatoonPunish 处理公排处罚 +func OneCirclesDealUserPublicPlatoonPunish(engine *xorm.Engine, masterId string) (err error) { + now := time.Now() + fmt.Println(now.Hour()) + if !(now.Hour() > 3 && now.Hour() < 8) { + //TODO::只在凌晨一点 ~ 凌晨 8 点运行 + return errors.New("非运行时间") + } + //TODO::增加“悲观锁”防止串行 + getString, _ := cache.GetString(OneCirclesDealUserPublicPlatoonPunishLockKey) + if getString != "" { + fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") + return errors.New("上一次结算未执行完") + } + cache.SetEx(OneCirclesDealUserPublicPlatoonPunishLockKey, "running", 3600*8) //8小时 + + //查找 `one_circles_public_platoon_basic_setting` 基础设置 + oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{ + "key": "is_open", + "value": 1, + }) + if err != nil { + return err + } + if oneCirclesPublicPlatoonBasicSetting == nil { + return errors.New("公排制度未开启") + } + if oneCirclesPublicPlatoonBasicSetting.SystemPunishReplace == 1 { + systemPunishReplaceValue := oneCirclesPublicPlatoonBasicSetting.SystemPunishReplaceValue + startDate := now.AddDate(0, 0, -systemPunishReplaceValue).Format("2006-01-02") + " 00:00:00" + var page = 1 + var pageSize = 100 + for { + var users []model.User + err = engine.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&users) + if err != nil { + return err + } + if len(users) <= 0 { + break + } + for _, user := range users { + var list []model.OneCirclesGreenEnergySignIn + err = engine.Where("start_time >= ?", startDate).And("uid = ?", user.Uid).Find(&list) + if len(list) <= 0 { + //进行处罚 + err, _ = OneCirclesDealCommonWealthPunish(engine, user.Uid, "公排处罚") + if err != nil { + return err + } + } + } + page++ + } + } + return +}