mysql.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/job/main/credit-timer/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _updateKPISQL = "INSERT INTO blocked_kpi(mid,day,rate,rank,rank_per,rank_total) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE rate=?,rank=?,rank_per=?,rank_total=?"
  12. _updateKPIDataSQL = "INSERT INTO blocked_kpi_data(mid,day,point,active_days,vote_total,vote_radio,blocked_total,opinion_num,opinion_likes,opinion_hates,vote_real_total) VALUES (?,?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE point=?,active_days=?,vote_total=?,vote_radio=?,blocked_total=?,opinion_num=?,opinion_likes=?,opinion_hates=?,vote_real_total=?"
  13. _updateKPIPointSQL = "INSERT INTO blocked_kpi_point(mid,day,point,active_days,vote_total,vote_radio,blocked_total,opinion_num,opinion_likes,opinion_hates) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE point=?,active_days=?,vote_total=?,vote_radio=?,blocked_total=?,opinion_num=?,opinion_likes=?,opinion_hates=?"
  14. _updateCaseEndTimeSQL = "UPDATE blocked_case SET status=3 WHERE status = 1 AND end_time < ?"
  15. _updateCaseEndVoteSQL = "UPDATE blocked_case SET end_time=? WHERE status = 1 AND end_time > ? AND (vote_rule+vote_break+vote_delete) >= ?"
  16. _updateJurySQL = "UPDATE blocked_jury SET status=2,invalid_reason=? WHERE status=1 AND expired < ?"
  17. _updateJuryExpiredSQL = "UPDATE blocked_jury SET status=1, expired=? WHERE mid = ?"
  18. _updateVoteSQL = "UPDATE blocked_case_vote SET vote=3 WHERE vote=0 AND expired > ? AND expired < ? "
  19. _selConfSQL = "SELECT config_key,content FROM blocked_config"
  20. _selJurySQL = "SELECT mid FROM blocked_jury WHERE status=1"
  21. _selJuryKPISQL = "SELECT mid FROM blocked_jury WHERE expired BETWEEN ? AND ?"
  22. _countVoteTotalSQL = "SELECT COUNT(*) FROM blocked_case_vote v INNER JOIN blocked_case c ON c.id=v.cid AND c.status=4 WHERE v.mid = ? AND v.vote IN (1,2,4) AND v.ctime BETWEEN ? AND ?"
  23. _countRightViolateSQL = "SELECT COUNT(*) FROM blocked_case_vote v INNER JOIN blocked_case c ON c.id=v.cid AND v.vote IN(1,4) AND c.judge_type = 1 AND c.status=4 WHERE v.mid = ? AND v.ctime BETWEEN ? AND ?"
  24. _countRightLegalSQL = "SELECT COUNT(*) FROM blocked_case_vote v INNER JOIN blocked_case c ON c.id=v.cid AND v.vote = 2 AND c.judge_type = 2 AND c.status=4 WHERE v.mid = ? AND v.ctime BETWEEN ? AND ?"
  25. _CountBlockedSQL = "SELECT COUNT(*) FROM blocked_info WHERE uid = ? AND status=0 AND ctime BETWEEN ? AND ?"
  26. _selKPIPointDaySQL = "SELECT k.mid,k.day,k.point,k.active_days,k.vote_total,k.vote_radio,k.blocked_total,k.opinion_num,k.opinion_likes,k.opinion_hates,j.expired FROM blocked_kpi_point k INNER JOIN blocked_jury j ON k.mid = j.mid WHERE day = ? ORDER BY k.point desc"
  27. _selKPIPointSQL = "SELECT mid,day,point,active_days,vote_total,vote_radio,blocked_total FROM blocked_kpi_point WHERE mid = ? AND day = ? ORDER BY point desc"
  28. _selKPISQL = "SELECT mid,day,rate,rank,rank_per,rank_total FROM blocked_kpi WHERE mid = ?"
  29. _countActiveSQL = "SELECT COUNT(*) FROM (SELECT DATE_FORMAT(ctime,'%Y-%m-%d') FROM blocked_case_vote WHERE vote IN(1,2,4) AND mid=? AND ctime BETWEEN ? AND ? GROUP BY DATE_FORMAT(ctime,'%Y-%m-%d')) t"
  30. _countOpinionSQL = "SELECT COUNT(*) FROM blocked_opinion WHERE mid = ? AND state = 0 AND ctime BETWEEN ? AND ?"
  31. _opinionQualitySQL = "SELECT COALESCE(SUM(likes),0),COALESCE(SUM(hates),0) FROM blocked_opinion WHERE mid = ? AND state = 0 AND ctime BETWEEN ? AND ?"
  32. _countVoteByTimeSQL = "SELECT count(*) from blocked_case_vote where vote in(1,2,4) and mid=? and ctime between ? and ?"
  33. )
  34. // UpdateKPI update KPI info.
  35. func (d *Dao) UpdateKPI(c context.Context, r *model.Kpi) (err error) {
  36. if _, err = d.db.Exec(c, _updateKPISQL, r.Mid, r.Day, r.Rate, r.Rank, r.RankPer, r.RankTotal, r.Rate, r.Rank, r.RankPer, r.RankTotal); err != nil {
  37. log.Error("d.UpdateKPI err(%v)", err)
  38. }
  39. return
  40. }
  41. // UpdateKPIData update kpi_data info.
  42. func (d *Dao) UpdateKPIData(c context.Context, r *model.KpiData) (err error) {
  43. if _, err = d.db.Exec(c, _updateKPIDataSQL, r.Mid, r.Day, r.Point, r.ActiveDays, r.VoteTotal, r.VoteRadio, r.BlockedTotal, r.OpinionNum, r.OpinionLikes, r.OpinionHates, r.VoteRealTotal, r.Point, r.ActiveDays, r.VoteTotal, r.VoteRadio, r.BlockedTotal, r.OpinionNum, r.OpinionLikes, r.OpinionHates, r.VoteRealTotal); err != nil {
  44. log.Error("d.UpdateKPIPoint err(%v)", err)
  45. }
  46. return
  47. }
  48. // UpdateKPIPoint update kpi point info.
  49. func (d *Dao) UpdateKPIPoint(c context.Context, r *model.KpiPoint) (err error) {
  50. if _, err = d.db.Exec(c, _updateKPIPointSQL, r.Mid, r.Day, r.Point, r.ActiveDays, r.VoteTotal, r.VoteRadio, r.BlockedTotal, r.OpinionNum, r.OpinionLikes, r.OpinionHates, r.Point, r.ActiveDays, r.VoteTotal, r.VoteRadio, r.BlockedTotal, r.OpinionNum, r.OpinionLikes, r.OpinionHates); err != nil {
  51. log.Error("d.UpdateKPIPoint err(%v)", err)
  52. }
  53. return
  54. }
  55. // UpdateCaseEndTime update case status to CaseStatusDealing which expired time less than now.
  56. func (d *Dao) UpdateCaseEndTime(c context.Context, now time.Time) (affect int64, err error) {
  57. rows, err := d.db.Exec(c, _updateCaseEndTimeSQL, now)
  58. if err != nil {
  59. log.Error("d.UpdateCaseEndTime err(%v)", err)
  60. return
  61. }
  62. return rows.RowsAffected()
  63. }
  64. // UpdateCaseEndVote update case status to CaseStatusDealing which vote total more than conf case vote total.
  65. func (d *Dao) UpdateCaseEndVote(c context.Context, vt int64, ts time.Time) (affect int64, err error) {
  66. rows, err := d.db.Exec(c, _updateCaseEndVoteSQL, ts, ts, vt)
  67. if err != nil {
  68. log.Error("d.UpdateCaseEndVote err(%v)", err)
  69. return
  70. }
  71. return rows.RowsAffected()
  72. }
  73. // UpdateJury update jury status to expired which expired time less than ts.
  74. func (d *Dao) UpdateJury(c context.Context, now time.Time) (affect int64, err error) {
  75. rows, err := d.db.Exec(c, _updateJurySQL, model.JuryExpire, now)
  76. if err != nil {
  77. log.Error("d.UpdateJury err(%v)", err)
  78. return
  79. }
  80. return rows.RowsAffected()
  81. }
  82. // UpdateJuryExpired update jury expired.
  83. func (d *Dao) UpdateJuryExpired(c context.Context, mid int64, expired time.Time) (err error) {
  84. if _, err = d.db.Exec(c, _updateJuryExpiredSQL, expired, mid); err != nil {
  85. log.Error("d.UpdateJuryExpired err(%v)", err)
  86. }
  87. return
  88. }
  89. // UpdateVote update vote status to give up which do not vote and expired less than ts.
  90. func (d *Dao) UpdateVote(c context.Context, now time.Time) (affect int64, err error) {
  91. rows, err := d.db.Exec(c, _updateVoteSQL, now.Add(-4*time.Hour), now)
  92. if err != nil {
  93. log.Error("d.updateVote err(%v)", err)
  94. return
  95. }
  96. return rows.RowsAffected()
  97. }
  98. // LoadConf load conf.
  99. func (d *Dao) LoadConf(c context.Context) (vTotal int64, err error) {
  100. var (
  101. key string
  102. value string
  103. )
  104. rows, err := d.db.Query(c, _selConfSQL)
  105. if err != nil {
  106. log.Error("d.loadConf err(%v)", err)
  107. return
  108. }
  109. defer rows.Close()
  110. for rows.Next() {
  111. if err = rows.Scan(&key, &value); err != nil {
  112. log.Error("rows.Scan err(%v)", err)
  113. return
  114. }
  115. switch key {
  116. case "case_vote_max":
  117. if vTotal, err = strconv.ParseInt(value, 10, 64); err != nil {
  118. return
  119. }
  120. }
  121. }
  122. err = rows.Err()
  123. return
  124. }
  125. // JuryList get jury list.
  126. func (d *Dao) JuryList(c context.Context) (res []int64, err error) {
  127. var rows *sql.Rows
  128. if rows, err = d.db.Prepared(_selJurySQL).Query(c); err != nil {
  129. log.Error("dao.JuryList error(%v)", err)
  130. return
  131. }
  132. defer rows.Close()
  133. for rows.Next() {
  134. var r int64
  135. if err = rows.Scan(&r); err != nil {
  136. log.Error("row.Scan() error(%v)", err)
  137. res = nil
  138. return
  139. }
  140. res = append(res, r)
  141. }
  142. err = rows.Err()
  143. return
  144. }
  145. // JuryKPI get jury list.
  146. func (d *Dao) JuryKPI(c context.Context, begin, end string) (res []int64, err error) {
  147. var rows *sql.Rows
  148. if rows, err = d.db.Prepared(_selJuryKPISQL).Query(c, begin, end); err != nil {
  149. log.Error("dao.JuryKPI error(%v)", err)
  150. return
  151. }
  152. defer rows.Close()
  153. for rows.Next() {
  154. var r int64
  155. if err = rows.Scan(&r); err != nil {
  156. log.Error("row.Scan() error(%v)", err)
  157. res = nil
  158. return
  159. }
  160. res = append(res, r)
  161. }
  162. err = rows.Err()
  163. return
  164. }
  165. // CountVoteTotal get vote total.
  166. func (d *Dao) CountVoteTotal(c context.Context, mid int64, begin, end string) (count int64, err error) {
  167. row := d.db.QueryRow(c, _countVoteTotalSQL, mid, begin, end)
  168. if err = row.Scan(&count); err != nil {
  169. log.Error("d.CountVoteTotal err(%v)", err)
  170. }
  171. return
  172. }
  173. // CountVoteRightViolate get vote right violate count.
  174. func (d *Dao) CountVoteRightViolate(c context.Context, mid int64, begin, end string) (count int64, err error) {
  175. row := d.db.QueryRow(c, _countRightViolateSQL, mid, begin, end)
  176. if err = row.Scan(&count); err != nil {
  177. log.Error("d.CountVoteRightViolate err(%v)", err)
  178. }
  179. return
  180. }
  181. // CountVoteRightLegal get vote right legal count.
  182. func (d *Dao) CountVoteRightLegal(c context.Context, mid int64, begin, end string) (count int64, err error) {
  183. row := d.db.QueryRow(c, _countRightLegalSQL, mid, begin, end)
  184. if err = row.Scan(&count); err != nil {
  185. log.Error("d.CountVoteRightLegal err(%v)", err)
  186. }
  187. return
  188. }
  189. // CountBlocked get user block count ofter ts.
  190. func (d *Dao) CountBlocked(c context.Context, mid int64, begin, end string) (count int64, err error) {
  191. row := d.db.QueryRow(c, _CountBlockedSQL, mid, begin, end)
  192. if err = row.Scan(&count); err != nil {
  193. log.Error("d.CountBlocked err(%v)", err)
  194. }
  195. return
  196. }
  197. // KPIPointDay get KPI point day list.
  198. func (d *Dao) KPIPointDay(c context.Context, day string) (res []model.KpiPoint, err error) {
  199. var rows *sql.Rows
  200. if rows, err = d.db.Query(c, _selKPIPointDaySQL, day); err != nil {
  201. log.Error("dao.JuryKpi error(%v)", err)
  202. return
  203. }
  204. defer rows.Close()
  205. for rows.Next() {
  206. r := model.KpiPoint{}
  207. if err = rows.Scan(&r.Mid, &r.Day, &r.Point, &r.ActiveDays, &r.VoteTotal, &r.VoteRadio, &r.BlockedTotal, &r.OpinionNum, &r.OpinionLikes, &r.OpinionHates, &r.Expired); err != nil {
  208. log.Error("row.Scan() error(%v)", err)
  209. res = nil
  210. return
  211. }
  212. res = append(res, r)
  213. }
  214. err = rows.Err()
  215. return
  216. }
  217. // KPIPoint get kpi point.
  218. func (d *Dao) KPIPoint(c context.Context, mid int64, day string) (r model.KpiPoint, err error) {
  219. row := d.db.QueryRow(c, _selKPIPointSQL, mid, day)
  220. if err = row.Scan(&r.Mid, &r.Day, &r.Point, &r.ActiveDays, &r.VoteTotal, &r.VoteRadio, &r.BlockedTotal); err != nil {
  221. log.Error("d.KPIPoint err(%v)", err)
  222. }
  223. return
  224. }
  225. // KPIList get kpi list.
  226. func (d *Dao) KPIList(c context.Context, mid int64) (res []model.Kpi, err error) {
  227. var rows *sql.Rows
  228. if rows, err = d.db.Query(c, _selKPISQL, mid); err != nil {
  229. log.Error("dao.KPIList error(%v)", err)
  230. return
  231. }
  232. defer rows.Close()
  233. for rows.Next() {
  234. r := model.Kpi{}
  235. if err = rows.Scan(&r.Mid, &r.Day, &r.Rate, &r.Rank, &r.RankPer, &r.RankTotal); err != nil {
  236. log.Error("row.Scan() error(%v)", err)
  237. res = nil
  238. return
  239. }
  240. res = append(res, r)
  241. }
  242. err = rows.Err()
  243. return
  244. }
  245. // CountVoteActive get vote active days count.
  246. func (d *Dao) CountVoteActive(c context.Context, mid int64, begin, end string) (count int64, err error) {
  247. row := d.db.QueryRow(c, _countActiveSQL, mid, begin, end)
  248. if err = row.Scan(&count); err != nil {
  249. log.Error("d.CountVoteActive err(%v)", err)
  250. }
  251. return
  252. }
  253. // CountOpinion count user opinion in 30 days.
  254. func (d *Dao) CountOpinion(c context.Context, mid int64, begin, end string) (count int64, err error) {
  255. row := d.db.QueryRow(c, _countOpinionSQL, mid, begin, end)
  256. if err = row.Scan(&count); err != nil {
  257. log.Error("d.CountOpinion(mid:%d begin:%s end:%s) err(%v)", mid, begin, end, err)
  258. }
  259. return
  260. }
  261. // OpinionQuality count user opinion quality(fields likes - hates) in 30days.
  262. func (d *Dao) OpinionQuality(c context.Context, mid int64, begin, end string) (likes, hates int64, err error) {
  263. row := d.db.QueryRow(c, _opinionQualitySQL, mid, begin, end)
  264. if err = row.Scan(&likes, &hates); err != nil {
  265. if err != sql.ErrNoRows {
  266. log.Error("d.OpinionQuality(mid:%d begin:%s end:%s) err(%v)", mid, begin, end, err)
  267. return
  268. }
  269. err = nil
  270. }
  271. return
  272. }
  273. // CountVoteByTime count vote by time.
  274. func (d *Dao) CountVoteByTime(c context.Context, mid int64, begin, end time.Time) (count int64, err error) {
  275. var row *sql.Row
  276. if row = d.db.QueryRow(c, _countVoteByTimeSQL, mid, begin, end); err != nil {
  277. log.Error("d.CountVoteByTime.Query(%d) error(%v)", mid, err)
  278. return
  279. }
  280. if err = row.Scan(&count); err != nil {
  281. log.Error("row.Scan() error(%v)", err)
  282. }
  283. return
  284. }