case.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/credit/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. "go-common/library/xstr"
  10. )
  11. // loadCase load grant case to list.
  12. func (s *Service) loadCase() (err error) {
  13. c := context.TODO()
  14. if s.c.Judge.CaseLoadSwitch != model.StateCaseLoadOpen {
  15. log.Warn("case load switch off!")
  16. return
  17. }
  18. count, err := s.dao.TotalGrantCase(c)
  19. if err != nil {
  20. log.Error("s.dao.CaseGrantCount error(%v)", err)
  21. return
  22. }
  23. loadCount := s.c.Judge.CaseLoadMax - count
  24. if loadCount <= 0 {
  25. log.Info("load case full caseLoadMax(%d) listCount(%d)", s.c.Judge.CaseLoadMax, count)
  26. return
  27. }
  28. log.Info("need load count(%d) on already que len(%d) and max(%d)", loadCount, count, s.c.Judge.CaseLoadMax)
  29. mcases, err := s.dao.Grantcase(c, loadCount)
  30. if err != nil {
  31. log.Error("s.dao.Grantcase(%d) error(%v)", loadCount, err)
  32. return
  33. }
  34. if len(mcases) == 0 {
  35. log.Warn("granting case is zero!")
  36. return
  37. }
  38. var cids []int64
  39. for cid := range mcases {
  40. cids = append(cids, cid)
  41. }
  42. now := time.Now()
  43. stime := xtime.Time(now.Unix())
  44. etime := xtime.Time(now.Add(time.Duration(s.c.Judge.CaseGiveHours) * time.Hour).Unix())
  45. if err = s.dao.UpGrantCase(c, cids, stime, etime); err != nil {
  46. log.Error("s.dao.UpGrantCase(%s) error(%v)", xstr.JoinInts(cids), err)
  47. return
  48. }
  49. for _, v := range mcases {
  50. v.Stime = stime
  51. v.Etime = etime
  52. }
  53. if err = s.dao.SetGrantCase(c, mcases); err != nil {
  54. log.Error("s.dao.SetMIDCaseGrant(%s) error(%v)", xstr.JoinInts(cids), err)
  55. }
  56. log.Info("load cases(%s) to queue on start_time(%v) and end_time(%v)", xstr.JoinInts(cids), stime, etime)
  57. return
  58. }
  59. // DealCaseApplyReason deal with case apply reason.
  60. func (s *Service) DealCaseApplyReason(c context.Context, nwMsg []byte) (err error) {
  61. mr := &model.CaseApplyModifyLog{}
  62. if err = json.Unmarshal(nwMsg, mr); err != nil {
  63. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  64. return
  65. }
  66. var (
  67. aReasons []int64
  68. cas []*model.CaseApplyModifyLog
  69. )
  70. if aReasons, err = s.dao.CaseApplyReasons(c, mr.CID); err != nil {
  71. log.Error("s.dao.CaseApplyReasons(%d) error(%v)", mr.CID, err)
  72. return
  73. }
  74. if len(aReasons) == 0 {
  75. log.Warn("apply reason(%s) is nil", xstr.JoinInts(aReasons))
  76. return
  77. }
  78. if cas, err = s.dao.CaseApplyReasonNum(c, mr.CID, aReasons); err != nil {
  79. log.Error("s.dao.CaseApplyReasonNum(%d,%s) error(%v)", mr.CID, xstr.JoinInts(aReasons), err)
  80. return
  81. }
  82. var (
  83. max int
  84. reason int8
  85. )
  86. for _, v := range cas {
  87. if v.Num > max {
  88. max = v.Num
  89. reason = v.AReason
  90. }
  91. }
  92. standard := int(s.c.Judge.CaseVoteMax * int64(s.c.Judge.CaseVoteMaxPercent) / 100)
  93. if max < standard {
  94. log.Warn("apply reason num(%d) not enough standard(%d)", max, standard)
  95. return
  96. }
  97. if reason == mr.OReason {
  98. log.Warn("max reason(%d) eq orgin reason(%d)", reason, mr.OReason)
  99. return
  100. }
  101. var effect int64
  102. if effect, err = s.dao.UpBlockedCaseReason(c, mr.CID, reason); err != nil {
  103. log.Error("s.dao.UpBlockedCaseReason(%d,%d) error(%v)", mr.CID, reason, err)
  104. return
  105. }
  106. if effect <= 0 {
  107. log.Warn("update case_id(%d) and reason(%d) notIdempotent", mr.CID, reason)
  108. return
  109. }
  110. if model.ReasonToFreeze(reason) {
  111. if err = s.dao.UpBlockedCaseStatus(c, mr.CID, model.CaseStatusFreeze); err != nil {
  112. log.Error("s.dao.UpBlockedCaseStatus(%d,%d) error(%v)", mr.CID, model.CaseStatusFreeze, err)
  113. return
  114. }
  115. }
  116. if err = s.dao.AddBlockedCaseModifyLog(c, mr.CID, mr.AType, mr.OReason, mr.AReason); err != nil {
  117. log.Error("s.dao.AddBlockedCaseModifyLog(%d,%d,%d,%d) error(%v)", mr.CID, mr.AType, mr.OReason, mr.AReason, err)
  118. }
  119. return
  120. }
  121. // GrantCase push case in list.
  122. func (s *Service) GrantCase(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
  123. mr := &model.Case{}
  124. if err = json.Unmarshal(nwMsg, mr); err != nil {
  125. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  126. return
  127. }
  128. if mr.Status != model.CaseStatusGranting {
  129. return
  130. }
  131. stime, err := time.ParseInLocation(time.RFC3339, mr.Stime, time.Local)
  132. if err != nil {
  133. stime, err = time.ParseInLocation("2006-01-02 15:04:05", mr.Stime, time.Local)
  134. if err != nil {
  135. log.Error("time.ParseInLocation(%s) error(%v)", mr.Stime, err)
  136. return
  137. }
  138. err = nil
  139. }
  140. etime, err := time.ParseInLocation(time.RFC3339, mr.Etime, time.Local)
  141. if err != nil {
  142. etime, err = time.ParseInLocation("2006-01-02 15:04:05", mr.Etime, time.Local)
  143. if err != nil {
  144. log.Error("time.ParseInLocation(%s) error(%v)", mr.Etime, err)
  145. return
  146. }
  147. err = nil
  148. }
  149. simCase := &model.SimCase{
  150. ID: mr.ID,
  151. Mid: mr.Mid,
  152. VoteRule: mr.Agree,
  153. VoteBreak: mr.Against,
  154. VoteDelete: mr.VoteDelete,
  155. CaseType: mr.CaseType,
  156. Stime: xtime.Time(stime.Unix()),
  157. Etime: xtime.Time(etime.Unix()),
  158. }
  159. mcases := make(map[int64]*model.SimCase)
  160. mcases[mr.ID] = simCase
  161. if err = s.dao.SetGrantCase(c, mcases); err != nil {
  162. log.Error("s.dao.SetMIDCaseGrant(%+v) error(%v)", mr, err)
  163. }
  164. return
  165. }
  166. // DelGrantCase del case in list.
  167. func (s *Service) DelGrantCase(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
  168. mr := &model.Case{}
  169. if err = json.Unmarshal(nwMsg, mr); err != nil {
  170. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  171. return
  172. }
  173. if mr.Status == model.CaseStatusDealing && mr.CaseType == model.JudeCaseTypePrivate {
  174. return
  175. }
  176. if mr.Status == model.CaseStatusGranting || mr.Status == model.CaseStatusDealed || mr.Status == model.CaseStatusRestart {
  177. return
  178. }
  179. cids := []int64{mr.ID}
  180. // 删除冻结和停止发放中的cid
  181. if err = s.dao.DelGrantCase(c, cids); err != nil {
  182. log.Error("s.dao.SetMIDCaseGrant(%d) error(%v)", mr.ID, err)
  183. }
  184. log.Info("cid(%d) status(%d) remove hash list on start_time(%s) and end_time(%s)", mr.ID, mr.Status, mr.Stime, mr.Etime)
  185. return
  186. }
  187. // DelCaseInfoCache del case info cache.
  188. func (s *Service) DelCaseInfoCache(c context.Context, nwMsg []byte) (err error) {
  189. mr := &model.Case{}
  190. if err = json.Unmarshal(nwMsg, mr); err != nil {
  191. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  192. return
  193. }
  194. if err = s.dao.DelCaseInfoCache(c, mr.ID); err != nil {
  195. log.Error("s.dao.DelCaseInfoCache(%d) error(%v)", mr.ID, err)
  196. return
  197. }
  198. log.Info("cid(%d) del case_info cache", mr.ID)
  199. return
  200. }
  201. // DelVoteCaseCache del vote case cache.
  202. func (s *Service) DelVoteCaseCache(c context.Context, nwMsg []byte) (err error) {
  203. mr := &model.BLogCaseVote{}
  204. if err = json.Unmarshal(nwMsg, mr); err != nil {
  205. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  206. return
  207. }
  208. if err = s.dao.DelVoteCaseCache(c, mr.MID, mr.CID); err != nil {
  209. log.Error("s.dao.DelCaseInfoCache(%d,%d) error(%v)", mr.MID, mr.CID, err)
  210. return
  211. }
  212. log.Info("mid(%d) cid(%d) del vote_case cache", mr.MID, mr.CID)
  213. if err = s.dao.DelJuryInfoCache(c, mr.MID); err != nil {
  214. log.Error("s.dao.DelJuryInfoCache(%d) error(%v)", mr.MID, err)
  215. }
  216. log.Info("mid(%d) del jury cache", mr.MID, mr.CID)
  217. if err = s.dao.DelCaseVoteTopCache(c, mr.MID); err != nil {
  218. log.Error("s.dao.DelCaseVoteTopCache(%d) error(%v)", mr.MID, err)
  219. }
  220. log.Info("mid(%d) del vote top cache", mr.MID)
  221. return
  222. }
  223. // loadDealWrongCase deal wrong case status in cache .
  224. func (s *Service) loadDealWrongCase() (err error) {
  225. var (
  226. cids, wcids []int64
  227. mcids map[int64]int8
  228. c = context.TODO()
  229. )
  230. if cids, err = s.dao.GrantCases(c); err != nil {
  231. log.Error("s.dao.GrantCases error(%+v)", err)
  232. return
  233. }
  234. if len(cids) == 0 {
  235. log.Warn("deal wrong (granting case is zero!)")
  236. return
  237. }
  238. if mcids, err = s.dao.CasesStatus(c, cids); err != nil {
  239. log.Error("s.dao.CasesStatus(%s) error(%+v)", xstr.JoinInts(cids), err)
  240. return
  241. }
  242. for cid, status := range mcids {
  243. if status != model.CaseStatusGranting {
  244. wcids = append(wcids, cid)
  245. }
  246. }
  247. if len(wcids) == 0 {
  248. return
  249. }
  250. if err = s.dao.DelGrantCase(c, wcids); err != nil {
  251. log.Error("s.dao.DelGrantCase(%s) error(%+v)", xstr.JoinInts(wcids), err)
  252. return
  253. }
  254. log.Info("load del wrong cases(%s) from queue", xstr.JoinInts(wcids))
  255. return
  256. }