index_notice.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_task/models"
  6. "eta/eta_task/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/task"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // EdbTaskNameMap 手工指标定时任务名称map集合
  14. var EdbTaskNameMap map[string]map[string]bool
  15. // EdbTaskNameChannel 手工指标定时任务名称channel
  16. var EdbTaskNameChannel chan string
  17. // EdbTaskStopChannel 手工指标定时任务停止channel
  18. var EdbTaskStopChannel chan string
  19. // EdbTaskRunNum 手工指标定时任务开始次数
  20. var EdbTaskRunNum int
  21. // AddEdbTask 新增手工指标数据录入提醒
  22. func AddEdbTask(cont context.Context) (err error) {
  23. //失败列表
  24. failList := make([]string, 0)
  25. defer func() {
  26. if len(failList) > 0 {
  27. fmt.Println("提醒失败:")
  28. for _, v := range failList {
  29. fmt.Println(v)
  30. }
  31. }
  32. }()
  33. list, err := models.GetEdbInfoByFrequencyNotDay()
  34. if err != nil {
  35. fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
  36. }
  37. //如果还没有初始化map,那么先初始
  38. if EdbTaskNameMap == nil {
  39. EdbTaskNameMap = make(map[string]map[string]bool)
  40. }
  41. tmpEdbTaskNameMap := make(map[string]bool)
  42. // 今天的日期字符串(格式:2021-10-25)
  43. todayStr := time.Now().Format(utils.FormatDate)
  44. //当前周的周一与周日
  45. nowWeekFirstDay := utils.GetNowWeekMonday()
  46. nowWeekLastDay := utils.GetNowWeekLastDay()
  47. //当前月的一号与最后一天
  48. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  49. nowMonthLastDay := utils.GetNowMonthLastDay()
  50. //当前季度的第一天与最后一天
  51. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  52. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  53. //当前半年的第一天与最后一天
  54. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  55. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  56. // 当前年的第一天与最后一天
  57. nowYearFirstDay := utils.GetNowYearFirstDay()
  58. nowYearLastDay := utils.GetNowYearLastDay()
  59. debugNoticeUserId := 0 //测试环境,需要发送消息的用户
  60. //测试环境也不发了
  61. //if utils.RunMode == "debug" {
  62. // tmpWxUser, tmpErr := models.GetWxUserByMobile("17634786714")
  63. // if tmpErr == nil && tmpWxUser != nil {
  64. // //debugNoticeUserId = 44078 //测试环境的话,发送邮箱给颜鹏
  65. // debugNoticeUserId = int(tmpWxUser.UserId) //测试环境的话,发送邮箱给嘉豪
  66. // }
  67. //}
  68. //task.globalTaskManager.adminTaskList
  69. for _, edb := range list {
  70. if edb.UserId <= 0 {
  71. continue //没有配置user_id的话,那么不需要提醒
  72. }
  73. tmpEdb := edb //指标信息
  74. isNotice := false //是否需要提醒
  75. noticeTime := "12:00:00" //提醒时间
  76. var dataDtTime time.Time
  77. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  78. if tmpErr != nil {
  79. if tmpErr.Error() != utils.ErrNoRow() {
  80. failList = append(failList, fmt.Sprint(tmpEdb.TradeCode, "失败,Err:", tmpErr.Error()))
  81. continue
  82. }
  83. }
  84. //如果确实是有数据的
  85. if edbData != nil {
  86. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  87. dataDtTime = tmpDataDtTime
  88. }
  89. switch tmpEdb.Frequency {
  90. case "周度":
  91. modifyDate := nowWeekLastDay //下次更新日期
  92. if tmpEdb.NoticeTime != "" {
  93. addDay := 7
  94. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  95. if len(noticeArr) >= 2 {
  96. noticeTime = noticeArr[1]
  97. }
  98. noticeWeek := noticeArr[0]
  99. switch noticeWeek {
  100. case "周一":
  101. addDay = 1
  102. case "周二":
  103. addDay = 2
  104. case "周三":
  105. addDay = 3
  106. case "周四":
  107. addDay = 4
  108. case "周五":
  109. addDay = 5
  110. case "周六":
  111. addDay = 6
  112. case "周日":
  113. addDay = 7
  114. }
  115. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  116. }
  117. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  118. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
  119. isNotice = true
  120. }
  121. case "月度":
  122. addDay := 0
  123. modifyDate := nowMonthLastDay //下次更新日期
  124. if tmpEdb.NoticeTime != "" {
  125. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  126. if len(strArr) >= 2 {
  127. noticeTime = strArr[1]
  128. }
  129. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  130. if tmpErr != nil {
  131. continue
  132. }
  133. addDay = tmpAddDay - 1
  134. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  135. }
  136. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  137. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
  138. isNotice = true
  139. }
  140. case "季度":
  141. //提醒时间
  142. if tmpEdb.NoticeTime != "" {
  143. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  144. if len(noticeArr) >= 2 {
  145. noticeTime = noticeArr[1]
  146. }
  147. }
  148. //每季度更新数据时间
  149. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  150. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
  151. isNotice = true
  152. }
  153. case "半年度":
  154. //提醒时间
  155. if tmpEdb.NoticeTime != "" {
  156. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  157. if len(noticeArr) >= 2 {
  158. noticeTime = noticeArr[1]
  159. }
  160. }
  161. //每半年度更新数据时间
  162. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  163. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
  164. isNotice = true
  165. }
  166. case "年度":
  167. //提醒时间
  168. if tmpEdb.NoticeTime != "" {
  169. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  170. if len(noticeArr) >= 2 {
  171. noticeTime = noticeArr[1]
  172. }
  173. }
  174. //每年度更新数据时间
  175. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  176. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
  177. isNotice = true
  178. }
  179. }
  180. if isNotice {
  181. taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(tmpEdb.TradeCode)
  182. //fmt.Println(taskName, ";", tmpEdb.SecName)
  183. //定时任务
  184. tmpTaskFunc := func(ctx context.Context) (funcErr error) {
  185. //方法执行结束后,移除定时任务
  186. defer func() {
  187. EdbTaskNameChannel <- taskName
  188. }()
  189. // 匿名方法内判断是否发送提醒,因为可能时间到的时候,发现
  190. funcIsNotice := false
  191. // 再次获取指标数据详情
  192. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  193. if tmpErr != nil {
  194. if tmpErr.Error() != utils.ErrNoRow() {
  195. funcErr = tmpErr
  196. return
  197. }
  198. }
  199. if utils.RunMode == "debug" && debugNoticeUserId > 0 {
  200. tmpEdb.UserId = debugNoticeUserId //测试环境的话,发送邮箱给嘉豪
  201. }
  202. //数据过期时间
  203. var funcDataDtTime time.Time
  204. //如果确实是有数据的
  205. if edbData != nil {
  206. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  207. funcDataDtTime = tmpDataDtTime
  208. }
  209. //提示频度文案
  210. notifyFrequency := "每日"
  211. switch tmpEdb.Frequency {
  212. case "周度":
  213. notifyFrequency = "每周"
  214. modifyDate := nowWeekLastDay //下次更新日期
  215. if tmpEdb.NoticeTime != "" {
  216. addDay := 7
  217. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  218. if len(noticeArr) >= 2 {
  219. noticeTime = noticeArr[1]
  220. }
  221. noticeWeek := noticeArr[0]
  222. switch noticeWeek {
  223. case "周一":
  224. addDay = 1
  225. case "周二":
  226. addDay = 2
  227. case "周三":
  228. addDay = 3
  229. case "周四":
  230. addDay = 4
  231. case "周五":
  232. addDay = 5
  233. case "周六":
  234. addDay = 6
  235. case "周日":
  236. addDay = 7
  237. }
  238. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  239. }
  240. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  241. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
  242. funcIsNotice = true
  243. }
  244. case "月度":
  245. notifyFrequency = "每月"
  246. addDay := 0
  247. modifyDate := nowMonthLastDay //下次更新日期
  248. if tmpEdb.NoticeTime != "" {
  249. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  250. if len(strArr) >= 2 {
  251. noticeTime = strArr[1]
  252. }
  253. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  254. if tmpErr != nil {
  255. funcErr = tmpErr
  256. }
  257. addDay = tmpAddDay - 1
  258. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  259. }
  260. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  261. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
  262. funcIsNotice = true
  263. }
  264. case "季度":
  265. notifyFrequency = "每季度"
  266. //提醒时间
  267. if tmpEdb.NoticeTime != "" {
  268. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  269. if len(noticeArr) >= 2 {
  270. noticeTime = noticeArr[1]
  271. }
  272. }
  273. //每季度更新数据时间
  274. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  275. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
  276. funcIsNotice = true
  277. }
  278. case "半年度":
  279. notifyFrequency = "每半年度"
  280. //提醒时间
  281. if tmpEdb.NoticeTime != "" {
  282. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  283. if len(noticeArr) >= 2 {
  284. noticeTime = noticeArr[1]
  285. }
  286. }
  287. //每半年度更新数据时间
  288. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  289. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
  290. funcIsNotice = true
  291. }
  292. case "年度":
  293. notifyFrequency = "每年"
  294. //提醒时间
  295. if tmpEdb.NoticeTime != "" {
  296. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  297. if len(noticeArr) >= 2 {
  298. noticeTime = noticeArr[1]
  299. }
  300. }
  301. //每年度更新数据时间
  302. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  303. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
  304. funcIsNotice = true
  305. }
  306. }
  307. //fmt.Println(tmpEdb.TradeCode, " funcIsNotice:", funcIsNotice)
  308. //如果还是要提醒
  309. if funcIsNotice {
  310. //用户微信openid列表数据
  311. openIdList := make([]*models.OpenIdList, 0)
  312. //获取用户信息
  313. admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
  314. if err != nil {
  315. if err.Error() == utils.ErrNoRow() {
  316. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  317. return
  318. } else {
  319. return err
  320. }
  321. }
  322. if admin == nil {
  323. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  324. return
  325. }
  326. //发送消息
  327. if admin.OpenId == "" {
  328. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  329. return
  330. }
  331. openIdTemp := new(models.OpenIdList)
  332. openIdTemp.OpenId = admin.OpenId
  333. openIdList = append(openIdList, openIdTemp)
  334. first := "数据录入提醒"
  335. keyword1 := tmpEdb.SecName + "该更新了"
  336. keyword2 := notifyFrequency + " " + tmpEdb.NoticeTime
  337. remark := tmpEdb.SecName + "该更新了"
  338. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  339. if err != nil {
  340. return err
  341. }
  342. //发送成功,记录发送日志
  343. {
  344. sendRecord := new(models.EdbinfoSendMsgRecord)
  345. sendRecord.UserId = tmpEdb.UserId
  346. sendRecord.TradeCode = tmpEdb.TradeCode
  347. sendRecord.CreateTime = time.Now()
  348. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  349. if err != nil {
  350. return err
  351. }
  352. }
  353. }
  354. return
  355. }
  356. //添加定时任务(没有设置通知时间就不进行定时任务通知了)
  357. spec := ``
  358. if noticeTime != "" {
  359. noticeArr := strings.Split(noticeTime, ":")
  360. if len(noticeArr) == 3 {
  361. //spec = ` */20 * * * * * `
  362. spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])
  363. //定时任务开始的时间
  364. tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)
  365. task.AddTask(taskName, tmpTask)
  366. tmpEdbTaskNameMap[taskName] = true
  367. }
  368. }
  369. }
  370. }
  371. //将当天的手工指标加入到手工指标池去
  372. EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
  373. //开启协程,用来清除定时任务
  374. go deleteTask()
  375. //如果当前定时任务执行次数大于0次,那么需要往手工指标定时任务停止channel写入数据,用来关闭昨天没有执行的的定时任务
  376. if EdbTaskRunNum > 0 {
  377. //清除昨天的数据
  378. EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  379. }
  380. //手工指标定时任务开始次数累加
  381. EdbTaskRunNum++
  382. return
  383. //fmt.Println(task.NewMapSorter())
  384. }
  385. // deleteTask 清除已通知的任务
  386. func deleteTask() {
  387. for {
  388. select {
  389. case taskName := <-EdbTaskNameChannel:
  390. task.DeleteTask(taskName)
  391. delete(EdbTaskNameMap, taskName)
  392. case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
  393. for taskName := range EdbTaskNameMap[dayStr] {
  394. task.DeleteTask(taskName)
  395. delete(EdbTaskNameMap, taskName)
  396. }
  397. break
  398. }
  399. }
  400. }