autoTask.go 10 KB

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