autoTask.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/robfig/cron/v3"
  6. log "github.com/sirupsen/logrus"
  7. "time"
  8. "youngee_b_api/db"
  9. "youngee_b_api/model/gorm_model"
  10. )
  11. func AutoTask() error {
  12. c := cron.New(cron.WithSeconds())
  13. //spec := "0 */30 * * * ?" //cron表达式,每半小时执行一次
  14. spec := "0 */2 * * * ?" //cron表达式,每1分钟一次
  15. //spec := "*/10 * * * * ?" //cron表达式,每10秒一次
  16. /*
  17. _, err1 := c.AddFunc(spec, AutoTaskUpdateStatus)
  18. if err1 != nil {
  19. log.Println("service [AutoTaskUpdateStatus] error:", err1)
  20. return err1
  21. }
  22. _, err2 := c.AddFunc("@midnight", AutoTaskUpdateApplyTimes)
  23. if err2 != nil {
  24. log.Println("service [AutoTaskUpdateApplyTimes] error:", err2)
  25. return err2
  26. }
  27. _, err3 := c.AddFunc(spec, AutoTaskCompleteSelection)
  28. if err3 != nil {
  29. log.Println("service [AutoTaskCompleteSecTask] error:", err2)
  30. return err3
  31. }
  32. */
  33. _, err4 := c.AddFunc(spec, GetAutoDraftDefaultTask)
  34. if err4 != nil {
  35. log.Println("service [GetAutoDraftDefaultTask] error:", err4)
  36. return err4
  37. }
  38. _, err5 := c.AddFunc(spec, GetAutoLinkDefaultTask)
  39. if err5 != nil {
  40. log.Println("service [GetAutoLinkDefaultTask] error:", err4)
  41. return err5
  42. }
  43. _, err6 := c.AddFunc(spec, GetAutoCaseCloseDefaultTask)
  44. if err6 != nil {
  45. log.Println("service [GetAutoCaseCloseDefaultTask] error:", err4)
  46. return err6
  47. }
  48. fmt.Println(spec)
  49. c.Start()
  50. return nil
  51. }
  52. func AutoTaskUpdateStatus() {
  53. err := db.AutoUpdateStatus()
  54. log.Println("AutoTaskUpdateStatus is running ,Time :", time.Now())
  55. if err != nil {
  56. log.Println("AutoTaskUpdateStatus error : ", err)
  57. }
  58. }
  59. func AutoTaskUpdateApplyTimes() {
  60. err := db.AutoUpdateApplyTimes()
  61. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  62. if err != nil {
  63. log.Println("AutoUpdateApplyTimes error : ", err)
  64. }
  65. }
  66. func AutoTaskCompleteSelection() {
  67. err := db.AutoCompleteSelection()
  68. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  69. if err != nil {
  70. log.Println("AutoUpdateApplyTimes error : ", err)
  71. }
  72. }
  73. // GetAutoDraftDefaultTask 初稿超时违约
  74. func GetAutoDraftDefaultTask() {
  75. err := db.GetAutoDraftDefaultTask()
  76. log.Println("GetAutoDraftDefaultInPicTask is running ,Time :", time.Now())
  77. if err != nil {
  78. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  79. }
  80. }
  81. // GetAutoLinkDefaultTask 链接超时违约
  82. func GetAutoLinkDefaultTask() {
  83. err := db.GetAutoLinkDefaultTask()
  84. log.Println("GetAutoLinkDefaultTask is running ,Time :", time.Now())
  85. if err != nil {
  86. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  87. }
  88. }
  89. // GetAutoCaseCloseDefaultTask 数据超时违约
  90. func GetAutoCaseCloseDefaultTask() {
  91. err := db.GetAutoCaseCloseDefaultTask()
  92. log.Println("GetAutoCaseCloseDefaultTask is running ,Time :", time.Now())
  93. if err != nil {
  94. log.Println("GetAutoCaseCloseDefaultTask error : ", err)
  95. }
  96. }
  97. // UpdateProjectTaskRedBookLinkData 定时拉取小红书平台,种草子任务链接数据
  98. func UpdateProjectTaskRedBookLinkData() {
  99. // 1. 符合条件的project
  100. ctx := context.Background()
  101. projectIdList, projectIdListTotal, projectIdListErr := db.GetProjectIdList(ctx, 8, 1)
  102. if projectIdListErr != nil {
  103. log.Println("GetProjectIdList error : ", projectIdListErr)
  104. return
  105. }
  106. // 2. 符合条件的task
  107. if projectIdList != nil && projectIdListTotal != 0 {
  108. for _, projectId := range projectIdList {
  109. taskIdList, taskIdTotal, taskIdErr := db.GetProjectTaskIdList(ctx, projectId)
  110. if taskIdErr != nil {
  111. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  112. continue
  113. }
  114. if taskIdList != nil && taskIdTotal != 0 {
  115. for _, taskId := range taskIdList {
  116. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  117. if linkErr != nil {
  118. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  119. continue
  120. }
  121. if linkInfo != "" {
  122. like, comment, collect, share, moreApiErr := GetRedBookLinkDetail(ctx, linkInfo)
  123. if moreApiErr != nil {
  124. log.Println("GetRedBookLinkDetail error : ", moreApiErr)
  125. continue
  126. }
  127. createData := gorm_model.ProjectTaskLinkStatistic{
  128. ProjectId: projectId,
  129. TaskID: taskId,
  130. PlatformId: 1,
  131. VoteCount: like,
  132. CommitCount: comment,
  133. CollectionCount: collect,
  134. ViewCount: share,
  135. }
  136. createErr := db.CreateProjectTaskLinkStatistic(ctx, &createData)
  137. if createErr != nil {
  138. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  139. continue
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }