case.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/credit/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. _inBlockedCasesSQL = "INSERT INTO blocked_case(mid,status,origin_content,punish_result,origin_title,origin_type,origin_url,blocked_days,reason_type,relation_id,oper_id,business_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)"
  15. _inBlockedCaseModifyLogSQL = "INSERT INTO blocked_case_modify_log(case_id,modify_type,origin_reason,modify_reason) VALUES (?,?,?,?)"
  16. _upBlockedCaseReasonSQL = "UPDATE blocked_case SET reason_type = ? WHERE id = ?"
  17. _upBlockedCaseStatusSQL = "UPDATE blocked_case SET status = ? WHERE id = ?"
  18. _upGrantCaseSQL = "UPDATE blocked_case SET status = 1, start_time = ?, end_time = ? WHERE id IN (%s)"
  19. _grantCaseSQL = "SELECT id,mid,start_time,end_time,vote_rule,vote_break,vote_delete,case_type FROM blocked_case WHERE status = 8 ORDER BY mtime ASC limit ?"
  20. _caseVoteSQL = "SELECT mid,vote,expired FROM blocked_case_vote WHERE id = ?"
  21. _caseRelationIDCountSQL = "SELECT COUNT(*) FROM blocked_case WHERE origin_type=? AND relation_id =?"
  22. _caseVotesCIDSQL = "SELECT mid,vote FROM blocked_case_vote WHERE cid = ?"
  23. _countCaseMIDSQL = "SELECT COUNT(*) FROM blocked_case WHERE mid = ? AND origin_type = ? AND ctime >= ?"
  24. _caseApplyReasonsSQL = "SELECT DISTINCT(apply_reason) FROM blocked_case_apply_log WHERE case_id = ? AND apply_type = 1"
  25. _caseApplyReasonNumSQL = "SELECT COUNT(*) AS num, case_id, apply_type, apply_reason, origin_reason FROM `blocked_case_apply_log` WHERE case_id = ? AND apply_type = 1 AND apply_reason IN (%s) GROUP BY apply_reason ORDER BY mtime ASC"
  26. _casesStatusSQL = "SELECT id,status FROM blocked_case WHERE id IN (%s)"
  27. )
  28. // AddBlockedCase add blocked case.
  29. func (d *Dao) AddBlockedCase(c context.Context, ca *model.Case) (err error) {
  30. if _, err = d.db.Exec(c, _inBlockedCasesSQL, ca.Mid, ca.Status, ca.OriginContent, ca.PunishResult, ca.OriginTitle, ca.OriginType, ca.OriginURL, ca.BlockedDay, ca.ReasonType, ca.RelationID, ca.OPID, ca.BCtime); err != nil {
  31. log.Error("d.AddBlockedCase(%+v) err(%v)", ca, err)
  32. }
  33. return
  34. }
  35. // AddBlockedCaseModifyLog add blocked case modify log.
  36. func (d *Dao) AddBlockedCaseModifyLog(c context.Context, cid int64, mType, oReason, mReason int8) (err error) {
  37. if _, err = d.db.Exec(c, _inBlockedCaseModifyLogSQL, cid, mType, oReason, mReason); err != nil {
  38. log.Error("d.AddBlockedCaseModifyLog(%d,%d,%d,%d) err(%v)", cid, mType, oReason, mReason, err)
  39. }
  40. return
  41. }
  42. // UpBlockedCaseReason update blocked case reason.
  43. func (d *Dao) UpBlockedCaseReason(c context.Context, cid int64, reasonType int8) (affect int64, err error) {
  44. var result sql.Result
  45. if result, err = d.db.Exec(c, _upBlockedCaseReasonSQL, reasonType, cid); err != nil {
  46. log.Error("d.UpBlockedCaseReason(%d,%d) err(%v)", cid, reasonType, err)
  47. return
  48. }
  49. return result.RowsAffected()
  50. }
  51. // UpBlockedCaseStatus update blocked case status.
  52. func (d *Dao) UpBlockedCaseStatus(c context.Context, cid int64, status int8) (err error) {
  53. if _, err = d.db.Exec(c, _upBlockedCaseStatusSQL, status, cid); err != nil {
  54. log.Error("d.UpBlockedCaseStatus(%d,%d) err(%v)", cid, status, err)
  55. }
  56. return
  57. }
  58. // UpGrantCase update blocked_case from que to granting.
  59. func (d *Dao) UpGrantCase(c context.Context, ids []int64, stime xtime.Time, etime xtime.Time) (err error) {
  60. if _, err = d.db.Exec(c, fmt.Sprintf(_upGrantCaseSQL, xstr.JoinInts(ids)), stime, etime); err != nil {
  61. log.Error("d.UpGrantCase(%s,%v,%v) err(%v)", xstr.JoinInts(ids), stime, etime, err)
  62. }
  63. return
  64. }
  65. // Grantcase get case from state of CaseStatusQueueing.
  66. func (d *Dao) Grantcase(c context.Context, limit int) (mcases map[int64]*model.SimCase, err error) {
  67. mcases = make(map[int64]*model.SimCase)
  68. rows, err := d.db.Query(c, _grantCaseSQL, limit)
  69. if err != nil {
  70. log.Error("d.Grantcase err(%v)", err)
  71. return
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. ca := &model.SimCase{}
  76. if err = rows.Scan(&ca.ID, &ca.Mid, &ca.Stime, &ca.Etime, &ca.VoteRule, &ca.VoteBreak, &ca.VoteDelete, &ca.CaseType); err != nil {
  77. log.Error("rows.Scan err(%v)", err)
  78. return
  79. }
  80. mcases[ca.ID] = ca
  81. }
  82. err = rows.Err()
  83. return
  84. }
  85. // CaseVote get blocked case vote info.
  86. func (d *Dao) CaseVote(c context.Context, id int64) (res *model.CaseVote, err error) {
  87. res = &model.CaseVote{}
  88. row := d.db.QueryRow(c, _caseVoteSQL, id)
  89. if err = row.Scan(&res.MID, &res.Vote, &res.Expired); err != nil {
  90. log.Error("d.CaseVote err(%v)", err)
  91. }
  92. return
  93. }
  94. // CaseRelationIDCount get case relation_id count.
  95. func (d *Dao) CaseRelationIDCount(c context.Context, tp int8, relationID string) (count int64, err error) {
  96. row := d.db.QueryRow(c, _caseRelationIDCountSQL, tp, relationID)
  97. if err = row.Scan(&count); err != nil {
  98. log.Error("d.caseRelationIDCount err(%v)", err)
  99. }
  100. return
  101. }
  102. // CaseVotesCID is blocked case vote list by cid.
  103. func (d *Dao) CaseVotesCID(c context.Context, cid int64) (res []*model.CaseVote, err error) {
  104. rows, err := d.db.Query(c, _caseVotesCIDSQL, cid)
  105. if err != nil {
  106. log.Error("d.CaseVoteCID(%d) err(%v)", cid, err)
  107. return
  108. }
  109. defer rows.Close()
  110. for rows.Next() {
  111. ca := &model.CaseVote{}
  112. if err = rows.Scan(&ca.MID, &ca.Vote); err != nil {
  113. log.Error("rows.Scan err(%v)", err)
  114. return
  115. }
  116. res = append(res, ca)
  117. }
  118. err = rows.Err()
  119. return
  120. }
  121. // CountCaseMID get count case by mid.
  122. func (d *Dao) CountCaseMID(c context.Context, mid int64, tp int8) (count int64, err error) {
  123. row := d.db.QueryRow(c, _countCaseMIDSQL, mid, tp, time.Now().AddDate(0, 0, -2))
  124. if err = row.Scan(&count); err != nil {
  125. log.Error("d.CountCaseMID err(%v)", err)
  126. }
  127. return
  128. }
  129. // CaseApplyReasons case apply reasons.
  130. func (d *Dao) CaseApplyReasons(c context.Context, cid int64) (aReasons []int64, err error) {
  131. rows, err := d.db.Query(c, _caseApplyReasonsSQL, cid)
  132. if err != nil {
  133. log.Error("d.CaseApplyReasons(%d) err(%v)", cid, err)
  134. return
  135. }
  136. defer rows.Close()
  137. for rows.Next() {
  138. var aReason int64
  139. if err = rows.Scan(&aReason); err != nil {
  140. log.Error("rows.Scan err(%v)", err)
  141. return
  142. }
  143. aReasons = append(aReasons, aReason)
  144. }
  145. err = rows.Err()
  146. return
  147. }
  148. // CaseApplyReasonNum case group apply reasons num.
  149. func (d *Dao) CaseApplyReasonNum(c context.Context, cid int64, aReasons []int64) (cas []*model.CaseApplyModifyLog, err error) {
  150. rows, err := d.db.Query(c, fmt.Sprintf(_caseApplyReasonNumSQL, xstr.JoinInts(aReasons)), cid)
  151. if err != nil {
  152. log.Error("d.CaseApplyReasonNum(%s) err(%v)", xstr.JoinInts(aReasons), err)
  153. return
  154. }
  155. defer rows.Close()
  156. for rows.Next() {
  157. ca := &model.CaseApplyModifyLog{}
  158. if err = rows.Scan(&ca.Num, &ca.CID, &ca.AType, &ca.AReason, &ca.OReason); err != nil {
  159. log.Error("rows.Scan err(%v)", err)
  160. return
  161. }
  162. cas = append(cas, ca)
  163. }
  164. err = rows.Err()
  165. return
  166. }
  167. // CasesStatus get cases status.
  168. func (d *Dao) CasesStatus(c context.Context, cids []int64) (map[int64]int8, error) {
  169. rows, err := d.db.Query(c, fmt.Sprintf(_casesStatusSQL, xstr.JoinInts(cids)))
  170. if err != nil {
  171. return nil, err
  172. }
  173. defer rows.Close()
  174. cs := make(map[int64]int8, len(cids))
  175. for rows.Next() {
  176. var (
  177. cid int64
  178. status int8
  179. )
  180. if err = rows.Scan(&cid, &status); err != nil {
  181. if err == xsql.ErrNoRows {
  182. err = nil
  183. }
  184. return nil, err
  185. }
  186. cs[cid] = status
  187. }
  188. err = rows.Err()
  189. return cs, err
  190. }