task.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "context"
  7. "go-common/app/admin/main/laser/model"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. )
  11. // AddTask is add a log task
  12. func (s *Service) AddTask(ctx context.Context, mid int64, username string, adminID int64, logDate int64, contactEmail string, platform int, sourceType int) (err error) {
  13. t, err := s.dao.FindTask(ctx, mid, 0)
  14. if err != nil {
  15. return
  16. }
  17. if t != nil {
  18. err = fmt.Errorf("存在该 mid:%d 未完成的任务,请先删除后再添加", mid)
  19. log.Error("s.AddTask() error(%v)", err)
  20. return
  21. }
  22. _, err = s.dao.AddTask(ctx, mid, username, adminID, time.Unix(logDate, 0).Format("2006-01-02 15:03:04"), contactEmail, platform, sourceType)
  23. go s.dao.AddTaskInfoCache(ctx, mid, &model.TaskInfo{
  24. MID: mid,
  25. LogDate: xtime.Time(logDate),
  26. SourceType: sourceType,
  27. Platform: platform,
  28. Empty: false,
  29. })
  30. return
  31. }
  32. // DeleteTask is delete a log task
  33. func (s *Service) DeleteTask(ctx context.Context, taskID int64, username string, adminID int64) (err error) {
  34. t, err := s.dao.QueryTaskInfoByIDSQL(ctx, taskID)
  35. if err != nil {
  36. return
  37. }
  38. go s.dao.RemoveTaskInfoCache(ctx, t.MID)
  39. return s.dao.DeleteTask(ctx, taskID, username, adminID)
  40. }
  41. // QueryTask is query Task by query params
  42. func (s *Service) QueryTask(ctx context.Context, mid int64, logDateStart int64, logDateEnd int64, sourceType int, platform int, state int, sortBy string, pageNo int, pageSize int) (tasks []*model.Task, count int64, err error) {
  43. var wherePairs []string
  44. wherePairs = append(wherePairs, "is_deleted = 0")
  45. wherePairs = append(wherePairs, fmt.Sprintf("state = %d", state))
  46. if sourceType > 0 {
  47. wherePairs = append(wherePairs, fmt.Sprintf("source_type = %d", sourceType))
  48. }
  49. if platform > 0 {
  50. wherePairs = append(wherePairs, fmt.Sprintf("platform = %d", platform))
  51. }
  52. if mid > 0 {
  53. wherePairs = append(wherePairs, fmt.Sprintf("mid = %d", mid))
  54. }
  55. layout := "2006-01-02 15:03:04"
  56. if logDateStart > 0 && logDateEnd > 0 {
  57. start := time.Unix(logDateStart, 0).Format(layout)
  58. end := time.Unix(logDateEnd, 0).Format(layout)
  59. wherePairs = append(wherePairs, fmt.Sprintf(" log_date between '%s' and '%s' ", start, end))
  60. } else if logDateStart > 0 {
  61. start := time.Unix(logDateStart, 0).Format(layout)
  62. wherePairs = append(wherePairs, fmt.Sprintf("log_date >= '%s' ", start))
  63. }
  64. var queryStmt string
  65. if len(wherePairs) > 0 {
  66. queryStmt = strings.Join(wherePairs, " AND ")
  67. }
  68. sort := buildSortStmt(sortBy)
  69. var limit, offset int
  70. if pageNo > 0 && pageSize > 0 {
  71. offset = (pageNo - 1) * pageSize
  72. limit = pageSize
  73. } else {
  74. offset = 0
  75. limit = 20
  76. }
  77. return s.dao.QueryTask(ctx, queryStmt, sort, offset, limit)
  78. }
  79. func buildSortStmt(sortBy string) (sort string) {
  80. if sortBy == "" {
  81. sort = "mtime Desc"
  82. return
  83. }
  84. if strings.HasPrefix(sortBy, "-") {
  85. sort = strings.TrimPrefix(sortBy, "-") + " " + "Desc"
  86. }
  87. return sort
  88. }
  89. // UpdateTask is update undone task where state = 0.
  90. func (s *Service) UpdateTask(ctx context.Context, username string, adminID int64, taskID int64, mid int64, logDate int64, contactEmail string, sourceType int, platform int) (err error) {
  91. var updatePairs []string
  92. updatePairs = append(updatePairs, fmt.Sprintf("source_type = %d", sourceType))
  93. updatePairs = append(updatePairs, fmt.Sprintf("platform = %d", platform))
  94. updatePairs = append(updatePairs, fmt.Sprintf("mid = %d", mid))
  95. updatePairs = append(updatePairs, fmt.Sprintf("log_date = '%s' ", time.Unix(logDate, 0).Format("2006-01-02 15:03:04")))
  96. updatePairs = append(updatePairs, fmt.Sprintf("contact_email = '%s'", contactEmail))
  97. if adminID > 0 {
  98. updatePairs = append(updatePairs, fmt.Sprintf("admin_id = %d", adminID))
  99. }
  100. if len(username) != 0 {
  101. updatePairs = append(updatePairs, fmt.Sprintf("username = '%s' ", username))
  102. }
  103. updateStmt := strings.Join(updatePairs, ", ")
  104. // 0 undone
  105. state := 0
  106. // 下列参数前端必传.
  107. go s.dao.AddTaskInfoCache(ctx, mid, &model.TaskInfo{
  108. MID: mid,
  109. LogDate: xtime.Time(logDate),
  110. SourceType: sourceType,
  111. Platform: platform,
  112. Empty: false,
  113. })
  114. return s.dao.UpdateTask(ctx, taskID, state, updateStmt)
  115. }