mysql_protect.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "time"
  8. "go-common/app/interface/main/dm/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. // dm protect apply
  15. _addProtect = "INSERT INTO dm_protect_apply (cid, uid, apply_uid, aid, playtime, dmid, msg, status, ctime, mtime) VALUES %s"
  16. _selProtect = "SELECT ctime FROM dm_protect_apply WHERE dmid=? ORDER BY id DESC"
  17. _getPrttApply = "SELECT id,cid,apply_uid,aid,playtime,msg,ctime from dm_protect_apply where uid=? %s and status=-1"
  18. _getProtectAids = "SELECT aid FROM dm_protect_apply WHERE uid=? GROUP BY aid"
  19. _paStatus = "UPDATE dm_protect_apply SET status=%d WHERE uid=%d AND id IN (%s)"
  20. _chgPaSwitch = "INSERT INTO dm_protect_up (uid, notice_switch) VALUES(?, ?) ON DUPLICATE KEY UPDATE notice_switch=?"
  21. _getProtectByIDs = "SELECT cid,dmid FROM dm_protect_apply WHERE uid=%d AND id IN (%s)"
  22. _paUsrStat = "SELECT aid,apply_uid,status,ctime from dm_protect_apply where ctime>?"
  23. _paStatistics = "SELECT uid from dm_protect_apply where status=-1 GROUP BY uid"
  24. _paNoticeSwitch = "SELECT uid FROM dm_protect_up WHERE uid IN (%s) AND notice_switch=0"
  25. )
  26. // AddProtectApply 添加保护弹幕申请
  27. func (d *Dao) AddProtectApply(c context.Context, pas []*model.Pa) (affect int64, err error) {
  28. var (
  29. values, s string
  30. )
  31. for _, pa := range pas {
  32. pa.Msg = strings.Replace(pa.Msg, `\`, `\\`, -1)
  33. pa.Msg = strings.Replace(pa.Msg, `'`, `\'`, -1)
  34. values += fmt.Sprintf(`(%d, %d, %d, %d, %f, %d, '%s', %d, '%s', '%s'),`, pa.CID, pa.UID, pa.ApplyUID, pa.AID, pa.Playtime, pa.DMID, pa.Msg, pa.Status, pa.Ctime.Format("2006-01-02 15:04:05"), pa.Mtime.Format("2006-01-02 15:04:05"))
  35. }
  36. if len(values) > 0 {
  37. values = values[0:(len(values) - 1)]
  38. }
  39. s = fmt.Sprintf(_addProtect, values)
  40. res, err := d.biliDM.Exec(c, s)
  41. if err != nil {
  42. log.Error("d.biliDM.Exec(%v) error(%v)", s, err)
  43. return
  44. }
  45. affect, err = res.RowsAffected()
  46. return
  47. }
  48. // ProtectApplyTime 根据dmid获取保护弹幕申请
  49. func (d *Dao) ProtectApplyTime(c context.Context, dmid int64) (t time.Time, err error) {
  50. row := d.biliDM.QueryRow(c, _selProtect, dmid)
  51. if err = row.Scan(&t); err != nil {
  52. if err == sql.ErrNoRows {
  53. err = nil
  54. } else {
  55. log.Error("row.Scan error(%v)", err)
  56. }
  57. }
  58. return
  59. }
  60. // ProtectApplies 保护弹幕申请列表
  61. func (d *Dao) ProtectApplies(c context.Context, uid, aid int64, order string) (res []*model.Apply, err error) {
  62. var (
  63. query string
  64. rows *sql.Rows
  65. )
  66. if aid > 0 {
  67. query = fmt.Sprintf(_getPrttApply, "AND aid=?")
  68. rows, err = d.biliDM.Query(c, query, uid, aid)
  69. } else {
  70. query = fmt.Sprintf(_getPrttApply, "")
  71. rows, err = d.biliDM.Query(c, query, uid)
  72. }
  73. if err != nil {
  74. log.Error("d.biliDM.Query(%s,%v,%v) error(%v)", query, uid, aid, err)
  75. return
  76. }
  77. defer rows.Close()
  78. for rows.Next() {
  79. r := &model.Apply{}
  80. t := time.Time{}
  81. if err = rows.Scan(&r.ID, &r.CID, &r.ApplyUID, &r.AID, &r.Playtime, &r.Msg, &t); err != nil {
  82. log.Error("rows.Scan error(%v)", err)
  83. return
  84. }
  85. r.Ctime = t.Format("2006-01-02 15:04:05")
  86. res = append(res, r)
  87. }
  88. if err = rows.Err(); err != nil {
  89. log.Error("rows.Err() error(%v)", err)
  90. return
  91. }
  92. if order == "playtime" {
  93. sort.Sort(model.ApplySortPlaytime(res))
  94. } else {
  95. sort.Sort(model.ApplySortID(res))
  96. }
  97. return
  98. }
  99. // ProtectAids 被申请保护弹幕稿件列表
  100. func (d *Dao) ProtectAids(c context.Context, uid int64) (res []int64, err error) {
  101. rows, err := d.biliDM.Query(c, _getProtectAids, uid)
  102. if err != nil {
  103. log.Error("d.biliDM.Query(%s,%d) error(%v)", _getProtectAids, uid, err)
  104. return
  105. }
  106. defer rows.Close()
  107. for rows.Next() {
  108. var aid int64
  109. if err = rows.Scan(&aid); err != nil {
  110. log.Error("rows.Scan error(%v)", err)
  111. return
  112. }
  113. res = append(res, aid)
  114. }
  115. if err = rows.Err(); err != nil {
  116. log.Error("rows.Err() error(%v)", err)
  117. }
  118. return
  119. }
  120. // UptPaStatus 修改保护弹幕状态
  121. func (d *Dao) UptPaStatus(c context.Context, uid int64, ids string, status int) (affect int64, err error) {
  122. s := fmt.Sprintf(_paStatus, status, uid, ids)
  123. res, err := d.biliDM.Exec(c, s)
  124. if err != nil {
  125. log.Error("d.biliDM.Exec(%v) error(%v)", s, err)
  126. }
  127. affect, err = res.RowsAffected()
  128. return
  129. }
  130. // ProtectApplyByIDs get protect apply by dmid
  131. func (d *Dao) ProtectApplyByIDs(c context.Context, uid int64, ids string) (res map[int64][]int64, err error) {
  132. res = make(map[int64][]int64, 10)
  133. s := fmt.Sprintf(_getProtectByIDs, uid, ids)
  134. rows, err := d.biliDM.Query(c, s)
  135. if err != nil {
  136. log.Error("d.biliDM.Query(%s) error(%v)", s, err)
  137. return
  138. }
  139. defer rows.Close()
  140. for rows.Next() {
  141. var (
  142. dmid int64
  143. cid int64
  144. )
  145. if err = rows.Scan(&cid, &dmid); err != nil {
  146. log.Error("rows.Scan error(%v)", err)
  147. return
  148. }
  149. _, ok := res[cid]
  150. if ok {
  151. res[cid] = append(res[cid], dmid)
  152. } else {
  153. res[cid] = []int64{dmid}
  154. }
  155. }
  156. if err = rows.Err(); err != nil {
  157. log.Error("rows.Err() error(%v)", err)
  158. }
  159. return
  160. }
  161. // UptPaNoticeSwitch 设置申请保护弹幕站内通知开关
  162. func (d *Dao) UptPaNoticeSwitch(c context.Context, uid int64, status int) (affect int64, err error) {
  163. res, err := d.biliDM.Exec(c, _chgPaSwitch, uid, status, status)
  164. if err != nil {
  165. log.Error("d.biliDM.Exec(%s,%d,%d) error(%v)", _chgPaSwitch, status, uid, err)
  166. }
  167. affect, err = res.RowsAffected()
  168. return
  169. }
  170. // PaNoticeClose 获取关闭申请保护弹幕站内通知
  171. func (d *Dao) PaNoticeClose(c context.Context, uids []int64) (res map[int64]bool, err error) {
  172. if len(uids) < 1 {
  173. return
  174. }
  175. res = make(map[int64]bool, len(uids))
  176. s := fmt.Sprintf(_paNoticeSwitch, xstr.JoinInts(uids))
  177. rows, err := d.biliDM.Query(c, s)
  178. if err != nil {
  179. log.Error("d.biliDM.Query(%s) error(%v)", s, err)
  180. return
  181. }
  182. defer rows.Close()
  183. for rows.Next() {
  184. var uid int64
  185. if err = rows.Scan(&uid); err != nil {
  186. log.Error("rows.Scan error(%v)", err)
  187. return
  188. }
  189. res[uid] = true
  190. }
  191. if err = rows.Err(); err != nil {
  192. log.Error("rows.Err() error(%v)", err)
  193. }
  194. return
  195. }
  196. // ProtectApplyStatistics 保护弹幕申请up统计
  197. func (d *Dao) ProtectApplyStatistics(c context.Context) (res []int64, err error) {
  198. rows, err := d.biliDM.Query(c, _paStatistics)
  199. if err != nil {
  200. log.Error("d.biliDM.Query(%s) error(%v)", _paStatistics, err)
  201. return
  202. }
  203. defer rows.Close()
  204. for rows.Next() {
  205. var uid int64
  206. if err = rows.Scan(&uid); err != nil {
  207. log.Error("rows.Scan error(%v)", err)
  208. return
  209. }
  210. res = append(res, uid)
  211. }
  212. if err = rows.Err(); err != nil {
  213. log.Error("rows.Err() error(%v)", err)
  214. }
  215. return
  216. }
  217. // twoDayAgo22 两天前的22点
  218. func twoDayAgo22() string {
  219. yesterday := time.Now().Add(-48 * time.Hour)
  220. year, month, day := yesterday.Date()
  221. return time.Date(year, month, day, 0, 0, 0, 0, time.Local).Format("2006-01-02") + " 22:00:00"
  222. }
  223. // PaUsrStat 保护弹幕申请用户统计
  224. func (d *Dao) PaUsrStat(c context.Context) (res []*model.ApplyUserStat, err error) {
  225. rows, err := d.biliDM.Query(c, _paUsrStat, twoDayAgo22())
  226. if err != nil {
  227. log.Error("d.biliDM.Query(%s) error(%v)", _paUsrStat, err)
  228. return
  229. }
  230. defer rows.Close()
  231. for rows.Next() {
  232. r := &model.ApplyUserStat{}
  233. if err = rows.Scan(&r.Aid, &r.UID, &r.Status, &r.Ctime); err != nil {
  234. log.Error("rows.Scan error(%v)", err)
  235. return
  236. }
  237. res = append(res, r)
  238. }
  239. if err = rows.Err(); err != nil {
  240. log.Error("rows.Err() error(%v)", err)
  241. }
  242. return
  243. }