蛋蛋星球-制度模式
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

911 lines
36 KiB

  1. package rule
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  6. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  10. "errors"
  11. "fmt"
  12. "math"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "xorm.io/xorm"
  17. )
  18. // AddPublicPlatoonUserRelationCommission 新增公排用户关系记录
  19. func AddPublicPlatoonUserRelationCommission(engine *xorm.Engine, AddPublicPlatoonUserRelationCommissionReqList []*md2.AddPublicPlatoonUserRelationCommissionReq) (map[string]*model.PublicPlatoonUserRelation, error) {
  20. var resp = map[string]*model.PublicPlatoonUserRelation{}
  21. //查找 `public_platoon_basic_setting` 基础设置
  22. publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
  23. publicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
  24. "key": "is_open",
  25. "value": 1,
  26. })
  27. if err != nil {
  28. return nil, err
  29. }
  30. if publicPlatoonBasicSetting == nil {
  31. return nil, errors.New("公排制度未开启")
  32. }
  33. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  34. for _, param := range AddPublicPlatoonUserRelationCommissionReqList {
  35. isHasPublicPlatoonUserRelation, err11 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  36. "key": "uid",
  37. "value": param.Uid,
  38. })
  39. if err11 != nil {
  40. return nil, err11
  41. }
  42. if isHasPublicPlatoonUserRelation != nil {
  43. return nil, errors.New("当前用户已加入公排")
  44. }
  45. //1、判断是否有推荐人
  46. if param.RecommendUid != "" {
  47. //2、有推荐人
  48. //判断是否有uid为-1 (代表等待新用户填充) 的记录
  49. PublicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByPid(param.RecommendUid, map[string]interface{}{
  50. "key": "uid",
  51. "value": -1,
  52. })
  53. if err1 != nil {
  54. return nil, err1
  55. }
  56. if PublicPlatoonUserRelation != nil {
  57. now := time.Now()
  58. oldUid := PublicPlatoonUserRelation.Uid
  59. PublicPlatoonUserRelation.Uid = egg_system_rules.StrToInt64(param.Uid)
  60. PublicPlatoonUserRelation.RecommendUid = egg_system_rules.StrToInt64(param.RecommendUid)
  61. PublicPlatoonUserRelation.CreateAt = now.Format("2006-01-02 15:04:05")
  62. PublicPlatoonUserRelation.UpdateAt = now.Format("2006-01-02 15:04:05")
  63. PublicPlatoonUserRelation.JoinAt = now.Format("2006-01-02 15:04:05")
  64. updateAffected, err2 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationUpdate(engine.NewSession(), PublicPlatoonUserRelation.Id, PublicPlatoonUserRelation)
  65. if err2 != nil {
  66. return nil, err2
  67. }
  68. if updateAffected == 0 {
  69. err = errors.New("更新 public_platoon_user_relation 记录失败")
  70. return nil, err
  71. } else {
  72. for n := 1; n <= 9; n++ {
  73. str := "father_uid" + strconv.Itoa(n)
  74. sql := fmt.Sprintf("UPDATE `public_platoon_user_relation` SET %s=%s where %s=%s", str, param.Uid, str, egg_system_rules.Int64ToStr(oldUid))
  75. fmt.Println(">>>>>>>sql>>>>>>>", sql)
  76. _, err = engine.Exec(sql)
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. resp[param.Uid] = PublicPlatoonUserRelation
  82. }
  83. continue
  84. }
  85. res, err := PublicPlatoon(engine, egg_system_rules.StrToInt64(param.Uid), egg_system_rules.StrToInt64(param.RecommendUid), *publicPlatoonBasicSetting)
  86. if err != nil {
  87. return nil, err
  88. }
  89. resp[param.Uid] = &res
  90. }
  91. }
  92. return resp, nil
  93. }
  94. /*
  95. 公排方法
  96. TODO 相关公式:
  97. 1: 每个等级的起始值(1+5^0+5^1+5^2+...+5^x-2), 每个等级的结束值(`5^0+5^1+5^2+...+5^x-1)
  98. 2: 根据position查找父级position { (position-1)/5 }
  99. 3: 根据position查找等级level {position-5^0-5^1-5^2-...-5^x 是否 <0 ? => x+1 }
  100. 4: 根据最新自增`id` 逆向推导 position {levelFirstPosition + (position-1)%5}
  101. */
  102. func PublicPlatoon(engine *xorm.Engine, uid, recommendUid int64, PublicPlatoonBasicSetting model.PublicPlatoonBasicSetting) (model.PublicPlatoonUserRelation, error) {
  103. var standardPublicPlatoonRelation md.StandardPublicPlatoonRelation
  104. var PublicPlatoonUserRelation model.PublicPlatoonUserRelation
  105. var fatherUid int64
  106. //1、 查找当前 public_platoon_user_relation 中 recommendUid 的记录
  107. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  108. m, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  109. "key": "uid",
  110. "value": recommendUid,
  111. })
  112. if err != nil {
  113. return model.PublicPlatoonUserRelation{}, err
  114. }
  115. //2、 查找当前 public_platoon_user_relation 中 recommendUid 所有的子记录
  116. PublicPlatoonUserRelations, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByPid(m.Uid, "father_uid1", "position1")
  117. if err != nil {
  118. return model.PublicPlatoonUserRelation{}, err
  119. }
  120. if len(PublicPlatoonUserRelations) == 0 {
  121. fatherUid = m.Uid
  122. //证明是第一个
  123. standardPublicPlatoonRelation.FatherUid1 = m.Uid
  124. standardPublicPlatoonRelation.FatherUid2 = m.FatherUid1
  125. standardPublicPlatoonRelation.FatherUid3 = m.FatherUid2
  126. standardPublicPlatoonRelation.FatherUid4 = m.FatherUid3
  127. standardPublicPlatoonRelation.FatherUid5 = m.FatherUid4
  128. standardPublicPlatoonRelation.FatherUid6 = m.FatherUid5
  129. standardPublicPlatoonRelation.FatherUid7 = m.FatherUid6
  130. standardPublicPlatoonRelation.FatherUid8 = m.FatherUid7
  131. standardPublicPlatoonRelation.FatherUid9 = m.FatherUid8
  132. standardPublicPlatoonRelation.Pid1 = m.Position
  133. standardPublicPlatoonRelation.Pid2 = m.Position1
  134. standardPublicPlatoonRelation.Pid3 = m.Position2
  135. standardPublicPlatoonRelation.Pid4 = m.Position3
  136. standardPublicPlatoonRelation.Pid5 = m.Position4
  137. standardPublicPlatoonRelation.Pid6 = m.Position5
  138. standardPublicPlatoonRelation.Pid7 = m.Position6
  139. standardPublicPlatoonRelation.Pid8 = m.Position7
  140. standardPublicPlatoonRelation.Pid9 = m.Position8
  141. standardPublicPlatoonRelation.Position1 = SearchPositionFirstSonPosition(m.Position, int64(PublicPlatoonBasicSetting.SeveralTimes))
  142. standardPublicPlatoonRelation.Position2 = ReverseDeductionPositionByFather(m.Position1, int64(m.Level1), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  143. standardPublicPlatoonRelation.Position3 = ReverseDeductionPositionByFather(m.Position2, int64(m.Level2), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  144. standardPublicPlatoonRelation.Position4 = ReverseDeductionPositionByFather(m.Position3, int64(m.Level3), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  145. standardPublicPlatoonRelation.Position5 = ReverseDeductionPositionByFather(m.Position4, int64(m.Level4), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  146. standardPublicPlatoonRelation.Position6 = ReverseDeductionPositionByFather(m.Position5, int64(m.Level5), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  147. standardPublicPlatoonRelation.Position7 = ReverseDeductionPositionByFather(m.Position6, int64(m.Level6), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  148. standardPublicPlatoonRelation.Position8 = ReverseDeductionPositionByFather(m.Position7, int64(m.Level7), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  149. standardPublicPlatoonRelation.Position9 = ReverseDeductionPositionByFather(m.Position8, int64(m.Level8), int64(PublicPlatoonBasicSetting.SeveralTimes), 0)
  150. if m.Level1 == 0 {
  151. m.Level1--
  152. }
  153. if m.Level2 == 0 {
  154. m.Level2--
  155. }
  156. if m.Level3 == 0 {
  157. m.Level3--
  158. }
  159. if m.Level4 == 0 {
  160. m.Level4--
  161. }
  162. if m.Level5 == 0 {
  163. m.Level5--
  164. }
  165. if m.Level6 == 0 {
  166. m.Level6--
  167. }
  168. if m.Level7 == 0 {
  169. m.Level7--
  170. }
  171. if m.Level8 == 0 {
  172. m.Level8--
  173. }
  174. if m.Level9 == 0 {
  175. m.Level9--
  176. }
  177. standardPublicPlatoonRelation.Level1 = m.Level + 1
  178. standardPublicPlatoonRelation.Level2 = m.Level1 + 1
  179. standardPublicPlatoonRelation.Level3 = m.Level2 + 1
  180. standardPublicPlatoonRelation.Level4 = m.Level3 + 1
  181. standardPublicPlatoonRelation.Level5 = m.Level4 + 1
  182. standardPublicPlatoonRelation.Level6 = m.Level5 + 1
  183. standardPublicPlatoonRelation.Level7 = m.Level6 + 1
  184. standardPublicPlatoonRelation.Level8 = m.Level7 + 1
  185. standardPublicPlatoonRelation.Level9 = m.Level8 + 1
  186. standardPublicPlatoonRelation.LevelTotal = m.LevelTotal + 1
  187. } else {
  188. //TODO::判断直属下级是否排满
  189. if len(PublicPlatoonUserRelations) >= PublicPlatoonBasicSetting.SeveralTimes {
  190. var fatherLevel float64
  191. makeSearchLevel(&m.Position, float64(PublicPlatoonBasicSetting.SeveralTimes), &fatherLevel)
  192. var times = 0
  193. data, father, err := FindSuitablePosition(engine, &m.Position, float64(PublicPlatoonBasicSetting.SeveralTimes), m.Uid, int64(fatherLevel), &times)
  194. if err != nil {
  195. return model.PublicPlatoonUserRelation{}, err
  196. }
  197. fatherUid = father
  198. standardPublicPlatoonRelation = data
  199. } else {
  200. fatherUid = m.Uid
  201. standardPublicPlatoonRelation.FatherUid1 = PublicPlatoonUserRelations[0].FatherUid1
  202. standardPublicPlatoonRelation.FatherUid2 = PublicPlatoonUserRelations[0].FatherUid2
  203. standardPublicPlatoonRelation.FatherUid3 = PublicPlatoonUserRelations[0].FatherUid3
  204. standardPublicPlatoonRelation.FatherUid4 = PublicPlatoonUserRelations[0].FatherUid4
  205. standardPublicPlatoonRelation.FatherUid5 = PublicPlatoonUserRelations[0].FatherUid5
  206. standardPublicPlatoonRelation.FatherUid6 = PublicPlatoonUserRelations[0].FatherUid6
  207. standardPublicPlatoonRelation.FatherUid7 = PublicPlatoonUserRelations[0].FatherUid7
  208. standardPublicPlatoonRelation.FatherUid8 = PublicPlatoonUserRelations[0].FatherUid8
  209. standardPublicPlatoonRelation.FatherUid9 = PublicPlatoonUserRelations[0].FatherUid9
  210. standardPublicPlatoonRelation.Pid1 = PublicPlatoonUserRelations[0].Pid1
  211. standardPublicPlatoonRelation.Pid2 = PublicPlatoonUserRelations[0].Pid2
  212. standardPublicPlatoonRelation.Pid3 = PublicPlatoonUserRelations[0].Pid3
  213. standardPublicPlatoonRelation.Pid4 = PublicPlatoonUserRelations[0].Pid4
  214. standardPublicPlatoonRelation.Pid5 = PublicPlatoonUserRelations[0].Pid5
  215. standardPublicPlatoonRelation.Pid6 = PublicPlatoonUserRelations[0].Pid6
  216. standardPublicPlatoonRelation.Pid7 = PublicPlatoonUserRelations[0].Pid7
  217. standardPublicPlatoonRelation.Pid8 = PublicPlatoonUserRelations[0].Pid8
  218. standardPublicPlatoonRelation.Pid9 = PublicPlatoonUserRelations[0].Pid9
  219. standardPublicPlatoonRelation.Position1 = PublicPlatoonUserRelations[0].Position1 + 1
  220. if PublicPlatoonUserRelations[0].Position2 != 0 {
  221. standardPublicPlatoonRelation.Position2 = PublicPlatoonUserRelations[0].Position2 + 1
  222. }
  223. if PublicPlatoonUserRelations[0].Position3 != 0 {
  224. standardPublicPlatoonRelation.Position3 = PublicPlatoonUserRelations[0].Position3 + 1
  225. }
  226. if PublicPlatoonUserRelations[0].Position4 != 0 {
  227. standardPublicPlatoonRelation.Position4 = PublicPlatoonUserRelations[0].Position4 + 1
  228. }
  229. if PublicPlatoonUserRelations[0].Position5 != 0 {
  230. standardPublicPlatoonRelation.Position5 = PublicPlatoonUserRelations[0].Position5 + 1
  231. }
  232. if PublicPlatoonUserRelations[0].Position6 != 0 {
  233. standardPublicPlatoonRelation.Position6 = PublicPlatoonUserRelations[0].Position6 + 1
  234. }
  235. if PublicPlatoonUserRelations[0].Position7 != 0 {
  236. standardPublicPlatoonRelation.Position7 = PublicPlatoonUserRelations[0].Position7 + 1
  237. }
  238. if PublicPlatoonUserRelations[0].Position8 != 0 {
  239. standardPublicPlatoonRelation.Position8 = PublicPlatoonUserRelations[0].Position8 + 1
  240. }
  241. if PublicPlatoonUserRelations[0].Position9 != 0 {
  242. standardPublicPlatoonRelation.Position9 = PublicPlatoonUserRelations[0].Position9 + 1
  243. }
  244. standardPublicPlatoonRelation.Level1 = PublicPlatoonUserRelations[0].Level1
  245. standardPublicPlatoonRelation.Level2 = PublicPlatoonUserRelations[0].Level2
  246. standardPublicPlatoonRelation.Level3 = PublicPlatoonUserRelations[0].Level3
  247. standardPublicPlatoonRelation.Level4 = PublicPlatoonUserRelations[0].Level4
  248. standardPublicPlatoonRelation.Level5 = PublicPlatoonUserRelations[0].Level5
  249. standardPublicPlatoonRelation.Level6 = PublicPlatoonUserRelations[0].Level6
  250. standardPublicPlatoonRelation.Level7 = PublicPlatoonUserRelations[0].Level7
  251. standardPublicPlatoonRelation.Level8 = PublicPlatoonUserRelations[0].Level8
  252. standardPublicPlatoonRelation.Level9 = PublicPlatoonUserRelations[0].Level9
  253. standardPublicPlatoonRelation.LevelTotal = PublicPlatoonUserRelations[0].LevelTotal
  254. }
  255. }
  256. //2、查找对应pid的 public_platoon_user_relation 记录
  257. var m1 model.PublicPlatoonUserRelation
  258. if has, err := engine.Where("uid=?", fatherUid).Get(&m1); err != nil || has == false {
  259. return PublicPlatoonUserRelation, err
  260. }
  261. var fatherUidStr string
  262. if m1.FatherUid == "" {
  263. //TODO::顶级
  264. fatherUidStr = egg_system_rules.Int64ToStr(m1.Id)
  265. } else {
  266. fatherUids := strings.Split(m1.FatherUid, "-")
  267. if len(fatherUids) >= PublicPlatoonBasicSetting.SeveralRows {
  268. fatherUidStr = egg_system_rules.Int64ToStr(m1.Id) + "-" + strings.Join(fatherUids[0:PublicPlatoonBasicSetting.SeveralRows:len(fatherUids)], "-")
  269. } else {
  270. fatherUidStr = egg_system_rules.Int64ToStr(m1.Id) + "-" + m1.FatherUid
  271. }
  272. }
  273. // 3、插入 public_platoon_user_relation 记录
  274. now := time.Now()
  275. PublicPlatoonUserRelation.Uid = uid
  276. PublicPlatoonUserRelation.FatherUid = fatherUidStr
  277. PublicPlatoonUserRelation.FatherUid1 = standardPublicPlatoonRelation.FatherUid1
  278. PublicPlatoonUserRelation.FatherUid2 = standardPublicPlatoonRelation.FatherUid2
  279. PublicPlatoonUserRelation.FatherUid3 = standardPublicPlatoonRelation.FatherUid3
  280. PublicPlatoonUserRelation.FatherUid4 = standardPublicPlatoonRelation.FatherUid4
  281. PublicPlatoonUserRelation.FatherUid5 = standardPublicPlatoonRelation.FatherUid5
  282. PublicPlatoonUserRelation.FatherUid6 = standardPublicPlatoonRelation.FatherUid6
  283. PublicPlatoonUserRelation.FatherUid7 = standardPublicPlatoonRelation.FatherUid7
  284. PublicPlatoonUserRelation.FatherUid8 = standardPublicPlatoonRelation.FatherUid8
  285. PublicPlatoonUserRelation.FatherUid9 = standardPublicPlatoonRelation.FatherUid9
  286. PublicPlatoonUserRelation.Pid1 = standardPublicPlatoonRelation.Pid1
  287. PublicPlatoonUserRelation.Pid2 = standardPublicPlatoonRelation.Pid2
  288. PublicPlatoonUserRelation.Pid3 = standardPublicPlatoonRelation.Pid3
  289. PublicPlatoonUserRelation.Pid4 = standardPublicPlatoonRelation.Pid4
  290. PublicPlatoonUserRelation.Pid5 = standardPublicPlatoonRelation.Pid5
  291. PublicPlatoonUserRelation.Pid6 = standardPublicPlatoonRelation.Pid6
  292. PublicPlatoonUserRelation.Pid7 = standardPublicPlatoonRelation.Pid7
  293. PublicPlatoonUserRelation.Pid8 = standardPublicPlatoonRelation.Pid8
  294. PublicPlatoonUserRelation.Pid9 = standardPublicPlatoonRelation.Pid9
  295. PublicPlatoonUserRelation.Position = 1
  296. PublicPlatoonUserRelation.Position1 = standardPublicPlatoonRelation.Position1
  297. PublicPlatoonUserRelation.Position2 = standardPublicPlatoonRelation.Position2
  298. PublicPlatoonUserRelation.Position3 = standardPublicPlatoonRelation.Position3
  299. PublicPlatoonUserRelation.Position4 = standardPublicPlatoonRelation.Position4
  300. PublicPlatoonUserRelation.Position5 = standardPublicPlatoonRelation.Position5
  301. PublicPlatoonUserRelation.Position6 = standardPublicPlatoonRelation.Position6
  302. PublicPlatoonUserRelation.Position7 = standardPublicPlatoonRelation.Position7
  303. PublicPlatoonUserRelation.Position8 = standardPublicPlatoonRelation.Position8
  304. PublicPlatoonUserRelation.Position9 = standardPublicPlatoonRelation.Position9
  305. PublicPlatoonUserRelation.Level = 1
  306. PublicPlatoonUserRelation.Level1 = standardPublicPlatoonRelation.Level1
  307. PublicPlatoonUserRelation.Level2 = standardPublicPlatoonRelation.Level2
  308. PublicPlatoonUserRelation.Level3 = standardPublicPlatoonRelation.Level3
  309. PublicPlatoonUserRelation.Level4 = standardPublicPlatoonRelation.Level4
  310. PublicPlatoonUserRelation.Level5 = standardPublicPlatoonRelation.Level5
  311. PublicPlatoonUserRelation.Level6 = standardPublicPlatoonRelation.Level6
  312. PublicPlatoonUserRelation.Level7 = standardPublicPlatoonRelation.Level7
  313. PublicPlatoonUserRelation.Level8 = standardPublicPlatoonRelation.Level8
  314. PublicPlatoonUserRelation.Level9 = standardPublicPlatoonRelation.Level9
  315. PublicPlatoonUserRelation.LevelTotal = standardPublicPlatoonRelation.LevelTotal
  316. PublicPlatoonUserRelation.RecommendUid = recommendUid
  317. PublicPlatoonUserRelation.JoinAt = now.Format("2006-01-02 15:04:05")
  318. PublicPlatoonUserRelation.CreateAt = now.Format("2006-01-02 15:04:05")
  319. PublicPlatoonUserRelation.UpdateAt = now.Format("2006-01-02 15:04:05")
  320. PublicPlatoonUserRelation.HasSonNum = 0
  321. _, err = publicPlatoonUserRelationDb.PublicPlatoonUserRelationInsert(&PublicPlatoonUserRelation)
  322. if err != nil {
  323. return model.PublicPlatoonUserRelation{}, err
  324. }
  325. // 4、改变直属父级的 has_son_num 数量
  326. update, err := engine.Where("uid = ?", fatherUid).SetExpr("has_son_num", "has_son_num + 1").Update(new(model.PublicPlatoonUserRelation))
  327. if err != nil {
  328. return PublicPlatoonUserRelation, err
  329. }
  330. if update == 0 {
  331. return PublicPlatoonUserRelation, errors.New("更新has_son_num数据出错")
  332. }
  333. return PublicPlatoonUserRelation, nil
  334. }
  335. // ReverseDeductionPosition 逆向推导 position
  336. func ReverseDeductionPosition(calcPosition, levelFirstPosition, severalTimes int) (position int) {
  337. remainder := (calcPosition - 1) % severalTimes
  338. position = levelFirstPosition + remainder
  339. return
  340. }
  341. func ReverseDeductionPositionByFather(fatherPosition, level, severalTimes, remainder int64) (position int64) {
  342. if fatherPosition == 0 {
  343. position = 0
  344. return
  345. }
  346. firstPosition := GetLevelForFirstPosition(level, severalTimes)
  347. endPosition := firstPosition + int64(math.Pow(float64(severalTimes), float64(level-1))) - 1
  348. position = fatherPosition + (fatherPosition-firstPosition)*severalTimes + (endPosition - fatherPosition) + 1
  349. return
  350. }
  351. func FindSuitablePosition(engine *xorm.Engine, position *int64, severalTimes float64, fuid, fatherLevel int64, times *int) (standardPublicPlatoonRelation md.StandardPublicPlatoonRelation, fatherUid int64, err error) {
  352. *times++
  353. positionName := "position" + egg_system_rules.IntToStr(*times)
  354. fatherName := "father_uid" + egg_system_rules.IntToStr(*times)
  355. firstSonPosition := SearchPositionFirstSonPosition(*position, int64(severalTimes))
  356. var tempLevel float64
  357. makeSearchLevel(&firstSonPosition, severalTimes, &tempLevel)
  358. firstSonLevel := int64(tempLevel)
  359. endSonPosition := firstSonPosition + int64(math.Pow(severalTimes, float64(firstSonLevel-fatherLevel))) - 1
  360. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  361. list, _, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindCountByPosition(fuid, fatherName, positionName, int64(firstSonPosition), int64(endSonPosition))
  362. if err != nil {
  363. return standardPublicPlatoonRelation, fatherUid, err
  364. }
  365. if list[0].HasSonNum == int(severalTimes) {
  366. //TODO::则证明该层直属下级无空位
  367. position = &firstSonPosition
  368. return FindSuitablePosition(engine, position, severalTimes, fuid, fatherLevel, times)
  369. } else if list[0].HasSonNum == 0 {
  370. fatherUid = list[0].Uid
  371. //TODO::新的一层
  372. standardPublicPlatoonRelation.FatherUid1 = list[0].Uid
  373. standardPublicPlatoonRelation.FatherUid2 = list[0].FatherUid1
  374. standardPublicPlatoonRelation.FatherUid3 = list[0].FatherUid2
  375. standardPublicPlatoonRelation.FatherUid4 = list[0].FatherUid3
  376. standardPublicPlatoonRelation.FatherUid5 = list[0].FatherUid4
  377. standardPublicPlatoonRelation.FatherUid6 = list[0].FatherUid5
  378. standardPublicPlatoonRelation.FatherUid7 = list[0].FatherUid6
  379. standardPublicPlatoonRelation.FatherUid8 = list[0].FatherUid7
  380. standardPublicPlatoonRelation.FatherUid9 = list[0].FatherUid8
  381. standardPublicPlatoonRelation.Pid1 = list[0].Position
  382. standardPublicPlatoonRelation.Pid2 = list[0].Position1
  383. standardPublicPlatoonRelation.Pid3 = list[0].Position2
  384. standardPublicPlatoonRelation.Pid4 = list[0].Position3
  385. standardPublicPlatoonRelation.Pid5 = list[0].Position4
  386. standardPublicPlatoonRelation.Pid6 = list[0].Position5
  387. standardPublicPlatoonRelation.Pid7 = list[0].Position6
  388. standardPublicPlatoonRelation.Pid8 = list[0].Position7
  389. standardPublicPlatoonRelation.Pid9 = list[0].Position8
  390. standardPublicPlatoonRelation.Position1 = SearchPositionFirstSonPosition(list[0].Position, int64(severalTimes))
  391. standardPublicPlatoonRelation.Position2 = ReverseDeductionPositionByFather(list[0].Position1, int64(list[0].Level1), int64(severalTimes), 0)
  392. standardPublicPlatoonRelation.Position3 = ReverseDeductionPositionByFather(list[0].Position2, int64(list[0].Level2), int64(severalTimes), 0)
  393. standardPublicPlatoonRelation.Position4 = ReverseDeductionPositionByFather(list[0].Position3, int64(list[0].Level3), int64(severalTimes), 0)
  394. standardPublicPlatoonRelation.Position5 = ReverseDeductionPositionByFather(list[0].Position4, int64(list[0].Level4), int64(severalTimes), 0)
  395. standardPublicPlatoonRelation.Position6 = ReverseDeductionPositionByFather(list[0].Position5, int64(list[0].Level5), int64(severalTimes), 0)
  396. standardPublicPlatoonRelation.Position7 = ReverseDeductionPositionByFather(list[0].Position6, int64(list[0].Level6), int64(severalTimes), 0)
  397. standardPublicPlatoonRelation.Position8 = ReverseDeductionPositionByFather(list[0].Position7, int64(list[0].Level7), int64(severalTimes), 0)
  398. standardPublicPlatoonRelation.Position9 = ReverseDeductionPositionByFather(list[0].Position8, int64(list[0].Level8), int64(severalTimes), 0)
  399. if list[0].Level1 == 0 {
  400. list[0].Level1--
  401. }
  402. if list[0].Level2 == 0 {
  403. list[0].Level2--
  404. }
  405. if list[0].Level3 == 0 {
  406. list[0].Level3--
  407. }
  408. if list[0].Level4 == 0 {
  409. list[0].Level4--
  410. }
  411. if list[0].Level5 == 0 {
  412. list[0].Level5--
  413. }
  414. if list[0].Level6 == 0 {
  415. list[0].Level6--
  416. }
  417. if list[0].Level7 == 0 {
  418. list[0].Level7--
  419. }
  420. if list[0].Level8 == 0 {
  421. list[0].Level8--
  422. }
  423. if list[0].Level9 == 0 {
  424. list[0].Level9--
  425. }
  426. standardPublicPlatoonRelation.Level1 = list[0].Level + 1
  427. standardPublicPlatoonRelation.Level2 = list[0].Level1 + 1
  428. standardPublicPlatoonRelation.Level3 = list[0].Level2 + 1
  429. standardPublicPlatoonRelation.Level4 = list[0].Level3 + 1
  430. standardPublicPlatoonRelation.Level5 = list[0].Level4 + 1
  431. standardPublicPlatoonRelation.Level6 = list[0].Level5 + 1
  432. standardPublicPlatoonRelation.Level7 = list[0].Level6 + 1
  433. standardPublicPlatoonRelation.Level8 = list[0].Level7 + 1
  434. standardPublicPlatoonRelation.Level9 = list[0].Level8 + 1
  435. standardPublicPlatoonRelation.LevelTotal = list[0].LevelTotal + 1
  436. } else {
  437. fatherUid = list[0].Uid
  438. _, _, _, _, getPositionName, _, _, _ := GetPosition(list[0], *times+1)
  439. //PublicPlatoonUserRelations, err := db.PublicPlatoonUserRelationFindByPid(engine, m.Position, m.Uid, "pid1", "father_uid1", "position1")
  440. PublicPlatoonUserRelations, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByPid(list[0].Uid, "father_uid1", getPositionName)
  441. if err != nil {
  442. return standardPublicPlatoonRelation, fatherUid, err
  443. }
  444. if PublicPlatoonUserRelations[0].Position2 == 0 {
  445. PublicPlatoonUserRelations[0].Position2 = -1
  446. }
  447. if PublicPlatoonUserRelations[0].Position3 == 0 {
  448. standardPublicPlatoonRelation.Position3 = -1
  449. }
  450. if PublicPlatoonUserRelations[0].Position4 == 0 {
  451. standardPublicPlatoonRelation.Position4 = -1
  452. }
  453. if PublicPlatoonUserRelations[0].Position5 == 0 {
  454. standardPublicPlatoonRelation.Position5 = -1
  455. }
  456. if PublicPlatoonUserRelations[0].Position6 == 0 {
  457. standardPublicPlatoonRelation.Position6 = -1
  458. }
  459. if PublicPlatoonUserRelations[0].Position7 == 0 {
  460. standardPublicPlatoonRelation.Position7 = -1
  461. }
  462. if PublicPlatoonUserRelations[0].Position8 == 0 {
  463. standardPublicPlatoonRelation.Position8 = -1
  464. }
  465. if PublicPlatoonUserRelations[0].Position9 == 0 {
  466. standardPublicPlatoonRelation.Position9 = -1
  467. }
  468. standardPublicPlatoonRelation = md.StandardPublicPlatoonRelation{
  469. FatherUid1: PublicPlatoonUserRelations[0].FatherUid1,
  470. FatherUid2: PublicPlatoonUserRelations[0].FatherUid2,
  471. FatherUid3: PublicPlatoonUserRelations[0].FatherUid3,
  472. FatherUid4: PublicPlatoonUserRelations[0].FatherUid4,
  473. FatherUid5: PublicPlatoonUserRelations[0].FatherUid5,
  474. FatherUid6: PublicPlatoonUserRelations[0].FatherUid6,
  475. FatherUid7: PublicPlatoonUserRelations[0].FatherUid7,
  476. FatherUid8: PublicPlatoonUserRelations[0].FatherUid8,
  477. FatherUid9: PublicPlatoonUserRelations[0].FatherUid9,
  478. Pid1: PublicPlatoonUserRelations[0].Pid1,
  479. Pid2: PublicPlatoonUserRelations[0].Pid2,
  480. Pid3: PublicPlatoonUserRelations[0].Pid3,
  481. Pid4: PublicPlatoonUserRelations[0].Pid4,
  482. Pid5: PublicPlatoonUserRelations[0].Pid5,
  483. Pid6: PublicPlatoonUserRelations[0].Pid6,
  484. Pid7: PublicPlatoonUserRelations[0].Pid7,
  485. Pid8: PublicPlatoonUserRelations[0].Pid8,
  486. Pid9: PublicPlatoonUserRelations[0].Pid9,
  487. Position1: PublicPlatoonUserRelations[0].Position1 + 1,
  488. Position2: PublicPlatoonUserRelations[0].Position2 + 1,
  489. Position3: PublicPlatoonUserRelations[0].Position3 + 1,
  490. Position4: PublicPlatoonUserRelations[0].Position4 + 1,
  491. Position5: PublicPlatoonUserRelations[0].Position5 + 1,
  492. Position6: PublicPlatoonUserRelations[0].Position6 + 1,
  493. Position7: PublicPlatoonUserRelations[0].Position7 + 1,
  494. Position8: PublicPlatoonUserRelations[0].Position8 + 1,
  495. Position9: PublicPlatoonUserRelations[0].Position9 + 1,
  496. Level1: PublicPlatoonUserRelations[0].Level1,
  497. Level2: PublicPlatoonUserRelations[0].Level2,
  498. Level3: PublicPlatoonUserRelations[0].Level3,
  499. Level4: PublicPlatoonUserRelations[0].Level4,
  500. Level5: PublicPlatoonUserRelations[0].Level5,
  501. Level6: PublicPlatoonUserRelations[0].Level6,
  502. Level7: PublicPlatoonUserRelations[0].Level7,
  503. Level8: PublicPlatoonUserRelations[0].Level8,
  504. Level9: PublicPlatoonUserRelations[0].Level9,
  505. LevelTotal: PublicPlatoonUserRelations[0].LevelTotal,
  506. }
  507. return standardPublicPlatoonRelation, fatherUid, nil
  508. }
  509. return
  510. }
  511. func GetPosition(m model.PublicPlatoonUserRelation, times int) (position, pid int64, level int, fatherUid int64, positionName, pidName, levelName, fatherUidName string) {
  512. if times == 1 {
  513. position = m.Position1
  514. positionName = "position1"
  515. pid = m.Pid1
  516. pidName = "pid1"
  517. level = m.Level1
  518. levelName = "level1"
  519. fatherUid = m.FatherUid1
  520. fatherUidName = "father_uid1"
  521. }
  522. if times == 2 {
  523. position = m.Position2
  524. positionName = "position2"
  525. pid = m.Pid2
  526. pidName = "pid2"
  527. level = m.Level2
  528. levelName = "level2"
  529. fatherUid = m.FatherUid2
  530. fatherUidName = "father_uid2"
  531. }
  532. if times == 3 {
  533. position = m.Position3
  534. positionName = "position3"
  535. pid = m.Pid3
  536. pidName = "pid3"
  537. level = m.Level3
  538. levelName = "level3"
  539. fatherUid = m.FatherUid3
  540. fatherUidName = "father_uid3"
  541. }
  542. if times == 4 {
  543. position = m.Position4
  544. positionName = "position4"
  545. pid = m.Pid4
  546. pidName = "pid4"
  547. level = m.Level4
  548. levelName = "level4"
  549. fatherUid = m.FatherUid4
  550. fatherUidName = "father_uid4"
  551. }
  552. if times == 5 {
  553. position = m.Position5
  554. positionName = "position5"
  555. pid = m.Pid5
  556. pidName = "pid5"
  557. level = m.Level5
  558. levelName = "level5"
  559. fatherUid = m.FatherUid5
  560. fatherUidName = "father_uid5"
  561. }
  562. if times == 6 {
  563. position = m.Position6
  564. positionName = "position6"
  565. pid = m.Pid6
  566. pidName = "pid6"
  567. level = m.Level6
  568. levelName = "level6"
  569. fatherUid = m.FatherUid6
  570. fatherUidName = "father_uid6"
  571. }
  572. if times == 7 {
  573. position = m.Position7
  574. positionName = "position7"
  575. pid = m.Pid7
  576. pidName = "pid7"
  577. level = m.Level7
  578. levelName = "level7"
  579. fatherUid = m.FatherUid7
  580. fatherUidName = "father_uid7"
  581. }
  582. if times == 8 {
  583. position = m.Position8
  584. positionName = "position8"
  585. pid = m.Pid8
  586. pidName = "pid8"
  587. level = m.Level8
  588. levelName = "level8"
  589. fatherUid = m.FatherUid8
  590. fatherUidName = "father_uid8"
  591. }
  592. if times == 9 {
  593. position = m.Position9
  594. positionName = "position9"
  595. pid = m.Pid9
  596. pidName = "pid9"
  597. level = m.Level9
  598. levelName = "level9"
  599. fatherUid = m.FatherUid9
  600. fatherUidName = "father_uid9"
  601. }
  602. return
  603. }
  604. // GetLevelForFirstPosition 返回当前等级的起始值
  605. func GetLevelForFirstPosition(level, severalTimes int64) (position int64) {
  606. position = position + 1
  607. for n := int64(0); n <= (level - 2); n++ {
  608. position += int64(math.Pow(float64(severalTimes), float64(n)))
  609. }
  610. return
  611. }
  612. // 查找归属父级id
  613. func MakeSearchPid(position int, row int) (pid int) {
  614. divisionValue := (position - 1) / row
  615. if divisionValue == 0 {
  616. pid = 1
  617. return
  618. } else {
  619. if (divisionValue*row + 1) == position {
  620. pid = divisionValue
  621. return
  622. } else {
  623. pid = divisionValue + 1
  624. return
  625. }
  626. }
  627. }
  628. // 查找当前位置下的第一个子集位置
  629. func SearchPositionFirstSonPosition(position int64, times int64) (sonPosition int64) {
  630. sonPosition = (position * times) - (times - 2)
  631. return
  632. }
  633. // 查找当前位位置相当于父级的等级
  634. func SearchPositionSonForLevel(fatherUid string, fatherPosition int64) (level int) {
  635. fatherUids := strings.Split(fatherUid, "-")
  636. for key, item := range fatherUids {
  637. if item == egg_system_rules.Int64ToStr(fatherPosition) {
  638. level = key + 1
  639. break
  640. }
  641. }
  642. return
  643. }
  644. // FindWaitForDealUsers 查询待处理的用户
  645. func FindWaitForDealUsers(engine *xorm.Engine, page, pageSize int) (err error, resp []int64) {
  646. now := time.Now().Format("2006-01-02")
  647. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  648. lists, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationFindByParamsByPage(map[string]interface{}{
  649. "key": "wait_for_settlement_date",
  650. "value": now,
  651. }, page, pageSize)
  652. if err != nil {
  653. return
  654. }
  655. for _, list := range *lists {
  656. resp = append(resp, list.Uid)
  657. }
  658. return
  659. }
  660. // 递归查找等级
  661. func makeSearchLevel(position *int64, rows float64, times *float64) (level int) {
  662. difference := *position - int64(math.Pow(rows, *times))
  663. *times++
  664. if difference <= 0 {
  665. return int(*times)
  666. } else {
  667. position = &difference
  668. return makeSearchLevel(position, rows, times)
  669. }
  670. }
  671. // DealCommonWealthPunish 处理公排处罚
  672. func DealCommonWealthPunish(engine *xorm.Engine, uid int64, reason string) (err error, resp []map[string]string) {
  673. session := engine.NewSession()
  674. defer func() {
  675. session.Close()
  676. if err := recover(); err != nil {
  677. _ = zhios_order_relate_logx.Error(err)
  678. }
  679. }()
  680. session.Begin()
  681. //1、查找 `user_public_platoon_setting` 基础设置
  682. publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
  683. PublicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
  684. "key": "is_open",
  685. "value": 1,
  686. })
  687. if err != nil {
  688. _ = session.Rollback()
  689. return
  690. }
  691. //2、查询出 `public_platoon_user_relation` 中相关记录 && 将该记录的uid置为 -1
  692. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  693. params, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  694. "key": "uid",
  695. "value": uid,
  696. })
  697. if err != nil {
  698. _ = session.Rollback()
  699. return
  700. }
  701. if params == nil {
  702. fmt.Println("未查询到公排关系记录!!!!!!!", uid)
  703. return
  704. }
  705. //TODO::判断是否为推荐用户
  706. if params.RecommendUid == 0 {
  707. params.Uid = params.Uid - int64(time.Now().Unix())
  708. } else {
  709. params.Uid = params.Uid - int64(time.Now().Unix())
  710. }
  711. for n := 1; n <= 9; n++ {
  712. str := "father_uid" + strconv.Itoa(n)
  713. sql := fmt.Sprintf("UPDATE `public_platoon_user_relation` SET %s=%s where %s=%s", str, egg_system_rules.Int64ToStr(params.Uid), str, egg_system_rules.Int64ToStr(uid))
  714. fmt.Println(">>>>>>>sql>>>>>>>", sql)
  715. _, err = session.Exec(sql)
  716. if err != nil {
  717. _ = session.Rollback()
  718. return
  719. }
  720. }
  721. updateAffected, err := publicPlatoonUserRelationDb.PublicPlatoonUserRelationUpdate(session, params.Id, params)
  722. if err != nil {
  723. _ = session.Rollback()
  724. return
  725. }
  726. if updateAffected == 0 {
  727. err = errors.New("更新 public_platoon_user_relation 记录失败")
  728. _ = session.Rollback()
  729. return
  730. }
  731. //3、新增一条 `public_platoon_user_relation` 记录
  732. params.Uid = uid
  733. res, err := PublicPlatoon(engine, params.Uid, params.RecommendUid, *PublicPlatoonBasicSetting)
  734. if err != nil {
  735. _ = session.Rollback()
  736. return
  737. }
  738. //4、新增一条 `public_platoon_user_system_punish_records` 记录
  739. now := time.Now()
  740. publicPlatoonUserSystemPunishRecordsDb := implement.NewPublicPlatoonUserSystemPunishRecordsDb(engine)
  741. insertAffected, err := publicPlatoonUserSystemPunishRecordsDb.PublicPlatoonUserSystemPunishRecordsInsert(session, &model.PublicPlatoonUserSystemPunishRecords{
  742. Uid: params.Uid,
  743. OldPosition: params.Position,
  744. NewPosition: res.Position,
  745. Date: now.AddDate(0, 0, 30).Format("2006-01-02"),
  746. Title: "系统处罚记录",
  747. Reason: reason,
  748. Type: 2,
  749. CreateAt: now.Format("2006-01-02 15:04:05"),
  750. UpdateAt: now.Format("2006-01-02 15:04:05"),
  751. })
  752. if err != nil {
  753. _ = session.Rollback()
  754. return
  755. }
  756. if insertAffected == 0 {
  757. err = errors.New("新增 public_platoon_user_system_punish_records 记录失败")
  758. _ = session.Rollback()
  759. return
  760. }
  761. err = session.Commit()
  762. return
  763. }
  764. const DealUserPublicPlatoonPunishLockKey = "deal_user_public_platoon_lock_key"
  765. // DealUserPublicPlatoonPunish 处理公排处罚
  766. func DealUserPublicPlatoonPunish(engine *xorm.Engine) (err error) {
  767. now := time.Now()
  768. fmt.Println(now.Hour())
  769. if !(now.Hour() > 22 && now.Hour() < 24) {
  770. //TODO::只在凌晨一点 ~ 凌晨 8 点运行
  771. return errors.New("非运行时间")
  772. }
  773. //TODO::增加“悲观锁”防止串行
  774. getString, _ := cache.GetString(DealUserPublicPlatoonPunishLockKey)
  775. if getString != "" {
  776. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
  777. return errors.New("上一次结算未执行完")
  778. }
  779. cache.SetEx(DealUserPublicPlatoonPunishLockKey, "running", 3600*8) //8小时
  780. //查找 `public_platoon_basic_setting` 基础设置
  781. publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
  782. PublicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
  783. "key": "is_open",
  784. "value": 1,
  785. })
  786. if err != nil {
  787. return err
  788. }
  789. if PublicPlatoonBasicSetting == nil {
  790. return errors.New("公排制度未开启")
  791. }
  792. publicPlatoonFreePunishWithUserDb := implement.NewPublicPlatoonFreePunishWithUserDb(engine)
  793. mapPublicPlatoonFreePunishWithUser, err := publicPlatoonFreePunishWithUserDb.FindAllPublicPlatoonFreePunishWithUser()
  794. if err != nil {
  795. return err
  796. }
  797. if PublicPlatoonBasicSetting.SystemPunishReplace == 1 {
  798. systemPunishReplaceValue := PublicPlatoonBasicSetting.SystemPunishReplaceValue
  799. startDate := now.AddDate(0, 0, -systemPunishReplaceValue).Format("2006-01-02") + " 00:00:00"
  800. var page = 1
  801. var pageSize = 100
  802. for {
  803. var users []model.User
  804. err = engine.Limit(pageSize, (page-1)*pageSize).Desc("uid").Find(&users)
  805. if err != nil {
  806. return err
  807. }
  808. if len(users) <= 0 {
  809. break
  810. }
  811. for _, user := range users {
  812. var list []model.EggSignIn
  813. err = engine.Where("start_time >= ?", startDate).And("uid = ?", user.Id).Find(&list)
  814. if len(list) <= 0 && mapPublicPlatoonFreePunishWithUser[user.Id] == nil {
  815. var PublicPlatoonRecordsPunishWithUser model.PublicPlatoonRecordsPunishWithUser
  816. has, err1 := engine.Where("uid = ?", user.Id).Get(&PublicPlatoonRecordsPunishWithUser)
  817. if err1 != nil {
  818. return err1
  819. }
  820. PublicPlatoonRecordsPunishWithUserDate, _ := time.ParseInLocation("2006-01-02", PublicPlatoonRecordsPunishWithUser.Date, time.Local)
  821. if has && now.AddDate(0, 0, -7).After(PublicPlatoonRecordsPunishWithUserDate) {
  822. //TODO::不进行重复处罚
  823. continue
  824. }
  825. //进行处罚
  826. err, _ = DealCommonWealthPunish(engine, user.Id, "公排处罚")
  827. if err != nil {
  828. return err
  829. }
  830. //添加 public_platoon_records_punish_with_user 记录
  831. publicPlatoonRecordsPunishWithUserDb := implement.NewPublicPlatoonRecordsPunishWithUserDb(engine)
  832. if has {
  833. PublicPlatoonRecordsPunishWithUser.Date = now.Format("2006-01-02")
  834. _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserUpdate(PublicPlatoonRecordsPunishWithUser.Id, &PublicPlatoonRecordsPunishWithUser, "date")
  835. if err2 != nil {
  836. return err2
  837. }
  838. } else {
  839. _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserInsert(&model.PublicPlatoonRecordsPunishWithUser{
  840. Uid: user.Id,
  841. Date: now.Format("2006-01-02"),
  842. })
  843. if err2 != nil {
  844. return err2
  845. }
  846. }
  847. }
  848. }
  849. page++
  850. }
  851. }
  852. return
  853. }