task.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dao
  2. import (
  3. "go-common/library/log"
  4. "context"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "go-common/app/admin/main/laser/model"
  8. "go-common/library/database/sql"
  9. )
  10. const (
  11. _findMIDTaskSQL = " SELECT id, admin_id, username, mid, log_date, contact_email, source_type, platform, state, is_deleted, ctime, mtime FROM task WHERE mid = ? AND state = ? and is_deleted = 0"
  12. _queryTaskInfoByIDSQL = " SELECT mid, log_date, source_type, platform FROM task WHERE state = 0 AND is_deleted = 0 AND id = ? "
  13. _insertTaskSQL = " INSERT INTO task (mid, admin_id, username, log_date, contact_email, platform, source_type) VALUES (?, ?, ?, ?, ?, ?, ?) "
  14. _deleteTaskSQL = " UPDATE task SET is_deleted = 1 , username = ? , admin_id = ? WHERE id = ? AND is_deleted = 0 "
  15. _countTaskSQL = " SELECT count(*) FROM task WHERE %s "
  16. _queryTaskSQL = " SELECT id, admin_id, username, mid, log_date, contact_email, source_type, platform, state, is_deleted, ctime, mtime FROM task WHERE %s ORDER BY %s LIMIT %d,%d "
  17. _updateTaskSQL = " UPDATE task SET %s WHERE id = ? AND is_deleted = 0 AND state = ? "
  18. )
  19. // AddTask is add a unique task by mid and state(0).
  20. func (d *Dao) AddTask(ctx context.Context, mid int64, username string, adminID int64, logDate string, contactEmail string, platform int, sourceType int) (lastInsertID int64, err error) {
  21. res, err := d.laserDB.Exec(ctx, _insertTaskSQL, mid, adminID, username, logDate, contactEmail, platform, sourceType)
  22. if err != nil {
  23. log.Error("d.AddTask() error(%v)", err)
  24. }
  25. return res.LastInsertId()
  26. }
  27. // FindTask is find task by mid and state.
  28. func (d *Dao) FindTask(context context.Context, mid int64, state int) (t *model.Task, err error) {
  29. t = &model.Task{}
  30. row := d.laserDB.QueryRow(context, _findMIDTaskSQL, mid, state)
  31. if err = row.Scan(&t.ID, &t.AdminID, &t.Username, &t.MID, &t.LogDate, &t.ContactEmail, &t.SourceType, &t.Platform, &t.State, &t.IsDeleted, &t.CTime, &t.MTime); err != nil {
  32. if err == sql.ErrNoRows {
  33. t = nil
  34. err = nil
  35. return
  36. }
  37. log.Error("row.Scan() error(%v)", err)
  38. }
  39. return
  40. }
  41. // QueryTaskInfoByIDSQL is query task by task id.
  42. func (d *Dao) QueryTaskInfoByIDSQL(c context.Context, id int64) (t *model.TaskInfo, err error) {
  43. t = &model.TaskInfo{}
  44. row := d.laserDB.QueryRow(c, _queryTaskInfoByIDSQL, id)
  45. if err = row.Scan(&t.MID, &t.LogDate, &t.SourceType, &t.Platform); err != nil {
  46. if err == sql.ErrNoRows {
  47. t = nil
  48. err = nil
  49. } else {
  50. err = errors.WithStack(err)
  51. log.Error("row.Scan() error(%v)", err)
  52. }
  53. }
  54. return
  55. }
  56. // DeleteTask is delete task by TaskID.
  57. func (d *Dao) DeleteTask(ctx context.Context, taskID int64, username string, adminID int64) (err error) {
  58. _, err = d.laserDB.Exec(ctx, _deleteTaskSQL, username, adminID, taskID)
  59. if err != nil {
  60. log.Error("d.DeleteTask() error(%v)", err)
  61. }
  62. return
  63. }
  64. // UpdateTask is update undone task where state = 0.
  65. func (d *Dao) UpdateTask(ctx context.Context, taskID int64, state int, updateStmt string) (err error) {
  66. _, err = d.laserDB.Exec(ctx, fmt.Sprintf(_updateTaskSQL, updateStmt), taskID, state)
  67. if err != nil {
  68. log.Error("d.UpdateTask() error(%v)", err)
  69. }
  70. return
  71. }
  72. // QueryTask is query task by condition.
  73. func (d *Dao) QueryTask(ctx context.Context, queryStmt string, sort string, offset int, limit int) (tasks []*model.Task, count int64, err error) {
  74. row := d.laserDB.QueryRow(ctx, fmt.Sprintf(_countTaskSQL, queryStmt))
  75. if err = row.Scan(&count); err != nil {
  76. return
  77. }
  78. rows, err := d.laserDB.Query(ctx, fmt.Sprintf(_queryTaskSQL, queryStmt, sort, offset, limit))
  79. if err != nil {
  80. log.Error("d.Query() error(%v)", err)
  81. return
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. item := &model.Task{}
  86. if err = rows.Scan(&item.ID, &item.AdminID, &item.Username, &item.MID, &item.LogDate, &item.ContactEmail, &item.SourceType, &item.Platform, &item.State, &item.IsDeleted, &item.CTime, &item.MTime); err != nil {
  87. return
  88. }
  89. tasks = append(tasks, item)
  90. }
  91. return
  92. }