蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

public_platoon_user_relation_commission.go 39 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 weeks ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 weeks ago
3 days ago
2 weeks ago
3 days ago
2 weeks ago
3 days ago
2 weeks ago
1 month ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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, err1 := publicPlatoonUserSystemPunishRecordsDb.PublicPlatoonUserSystemPunishRecordsInsert(session, &model.PublicPlatoonUserSystemPunishRecords{
  838. Uid: params.Uid,
  839. OldPosition: params.Id,
  840. NewPosition: res.Id,
  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 err1 != nil {
  849. fmt.Println(err1.Error())
  850. _ = session.Rollback()
  851. return
  852. }
  853. if insertAffected == 0 {
  854. err = errors.New("新增 public_platoon_user_system_punish_records 记录失败")
  855. _ = session.Rollback()
  856. return
  857. }
  858. err = session.Commit()
  859. return
  860. }
  861. const DealUserPublicPlatoonPunishLockKey = "deal_user_public_platoon_lock_key"
  862. // DealUserPublicPlatoonPunish 处理公排处罚
  863. func DealUserPublicPlatoonPunish(engine *xorm.Engine) (err error) {
  864. now := time.Now()
  865. fmt.Println(now.Hour())
  866. if !(now.Hour() > 22 && now.Hour() < 24) {
  867. //TODO::只在凌晨一点 ~ 凌晨 8 点运行
  868. return errors.New("非运行时间")
  869. }
  870. //TODO::增加“悲观锁”防止串行
  871. getString, _ := cache.GetString(DealUserPublicPlatoonPunishLockKey)
  872. if getString != "" {
  873. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
  874. return errors.New("上一次结算未执行完")
  875. }
  876. cache.SetEx(DealUserPublicPlatoonPunishLockKey, "running", 3600*8) //8小时
  877. //查找 `public_platoon_basic_setting` 基础设置
  878. publicPlatoonBasicSettingDb := implement.NewPublicPlatoonBasicSettingDb(engine)
  879. PublicPlatoonBasicSetting, err := publicPlatoonBasicSettingDb.PublicPlatoonBasicSettingGetOneByParams(map[string]interface{}{
  880. "key": "is_open",
  881. "value": 1,
  882. })
  883. if err != nil {
  884. return err
  885. }
  886. if PublicPlatoonBasicSetting == nil {
  887. return errors.New("公排制度未开启")
  888. }
  889. publicPlatoonFreePunishWithUserDb := implement.NewPublicPlatoonFreePunishWithUserDb(engine)
  890. mapPublicPlatoonFreePunishWithUser, err := publicPlatoonFreePunishWithUserDb.FindAllPublicPlatoonFreePunishWithUser()
  891. if err != nil {
  892. return err
  893. }
  894. if PublicPlatoonBasicSetting.SystemPunishReplace == 1 {
  895. systemPunishReplaceValue := PublicPlatoonBasicSetting.SystemPunishReplaceValue
  896. startDate := now.AddDate(0, 0, -systemPunishReplaceValue).Format("2006-01-02") + " 00:00:00"
  897. var page = 1
  898. var pageSize = 100
  899. for {
  900. var users []model.User
  901. err = engine.Limit(pageSize, (page-1)*pageSize).Desc("uid").Find(&users)
  902. if err != nil {
  903. return err
  904. }
  905. if len(users) <= 0 {
  906. break
  907. }
  908. for _, user := range users {
  909. var list []model.EggSignIn
  910. err = engine.Where("start_time >= ?", startDate).And("uid = ?", user.Id).Find(&list)
  911. if len(list) <= 0 && mapPublicPlatoonFreePunishWithUser[user.Id] == nil {
  912. var PublicPlatoonRecordsPunishWithUser model.PublicPlatoonRecordsPunishWithUser
  913. has, err1 := engine.Where("uid = ?", user.Id).Get(&PublicPlatoonRecordsPunishWithUser)
  914. if err1 != nil {
  915. return err1
  916. }
  917. PublicPlatoonRecordsPunishWithUserDate, _ := time.ParseInLocation("2006-01-02", PublicPlatoonRecordsPunishWithUser.Date, time.Local)
  918. if has && now.AddDate(0, 0, -7).After(PublicPlatoonRecordsPunishWithUserDate) {
  919. //TODO::不进行重复处罚
  920. continue
  921. }
  922. //进行处罚
  923. err, _ = DealCommonWealthPunish(engine, user.Id, "公排处罚")
  924. if err != nil {
  925. return err
  926. }
  927. //添加 public_platoon_records_punish_with_user 记录
  928. publicPlatoonRecordsPunishWithUserDb := implement.NewPublicPlatoonRecordsPunishWithUserDb(engine)
  929. if has {
  930. PublicPlatoonRecordsPunishWithUser.Date = now.Format("2006-01-02")
  931. _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserUpdate(PublicPlatoonRecordsPunishWithUser.Id, &PublicPlatoonRecordsPunishWithUser, "date")
  932. if err2 != nil {
  933. return err2
  934. }
  935. } else {
  936. _, err2 := publicPlatoonRecordsPunishWithUserDb.PublicPlatoonRecordsPunishWithUserInsert(&model.PublicPlatoonRecordsPunishWithUser{
  937. Uid: user.Id,
  938. Date: now.Format("2006-01-02"),
  939. })
  940. if err2 != nil {
  941. return err2
  942. }
  943. }
  944. }
  945. }
  946. page++
  947. }
  948. }
  949. return
  950. }