autoTask.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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:", err5)
  41. return err5
  42. }
  43. _, err6 := c.AddFunc(spec, GetAutoCaseCloseDefaultTask)
  44. if err6 != nil {
  45. log.Println("service [GetAutoCaseCloseDefaultTask] error:", err6)
  46. return err6
  47. }
  48. _, err7 := c.AddFunc(spec, UpdateProjectTaskRedBookLinkData)
  49. if err7 != nil {
  50. log.Println("service [UpdateProjectTaskRedBookLinkData] error:", err7)
  51. return err7
  52. }
  53. _, err8 := c.AddFunc(spec, UpdateLocalTaskRedBookLinkData)
  54. if err8 != nil {
  55. log.Println("service [UpdateLocalTaskRedBookLinkData] error:", err8)
  56. return err8
  57. }
  58. fmt.Println(spec)
  59. c.Start()
  60. return nil
  61. }
  62. func AutoTaskUpdateStatus() {
  63. err := db.AutoUpdateStatus()
  64. log.Println("AutoTaskUpdateStatus is running ,Time :", time.Now())
  65. if err != nil {
  66. log.Println("AutoTaskUpdateStatus error : ", err)
  67. }
  68. }
  69. func AutoTaskUpdateApplyTimes() {
  70. err := db.AutoUpdateApplyTimes()
  71. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  72. if err != nil {
  73. log.Println("AutoUpdateApplyTimes error : ", err)
  74. }
  75. }
  76. func AutoTaskCompleteSelection() {
  77. err := db.AutoCompleteSelection()
  78. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  79. if err != nil {
  80. log.Println("AutoUpdateApplyTimes error : ", err)
  81. }
  82. }
  83. // GetAutoDraftDefaultTask 初稿超时违约
  84. func GetAutoDraftDefaultTask() {
  85. err := db.GetAutoDraftDefaultTask()
  86. log.Println("GetAutoDraftDefaultInPicTask is running ,Time :", time.Now())
  87. if err != nil {
  88. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  89. }
  90. }
  91. // GetAutoLinkDefaultTask 链接超时违约
  92. func GetAutoLinkDefaultTask() {
  93. err := db.GetAutoLinkDefaultTask()
  94. log.Println("GetAutoLinkDefaultTask is running ,Time :", time.Now())
  95. if err != nil {
  96. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  97. }
  98. }
  99. // GetAutoCaseCloseDefaultTask 数据超时违约
  100. func GetAutoCaseCloseDefaultTask() {
  101. err := db.GetAutoCaseCloseDefaultTask()
  102. log.Println("GetAutoCaseCloseDefaultTask is running ,Time :", time.Now())
  103. if err != nil {
  104. log.Println("GetAutoCaseCloseDefaultTask error : ", err)
  105. }
  106. }
  107. // UpdateProjectTaskRedBookLinkData 定时拉取小红书平台,种草子任务链接数据
  108. func UpdateProjectTaskRedBookLinkData() {
  109. log.Println("UpdateProjectTaskRedBookLinkData is running ,Time :", time.Now())
  110. // 1. 符合条件的project
  111. ctx := context.Background()
  112. projectIdList, projectIdListTotal, projectIdListErr := db.GetProjectIdList(ctx, 8, 1)
  113. if projectIdListErr != nil {
  114. log.Println("GetProjectIdList error : ", projectIdListErr)
  115. return
  116. }
  117. // 2. 符合条件的task
  118. if projectIdList != nil && projectIdListTotal != 0 {
  119. for _, projectId := range projectIdList {
  120. taskIdList, taskIdTotal, taskIdErr := db.GetProjectTaskIdList(ctx, projectId)
  121. if taskIdErr != nil {
  122. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  123. continue
  124. }
  125. if taskIdList != nil && taskIdTotal != 0 {
  126. for _, taskId := range taskIdList {
  127. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  128. if linkErr != nil {
  129. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  130. continue
  131. }
  132. if linkInfo != "" {
  133. like, comment, collect, share, moreApiErr := GetRedBookLinkDetail(ctx, linkInfo)
  134. if moreApiErr != nil {
  135. log.Println("GetRedBookLinkDetail error : ", moreApiErr)
  136. continue
  137. }
  138. createData := gorm_model.ProjectTaskLinkStatistic{
  139. ProjectId: projectId,
  140. TaskId: taskId,
  141. PlatformId: 1,
  142. VoteCount: like,
  143. CommitCount: comment,
  144. CollectionCount: collect,
  145. ViewCount: share,
  146. }
  147. createErr := db.CreateProjectTaskLinkStatistic(ctx, &createData)
  148. if createErr != nil {
  149. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  150. continue
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. // UpdateLocalTaskRedBookLinkData 定时拉取小红书平台,本地生活子任务链接数据
  159. func UpdateLocalTaskRedBookLinkData() {
  160. log.Println("UpdateLocalTaskRedBookLinkData is running ,Time :", time.Now())
  161. // 1. 符合条件的local
  162. ctx := context.Background()
  163. localIdList, localIdListTotal, localIdListErr := db.GetLocalIdList(ctx, 8, 1)
  164. if localIdListErr != nil {
  165. log.Println("GetLocalIdList error : ", localIdListErr)
  166. return
  167. }
  168. // 2. 符合条件的task
  169. if localIdList != nil && localIdListTotal != 0 {
  170. for _, localId := range localIdList {
  171. taskIdList, taskIdTotal, taskIdErr := db.GetLocalTaskIdList(ctx, localId)
  172. if taskIdErr != nil {
  173. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  174. continue
  175. }
  176. if taskIdList != nil && taskIdTotal != 0 {
  177. for _, taskId := range taskIdList {
  178. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  179. if linkErr != nil {
  180. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  181. continue
  182. }
  183. if linkInfo != "" {
  184. like, comment, collect, share, moreApiErr := GetRedBookLinkDetail(ctx, linkInfo)
  185. if moreApiErr != nil {
  186. log.Println("GetRedBookLinkDetail error : ", moreApiErr)
  187. continue
  188. }
  189. createData := gorm_model.LocalTaskLinkStatistic{
  190. LocalId: localId,
  191. TaskId: taskId,
  192. PlatformId: 1,
  193. VoteCount: like,
  194. CommitCount: comment,
  195. CollectionCount: collect,
  196. ViewCount: share,
  197. }
  198. createErr := db.CreateLocalTaskLinkStatistic(ctx, &createData)
  199. if createErr != nil {
  200. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  201. continue
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }