蛋蛋星球-制度模式
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

public_platoon_user_relation_commission.go 39 KiB

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