case.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. model "go-common/app/interface/main/credit/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _addBlockedCasesSQL = "INSERT INTO blocked_case(status,mid,operator,origin_content,punish_result,origin_title,origin_type,origin_url,blocked_days,reason_type,relation_id,oper_id,business_time) VALUES %s"
  16. _insertVoteSQL = "INSERT INTO blocked_case_vote(mid,cid,expired) VALUES(?,?,?)"
  17. _updateVoteSQL = "INSERT INTO blocked_case_vote(mid,cid,vote) VALUES(?,?,?) ON DUPLICATE KEY UPDATE vote=?"
  18. _inBlockedCaseApplyLogSQL = "INSERT INTO blocked_case_apply_log(mid,case_id,apply_type,origin_reason,apply_reason) VALUES(?,?,?,?,?)"
  19. _updateCaseVoteTotalSQL = "UPDATE blocked_case SET %s=%s+? WHERE id=?"
  20. _getCaseByIDSQL = `SELECT id,mid,status,origin_content,punish_result,origin_title,origin_url,end_time,vote_rule,vote_break,vote_delete,origin_type,reason_type,judge_type,blocked_days,
  21. put_total,start_time,end_time,operator,ctime,mtime,relation_id,case_type FROM blocked_case WHERE id=? AND status IN (1,3,4,6)`
  22. _countCaseVoteSQL = "SELECT COUNT(*) FROM blocked_case_vote WHERE mid=? AND vote!=3"
  23. _isVoteByMIDSQL = "SELECT id FROM blocked_case_vote WHERE mid=? AND cid=? AND vote=0"
  24. _getVoteInfoSQL = "SELECT id,cid,mid,vote,expired,mtime FROM blocked_case_vote WHERE mid=? AND cid=?"
  25. _loadMidVoteIDSQL = "SELECT v.cid,c.case_type FROM blocked_case_vote v INNER JOIN blocked_case c ON v.cid=c.id WHERE v.mid =? AND v.ctime >=? ORDER BY v.id DESC"
  26. _getCaseByIDsSQL = `SELECT id,mid,status,origin_content,punish_result,origin_title,origin_url,end_time,vote_rule,vote_break,vote_delete,origin_type,reason_type,judge_type,blocked_days,
  27. put_total,start_time,end_time,operator,ctime,mtime,relation_id,case_type FROM blocked_case WHERE id IN(%s) AND status IN (1,3,4,6)`
  28. _caseRelationIDCountSQL = "SELECT COUNT(*) FROM blocked_case WHERE origin_type=? AND relation_id =?"
  29. _caseInfoIDsSQL = `SELECT id,mid,status,origin_content,punish_result,origin_title,origin_url,end_time,vote_rule,vote_break,vote_delete,origin_type,reason_type,judge_type,blocked_days,
  30. put_total,start_time,end_time,operator,ctime,mtime,relation_id,case_type FROM blocked_case WHERE id IN (%s)`
  31. _caseVotesMIDSQL = "SELECT id,cid,mid,vote,expired,mtime FROM blocked_case_vote WHERE id IN (%s)"
  32. _caseVoteIDMIDSQL = "SELECT id,cid FROM blocked_case_vote WHERE mid = ? ORDER BY mtime DESC LIMIT ?,?"
  33. _caseVoteIDTopSQL = "SELECT id,cid FROM blocked_case_vote WHERE mid = ? ORDER BY mtime DESC LIMIT 100"
  34. )
  35. // AddBlockedCases batch add blocked cases.
  36. func (d *Dao) AddBlockedCases(c context.Context, bc []*model.ArgJudgeCase) (err error) {
  37. l := len(bc)
  38. valueStrings := make([]string, 0, l)
  39. valueArgs := make([]interface{}, 0, l*13)
  40. for _, b := range bc {
  41. valueStrings = append(valueStrings, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  42. valueArgs = append(valueArgs, strconv.FormatInt(2, 10))
  43. valueArgs = append(valueArgs, strconv.FormatInt(b.MID, 10))
  44. valueArgs = append(valueArgs, b.Operator)
  45. valueArgs = append(valueArgs, b.OContent)
  46. valueArgs = append(valueArgs, strconv.FormatInt(int64(b.PunishResult), 10))
  47. valueArgs = append(valueArgs, b.OTitle)
  48. valueArgs = append(valueArgs, strconv.FormatInt(int64(b.OType), 10))
  49. valueArgs = append(valueArgs, b.OURL)
  50. valueArgs = append(valueArgs, strconv.FormatInt(int64(b.BlockedDays), 10))
  51. valueArgs = append(valueArgs, strconv.FormatInt(int64(b.ReasonType), 10))
  52. valueArgs = append(valueArgs, b.RelationID)
  53. valueArgs = append(valueArgs, strconv.FormatInt(b.OperID, 10))
  54. if b.BCTime != 0 {
  55. valueArgs = append(valueArgs, b.BCTime)
  56. } else {
  57. valueArgs = append(valueArgs, "1979-12-31 16:00:00")
  58. }
  59. }
  60. stmt := fmt.Sprintf(_addBlockedCasesSQL, strings.Join(valueStrings, ","))
  61. _, err = d.db.Exec(c, stmt, valueArgs...)
  62. if err != nil {
  63. log.Error("AddBlockedCases: db.Exec(bc(%+v)) error(%v)", bc, err)
  64. }
  65. return
  66. }
  67. // InsVote insert user vote.
  68. func (d *Dao) InsVote(c context.Context, mid int64, cid int64, t int64) (err error) {
  69. if _, err = d.db.Exec(c, _insertVoteSQL, mid, cid, time.Now().Add(time.Duration(t)*time.Minute)); err != nil {
  70. log.Error("InsVote: db.Exec(%d,%d) error(%v)", mid, cid, err)
  71. }
  72. return
  73. }
  74. // Setvote set user vote.
  75. func (d *Dao) Setvote(c context.Context, mid, cid, vote int64) (err error) {
  76. if _, err = d.db.Exec(c, _updateVoteSQL, mid, cid, vote, vote); err != nil {
  77. log.Error("Setvote: db.Exec(%d,%d,%d) error(%v)", mid, cid, vote, err)
  78. }
  79. return
  80. }
  81. // SetVoteTx set vote info by tx.
  82. func (d *Dao) SetVoteTx(tx *sql.Tx, mid, cid int64, vote int8) (affect int64, err error) {
  83. row, err := tx.Exec(_updateVoteSQL, mid, cid, vote, vote)
  84. if err != nil {
  85. log.Error("SetVoteTx err(%v)", err)
  86. return
  87. }
  88. return row.LastInsertId()
  89. }
  90. // AddCaseReasonApply add case reason apply log.
  91. func (d *Dao) AddCaseReasonApply(c context.Context, mid, cid int64, applyType, originReason, applyReason int8) (err error) {
  92. _, err = d.db.Exec(c, _inBlockedCaseApplyLogSQL, mid, cid, applyType, originReason, applyReason)
  93. if err != nil {
  94. log.Error("AddCaseReasonApply err(%v)", err)
  95. return
  96. }
  97. return
  98. }
  99. // AddCaseVoteTotal add case vote total.
  100. func (d *Dao) AddCaseVoteTotal(c context.Context, field string, cid int64, voteNum int8) (err error) {
  101. sql := fmt.Sprintf(_updateCaseVoteTotalSQL, field, field)
  102. if _, err = d.db.Exec(c, sql, voteNum, cid); err != nil {
  103. log.Error("AddCaseVoteTotal: db.Exec(%d) error(%v)", cid, err)
  104. }
  105. return
  106. }
  107. // CaseInfo jury get case info.
  108. func (d *Dao) CaseInfo(c context.Context, cid int64) (r *model.BlockedCase, err error) {
  109. row := d.db.QueryRow(c, _getCaseByIDSQL, cid)
  110. r = &model.BlockedCase{}
  111. if err = row.Scan(&r.ID, &r.MID, &r.Status, &r.OriginContent, &r.PunishResult, &r.OriginTitle, &r.OriginURL, &r.EndTime, &r.VoteRule, &r.VoteBreak, &r.VoteDelete, &r.OriginType, &r.ReasonType, &r.JudgeType, &r.BlockedDays, &r.PutTotal, &r.StartTime, &r.EndTime, &r.Operator, &r.CTime, &r.MTime, &r.RelationID, &r.CaseType); err != nil {
  112. if err == sql.ErrNoRows {
  113. err = nil
  114. r = nil
  115. return
  116. }
  117. }
  118. return
  119. }
  120. // CountCaseVote jury count case vote total.
  121. func (d *Dao) CountCaseVote(c context.Context, mid int64) (r int64, err error) {
  122. row := d.db.QueryRow(c, _countCaseVoteSQL, mid)
  123. if err = row.Scan(&r); err != nil {
  124. log.Error("row.Scan() error(%v)", err)
  125. }
  126. return
  127. }
  128. // IsVote jury user is vote.
  129. func (d *Dao) IsVote(c context.Context, mid int64, cid int64) (r int64, err error) {
  130. row := d.db.QueryRow(c, _isVoteByMIDSQL, mid, cid)
  131. if err = row.Scan(&r); err != nil {
  132. if err == sql.ErrNoRows {
  133. r = 0
  134. err = nil
  135. } else {
  136. log.Error("row.Scan() error(%v)", err)
  137. }
  138. }
  139. return
  140. }
  141. // VoteInfo jury user get vote info.
  142. func (d *Dao) VoteInfo(c context.Context, mid int64, cid int64) (r *model.VoteInfo, err error) {
  143. row := d.db.QueryRow(c, _getVoteInfoSQL, mid, cid)
  144. r = &model.VoteInfo{}
  145. if err = row.Scan(&r.ID, &r.CID, &r.MID, &r.Vote, &r.Expired, &r.Mtime); err != nil {
  146. if err == sql.ErrNoRows {
  147. err = nil
  148. r = nil
  149. }
  150. }
  151. return
  152. }
  153. // LoadVoteIDsMid load user vote case ids.
  154. func (d *Dao) LoadVoteIDsMid(c context.Context, mid int64, day int) (cases map[int64]*model.SimCase, err error) {
  155. now := time.Now()
  156. t := time.Date(now.Year(), now.Month(), now.Day()-day, 0, 0, 0, 0, now.Location())
  157. rows, err := d.db.Query(c, _loadMidVoteIDSQL, mid, t)
  158. if err != nil {
  159. log.Error("d.db.Query(%d %v) error(%v)", mid, t, err)
  160. return
  161. }
  162. defer rows.Close()
  163. cases = make(map[int64]*model.SimCase)
  164. for rows.Next() {
  165. mcase := &model.SimCase{}
  166. if err = rows.Scan(&mcase.ID, &mcase.CaseType); err != nil {
  167. log.Error("rows.Scan error(%v)", err)
  168. return
  169. }
  170. cases[mcase.ID] = mcase
  171. }
  172. return
  173. }
  174. // CaseVoteIDs get user's vote info by ids.
  175. func (d *Dao) CaseVoteIDs(c context.Context, ids []int64) (mbc map[int64]*model.BlockedCase, err error) {
  176. rows, err := d.db.Query(c, fmt.Sprintf(_getCaseByIDsSQL, xstr.JoinInts(ids)))
  177. if err != nil {
  178. err = errors.WithStack(err)
  179. return
  180. }
  181. defer rows.Close()
  182. mbc = make(map[int64]*model.BlockedCase, len(ids))
  183. for rows.Next() {
  184. r := new(model.BlockedCase)
  185. if err = rows.Scan(&r.ID, &r.MID, &r.Status, &r.OriginContent, &r.PunishResult, &r.OriginTitle, &r.OriginURL, &r.EndTime, &r.VoteRule, &r.VoteBreak, &r.VoteDelete, &r.OriginType, &r.ReasonType, &r.JudgeType, &r.BlockedDays, &r.PutTotal, &r.StartTime, &r.EndTime, &r.Operator, &r.CTime, &r.MTime, &r.RelationID, &r.CaseType); err != nil {
  186. if err == sql.ErrNoRows {
  187. mbc = nil
  188. err = nil
  189. return
  190. }
  191. err = errors.WithStack(err)
  192. return
  193. }
  194. mbc[r.ID] = r
  195. }
  196. err = rows.Err()
  197. return
  198. }
  199. // CaseRelationIDCount get case relation_id count.
  200. func (d *Dao) CaseRelationIDCount(c context.Context, tp int8, relationID string) (count int64, err error) {
  201. row := d.db.QueryRow(c, _caseRelationIDCountSQL, tp, relationID)
  202. if err = row.Scan(&count); err != nil {
  203. log.Error("d.caseRelationIDCount err(%v)", err)
  204. }
  205. return
  206. }
  207. // CaseInfoIDs get case info by ids.
  208. func (d *Dao) CaseInfoIDs(c context.Context, ids []int64) (cases map[int64]*model.BlockedCase, err error) {
  209. rows, err := d.db.Query(c, fmt.Sprintf(_caseInfoIDsSQL, xstr.JoinInts(ids)))
  210. if err != nil {
  211. log.Error("d.CaseInfoIDs err(%v)", err)
  212. return
  213. }
  214. defer rows.Close()
  215. cases = make(map[int64]*model.BlockedCase, len(ids))
  216. for rows.Next() {
  217. ca := &model.BlockedCase{}
  218. if err = rows.Scan(&ca.ID, &ca.MID, &ca.Status, &ca.OriginContent, &ca.PunishResult, &ca.OriginTitle, &ca.OriginURL, &ca.EndTime, &ca.VoteRule,
  219. &ca.VoteBreak, &ca.VoteDelete, &ca.OriginType, &ca.ReasonType, &ca.JudgeType, &ca.BlockedDays, &ca.PutTotal, &ca.StartTime, &ca.EndTime,
  220. &ca.Operator, &ca.CTime, &ca.MTime, &ca.RelationID, &ca.CaseType); err != nil {
  221. log.Error("row.Scan err(%v)", err)
  222. return
  223. }
  224. cases[ca.ID] = ca
  225. }
  226. return
  227. }
  228. // CaseVotesMID get user's vote case ids.
  229. func (d *Dao) CaseVotesMID(c context.Context, ids []int64) (mvo map[int64]*model.VoteInfo, err error) {
  230. rows, err := d.db.Query(c, fmt.Sprintf(_caseVotesMIDSQL, xstr.JoinInts(ids)))
  231. if err != nil {
  232. err = errors.WithStack(err)
  233. return
  234. }
  235. defer rows.Close()
  236. mvo = make(map[int64]*model.VoteInfo, len(ids))
  237. for rows.Next() {
  238. vo := new(model.VoteInfo)
  239. if err = rows.Scan(&vo.ID, &vo.CID, &vo.MID, &vo.Vote, &vo.Expired, &vo.Mtime); err != nil {
  240. if err == sql.ErrNoRows {
  241. mvo = nil
  242. err = nil
  243. return
  244. }
  245. err = errors.WithStack(err)
  246. return
  247. }
  248. mvo[vo.CID] = vo
  249. }
  250. err = rows.Err()
  251. return
  252. }
  253. // CaseVoteIDMID get user's vote case ids and cids.
  254. func (d *Dao) CaseVoteIDMID(c context.Context, mid, pn, ps int64) (vids []int64, cids []int64, err error) {
  255. rows, err := d.db.Query(c, _caseVoteIDMIDSQL, mid, (pn-1)*ps, ps)
  256. if err != nil {
  257. err = errors.WithStack(err)
  258. return
  259. }
  260. defer rows.Close()
  261. for rows.Next() {
  262. var vid, cid int64
  263. if err = rows.Scan(&vid, &cid); err != nil {
  264. if err == sql.ErrNoRows {
  265. vids = nil
  266. cids = nil
  267. err = nil
  268. return
  269. }
  270. err = errors.WithStack(err)
  271. return
  272. }
  273. vids = append(vids, vid)
  274. cids = append(cids, cid)
  275. }
  276. err = rows.Err()
  277. return
  278. }
  279. // CaseVoteIDTop get user's vote case ids and cids by top 100.
  280. func (d *Dao) CaseVoteIDTop(c context.Context, mid int64) (vids []int64, cids []int64, err error) {
  281. rows, err := d.db.Query(c, _caseVoteIDTopSQL, mid)
  282. if err != nil {
  283. err = errors.WithStack(err)
  284. return
  285. }
  286. defer rows.Close()
  287. for rows.Next() {
  288. var vid, cid int64
  289. if err = rows.Scan(&vid, &cid); err != nil {
  290. if err == sql.ErrNoRows {
  291. vids = nil
  292. cids = nil
  293. err = nil
  294. return
  295. }
  296. err = errors.WithStack(err)
  297. return
  298. }
  299. vids = append(vids, vid)
  300. cids = append(cids, cid)
  301. }
  302. err = rows.Err()
  303. return
  304. }