blocked.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/interface/main/credit/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. xtime "go-common/library/time"
  12. "go-common/library/xstr"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _addBlockedInfoSQL = `INSERT INTO blocked_info(uid,origin_title,blocked_remark,origin_url,origin_content,origin_content_modify,origin_type,
  17. punish_time,punish_type,blocked_days,publish_status,blocked_type,blocked_forever,reason_type,oper_id,moral_num,operator_name) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`
  18. _addBatchBlockedInfoSQL = `INSERT INTO blocked_info(uid,origin_title,blocked_remark,origin_url,origin_content,origin_content_modify,origin_type,
  19. punish_time,punish_type,blocked_days,publish_status,blocked_type,blocked_forever,reason_type,oper_id,moral_num,operator_name) VALUES %s`
  20. _blockedCountSQL = `SELECT COUNT(*) FROM blocked_info WHERE uid=? AND punish_type IN (2,3) AND status = 0`
  21. _blockedNumUserSQL = `SELECT COUNT(*) FROM blocked_info WHERE uid = ? AND status = 0`
  22. _blkHistoryCountSQL = "SELECT COUNT(*) FROM blocked_info WHERE uid = ? AND ctime >= ? AND status = 0"
  23. _blockedTotalSQL = "SELECT COUNT(*) AS num FROM blocked_info WHERE uid=? AND ctime >? AND status = 0"
  24. _blockedInfosByMidSQL = `SELECT id,case_id,uid,origin_title,origin_url,origin_content,origin_content_modify,origin_type,punish_time,punish_type,blocked_days,publish_status,blocked_type,
  25. reason_type,blocked_remark,ctime from blocked_info WHERE uid=? AND status = 0`
  26. _blockedListSQL = `SELECT id,origin_type,blocked_type,publish_time FROM blocked_info WHERE publish_status = 1 %s %s AND status = 0 ORDER BY publish_time desc`
  27. _blkHistorysSQL = `SELECT id,uid,blocked_days,blocked_forever,blocked_remark,moral_num,origin_content_modify,origin_title,origin_type,origin_url,punish_time,
  28. punish_type,reason_type FROM blocked_info WHERE uid = ? AND ctime >= ? AND status = 0 ORDER BY id LIMIT ?,?`
  29. _blockedInfoIDSQL = `SELECT id,uid,uname,origin_content,origin_content_modify,origin_type,punish_time,punish_type,moral_num,blocked_days,reason_type,blocked_forever,origin_title,
  30. origin_url,blocked_type,blocked_remark,case_id,ctime,publish_status from blocked_info WHERE id=? AND status = 0`
  31. _blockedInfoIDsSQL = `SELECT id,uid,blocked_days,blocked_forever,blocked_remark,moral_num,origin_content_modify,origin_title,origin_type,origin_url,punish_time,
  32. punish_type,reason_type FROM blocked_info WHERE id IN (%s) AND status = 0`
  33. _blockedInfosSQL = `SELECT id,uid,uname,origin_content_modify,origin_type,punish_time,punish_type,moral_num,blocked_days,reason_type,blocked_forever,origin_title,
  34. origin_url,blocked_type,blocked_remark,case_id,ctime,publish_status FROM blocked_info WHERE id IN (%s) AND publish_status = 1 AND status = 0 ORDER BY publish_time desc`
  35. )
  36. // AddBlockedInfo add blocked info
  37. func (d *Dao) AddBlockedInfo(c context.Context, r *model.BlockedInfo) (err error) {
  38. if _, err = d.db.Exec(c, _addBlockedInfoSQL, r.UID, r.OriginTitle, r.BlockedRemark, r.OriginURL, r.OriginContent, r.OriginContent,
  39. r.OriginType, r.PunishTime.Time(), r.PunishType, r.BlockedDays, r.PublishStatus, r.BlockedType, r.BlockedForever,
  40. r.ReasonType, r.OID, r.MoralNum, r.OperatorName); err != nil {
  41. err = errors.Wrap(err, "AddBlockedInfo")
  42. }
  43. return
  44. }
  45. // TxAddBlockedInfo add blocked info
  46. func (d *Dao) TxAddBlockedInfo(tx *sql.Tx, rs []*model.BlockedInfo) (err error) {
  47. l := len(rs)
  48. valueStrings := make([]string, 0, l)
  49. valueArgs := make([]interface{}, 0, l*17)
  50. for _, v := range rs {
  51. valueStrings = append(valueStrings, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  52. valueArgs = append(valueArgs, strconv.FormatInt(v.UID, 10))
  53. valueArgs = append(valueArgs, v.OriginTitle)
  54. valueArgs = append(valueArgs, v.BlockedRemark)
  55. valueArgs = append(valueArgs, v.OriginURL)
  56. valueArgs = append(valueArgs, v.OriginContent)
  57. valueArgs = append(valueArgs, v.OriginContent)
  58. valueArgs = append(valueArgs, strconv.FormatInt(v.OriginType, 10))
  59. valueArgs = append(valueArgs, v.PunishTime.Time())
  60. valueArgs = append(valueArgs, strconv.FormatInt(v.PunishType, 10))
  61. valueArgs = append(valueArgs, strconv.FormatInt(v.BlockedDays, 10))
  62. valueArgs = append(valueArgs, strconv.FormatInt(v.PublishStatus, 10))
  63. valueArgs = append(valueArgs, strconv.FormatInt(v.BlockedType, 10))
  64. valueArgs = append(valueArgs, strconv.FormatInt(v.BlockedForever, 10))
  65. valueArgs = append(valueArgs, strconv.FormatInt(v.ReasonType, 10))
  66. valueArgs = append(valueArgs, strconv.FormatInt(v.OID, 10))
  67. valueArgs = append(valueArgs, strconv.FormatInt(v.MoralNum, 10))
  68. valueArgs = append(valueArgs, v.OperatorName)
  69. }
  70. stmt := fmt.Sprintf(_addBatchBlockedInfoSQL, strings.Join(valueStrings, ","))
  71. _, err = tx.Exec(stmt, valueArgs...)
  72. if err != nil {
  73. err = errors.Wrapf(err, "TxAddBlockedInfo tx.Exec() error(%+v)", err)
  74. }
  75. return
  76. }
  77. // BlockedCount get user blocked count.
  78. func (d *Dao) BlockedCount(c context.Context, mid int64) (count int, err error) {
  79. row := d.db.QueryRow(c, _blockedCountSQL, mid)
  80. if err = row.Scan(&count); err != nil {
  81. err = errors.Wrap(err, "BlockedCount scan fail")
  82. }
  83. return
  84. }
  85. // BlockedNumUser get blocked user number.
  86. func (d *Dao) BlockedNumUser(c context.Context, mid int64) (count int, err error) {
  87. row := d.db.QueryRow(c, _blockedNumUserSQL, mid)
  88. if err = row.Scan(&count); err != nil {
  89. if err == sql.ErrNoRows {
  90. err = nil
  91. return
  92. }
  93. err = errors.Wrap(err, "BlockedNumUser")
  94. }
  95. return
  96. }
  97. // BLKHistoryCount get blocked historys count.
  98. func (d *Dao) BLKHistoryCount(c context.Context, ArgHis *model.ArgHistory) (count int64, err error) {
  99. row := d.db.QueryRow(c, _blkHistoryCountSQL, ArgHis.MID, xtime.Time(ArgHis.STime))
  100. if err = row.Scan(&count); err != nil {
  101. if err == sql.ErrNoRows {
  102. err = nil
  103. return
  104. }
  105. err = errors.Wrap(err, "BLKHistoryCount")
  106. }
  107. return
  108. }
  109. // BlockTotalTime get block total by time.
  110. func (d *Dao) BlockTotalTime(c context.Context, mid int64, ts time.Time) (total int64, err error) {
  111. row := d.db.QueryRow(c, _blockedTotalSQL, mid, ts)
  112. if err = row.Scan(&total); err != nil {
  113. if err != sql.ErrNoRows {
  114. log.Error("row.Scan() error(%v)", err)
  115. return
  116. }
  117. err = nil
  118. total = 0
  119. }
  120. return
  121. }
  122. // BlockedUserList get user blocked list.
  123. func (d *Dao) BlockedUserList(c context.Context, mid int64) (res []*model.BlockedInfo, err error) {
  124. rows, err := d.db.Query(c, _blockedInfosByMidSQL, mid)
  125. if err != nil {
  126. log.Error("d.getBlockedInfosByMidStmt.Query(mid %d) error(%v)", mid, err)
  127. err = errors.Wrap(err, "BlockedUserList")
  128. return
  129. }
  130. defer rows.Close()
  131. for rows.Next() {
  132. r := &model.BlockedInfo{}
  133. if err = rows.Scan(&r.ID, &r.CaseID, &r.UID, &r.OriginTitle, &r.OriginURL, &r.OriginContent, &r.OriginContentModify, &r.OriginType, &r.PunishTime, &r.PunishType,
  134. &r.BlockedDays, &r.PublishStatus, &r.BlockedType, &r.ReasonType, &r.BlockedRemark, &r.CTime); err != nil {
  135. if err == sql.ErrNoRows {
  136. err = nil
  137. return
  138. }
  139. }
  140. res = append(res, r)
  141. }
  142. err = rows.Err()
  143. return
  144. }
  145. // BlockedList get blocked list.
  146. func (d *Dao) BlockedList(c context.Context, otype, btype int8) (res []*model.BlockedInfo, err error) {
  147. var ostr, bstr string
  148. if otype != 0 {
  149. ostr = fmt.Sprintf("AND origin_type=%d ", otype)
  150. }
  151. if btype >= 0 {
  152. bstr = fmt.Sprintf("AND blocked_type=%d ", btype)
  153. }
  154. rows, err := d.db.Query(c, fmt.Sprintf(_blockedListSQL, ostr, bstr))
  155. if err != nil {
  156. err = errors.Wrap(err, "BlockedInfos")
  157. return
  158. }
  159. defer rows.Close()
  160. for rows.Next() {
  161. r := &model.BlockedInfo{}
  162. if err = rows.Scan(&r.ID, &r.OriginType, &r.BlockedType, &r.PublishTime); err != nil {
  163. if err == sql.ErrNoRows {
  164. err = nil
  165. return
  166. }
  167. }
  168. res = append(res, r)
  169. }
  170. err = rows.Err()
  171. return
  172. }
  173. // BLKHistorys get blocked historys list.
  174. func (d *Dao) BLKHistorys(c context.Context, ah *model.ArgHistory) (res []*model.BlockedInfo, err error) {
  175. rows, err := d.db.Query(c, _blkHistorysSQL, ah.MID, xtime.Time(ah.STime), (ah.PN-1)*ah.PS, ah.PS)
  176. if err != nil {
  177. err = errors.Wrap(err, "BLKHistorys")
  178. return
  179. }
  180. defer rows.Close()
  181. for rows.Next() {
  182. bi := new(model.BlockedInfo)
  183. if err = rows.Scan(&bi.ID, &bi.UID, &bi.BlockedDays, &bi.BlockedForever, &bi.BlockedRemark, &bi.MoralNum, &bi.OriginContentModify, &bi.OriginTitle,
  184. &bi.OriginType, &bi.OriginURL, &bi.PunishTime, &bi.PunishType, &bi.ReasonType); err != nil {
  185. if err == sql.ErrNoRows {
  186. err = nil
  187. return
  188. }
  189. err = errors.Wrap(err, "BLKHistorys")
  190. return
  191. }
  192. res = append(res, bi)
  193. }
  194. err = rows.Err()
  195. return
  196. }
  197. // BlockedInfoByID get blocked info by id.
  198. func (d *Dao) BlockedInfoByID(c context.Context, id int64) (r *model.BlockedInfo, err error) {
  199. row := d.db.QueryRow(c, _blockedInfoIDSQL, id)
  200. r = new(model.BlockedInfo)
  201. if err = row.Scan(&r.ID, &r.UID, &r.Uname, &r.OriginContent, &r.OriginContentModify, &r.OriginType, &r.PunishTime, &r.PunishType, &r.MoralNum,
  202. &r.BlockedDays, &r.ReasonType, &r.BlockedForever, &r.OriginTitle, &r.OriginURL, &r.BlockedType, &r.BlockedRemark, &r.CaseID, &r.CTime, &r.PublishStatus); err != nil {
  203. if err == sql.ErrNoRows {
  204. err = nil
  205. r = nil
  206. return
  207. }
  208. err = errors.Wrap(err, "BlockedInfoByID")
  209. }
  210. return
  211. }
  212. // BlockedInfoIDs get blocked info by ids
  213. func (d *Dao) BlockedInfoIDs(c context.Context, ids []int64) (res map[int64]*model.BlockedInfo, err error) {
  214. rows, err := d.db.Query(c, fmt.Sprintf(_blockedInfoIDsSQL, xstr.JoinInts(ids)))
  215. if err != nil {
  216. err = errors.Wrap(err, "BlockedInfoIDs")
  217. return
  218. }
  219. defer rows.Close()
  220. res = make(map[int64]*model.BlockedInfo, len(ids))
  221. for rows.Next() {
  222. bi := new(model.BlockedInfo)
  223. if err = rows.Scan(&bi.ID, &bi.UID, &bi.BlockedDays, &bi.BlockedForever, &bi.BlockedRemark, &bi.MoralNum, &bi.OriginContentModify, &bi.OriginTitle,
  224. &bi.OriginType, &bi.OriginURL, &bi.PunishTime, &bi.PunishType, &bi.ReasonType); err != nil {
  225. if err == sql.ErrNoRows {
  226. err = nil
  227. return
  228. }
  229. err = errors.Wrap(err, "BlockedInfoIDs")
  230. return
  231. }
  232. res[bi.ID] = bi
  233. }
  234. err = rows.Err()
  235. return
  236. }
  237. // BlockedInfos get blocked infos. Queryed without mid or id, public default.
  238. func (d *Dao) BlockedInfos(c context.Context, ids []int64) (res []*model.BlockedInfo, err error) {
  239. rows, err := d.db.Query(c, fmt.Sprintf(_blockedInfosSQL, xstr.JoinInts(ids)))
  240. if err != nil {
  241. err = errors.Wrap(err, "BlockedInfos")
  242. return
  243. }
  244. defer rows.Close()
  245. for rows.Next() {
  246. r := &model.BlockedInfo{}
  247. if err = rows.Scan(&r.ID, &r.UID, &r.Uname, &r.OriginContentModify, &r.OriginType, &r.PunishTime, &r.PunishType, &r.MoralNum,
  248. &r.BlockedDays, &r.ReasonType, &r.BlockedForever, &r.OriginTitle, &r.OriginURL, &r.BlockedType, &r.BlockedRemark, &r.CaseID, &r.CTime, &r.PublishStatus); err != nil {
  249. log.Error("BlockedInfos err %v", err)
  250. return
  251. }
  252. res = append(res, r)
  253. }
  254. err = rows.Err()
  255. return
  256. }