mysql_task.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "go-common/app/admin/main/dm/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _countDMTask = "SELECT count(*) FROM dm_task"
  14. _selectDMTask = "SELECT id,title,creator,reviewer,state,result,ctime,mtime FROM dm_task%s order by ctime limit ?,? "
  15. _insertDMTask = "INSERT INTO dm_task(title,creator,regex,keywords,ips,mids,cids,start,end,state,sub) VALUES(?,?,?,?,?,?,?,?,?,?,?)"
  16. _insertDMSubTask = "INSERT INTO dm_sub_task(task_id,operation,start,rate) VALUES(?,?,?,?)"
  17. _updateDMTaskState = "UPDATE dm_task SET state=? WHERE id IN (%s) AND state!=?"
  18. _reviewDmTask = "UPDATE dm_task SET state=?,reviewer=?,topic=? WHERE id=? AND state=0"
  19. _selectTaskByID = "SELECT id,title,creator,reviewer,regex,keywords,ips,mids,cids,start,end,qcount,state,result,ctime,mtime FROM dm_task WHERE id=?"
  20. _selectSubTask = "SELECT id,operation,rate,tcount,start,end FROM dm_sub_task WHERE task_id=?"
  21. _editTaskPriority = "UPDATE dm_task SET priority=? WHERE id IN (%s)"
  22. )
  23. // TaskList dm task list
  24. func (d *Dao) TaskList(c context.Context, taskSQL []string, pn, ps int64) (tasks []*model.TaskInfo, total int64, err error) {
  25. var sql string
  26. tasks = make([]*model.TaskInfo, 0)
  27. if len(taskSQL) > 0 {
  28. sql = fmt.Sprintf(" WHERE %s", strings.Join(taskSQL, " AND "))
  29. }
  30. countRow := d.biliDM.QueryRow(c, _countDMTask+sql)
  31. if err = countRow.Scan(&total); err != nil {
  32. log.Error("row.ScanCount(%s) error(%v)", _countDMTask+sql, err)
  33. return
  34. }
  35. rows, err := d.biliDM.Query(c, fmt.Sprintf(_selectDMTask, sql), (pn-1)*ps, ps)
  36. if err != nil {
  37. log.Error("biliDM.Query(%s) error(%v)", fmt.Sprintf(_selectDMTask, sql), err)
  38. return
  39. }
  40. defer rows.Close()
  41. for rows.Next() {
  42. task := &model.TaskInfo{}
  43. var cTime, mTime time.Time
  44. if err = rows.Scan(&task.ID, &task.Title, &task.Creator, &task.Reviewer, &task.State, &task.Result, &cTime, &mTime); err != nil {
  45. log.Error("biliDM.Scan(%s) error(%v)", fmt.Sprintf(_selectDMTask, sql), err)
  46. return
  47. }
  48. task.Ctime = cTime.Format("2006-01-02 15:04:05")
  49. task.Mtime = mTime.Format("2006-01-02 15:04:05")
  50. tasks = append(tasks, task)
  51. }
  52. if err = rows.Err(); err != nil {
  53. log.Error("biliDM.rows.Err() error(%v)", err)
  54. }
  55. return
  56. }
  57. // AddTask add dm task
  58. func (d *Dao) AddTask(tx *sql.Tx, v *model.AddTaskArg, sub int32) (taskID int64, err error) {
  59. var sTime, eTime time.Time
  60. if sTime, err = time.ParseInLocation("2006-01-02 15:04:05", v.Start, time.Local); err != nil {
  61. log.Error("d.AddTask time.Parse(%s) error(%v)", v.Start, err)
  62. return
  63. }
  64. if eTime, err = time.ParseInLocation("2006-01-02 15:04:05", v.End, time.Local); err != nil {
  65. log.Error("d.AddTask time.Parse(%s) error(%v)", v.End, err)
  66. return
  67. }
  68. // regex add slash
  69. rows, err := tx.Exec(_insertDMTask, v.Title, v.Creator, v.Regex, v.KeyWords, v.IPs, v.Mids, v.Cids, sTime, eTime, v.State, sub)
  70. if err != nil {
  71. log.Error("tx.Exec(%s params:%+v) error(%v)", _insertDMTask, v, err)
  72. return
  73. }
  74. return rows.LastInsertId()
  75. }
  76. // AddSubTask add dm sub task
  77. func (d *Dao) AddSubTask(tx *sql.Tx, taskID int64, operation int32, start string, rate int32) (id int64, err error) {
  78. sTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, time.Local)
  79. if err != nil {
  80. log.Error("d.AddSubTask time.Parse(%s) error(%v)", start, err)
  81. return
  82. }
  83. rows, err := tx.Exec(_insertDMSubTask, taskID, operation, sTime, rate)
  84. if err != nil {
  85. log.Error("tx.Exec(%s,%d,%d,%s,%d) error(%v)", _insertDMSubTask, taskID, operation, start, rate, err)
  86. return
  87. }
  88. return rows.LastInsertId()
  89. }
  90. // EditTaskState .
  91. func (d *Dao) EditTaskState(c context.Context, v *model.EditTasksStateArg) (affected int64, err error) {
  92. updateSQL := fmt.Sprintf(_updateDMTaskState, v.IDs)
  93. rows, err := d.biliDM.Exec(c, updateSQL, v.State, v.State)
  94. if err != nil {
  95. log.Error("d.EditTaskState.Exec(id:%s, state:%d) error(%v)", v.IDs, v.State, err)
  96. return
  97. }
  98. return rows.RowsAffected()
  99. }
  100. // EditTaskPriority .
  101. func (d *Dao) EditTaskPriority(c context.Context, ids string, priority int64) (affected int64, err error) {
  102. updateSQL := fmt.Sprintf(_editTaskPriority, ids)
  103. rows, err := d.biliDM.Exec(c, updateSQL, priority)
  104. if err != nil {
  105. log.Error("d.EditTaskPriority.Exec(ids:%s, priority:%d) error(%v)", ids, priority, err)
  106. return
  107. }
  108. return rows.RowsAffected()
  109. }
  110. // ReviewTask .
  111. func (d *Dao) ReviewTask(c context.Context, v *model.ReviewTaskArg) (affected int64, err error) {
  112. row, err := d.biliDM.Exec(c, _reviewDmTask, v.State, v.Reviewer, v.Topic, v.ID)
  113. if err != nil {
  114. log.Error("d.ReviewTask.Exec(id:%d, state:%d) error(%v)", v.ID, v.State, err)
  115. return
  116. }
  117. return row.RowsAffected()
  118. }
  119. // TaskView .
  120. func (d *Dao) TaskView(c context.Context, id int64) (task *model.TaskView, err error) {
  121. task = new(model.TaskView)
  122. row := d.biliDM.QueryRow(c, _selectTaskByID, id)
  123. var sTime, eTime, cTime, mTime time.Time
  124. if err = row.Scan(&task.ID, &task.Title, &task.Creator, &task.Reviewer, &task.Regex, &task.KeyWords, &task.IPs, &task.Mids, &task.Cids, &sTime, &eTime, &task.QCount, &task.State, &task.Result, &cTime, &mTime); err != nil {
  125. if err == sql.ErrNoRows {
  126. err = ecode.NothingFound
  127. }
  128. log.Error("biliDM.Scan(%s, id:%d) error(%v)", _selectTaskByID, id, err)
  129. return
  130. }
  131. task.Start = sTime.Format("2006-01-02 15:04:05")
  132. task.End = eTime.Format("2006-01-02 15:04:05")
  133. task.Ctime = cTime.Format("2006-01-02 15:04:05")
  134. task.Mtime = mTime.Format("2006-01-02 15:04:05")
  135. return
  136. }
  137. // SubTask .
  138. func (d *Dao) SubTask(c context.Context, id int64) (subTask *model.SubTask, err error) {
  139. // TODO: operation time
  140. subTask = new(model.SubTask)
  141. row := d.biliDM.QueryRow(c, _selectSubTask, id)
  142. var sTime time.Time
  143. var eTime time.Time
  144. if err = row.Scan(&subTask.ID, &subTask.Operation, &subTask.Rate, &subTask.Tcount, &sTime, &eTime); err != nil {
  145. if err == sql.ErrNoRows {
  146. err = nil
  147. subTask = nil
  148. }
  149. log.Error("biliDM.Scan(%s, taskID:%d) error*(%v)", _selectSubTask, id, err)
  150. return
  151. }
  152. subTask.Start = sTime.Format("2006-01-02 15:04:05")
  153. subTask.End = eTime.Format("2006-01-02 15:04:05")
  154. return
  155. }