jury.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/credit-timer/model"
  6. "go-common/library/log"
  7. )
  8. func (s *Service) caseProc(ctx context.Context) {
  9. affect, err := s.dao.UpdateCaseEndTime(ctx, time.Now())
  10. log.Info("UpdateCaseEndTime affect %d err %v", affect, err)
  11. if s.c.Judge.CaseEndVoteTotal > 0 {
  12. affect, err = s.dao.UpdateCaseEndVote(ctx, s.c.Judge.CaseEndVoteTotal, time.Now().Add(time.Duration(s.c.Judge.ReservedTime)))
  13. log.Info("UpdateCaseEndVote affect %d err %v", affect, err)
  14. }
  15. }
  16. func (s *Service) loadConf(ctx context.Context) {
  17. vTotal, err := s.dao.LoadConf(ctx)
  18. if err != nil {
  19. log.Error("loadConf error(%v)", err)
  20. return
  21. }
  22. s.c.Judge.CaseEndVoteTotal = vTotal
  23. }
  24. func (s *Service) juryProc(c context.Context) {
  25. affect, err := s.dao.UpdateJury(c, time.Now())
  26. log.Info("update jury affect %d err %v", affect, err)
  27. }
  28. func (s *Service) voteProc(c context.Context) {
  29. affect, err := s.dao.UpdateVote(c, time.Now())
  30. log.Info("update vote affect %d err %v", affect, err)
  31. }
  32. // ComputePoint compute KPI point.
  33. func (s *Service) ComputePoint(c context.Context, mid int64) (r model.KpiPoint, err error) {
  34. var (
  35. voteTotal, voteRight, blockedTotal, activeDays, opinionNums, opinionQuality int64
  36. voteRightViolate, voteRightLegal, likes, hates int64
  37. //vr:投准率 vf:投准率系数 af:活跃系数 bf:违规系数 of:观点数量系数 oqf:观点质量系数
  38. vr, vf, af, bf, of, oqf float64
  39. point float64
  40. begin, end string
  41. )
  42. begin = time.Now().AddDate(0, 0, -30).Format("2006-01-02")
  43. end = time.Now().Format("2006-01-02")
  44. if blockedTotal, err = s.dao.CountBlocked(c, mid, begin, end); err != nil {
  45. return
  46. }
  47. if blockedTotal == 0 {
  48. bf = float64(1)
  49. }
  50. if voteTotal, err = s.dao.CountVoteTotal(c, mid, begin, end); err != nil {
  51. return
  52. }
  53. if voteRightViolate, err = s.dao.CountVoteRightViolate(c, mid, begin, end); err != nil {
  54. return
  55. }
  56. if voteRightLegal, err = s.dao.CountVoteRightLegal(c, mid, begin, end); err != nil {
  57. return
  58. }
  59. voteRight = voteRightViolate + voteRightLegal
  60. if voteTotal > 0 {
  61. vr = float64(voteRight) / float64(voteTotal)
  62. vf = s.voteRightRatio(vr)
  63. }
  64. if activeDays, err = s.dao.CountVoteActive(c, mid, begin, end); err != nil {
  65. return
  66. }
  67. af = s.activeDaysRatio(activeDays)
  68. if opinionNums, err = s.dao.CountOpinion(c, mid, begin, end); err != nil {
  69. return
  70. }
  71. of = s.opinionNumsRatio(opinionNums)
  72. if likes, hates, err = s.dao.OpinionQuality(c, mid, begin, end); err != nil {
  73. return
  74. }
  75. opinionQuality = likes - hates
  76. oqf = s.opinionQualityRatio(opinionQuality)
  77. log.Info("mid:%d voteTotal:%v vr:%v vf:%v af:%v bf:%v of:%v oqf:%v", mid, voteTotal, vr, vf, af, bf, of, oqf)
  78. point = float64(voteTotal) * vr * vf * af * bf * of * oqf * 100
  79. r.Point = int64(point)
  80. r.ActiveDays = activeDays
  81. r.Day = time.Now()
  82. r.Mid = mid
  83. r.VoteTotal = voteTotal
  84. r.VoteRadio = int64(vr * 100)
  85. r.BlockedTotal = blockedTotal
  86. r.OpinionNum = opinionNums
  87. r.OpinionLikes = likes
  88. r.OpinionHates = hates
  89. return
  90. }
  91. func (s *Service) kpiPointProc(c context.Context) (err error) {
  92. var (
  93. mids []int64
  94. mid int64
  95. r model.KpiPoint
  96. )
  97. if mids, err = s.dao.JuryList(c); err != nil {
  98. log.Error("kpiPoint err(%v)", err)
  99. return
  100. }
  101. for _, mid = range mids {
  102. if r, err = s.ComputePoint(c, mid); err != nil {
  103. log.Error("computePoint err(%v)", err)
  104. continue
  105. }
  106. s.dao.UpdateKPIPoint(c, &r)
  107. }
  108. return
  109. }
  110. // KPIProc KPI process.
  111. func (s *Service) KPIProc(c context.Context) (err error) {
  112. var (
  113. ps []model.Kpi
  114. res []model.KpiPoint
  115. k model.KpiPoint
  116. kd model.KpiData
  117. at int64
  118. day string
  119. m int64
  120. )
  121. day = time.Now().Format("2006-01-02")
  122. if res, err = s.dao.KPIPointDay(c, day); err != nil {
  123. log.Error("kpiPoint(%s) err(%v)", day, err)
  124. }
  125. for _, k = range res {
  126. a := model.Kpi{}
  127. a.Point = k.Point
  128. if len(ps) == 0 {
  129. a.PreCount = 0
  130. m = 1
  131. ps = append(ps, a)
  132. continue
  133. }
  134. if ps[len(ps)-1].Point == k.Point {
  135. m = m + 1
  136. continue
  137. }
  138. a.PreCount = m
  139. ps = append(ps, a)
  140. m = m + 1
  141. }
  142. at = int64(len(res))
  143. d := time.Now()
  144. for _, k = range res {
  145. if k.Expired.Format("2006-01-02") != day {
  146. log.Info("Expired(%s)!=day(%s)", k.Expired.Format("2006-01-02"), day)
  147. continue
  148. }
  149. for i, r := range ps {
  150. log.Info("RankPer r(%+v) k(%+v)", r, k)
  151. if r.Point == k.Point {
  152. b := model.Kpi{}
  153. b.Point = k.Point
  154. b.Rank = int64(i + 1)
  155. b.RankPer = (r.PreCount + 1) * 100 / at
  156. b.RankTotal = at
  157. p := b.RankPer
  158. if p == 0 {
  159. b.Rate = 1
  160. b.RankPer = 1
  161. } else if p > 0 && p <= 10 {
  162. b.Rate = 1
  163. } else if p > 10 && p <= 25 {
  164. b.Rate = 2
  165. } else if p > 25 && p <= 40 {
  166. b.Rate = 3
  167. } else if p > 40 && p <= 60 {
  168. b.Rate = 4
  169. } else if p > 60 && p <= 100 {
  170. b.Rate = 5
  171. }
  172. if r.Point == 0 {
  173. b.Rate = 5
  174. }
  175. b.Day = d
  176. b.Mid = k.Mid
  177. if err = s.dao.UpdateKPI(c, &b); err != nil {
  178. log.Error("dao.UpdateKPI(%+v) err(%v)", b, err)
  179. }
  180. kd.KpiPoint = k
  181. end := k.Day
  182. begin := k.Day.AddDate(0, 0, -30)
  183. count, _ := s.dao.CountVoteByTime(c, k.Mid, begin, end)
  184. kd.VoteRealTotal = count
  185. if err = s.dao.UpdateKPIData(c, &kd); err != nil {
  186. log.Error("dao.UpdateKPIData(%+v) err(%v)", kd, err)
  187. }
  188. break
  189. }
  190. }
  191. }
  192. return
  193. }
  194. // FixKPI fix kpi.
  195. func (s *Service) FixKPI(c context.Context, year, month, dd int, mid int64) (res []model.KpiPoint, err error) {
  196. var (
  197. ps []model.Kpi
  198. k model.KpiPoint
  199. kd model.KpiData
  200. at int64
  201. day string
  202. m int64
  203. )
  204. t := time.Date(year, time.Month(month), dd, 0, 0, 0, 0, time.UTC)
  205. day = t.Format("2006-01-02")
  206. if res, err = s.dao.KPIPointDay(c, day); err != nil {
  207. log.Error("kpiPoint(%s) err(%v)", day, err)
  208. }
  209. for _, k = range res {
  210. a := model.Kpi{}
  211. a.Point = k.Point
  212. if len(ps) == 0 {
  213. a.PreCount = 0
  214. m = 1
  215. ps = append(ps, a)
  216. continue
  217. }
  218. if ps[len(ps)-1].Point == k.Point {
  219. m = m + 1
  220. continue
  221. }
  222. a.PreCount = m
  223. ps = append(ps, a)
  224. m = m + 1
  225. }
  226. at = int64(len(res))
  227. for _, k = range res {
  228. for i, r := range ps {
  229. if r.Point == k.Point {
  230. b := model.Kpi{}
  231. b.Point = k.Point
  232. b.Rank = int64(i + 1)
  233. b.RankPer = (r.PreCount + 1) * 100 / at
  234. b.RankTotal = at
  235. p := b.RankPer
  236. if p == 0 {
  237. b.Rate = 1
  238. b.RankPer = 1
  239. } else if p > 0 && p <= 10 {
  240. b.Rate = 1
  241. } else if p > 10 && p <= 25 {
  242. b.Rate = 2
  243. } else if p > 25 && p <= 40 {
  244. b.Rate = 3
  245. } else if p > 40 && p <= 60 {
  246. b.Rate = 4
  247. } else if p > 60 && p <= 100 {
  248. b.Rate = 5
  249. }
  250. if r.Point == 0 {
  251. b.Rate = 5
  252. }
  253. b.Day = t
  254. b.Mid = k.Mid
  255. if b.Mid == mid {
  256. log.Info("fix kpi mid %d kpi %v", mid, b)
  257. if err = s.dao.UpdateKPI(c, &b); err != nil {
  258. log.Error("dao.UpdateKPI(%+v) err(%v)", b, err)
  259. }
  260. kd.KpiPoint = k
  261. end := k.Day
  262. begin := k.Day.AddDate(0, 0, -30)
  263. count, _ := s.dao.CountVoteByTime(c, k.Mid, begin, end)
  264. kd.VoteRealTotal = count
  265. if err = s.dao.UpdateKPIData(c, &kd); err != nil {
  266. log.Error("dao.UpdateKPIData(%+v) err(%v)", kd, err)
  267. }
  268. }
  269. break
  270. }
  271. }
  272. }
  273. return
  274. }