index_notice.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta_gn/eta_task/models"
  6. "eta_gn/eta_task/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/task"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. var EdbTaskNameMap map[string]map[string]bool
  14. var EdbTaskNameChannel chan string
  15. var EdbTaskStopChannel chan string
  16. var EdbTaskRunNum int
  17. func AddEdbTask(cont context.Context) (err error) {
  18. failList := make([]string, 0)
  19. defer func() {
  20. if len(failList) > 0 {
  21. fmt.Println("提醒失败:")
  22. for _, v := range failList {
  23. fmt.Println(v)
  24. }
  25. }
  26. }()
  27. list, err := models.GetEdbInfoByFrequencyNotDay()
  28. if err != nil {
  29. fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
  30. }
  31. if EdbTaskNameMap == nil {
  32. EdbTaskNameMap = make(map[string]map[string]bool)
  33. }
  34. tmpEdbTaskNameMap := make(map[string]bool)
  35. todayStr := time.Now().Format(utils.FormatDate)
  36. nowWeekFirstDay := utils.GetNowWeekMonday()
  37. nowWeekLastDay := utils.GetNowWeekLastDay()
  38. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  39. nowMonthLastDay := utils.GetNowMonthLastDay()
  40. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  41. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  42. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  43. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  44. nowYearFirstDay := utils.GetNowYearFirstDay()
  45. nowYearLastDay := utils.GetNowYearLastDay()
  46. debugNoticeUserId := 0 //测试环境,需要发送消息的用户
  47. for _, edb := range list {
  48. if edb.UserId <= 0 {
  49. continue //没有配置user_id的话,那么不需要提醒
  50. }
  51. tmpEdb := edb //指标信息
  52. isNotice := false //是否需要提醒
  53. noticeTime := "12:00:00" //提醒时间
  54. var dataDtTime time.Time
  55. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  56. if tmpErr != nil {
  57. if tmpErr.Error() != utils.ErrNoRow() {
  58. failList = append(failList, fmt.Sprint(tmpEdb.TradeCode, "失败,Err:", tmpErr.Error()))
  59. continue
  60. }
  61. }
  62. if edbData != nil {
  63. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  64. dataDtTime = tmpDataDtTime
  65. }
  66. switch tmpEdb.Frequency {
  67. case "周度":
  68. modifyDate := nowWeekLastDay //下次更新日期
  69. if tmpEdb.NoticeTime != "" {
  70. addDay := 7
  71. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  72. if len(noticeArr) >= 2 {
  73. noticeTime = noticeArr[1]
  74. }
  75. noticeWeek := noticeArr[0]
  76. switch noticeWeek {
  77. case "周一":
  78. addDay = 1
  79. case "周二":
  80. addDay = 2
  81. case "周三":
  82. addDay = 3
  83. case "周四":
  84. addDay = 4
  85. case "周五":
  86. addDay = 5
  87. case "周六":
  88. addDay = 6
  89. case "周日":
  90. addDay = 7
  91. }
  92. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  93. }
  94. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
  95. isNotice = true
  96. }
  97. case "月度":
  98. addDay := 0
  99. modifyDate := nowMonthLastDay //下次更新日期
  100. if tmpEdb.NoticeTime != "" {
  101. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  102. if len(strArr) >= 2 {
  103. noticeTime = strArr[1]
  104. }
  105. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  106. if tmpErr != nil {
  107. continue
  108. }
  109. addDay = tmpAddDay - 1
  110. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  111. }
  112. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
  113. isNotice = true
  114. }
  115. case "季度":
  116. if tmpEdb.NoticeTime != "" {
  117. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  118. if len(noticeArr) >= 2 {
  119. noticeTime = noticeArr[1]
  120. }
  121. }
  122. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
  123. isNotice = true
  124. }
  125. case "半年度":
  126. if tmpEdb.NoticeTime != "" {
  127. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  128. if len(noticeArr) >= 2 {
  129. noticeTime = noticeArr[1]
  130. }
  131. }
  132. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
  133. isNotice = true
  134. }
  135. case "年度":
  136. if tmpEdb.NoticeTime != "" {
  137. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  138. if len(noticeArr) >= 2 {
  139. noticeTime = noticeArr[1]
  140. }
  141. }
  142. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
  143. isNotice = true
  144. }
  145. }
  146. if isNotice {
  147. taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(tmpEdb.TradeCode)
  148. tmpTaskFunc := func(ctx context.Context) (funcErr error) {
  149. defer func() {
  150. EdbTaskNameChannel <- taskName
  151. }()
  152. funcIsNotice := false
  153. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  154. if tmpErr != nil {
  155. if tmpErr.Error() != utils.ErrNoRow() {
  156. funcErr = tmpErr
  157. return
  158. }
  159. }
  160. if utils.RunMode == "debug" && debugNoticeUserId > 0 {
  161. tmpEdb.UserId = debugNoticeUserId //测试环境的话,发送邮箱给嘉豪
  162. }
  163. var funcDataDtTime time.Time
  164. if edbData != nil {
  165. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  166. funcDataDtTime = tmpDataDtTime
  167. }
  168. notifyFrequency := "每日"
  169. switch tmpEdb.Frequency {
  170. case "周度":
  171. notifyFrequency = "每周"
  172. modifyDate := nowWeekLastDay //下次更新日期
  173. if tmpEdb.NoticeTime != "" {
  174. addDay := 7
  175. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  176. if len(noticeArr) >= 2 {
  177. noticeTime = noticeArr[1]
  178. }
  179. noticeWeek := noticeArr[0]
  180. switch noticeWeek {
  181. case "周一":
  182. addDay = 1
  183. case "周二":
  184. addDay = 2
  185. case "周三":
  186. addDay = 3
  187. case "周四":
  188. addDay = 4
  189. case "周五":
  190. addDay = 5
  191. case "周六":
  192. addDay = 6
  193. case "周日":
  194. addDay = 7
  195. }
  196. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  197. }
  198. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
  199. funcIsNotice = true
  200. }
  201. case "月度":
  202. notifyFrequency = "每月"
  203. addDay := 0
  204. modifyDate := nowMonthLastDay //下次更新日期
  205. if tmpEdb.NoticeTime != "" {
  206. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  207. if len(strArr) >= 2 {
  208. noticeTime = strArr[1]
  209. }
  210. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  211. if tmpErr != nil {
  212. funcErr = tmpErr
  213. }
  214. addDay = tmpAddDay - 1
  215. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  216. }
  217. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
  218. funcIsNotice = true
  219. }
  220. case "季度":
  221. notifyFrequency = "每季度"
  222. if tmpEdb.NoticeTime != "" {
  223. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  224. if len(noticeArr) >= 2 {
  225. noticeTime = noticeArr[1]
  226. }
  227. }
  228. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
  229. funcIsNotice = true
  230. }
  231. case "半年度":
  232. notifyFrequency = "每半年度"
  233. if tmpEdb.NoticeTime != "" {
  234. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  235. if len(noticeArr) >= 2 {
  236. noticeTime = noticeArr[1]
  237. }
  238. }
  239. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
  240. funcIsNotice = true
  241. }
  242. case "年度":
  243. notifyFrequency = "每年"
  244. if tmpEdb.NoticeTime != "" {
  245. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  246. if len(noticeArr) >= 2 {
  247. noticeTime = noticeArr[1]
  248. }
  249. }
  250. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
  251. funcIsNotice = true
  252. }
  253. }
  254. if funcIsNotice {
  255. openIdList := make([]*models.OpenIdList, 0)
  256. admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
  257. if err != nil {
  258. if err.Error() == utils.ErrNoRow() {
  259. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  260. return
  261. } else {
  262. return err
  263. }
  264. }
  265. if admin == nil {
  266. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  267. return
  268. }
  269. if admin.OpenId == "" {
  270. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  271. return
  272. }
  273. openIdTemp := new(models.OpenIdList)
  274. openIdTemp.OpenId = admin.OpenId
  275. openIdList = append(openIdList, openIdTemp)
  276. first := "数据录入提醒"
  277. keyword1 := tmpEdb.SecName + "该更新了"
  278. keyword2 := notifyFrequency + " " + tmpEdb.NoticeTime
  279. remark := tmpEdb.SecName + "该更新了"
  280. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  281. if err != nil {
  282. return err
  283. }
  284. {
  285. sendRecord := new(models.EdbinfoSendMsgRecord)
  286. sendRecord.UserId = tmpEdb.UserId
  287. sendRecord.TradeCode = tmpEdb.TradeCode
  288. sendRecord.CreateTime = time.Now()
  289. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  290. if err != nil {
  291. return err
  292. }
  293. }
  294. }
  295. return
  296. }
  297. spec := ``
  298. if noticeTime != "" {
  299. noticeArr := strings.Split(noticeTime, ":")
  300. if len(noticeArr) == 3 {
  301. spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])
  302. tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)
  303. task.AddTask(taskName, tmpTask)
  304. tmpEdbTaskNameMap[taskName] = true
  305. }
  306. }
  307. }
  308. }
  309. EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
  310. go deleteTask()
  311. if EdbTaskRunNum > 0 {
  312. EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  313. }
  314. EdbTaskRunNum++
  315. return
  316. }
  317. func deleteTask() {
  318. for {
  319. select {
  320. case taskName := <-EdbTaskNameChannel:
  321. task.DeleteTask(taskName)
  322. delete(EdbTaskNameMap, taskName)
  323. case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
  324. for taskName := range EdbTaskNameMap[dayStr] {
  325. task.DeleteTask(taskName)
  326. delete(EdbTaskNameMap, taskName)
  327. }
  328. break
  329. }
  330. }
  331. }