autoTask.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. _, err9 := c.AddFunc(spec, UpdateProjectTaskWeiBoLinkData)
  59. if err9 != nil {
  60. log.Println("service [UpdateProjectTaskWeiBoLinkData] error:", err9)
  61. return err9
  62. }
  63. fmt.Println(spec)
  64. c.Start()
  65. return nil
  66. }
  67. func AutoTaskUpdateStatus() {
  68. err := db.AutoUpdateStatus()
  69. log.Println("AutoTaskUpdateStatus is running ,Time :", time.Now())
  70. if err != nil {
  71. log.Println("AutoTaskUpdateStatus error : ", err)
  72. }
  73. }
  74. func AutoTaskUpdateApplyTimes() {
  75. err := db.AutoUpdateApplyTimes()
  76. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  77. if err != nil {
  78. log.Println("AutoUpdateApplyTimes error : ", err)
  79. }
  80. }
  81. func AutoTaskCompleteSelection() {
  82. err := db.AutoCompleteSelection()
  83. log.Println("AutoUpdateApplyTimes is running ,Time :", time.Now())
  84. if err != nil {
  85. log.Println("AutoUpdateApplyTimes error : ", err)
  86. }
  87. }
  88. // GetAutoDraftDefaultTask 初稿超时违约
  89. func GetAutoDraftDefaultTask() {
  90. err := db.GetAutoDraftDefaultTask()
  91. log.Println("GetAutoDraftDefaultInPicTask is running ,Time :", time.Now())
  92. if err != nil {
  93. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  94. }
  95. }
  96. // GetAutoLinkDefaultTask 链接超时违约
  97. func GetAutoLinkDefaultTask() {
  98. err := db.GetAutoLinkDefaultTask()
  99. log.Println("GetAutoLinkDefaultTask is running ,Time :", time.Now())
  100. if err != nil {
  101. log.Println("GetAutoDraftDefaultInPicTask error : ", err)
  102. }
  103. }
  104. // GetAutoCaseCloseDefaultTask 数据超时违约
  105. func GetAutoCaseCloseDefaultTask() {
  106. err := db.GetAutoCaseCloseDefaultTask()
  107. log.Println("GetAutoCaseCloseDefaultTask is running ,Time :", time.Now())
  108. if err != nil {
  109. log.Println("GetAutoCaseCloseDefaultTask error : ", err)
  110. }
  111. }
  112. // UpdateProjectTaskRedBookLinkData 定时拉取小红书平台,种草子任务链接数据
  113. func UpdateProjectTaskRedBookLinkData() {
  114. log.Println("UpdateProjectTaskRedBookLinkData is running ,Time :", time.Now())
  115. // 1. 符合条件的project
  116. ctx := context.Background()
  117. projectIdList, projectIdListTotal, projectIdListErr := db.GetProjectIdList(ctx, 8, 1)
  118. if projectIdListErr != nil {
  119. log.Println("GetProjectIdList error : ", projectIdListErr)
  120. return
  121. }
  122. // 2. 符合条件的task
  123. if projectIdList != nil && projectIdListTotal != 0 {
  124. for _, projectId := range projectIdList {
  125. taskIdList, taskIdTotal, taskIdErr := db.GetProjectTaskIdList(ctx, projectId)
  126. if taskIdErr != nil {
  127. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  128. continue
  129. }
  130. if taskIdList != nil && taskIdTotal != 0 {
  131. for _, taskId := range taskIdList {
  132. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  133. if linkErr != nil {
  134. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  135. continue
  136. }
  137. if linkInfo != "" {
  138. like, comment, collect, share, moreApiErr := GetRedBookLinkDetail(ctx, linkInfo)
  139. if moreApiErr != nil {
  140. log.Println("GetRedBookLinkDetail error : ", moreApiErr)
  141. continue
  142. }
  143. createData := gorm_model.ProjectTaskLinkStatistic{
  144. ProjectId: projectId,
  145. TaskId: taskId,
  146. PlatformId: 1,
  147. VoteCount: like,
  148. CommitCount: comment,
  149. CollectionCount: collect,
  150. ViewCount: share,
  151. }
  152. createErr := db.CreateProjectTaskLinkStatistic(ctx, &createData)
  153. if createErr != nil {
  154. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  155. continue
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. // UpdateLocalTaskRedBookLinkData 定时拉取小红书平台,本地生活子任务链接数据
  164. func UpdateLocalTaskRedBookLinkData() {
  165. log.Println("UpdateLocalTaskRedBookLinkData is running ,Time :", time.Now())
  166. // 1. 符合条件的local
  167. ctx := context.Background()
  168. localIdList, localIdListTotal, localIdListErr := db.GetLocalIdList(ctx, 8, 1)
  169. if localIdListErr != nil {
  170. log.Println("GetLocalIdList error : ", localIdListErr)
  171. return
  172. }
  173. // 2. 符合条件的task
  174. if localIdList != nil && localIdListTotal != 0 {
  175. for _, localId := range localIdList {
  176. taskIdList, taskIdTotal, taskIdErr := db.GetLocalTaskIdList(ctx, localId)
  177. if taskIdErr != nil {
  178. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  179. continue
  180. }
  181. if taskIdList != nil && taskIdTotal != 0 {
  182. for _, taskId := range taskIdList {
  183. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  184. if linkErr != nil {
  185. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  186. continue
  187. }
  188. if linkInfo != "" {
  189. like, comment, collect, share, moreApiErr := GetRedBookLinkDetail(ctx, linkInfo)
  190. if moreApiErr != nil {
  191. log.Println("GetRedBookLinkDetail error : ", moreApiErr)
  192. continue
  193. }
  194. createData := gorm_model.LocalTaskLinkStatistic{
  195. LocalId: localId,
  196. TaskId: taskId,
  197. PlatformId: 1,
  198. VoteCount: like,
  199. CommitCount: comment,
  200. CollectionCount: collect,
  201. ViewCount: share,
  202. }
  203. createErr := db.CreateLocalTaskLinkStatistic(ctx, &createData)
  204. if createErr != nil {
  205. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  206. continue
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. // UpdateProjectTaskWeiBoLinkData 定时拉取微博平台,种草子任务链接数据
  215. func UpdateProjectTaskWeiBoLinkData() {
  216. log.Println("UpdateProjectTaskWeiBoLinkData is running ,Time :", time.Now())
  217. // 1. 符合条件的project
  218. ctx := context.Background()
  219. projectIdList, projectIdListTotal, projectIdListErr := db.GetProjectIdList(ctx, 8, 3)
  220. if projectIdListErr != nil {
  221. log.Println("GetProjectIdList error : ", projectIdListErr)
  222. return
  223. }
  224. // 2. 符合条件的task
  225. if projectIdList != nil && projectIdListTotal != 0 {
  226. for _, projectId := range projectIdList {
  227. taskIdList, taskIdTotal, taskIdErr := db.GetProjectTaskIdList(ctx, projectId)
  228. if taskIdErr != nil {
  229. log.Println("GetProjectTaskIdList error : ", taskIdErr)
  230. continue
  231. }
  232. if taskIdList != nil && taskIdTotal != 0 {
  233. for _, taskId := range taskIdList {
  234. linkInfo, linkErr := db.GetProjectTaskLinkInfo(ctx, taskId, 1)
  235. if linkErr != nil {
  236. log.Println("GetProjectTaskLinkInfo error : ", linkErr)
  237. continue
  238. }
  239. if linkInfo != "" {
  240. like, comment, collect, share, moreApiErr := GetWeiBoLinkDetail(ctx, linkInfo)
  241. if moreApiErr != nil {
  242. log.Println("GetWeiBoLinkDetail error : ", moreApiErr)
  243. continue
  244. }
  245. createData := gorm_model.ProjectTaskLinkStatistic{
  246. ProjectId: projectId,
  247. TaskId: taskId,
  248. PlatformId: 3,
  249. VoteCount: like,
  250. CommitCount: comment,
  251. CollectionCount: collect,
  252. ViewCount: share,
  253. }
  254. createErr := db.CreateProjectTaskLinkStatistic(ctx, &createData)
  255. if createErr != nil {
  256. log.Println("CreateProjectTaskLinkStatistic error : ", createErr)
  257. continue
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }