task_status.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _taskStatus = "SELECT status FROM task_status WHERE date=? AND type=?"
  10. _upTaskStatus = "UPDATE task_status SET status=? WHERE date=? AND type=?"
  11. _inTaskStatus = "INSERT INTO task_status(type, date, status, message) VALUES (%d, '%s', %d, '%s') ON DUPLICATE KEY UPDATE status=VALUES(status), message=VALUES(message)"
  12. )
  13. // TaskStatus get task status
  14. func (d *Dao) TaskStatus(c context.Context, date string, typ int) (status int, err error) {
  15. row := d.db.QueryRow(c, _taskStatus, date, typ)
  16. if err = row.Scan(&status); err != nil {
  17. if err == sql.ErrNoRows {
  18. err = nil
  19. } else {
  20. log.Error("dao.GetTaskStatus error(%v)", err)
  21. }
  22. }
  23. return
  24. }
  25. // UpdateTaskStatus update task status
  26. func (d *Dao) UpdateTaskStatus(c context.Context, date string, typ int, status int) (rows int64, err error) {
  27. res, err := d.db.Exec(c, _upTaskStatus, status, date, typ)
  28. if err != nil {
  29. return
  30. }
  31. return res.RowsAffected()
  32. }
  33. // InsertTaskStatus insert task status
  34. func (d *Dao) InsertTaskStatus(c context.Context, typ, status int, date, message string) (rows int64, err error) {
  35. res, err := d.db.Exec(c, fmt.Sprintf(_inTaskStatus, typ, date, status, message))
  36. if err != nil {
  37. return
  38. }
  39. return res.RowsAffected()
  40. }