task_consumer.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package archive
  2. import (
  3. "context"
  4. "go-common/app/admin/main/videoup/model/archive"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _taskUserCheckInSQL = "INSERT INTO task_consumer (uid,state) VALUES (?,1) ON DUPLICATE KEY UPDATE state = 1"
  10. _taskUserCheckOffSQL = "UPDATE task_consumer SET state = 0 WHERE uid=?"
  11. _consumersSQL = "SELECT id,uid,state,ctime,mtime FROM task_consumer where state=1"
  12. _isConsumerOnSQL = "SELECT state FROM task_consumer WHERE uid=?"
  13. )
  14. // TaskUserCheckIn insert or update task consumer check state
  15. func (d *Dao) TaskUserCheckIn(c context.Context, uid int64) (rows int64, err error) {
  16. res, err := d.db.Exec(c, _taskUserCheckInSQL, uid)
  17. if err != nil {
  18. log.Error("tx.Exec(%s, %d) error(%v)", _taskUserCheckInSQL, uid, err)
  19. return
  20. }
  21. return res.RowsAffected()
  22. }
  23. // TaskUserCheckOff update task consumer check state
  24. func (d *Dao) TaskUserCheckOff(c context.Context, uid int64) (rows int64, err error) {
  25. res, err := d.db.Exec(c, _taskUserCheckOffSQL, uid)
  26. if err != nil {
  27. log.Error("tx.Exec(%s, %d) error(%v)", _taskUserCheckOffSQL, uid, err)
  28. return
  29. }
  30. return res.RowsAffected()
  31. }
  32. // Consumers 用户列表
  33. func (d *Dao) Consumers(c context.Context) (cms []*archive.Consumers, err error) {
  34. rows, err := d.rddb.Query(c, _consumersSQL)
  35. if err != nil {
  36. log.Error("d.rddb.Query(%s) error(%v)", _consumersSQL, err)
  37. return
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. cm := new(archive.Consumers)
  42. err = rows.Scan(&cm.ID, &cm.UID, &cm.State, &cm.Ctime, &cm.Mtime)
  43. if err != nil {
  44. log.Error("rows.Scan error(%v)", err)
  45. return
  46. }
  47. cms = append(cms, cm)
  48. }
  49. return
  50. }
  51. // IsConsumerOn 判断是否登入
  52. func (d *Dao) IsConsumerOn(c context.Context, uid int64) (state int8) {
  53. err := d.rddb.QueryRow(c, _isConsumerOnSQL, uid).Scan(&state)
  54. if err != nil {
  55. if err != sql.ErrNoRows {
  56. log.Error("d.rddb.QueryRow error(%v)", err)
  57. }
  58. }
  59. return
  60. }