task.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/beego/beego/v2/task"
  7. "hongze/hongze_task/models"
  8. "hongze/hongze_task/services/company_contract"
  9. "hongze/hongze_task/services/data"
  10. "hongze/hongze_task/utils"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. )
  16. func Task() {
  17. fmt.Println("task start")
  18. //如果是生产环境,才需要走这些任务
  19. if utils.RunMode == "release" {
  20. releaseTask()
  21. }
  22. //每日定时合同处理
  23. handleCompanyContract := task.NewTask("handleCompanyContract", "0 1 0 * * *", company_contract.HandleCompanyContract)
  24. task.AddTask("每日定时合同处理", handleCompanyContract)
  25. //正式->试用
  26. companyTryOut := task.NewTask("companyTryOut", "0 5 0 * * *", CompanyTryOut)
  27. task.AddTask("正式->试用", companyTryOut)
  28. //试用->冻结
  29. companyFreeze := task.NewTask("companyFreeze", "0 10 0 * * *", CompanyFreeze)
  30. task.AddTask("试用->冻结", companyFreeze)
  31. //冻结->流失
  32. companyLoss := task.NewTask("companyLoss", "0 20 0 * * *", CompanyLoss)
  33. task.AddTask("冻结->流失", companyLoss)
  34. //用户产品权限试用-->关闭
  35. companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0 30 0 * * *", CompanyReportPermissionClose)
  36. task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)
  37. //删除日志记录
  38. //deleteReportSaveLog := task.NewTask("deleteReportSaveLog", "0 30 08 * * *", DeleteReportSaveLog)
  39. //task.AddTask("deleteReportSaveLog", deleteReportSaveLog)
  40. // 存量客户数据统计
  41. stackCompanyStatistic := task.NewTask("stackCompanyStatistic", "0 35 0 * * *", StackCompanyStatistic)
  42. task.AddTask("存量客户数据统计", stackCompanyStatistic)
  43. // 定时往同花顺推送报告
  44. sendWaitReport := task.NewTask("sendWaitReport", "0 */1 * * * * ", SendWaitReport)
  45. task.AddTask("定时往同花顺推送报告", sendWaitReport)
  46. task.StartTask()
  47. //GetHistoryLzProductDetail()
  48. //GetLzPrice()
  49. //GetLzProductDetail()
  50. //LzExportExcel()
  51. //GetLzProductList()GetLzProductDetail
  52. // 定时新增手工指标数据提醒
  53. addEdbTask := task.NewTask("sendWaitReport", "1 0 0 * * * ", AddEdbTask)
  54. task.AddTask("定时新增手工指标数据提醒", addEdbTask)
  55. //每次服务启动都需要执行一次的
  56. _ = AddEdbTask(nil)
  57. fmt.Println("task end")
  58. }
  59. //生产环境需要走的任务
  60. func releaseTask() {
  61. //隆众指标获取
  62. //getLzProductList := task.NewTask("getLzProductList", "0 0 11-19/1 * * * ", GetLzProductList)
  63. //task.AddTask("getLzProductList", getLzProductList)
  64. //隆众指标数据获取
  65. //getLzProductDetail := task.NewTask("getLzProductDetail", "0 5 11-19/1 * * * ", GetLzProductDetail)
  66. //task.AddTask("getLzProductDetail", getLzProductDetail)
  67. //隆众价格指标获取
  68. //getLzProductPriceProduct := task.NewTask("getLzProductPriceProduct", "0 5 11-19/1 * * * ", GetLzProductPriceProduct)
  69. //task.AddTask("getLzProductPriceProduct", getLzProductPriceProduct)
  70. //隆众价格指标数据获取
  71. //getLzProductPriceData := task.NewTask("getLzProductPriceData", "0 10 11-19/1 * * * ", GetLzProductPriceData)
  72. //task.AddTask("getLzProductPriceData", getLzProductPriceData)
  73. //隆众调研指标获取
  74. getLzSurveyProduct := task.NewTask("getLzSurveyProduct", "0 5 11-19/1 * * * ", GetLzSurveyProduct)
  75. task.AddTask("getLzSurveyProduct", getLzSurveyProduct)
  76. //隆众调研指标数据获取
  77. getLzSurveyProductData := task.NewTask("getLzSurveyProductData", "0 10 11-19/1 * * * ", GetLzSurveyProductData)
  78. task.AddTask("getLzSurveyProductData", getLzSurveyProductData)
  79. //发送邮件
  80. sendEmail := task.NewTask("sendEmail", "0 0 12 * * 0 ", SendEmail)
  81. task.AddTask("sendEmail", sendEmail)
  82. //oneMinute := task.NewTask("oneMinute", "0 */1 7-23 * * * ", OneMinute)
  83. //task.AddTask("oneMinute", oneMinute)
  84. // 正式/试用 用户到期提醒
  85. companyRemind := task.NewTask("companyRemind", "0 30 08 * * *", CompanyRemind)
  86. task.AddTask("companyRemind", companyRemind)
  87. //潜在客户
  88. freeViewerDetail := task.NewTask("freeViewerDetail", "0 0 9 * * 1 ", FreeViewerDetail)
  89. task.AddTask("潜在客户", freeViewerDetail)
  90. //刷新指标数据
  91. refreshData := task.NewTask("refreshData", "0 1 0,19 * * *", RefreshData)
  92. task.AddTask("refreshData", refreshData)
  93. // 定时往同花顺同步微信群的截止日期(凌晨4点)
  94. syncThsWxGroupEveryDay := task.NewTask("syncThsWxGroupEveryDay", "0 1 4 * * * ", SyncWxGroupEveryDay)
  95. task.AddTask("定时往同花顺同步微信群的截止日期", syncThsWxGroupEveryDay)
  96. //刷新计算指标数据
  97. //refreshCalculateData := task.NewTask("refreshCalculateData", "0 15 0,19 * * *", RefreshCalculateData)
  98. //task.AddTask("refreshCalculateData", refreshCalculateData)
  99. checkDataInterface := task.NewTask("checkDataInterface", "0 */2 * * * * ", data.CheckDataInterface)
  100. task.AddTask("checkDataInterface", checkDataInterface)
  101. checkPbDataInterface := task.NewTask("checkPbDataInterface", "0 */5 * * * * ", data.CheckPbDataInterface)
  102. task.AddTask("checkPbDataInterface", checkPbDataInterface)
  103. }
  104. func TaskTest() {
  105. fmt.Println("The task is start")
  106. //companyReportPermissionClose := task.NewTask("companyTryOut", "0 5 0 * * *", CompanyReportPermissionClose)
  107. companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0/30 * * * * *", CompanyReportPermissionClose)
  108. task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)
  109. task.StartTask()
  110. fmt.Println("The task is end")
  111. }
  112. func SendEmail(cont context.Context) (err error) {
  113. //报告历史访问次数
  114. go ReportViewTimes()
  115. //报告访问详情
  116. go ReportViewDetail()
  117. //用户权限统计
  118. go HongzeUsers()
  119. return
  120. }
  121. func OneMinute(cont context.Context) (err error) {
  122. //日度
  123. //FrequencyByDay()
  124. //周度
  125. FrequencyByWeek()
  126. //月度
  127. FrequencyByMonth()
  128. return
  129. }
  130. func RefreshData(cont context.Context) (err error) {
  131. wg := sync.WaitGroup{}
  132. wg.Add(7)
  133. //wind
  134. go data.RefreshDataFromWind(&wg)
  135. //同花顺
  136. go data.RefreshDataFromThs(&wg)
  137. //彭博
  138. go data.RefreshDataFromPb(&wg)
  139. //手工数据
  140. go data.RefreshDataFromManual(&wg)
  141. //隆众数据
  142. go data.RefreshDataFromLz(&wg)
  143. //有色
  144. go data.RefreshDataFromYs(&wg)
  145. //钢联
  146. go data.RefreshDataFromGl(&wg)
  147. wg.Wait()
  148. data.RefreshDataFromCalculateAll()
  149. time.Sleep(2 * time.Second)
  150. data.RefreshNotice()
  151. return
  152. }
  153. //func RefreshCalculateData(cont context.Context) (err error) {
  154. // //计算指标
  155. // go data.RefreshDataFromCalculateAll()
  156. // //刷新公历转农历数据
  157. // //go data.RefreshDataFromQuarterAll()
  158. // return
  159. //}
  160. //func Task() {
  161. // fmt.Println("start")
  162. // data.RefreshDataFromCalculateAll()
  163. // //startDate := time.Now().AddDate(-30, 0, 0).UnixNano() / 1e6
  164. //
  165. // //fmt.Println(startDate)
  166. // //fmt.Println(endDate)
  167. // fmt.Println("end")
  168. //}
  169. /*
  170. endData:=time.Now().UnixNano()/1e6
  171. dateTime:=time.Unix(endData/1000,0)
  172. fmt.Println(dateTime)
  173. */
  174. // EdbTaskNameMap 手工指标定时任务名称map集合
  175. var EdbTaskNameMap map[string]map[string]bool
  176. // EdbTaskNameChannel 手工指标定时任务名称channel
  177. var EdbTaskNameChannel chan string
  178. // EdbTaskStopChannel 手工指标定时任务停止channel
  179. var EdbTaskStopChannel chan string
  180. // EdbTaskRunNum 手工指标定时任务开始次数
  181. var EdbTaskRunNum int
  182. // AddEdbTask 新增手工指标数据录入提醒
  183. func AddEdbTask(cont context.Context) (err error) {
  184. //func AddEdbTask() (err error) {
  185. list, err := models.GetEdbInfoByFrequencyNotDay()
  186. if err != nil {
  187. fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
  188. }
  189. //如果还没有初始化map,那么先初始
  190. if EdbTaskNameMap == nil {
  191. EdbTaskNameMap = make(map[string]map[string]bool)
  192. }
  193. tmpEdbTaskNameMap := make(map[string]bool)
  194. // 今天的日期字符串(格式:2021-10-25)
  195. todayStr := time.Now().Format(utils.FormatDate)
  196. //当前周的周一与周日
  197. nowWeekFirstDay := utils.GetNowWeekMonday()
  198. nowWeekLastDay := utils.GetNowWeekLastDay()
  199. //当前月的一号与最后一天
  200. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  201. nowMonthLastDay := utils.GetNowMonthLastDay()
  202. //当前季度的第一天与最后一天
  203. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  204. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  205. //当前半年的第一天与最后一天
  206. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  207. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  208. // 当前年的第一天与最后一天
  209. nowYearFirstDay := utils.GetNowYearFirstDay()
  210. nowYearLastDay := utils.GetNowYearLastDay()
  211. //失败列表
  212. failList := make([]string, 0)
  213. wxUser, _ := models.GetWxUserByMobile("17634786714")
  214. //task.globalTaskManager.adminTaskList
  215. for _, edb := range list {
  216. tmpEdb := edb //指标信息
  217. isNotice := false //是否需要提醒
  218. noticeTime := "12:00:00" //提醒时间
  219. var dataDtTime time.Time
  220. edbData, tmpErr := models.GetLastEdbdataInfo(edb.TradeCode)
  221. if tmpErr != nil {
  222. if tmpErr.Error() != utils.ErrNoRow() {
  223. failList = append(failList, fmt.Sprint(edb.TradeCode, "失败,Err:", tmpErr.Error()))
  224. continue
  225. }
  226. }
  227. //如果确实是有数据的
  228. if edbData != nil {
  229. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  230. dataDtTime = tmpDataDtTime
  231. }
  232. switch edb.Frequency {
  233. case "周度":
  234. modifyDate := nowWeekLastDay //下次更新日期
  235. if edb.NoticeTime != "" {
  236. addDay := 7
  237. noticeArr := strings.Split(edb.NoticeTime, " ")
  238. if len(noticeArr) >= 2 {
  239. noticeTime = noticeArr[1]
  240. }
  241. noticeWeek := noticeArr[0]
  242. switch noticeWeek {
  243. case "周一":
  244. addDay = 1
  245. case "周二":
  246. addDay = 2
  247. case "周三":
  248. addDay = 3
  249. case "周四":
  250. addDay = 4
  251. case "周五":
  252. addDay = 5
  253. case "周六":
  254. addDay = 6
  255. case "周日":
  256. addDay = 7
  257. }
  258. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  259. }
  260. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  261. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
  262. isNotice = true
  263. }
  264. case "月度":
  265. addDay := 0
  266. modifyDate := nowMonthLastDay //下次更新日期
  267. if edb.NoticeTime != "" {
  268. strArr := strings.Split(edb.NoticeTime, "日")
  269. if len(strArr) >= 2 {
  270. noticeTime = strArr[1]
  271. }
  272. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  273. if tmpErr != nil {
  274. continue
  275. }
  276. addDay = tmpAddDay - 1
  277. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  278. }
  279. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  280. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
  281. isNotice = true
  282. }
  283. case "季度":
  284. //提醒时间
  285. if edb.NoticeTime != "" {
  286. noticeArr := strings.Split(edb.NoticeTime, " ")
  287. if len(noticeArr) >= 2 {
  288. noticeTime = noticeArr[1]
  289. }
  290. }
  291. //每季度更新数据时间
  292. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  293. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
  294. isNotice = true
  295. }
  296. case "半年度":
  297. //提醒时间
  298. if edb.NoticeTime != "" {
  299. noticeArr := strings.Split(edb.NoticeTime, " ")
  300. if len(noticeArr) >= 2 {
  301. noticeTime = noticeArr[1]
  302. }
  303. }
  304. //每半年度更新数据时间
  305. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  306. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
  307. isNotice = true
  308. }
  309. case "年度":
  310. //提醒时间
  311. if edb.NoticeTime != "" {
  312. noticeArr := strings.Split(edb.NoticeTime, " ")
  313. if len(noticeArr) >= 2 {
  314. noticeTime = noticeArr[1]
  315. }
  316. }
  317. //每年度更新数据时间
  318. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  319. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
  320. isNotice = true
  321. }
  322. }
  323. if isNotice {
  324. taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(edb.TradeCode)
  325. fmt.Println(taskName, ";", edb.SecName)
  326. //定时任务
  327. tmpTaskFunc := func(ctx context.Context) (funcErr error) {
  328. //方法执行结束后,移除定时任务
  329. defer func() {
  330. EdbTaskNameChannel <- taskName
  331. }()
  332. // 匿名方法内判断是否发送提醒,因为可能时间到的时候,发现
  333. funcIsNotice := false
  334. // 再次获取指标数据详情
  335. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  336. if tmpErr != nil {
  337. if tmpErr.Error() != utils.ErrNoRow() {
  338. funcErr = tmpErr
  339. return
  340. }
  341. }
  342. if utils.RunMode == "debug" && wxUser != nil {
  343. //tmpEdb.UserId = 44078 //测试环境的话,发送邮箱给颜鹏
  344. tmpEdb.UserId = int(wxUser.UserId) //测试环境的话,发送邮箱给嘉豪
  345. }
  346. //数据过期时间
  347. var funcDataDtTime time.Time
  348. //如果确实是有数据的
  349. if edbData != nil {
  350. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  351. funcDataDtTime = tmpDataDtTime
  352. }
  353. switch tmpEdb.Frequency {
  354. case "周度":
  355. modifyDate := nowWeekLastDay //下次更新日期
  356. if tmpEdb.NoticeTime != "" {
  357. addDay := 7
  358. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  359. if len(noticeArr) >= 2 {
  360. noticeTime = noticeArr[1]
  361. }
  362. noticeWeek := noticeArr[0]
  363. switch noticeWeek {
  364. case "周一":
  365. addDay = 1
  366. case "周二":
  367. addDay = 2
  368. case "周三":
  369. addDay = 3
  370. case "周四":
  371. addDay = 4
  372. case "周五":
  373. addDay = 5
  374. case "周六":
  375. addDay = 6
  376. case "周日":
  377. addDay = 7
  378. }
  379. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  380. }
  381. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  382. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
  383. funcIsNotice = true
  384. }
  385. case "月度":
  386. addDay := 0
  387. modifyDate := nowMonthLastDay //下次更新日期
  388. if tmpEdb.NoticeTime != "" {
  389. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  390. if len(strArr) >= 2 {
  391. noticeTime = strArr[1]
  392. }
  393. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  394. if tmpErr != nil {
  395. funcErr = tmpErr
  396. }
  397. addDay = tmpAddDay - 1
  398. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  399. }
  400. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  401. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
  402. funcIsNotice = true
  403. }
  404. case "季度":
  405. //提醒时间
  406. if tmpEdb.NoticeTime != "" {
  407. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  408. if len(noticeArr) >= 2 {
  409. noticeTime = noticeArr[1]
  410. }
  411. }
  412. //每季度更新数据时间
  413. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  414. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
  415. funcIsNotice = true
  416. }
  417. case "半年度":
  418. //提醒时间
  419. if tmpEdb.NoticeTime != "" {
  420. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  421. if len(noticeArr) >= 2 {
  422. noticeTime = noticeArr[1]
  423. }
  424. }
  425. //每半年度更新数据时间
  426. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  427. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
  428. funcIsNotice = true
  429. }
  430. case "年度":
  431. //提醒时间
  432. if tmpEdb.NoticeTime != "" {
  433. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  434. if len(noticeArr) >= 2 {
  435. noticeTime = noticeArr[1]
  436. }
  437. }
  438. //每年度更新数据时间
  439. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  440. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
  441. funcIsNotice = true
  442. }
  443. }
  444. fmt.Println(tmpEdb.TradeCode, " funcIsNotice:", funcIsNotice)
  445. //如果还是要提醒
  446. if funcIsNotice {
  447. //用户微信openid列表数据
  448. openIdList := make([]*models.OpenIdList, 0)
  449. //获取用户信息
  450. isAdmin := true
  451. admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
  452. if err != nil {
  453. if err.Error() == utils.ErrNoRow() {
  454. isAdmin = false
  455. } else {
  456. return err
  457. }
  458. }
  459. if admin == nil {
  460. isAdmin = false
  461. }
  462. if isAdmin {
  463. if admin.Mobile == "" {
  464. } else {
  465. wxUser, err := models.GetWxUserByMobile(admin.Mobile)
  466. if err != nil {
  467. return err
  468. }
  469. if wxUser == nil {
  470. funcErr = errors.New("用户信息不存在:mobile:" + admin.Mobile)
  471. return err
  472. }
  473. tmpOpenidList, err := models.GetUserOpenidListByUserId(int(wxUser.UserId))
  474. if err != nil {
  475. return err
  476. }
  477. openIdList = tmpOpenidList
  478. }
  479. } else {
  480. tmpOpenidList, err := models.GetUserOpenidListByUserId(tmpEdb.UserId)
  481. if err != nil {
  482. return err
  483. }
  484. openIdList = tmpOpenidList
  485. }
  486. //发送消息
  487. if len(openIdList) <= 0 {
  488. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  489. return
  490. }
  491. first := "数据录入提醒"
  492. keyword1 := tmpEdb.SecName
  493. keyword2 := "每周 " + edb.NoticeTime
  494. remark := tmpEdb.SecName + "该更新了"
  495. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  496. if err != nil {
  497. return err
  498. }
  499. //发送成功,记录发送日志
  500. {
  501. sendRecord := new(models.EdbinfoSendMsgRecord)
  502. sendRecord.UserId = tmpEdb.UserId
  503. sendRecord.TradeCode = tmpEdb.TradeCode
  504. sendRecord.CreateTime = time.Now()
  505. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  506. if err != nil {
  507. return err
  508. }
  509. }
  510. }
  511. return
  512. }
  513. //添加定时任务
  514. spec := ``
  515. if noticeTime != "" {
  516. noticeArr := strings.Split(noticeTime, ":")
  517. if len(noticeArr) == 3 {
  518. //spec = ` */20 * * * * * `
  519. spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])
  520. }
  521. }
  522. //定时任务开始的时间
  523. tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)
  524. task.AddTask(taskName, tmpTask)
  525. tmpEdbTaskNameMap[taskName] = true
  526. }
  527. }
  528. //将当天的手工指标加入到手工指标池去
  529. EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
  530. //开启协程,用来清除定时任务
  531. go deleteTask()
  532. //如果当前定时任务执行次数大于0次,那么需要往手工指标定时任务停止channel写入数据,用来关闭昨天没有执行的的定时任务
  533. if EdbTaskRunNum > 0 {
  534. //清除昨天的数据
  535. EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  536. }
  537. //手工指标定时任务开始次数累加
  538. EdbTaskRunNum++
  539. for _, v := range failList {
  540. fmt.Println(v)
  541. }
  542. return
  543. //fmt.Println(task.NewMapSorter())
  544. }
  545. // deleteTask 清除已通知的任务
  546. func deleteTask() {
  547. for {
  548. select {
  549. case taskName := <-EdbTaskNameChannel:
  550. task.DeleteTask(taskName)
  551. delete(EdbTaskNameMap, taskName)
  552. case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
  553. for taskName := range EdbTaskNameMap[dayStr] {
  554. task.DeleteTask(taskName)
  555. delete(EdbTaskNameMap, taskName)
  556. }
  557. break
  558. }
  559. }
  560. }