task.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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"
  9. "hongze/hongze_task/services/company_contract"
  10. "hongze/hongze_task/services/data"
  11. "hongze/hongze_task/services/roadshow"
  12. "hongze/hongze_task/utils"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. func Task() {
  19. fmt.Println("task start")
  20. //如果是生产环境,才需要走这些任务
  21. if utils.RunMode == "release" {
  22. releaseTask()
  23. }
  24. //每日定时合同处理
  25. handleCompanyContract := task.NewTask("handleCompanyContract", "0 1 2 * * *", company_contract.HandleCompanyContract)
  26. task.AddTask("每日定时合同处理", handleCompanyContract)
  27. //正式->试用
  28. companyTryOut := task.NewTask("companyTryOut", "0 5 2 * * *", CompanyTryOut)
  29. task.AddTask("正式->试用", companyTryOut)
  30. //试用->冻结
  31. companyFreeze := task.NewTask("companyFreeze", "0 10 2 * * *", CompanyFreeze)
  32. task.AddTask("试用->冻结", companyFreeze)
  33. //冻结->流失
  34. companyLoss := task.NewTask("companyLoss", "0 20 2 * * *", CompanyLoss)
  35. task.AddTask("冻结->流失", companyLoss)
  36. //用户产品权限正式-->试用
  37. companyReportPermissionTryOut := task.NewTask("companyReportPermissionTryOut", "0 30 2 * * *", CompanyReportPermissionTryOut)
  38. task.AddTask("用户产品权限正式-->试用", companyReportPermissionTryOut)
  39. //用户产品权限试用-->关闭
  40. companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0 35 2 * * *", CompanyReportPermissionClose)
  41. task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)
  42. //删除日志记录
  43. //deleteReportSaveLog := task.NewTask("deleteReportSaveLog", "0 30 08 * * *", DeleteReportSaveLog)
  44. //task.AddTask("deleteReportSaveLog", deleteReportSaveLog)
  45. // 存量客户数据统计
  46. stackCompanyStatistic := task.NewTask("stackCompanyStatistic", "0 35 2 * * *", StackCompanyStatistic)
  47. task.AddTask("存量客户数据统计", stackCompanyStatistic)
  48. // 定时往同花顺推送报告
  49. sendWaitReport := task.NewTask("sendWaitReport", "0 */1 * * * * ", SendWaitReport)
  50. task.AddTask("定时往同花顺推送报告", sendWaitReport)
  51. // 研报电话会提醒
  52. ybTelRemind := task.NewTask("YbTelRemind", "0 */1 * * * * ", YbTelRemind)
  53. task.AddTask("研报电话会提醒", ybTelRemind)
  54. // 研报沙龙提醒
  55. ybSalonRemind := task.NewTask("ybSalonRemind", "0 */1 * * * * ", YbSalonRemind)
  56. task.AddTask("研报沙龙提醒", ybSalonRemind)
  57. //GetHistoryLzProductDetail()
  58. //GetLzPrice()
  59. //GetLzProductDetail()
  60. //LzExportExcel()
  61. //GetLzProductList()GetLzProductDetail
  62. // 定时新增手工指标数据提醒
  63. addEdbTask := task.NewTask("sendWaitReport", "1 0 2 * * * ", AddEdbTask)
  64. task.AddTask("定时新增手工指标数据提醒", addEdbTask)
  65. //每次服务启动都需要执行一次的
  66. _ = AddEdbTask(nil)
  67. //每日用户阅读数据统计
  68. statisticsUserView := task.NewTask("statisticsUserView", "0 5 2 * * *", StatisticsUserView)
  69. task.AddTask("每日用户阅读数据统计", statisticsUserView)
  70. //路演-活动状态修改
  71. modifyRsCalendarStatus := task.NewTask("modifyRsCalendarStatus", "0 */1 * * * * ", roadshow.ModifyRsCalendarResearcherStatus)
  72. task.AddTask("modifyRsCalendarStatus", modifyRsCalendarStatus)
  73. addReportRecord := task.NewTask("addReportRecord", "0 0 */1 * * *", roadshow.AddReportRecord)
  74. task.AddTask("addReportRecord", addReportRecord)
  75. //存量客户数据统计
  76. addCompanyReportRecord := task.NewTask("addCompanyReportRecord", "0 0 */1 * * *", company.AddCompanyReportRecord)
  77. task.AddTask("addCompanyReportRecord", addCompanyReportRecord)
  78. //CRM 6.3 客户列表路演次数统计
  79. roadShowTotal := task.NewTask("roadShowTotal", "0 */30 * * * *", roadshow.RoadShow)
  80. task.AddTask("roadShowTotal", roadShowTotal)
  81. //每半个小时用户阅读数据统计
  82. statisticsUserView1Hour := task.NewTask("statisticsUserView1Hour", "0 */30 * * * *", StatisticsUserView1Hour)
  83. task.AddTask("每半个小时用户阅读数据统计", statisticsUserView1Hour)
  84. //路演公开会议合并
  85. setPublicMeetingUnionCode := task.NewTask("setPublicMeetingUnionCode", "0 */10 * * * *", roadshow.SetPublicMeetingUnionCode)
  86. task.AddTask("setPublicMeetingUnionCode", setPublicMeetingUnionCode)
  87. task.StartTask()
  88. fmt.Println("task end")
  89. }
  90. //func Task() {
  91. // fmt.Println("start")
  92. // cont := new(context.Context)
  93. // roadshow.SetPublicMeetingUnionCode(*cont)
  94. // fmt.Println("end")
  95. //}
  96. //生产环境需要走的任务
  97. func releaseTask() {
  98. //隆众调研指标获取
  99. getLzSurveyProduct := task.NewTask("getLzSurveyProduct", "0 5 08-19/1 * * * ", GetLzSurveyProduct)
  100. task.AddTask("getLzSurveyProduct", getLzSurveyProduct)
  101. //隆众调研指标数据获取
  102. getLzSurveyProductData := task.NewTask("getLzSurveyProductData", "0 10 08-20/1 * * * ", GetLzSurveyProductData)
  103. task.AddTask("getLzSurveyProductData", getLzSurveyProductData)
  104. //发送邮件
  105. sendEmail := task.NewTask("sendEmail", "0 0 12 * * 0 ", SendEmail)
  106. task.AddTask("sendEmail", sendEmail)
  107. //oneMinute := task.NewTask("oneMinute", "0 */1 7-23 * * * ", OneMinute)
  108. //task.AddTask("oneMinute", oneMinute)
  109. // 正式/试用 用户到期提醒
  110. companyRemind := task.NewTask("companyRemind", "0 30 08 * * *", CompanyRemind)
  111. task.AddTask("companyRemind", companyRemind)
  112. //潜在客户
  113. freeViewerDetail := task.NewTask("freeViewerDetail", "0 0 9 * * 1 ", FreeViewerDetail)
  114. task.AddTask("潜在客户", freeViewerDetail)
  115. //上周增量客户列表
  116. incrementCompany := task.NewTask("incrementCompany", "0 0 9 * * 1 ", IncrementCompany)
  117. task.AddTask("上周增量客户列表", incrementCompany)
  118. //刷新指标数据
  119. refreshData := task.NewTask("refreshData", "0 1 0,19 * * *", RefreshData)
  120. task.AddTask("refreshData", refreshData)
  121. //刷新交易所指标数据
  122. refreshTradeData := task.NewTask("refreshData", "0 1 4 * * *", RefreshTradeData)
  123. task.AddTask("refreshTradeData", refreshTradeData)
  124. //刷新欧洲天然气指标数据
  125. refreshEicData := task.NewTask("refreshData", "0 1 3,7 * * *", RefreshEicData)
  126. task.AddTask("refreshEicData", refreshEicData)
  127. //刷新指标基础数据
  128. refreshBaseData := task.NewTask("refreshBaseData", "0 */30 * * * * ", RefreshBaseData)
  129. task.AddTask("refreshBaseData", refreshBaseData)
  130. //同步弘则数据库中来自,钢联,隆众,有色,人工等基础数据--每隔五分钟,同步一次最新数据
  131. syncBaseData := task.NewTask("syncBaseData", "0 */5 * * * * ", SyncBaseData)
  132. task.AddTask("syncBaseData", syncBaseData)
  133. syncBaseDataExt := task.NewTask("syncBaseDataExt", "0 */30 * * * * ", SyncBaseDataExt)
  134. task.AddTask("syncBaseDataExt", syncBaseDataExt)
  135. // 定时往同花顺同步微信群的截止日期(凌晨4点)
  136. syncThsWxGroupEveryDay := task.NewTask("syncThsWxGroupEveryDay", "0 1 4 * * * ", SyncWxGroupEveryDay)
  137. task.AddTask("定时往同花顺同步微信群的截止日期", syncThsWxGroupEveryDay)
  138. checkPbDataInterface := task.NewTask("checkPbDataInterface", "0 */2 * * * * ", data.CheckPbDataInterface)
  139. task.AddTask("checkPbDataInterface", checkPbDataInterface)
  140. checkLtDataInterface := task.NewTask("checkLtDataInterface", "0 */2 * * * * ", data.CheckLtDataInterface)
  141. task.AddTask("checkLtDataInterface", checkLtDataInterface)
  142. //检测wind新服务器
  143. checkWindDataInterface := task.NewTask("checkWindDataInterface", "0 */2 * * * * ", data.CheckWindDataInterface)
  144. task.AddTask("checkWindDataInterface", checkWindDataInterface)
  145. //初始化指标更新状态
  146. resetEdbInfoIsUpdate := task.NewTask("resetEdbInfoIsUpdate", "0 0 0 * * *", data.ResetEdbInfoIsUpdate)
  147. task.AddTask("resetEdbInfoIsUpdate", resetEdbInfoIsUpdate)
  148. }
  149. func TaskTest() {
  150. fmt.Println("The task is start")
  151. //companyReportPermissionClose := task.NewTask("companyTryOut", "0 5 0 * * *", CompanyReportPermissionClose)
  152. companyReportPermissionClose := task.NewTask("companyReportPermissionClose", "0/30 * * * * *", CompanyReportPermissionClose)
  153. task.AddTask("用户产品权限试用-->关闭", companyReportPermissionClose)
  154. task.StartTask()
  155. fmt.Println("The task is end")
  156. }
  157. func SendEmail(cont context.Context) (err error) {
  158. //报告历史访问次数
  159. go ReportViewTimes()
  160. //报告访问详情
  161. go ReportViewDetail()
  162. //用户权限统计
  163. go HongzeUsers()
  164. return
  165. }
  166. func OneMinute(cont context.Context) (err error) {
  167. //日度
  168. //FrequencyByDay()
  169. //周度
  170. FrequencyByWeek()
  171. //月度
  172. FrequencyByMonth()
  173. return
  174. }
  175. func RefreshData(cont context.Context) (err error) {
  176. wg := sync.WaitGroup{}
  177. wg.Add(10)
  178. //hour := time.Now().Hour()
  179. //if hour != 0 {
  180. //}
  181. go data.RefreshDataFromWind(&wg)
  182. //同花顺
  183. go data.RefreshDataFromThs(&wg)
  184. //彭博
  185. go data.RefreshDataFromPb(&wg)
  186. //彭博财务
  187. go data.RefreshDataFromPbFinance(&wg)
  188. //手工数据
  189. go data.RefreshDataFromManual(&wg)
  190. //隆众数据
  191. go data.RefreshDataFromLz(&wg)
  192. //有色
  193. go data.RefreshDataFromYs(&wg)
  194. //钢联
  195. go data.RefreshDataFromGl(&wg)
  196. //路透
  197. go data.RefreshDataFromLt(&wg)
  198. //煤炭
  199. go data.RefreshDataFromCoal(&wg)
  200. wg.Wait()
  201. ////计算指标
  202. data.RefreshDataFromCalculateAll()
  203. time.Sleep(5 * time.Second)
  204. data.RefreshNotice()
  205. fmt.Println("Refresh End")
  206. return
  207. }
  208. // RefreshTradeData 刷新交易所数据
  209. func RefreshTradeData(cont context.Context) (err error) {
  210. wg := sync.WaitGroup{}
  211. wg.Add(5)
  212. //郑商所
  213. go data.RefreshDataFromZz(&wg)
  214. //上期所
  215. go data.RefreshDataFromSh(&wg)
  216. //上期能源
  217. go data.RefreshDataFromShfe(&wg)
  218. //中金所
  219. go data.RefreshDataFromCffex(&wg)
  220. //大商所
  221. go data.RefreshDataFromDl(&wg)
  222. wg.Wait()
  223. //计算指标
  224. data.RefreshDataFromCalculateAll()
  225. return
  226. }
  227. // RefreshEicData 刷新欧洲天然气数据
  228. func RefreshEicData(cont context.Context) (err error) {
  229. wg := sync.WaitGroup{}
  230. wg.Add(1)
  231. //欧洲天然气
  232. go data.RefreshDataFromEic(&wg)
  233. wg.Wait()
  234. //计算指标
  235. data.RefreshDataFromCalculateAll()
  236. return
  237. }
  238. //刷新基础数据
  239. func RefreshBaseData(cont context.Context) (err error) {
  240. now := time.Now()
  241. if now.Hour() == 0 || now.Hour() == 19 {
  242. return nil
  243. }
  244. //同步有色基础指标数据
  245. go data.SyncSmmIndexDataBase()
  246. return
  247. }
  248. //刷新基础数据
  249. func SyncBaseData(cont context.Context) (err error) {
  250. now := time.Now()
  251. if now.Hour() == 0 || now.Hour() == 19 {
  252. return nil
  253. }
  254. //同步钢联基础数据
  255. go data.SyncGlDataBase()
  256. return
  257. }
  258. //刷新基础数据
  259. func SyncBaseDataExt(cont context.Context) (err error) {
  260. now := time.Now()
  261. if now.Hour() == 0 || now.Hour() == 19 {
  262. return nil
  263. }
  264. //同步隆众基础数据
  265. go data.SyncLzDataBase()
  266. //同步手工数据
  267. go data.SyncManualDataBase()
  268. //同步有色基础数据
  269. go data.SyncSmmDataBase()
  270. //刷新图表中,指标的最新日期
  271. go data.SetChartEdbEndDate()
  272. return
  273. }
  274. //func RefreshCalculateData(cont context.Context) (err error) {
  275. // //计算指标
  276. // go data.RefreshDataFromCalculateAll()
  277. // //刷新公历转农历数据
  278. // //go data.RefreshDataFromQuarterAll()
  279. // return
  280. //}
  281. //func Task() {
  282. // fmt.Println("start")
  283. // data.RefreshDataFromCalculateAll()
  284. // //startDate := time.Now().AddDate(-30, 0, 0).UnixNano() / 1e6
  285. //
  286. // //fmt.Println(startDate)
  287. // //fmt.Println(endDate)
  288. // fmt.Println("end")
  289. //}
  290. /*
  291. endData:=time.Now().UnixNano()/1e6
  292. dateTime:=time.Unix(endData/1000,0)
  293. fmt.Println(dateTime)
  294. */
  295. // EdbTaskNameMap 手工指标定时任务名称map集合
  296. var EdbTaskNameMap map[string]map[string]bool
  297. // EdbTaskNameChannel 手工指标定时任务名称channel
  298. var EdbTaskNameChannel chan string
  299. // EdbTaskStopChannel 手工指标定时任务停止channel
  300. var EdbTaskStopChannel chan string
  301. // EdbTaskRunNum 手工指标定时任务开始次数
  302. var EdbTaskRunNum int
  303. // AddEdbTask 新增手工指标数据录入提醒
  304. func AddEdbTask(cont context.Context) (err error) {
  305. //失败列表
  306. failList := make([]string, 0)
  307. defer func() {
  308. if len(failList) > 0 {
  309. fmt.Println("提醒失败:")
  310. for _, v := range failList {
  311. fmt.Println(v)
  312. }
  313. }
  314. }()
  315. list, err := models.GetEdbInfoByFrequencyNotDay()
  316. if err != nil {
  317. fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
  318. }
  319. //如果还没有初始化map,那么先初始
  320. if EdbTaskNameMap == nil {
  321. EdbTaskNameMap = make(map[string]map[string]bool)
  322. }
  323. tmpEdbTaskNameMap := make(map[string]bool)
  324. // 今天的日期字符串(格式:2021-10-25)
  325. todayStr := time.Now().Format(utils.FormatDate)
  326. //当前周的周一与周日
  327. nowWeekFirstDay := utils.GetNowWeekMonday()
  328. nowWeekLastDay := utils.GetNowWeekLastDay()
  329. //当前月的一号与最后一天
  330. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  331. nowMonthLastDay := utils.GetNowMonthLastDay()
  332. //当前季度的第一天与最后一天
  333. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  334. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  335. //当前半年的第一天与最后一天
  336. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  337. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  338. // 当前年的第一天与最后一天
  339. nowYearFirstDay := utils.GetNowYearFirstDay()
  340. nowYearLastDay := utils.GetNowYearLastDay()
  341. debugNoticeUserId := 0 //测试环境,需要发送消息的用户
  342. //测试环境也不发了
  343. //if utils.RunMode == "debug" {
  344. // tmpWxUser, tmpErr := models.GetWxUserByMobile("17634786714")
  345. // if tmpErr == nil && tmpWxUser != nil {
  346. // //debugNoticeUserId = 44078 //测试环境的话,发送邮箱给颜鹏
  347. // debugNoticeUserId = int(tmpWxUser.UserId) //测试环境的话,发送邮箱给嘉豪
  348. // }
  349. //}
  350. //task.globalTaskManager.adminTaskList
  351. for _, edb := range list {
  352. if edb.UserId <= 0 {
  353. continue //没有配置user_id的话,那么不需要提醒
  354. }
  355. tmpEdb := edb //指标信息
  356. isNotice := false //是否需要提醒
  357. noticeTime := "12:00:00" //提醒时间
  358. var dataDtTime time.Time
  359. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  360. if tmpErr != nil {
  361. if tmpErr.Error() != utils.ErrNoRow() {
  362. failList = append(failList, fmt.Sprint(tmpEdb.TradeCode, "失败,Err:", tmpErr.Error()))
  363. continue
  364. }
  365. }
  366. //如果确实是有数据的
  367. if edbData != nil {
  368. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  369. dataDtTime = tmpDataDtTime
  370. }
  371. switch tmpEdb.Frequency {
  372. case "周度":
  373. modifyDate := nowWeekLastDay //下次更新日期
  374. if tmpEdb.NoticeTime != "" {
  375. addDay := 7
  376. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  377. if len(noticeArr) >= 2 {
  378. noticeTime = noticeArr[1]
  379. }
  380. noticeWeek := noticeArr[0]
  381. switch noticeWeek {
  382. case "周一":
  383. addDay = 1
  384. case "周二":
  385. addDay = 2
  386. case "周三":
  387. addDay = 3
  388. case "周四":
  389. addDay = 4
  390. case "周五":
  391. addDay = 5
  392. case "周六":
  393. addDay = 6
  394. case "周日":
  395. addDay = 7
  396. }
  397. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  398. }
  399. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  400. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
  401. isNotice = true
  402. }
  403. case "月度":
  404. addDay := 0
  405. modifyDate := nowMonthLastDay //下次更新日期
  406. if tmpEdb.NoticeTime != "" {
  407. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  408. if len(strArr) >= 2 {
  409. noticeTime = strArr[1]
  410. }
  411. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  412. if tmpErr != nil {
  413. continue
  414. }
  415. addDay = tmpAddDay - 1
  416. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  417. }
  418. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  419. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
  420. isNotice = true
  421. }
  422. case "季度":
  423. //提醒时间
  424. if tmpEdb.NoticeTime != "" {
  425. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  426. if len(noticeArr) >= 2 {
  427. noticeTime = noticeArr[1]
  428. }
  429. }
  430. //每季度更新数据时间
  431. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  432. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
  433. isNotice = true
  434. }
  435. case "半年度":
  436. //提醒时间
  437. if tmpEdb.NoticeTime != "" {
  438. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  439. if len(noticeArr) >= 2 {
  440. noticeTime = noticeArr[1]
  441. }
  442. }
  443. //每半年度更新数据时间
  444. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  445. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
  446. isNotice = true
  447. }
  448. case "年度":
  449. //提醒时间
  450. if tmpEdb.NoticeTime != "" {
  451. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  452. if len(noticeArr) >= 2 {
  453. noticeTime = noticeArr[1]
  454. }
  455. }
  456. //每年度更新数据时间
  457. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  458. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
  459. isNotice = true
  460. }
  461. }
  462. if isNotice {
  463. taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(tmpEdb.TradeCode)
  464. //fmt.Println(taskName, ";", tmpEdb.SecName)
  465. //定时任务
  466. tmpTaskFunc := func(ctx context.Context) (funcErr error) {
  467. //方法执行结束后,移除定时任务
  468. defer func() {
  469. EdbTaskNameChannel <- taskName
  470. }()
  471. // 匿名方法内判断是否发送提醒,因为可能时间到的时候,发现
  472. funcIsNotice := false
  473. // 再次获取指标数据详情
  474. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  475. if tmpErr != nil {
  476. if tmpErr.Error() != utils.ErrNoRow() {
  477. funcErr = tmpErr
  478. return
  479. }
  480. }
  481. if utils.RunMode == "debug" && debugNoticeUserId > 0 {
  482. tmpEdb.UserId = debugNoticeUserId //测试环境的话,发送邮箱给嘉豪
  483. }
  484. //数据过期时间
  485. var funcDataDtTime time.Time
  486. //如果确实是有数据的
  487. if edbData != nil {
  488. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  489. funcDataDtTime = tmpDataDtTime
  490. }
  491. //提示频度文案
  492. notifyFrequency := "每日"
  493. switch tmpEdb.Frequency {
  494. case "周度":
  495. notifyFrequency = "每周"
  496. modifyDate := nowWeekLastDay //下次更新日期
  497. if tmpEdb.NoticeTime != "" {
  498. addDay := 7
  499. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  500. if len(noticeArr) >= 2 {
  501. noticeTime = noticeArr[1]
  502. }
  503. noticeWeek := noticeArr[0]
  504. switch noticeWeek {
  505. case "周一":
  506. addDay = 1
  507. case "周二":
  508. addDay = 2
  509. case "周三":
  510. addDay = 3
  511. case "周四":
  512. addDay = 4
  513. case "周五":
  514. addDay = 5
  515. case "周六":
  516. addDay = 6
  517. case "周日":
  518. addDay = 7
  519. }
  520. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  521. }
  522. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  523. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
  524. funcIsNotice = true
  525. }
  526. case "月度":
  527. notifyFrequency = "每月"
  528. addDay := 0
  529. modifyDate := nowMonthLastDay //下次更新日期
  530. if tmpEdb.NoticeTime != "" {
  531. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  532. if len(strArr) >= 2 {
  533. noticeTime = strArr[1]
  534. }
  535. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  536. if tmpErr != nil {
  537. funcErr = tmpErr
  538. }
  539. addDay = tmpAddDay - 1
  540. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  541. }
  542. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  543. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
  544. funcIsNotice = true
  545. }
  546. case "季度":
  547. notifyFrequency = "每季度"
  548. //提醒时间
  549. if tmpEdb.NoticeTime != "" {
  550. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  551. if len(noticeArr) >= 2 {
  552. noticeTime = noticeArr[1]
  553. }
  554. }
  555. //每季度更新数据时间
  556. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  557. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
  558. funcIsNotice = true
  559. }
  560. case "半年度":
  561. notifyFrequency = "每半年度"
  562. //提醒时间
  563. if tmpEdb.NoticeTime != "" {
  564. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  565. if len(noticeArr) >= 2 {
  566. noticeTime = noticeArr[1]
  567. }
  568. }
  569. //每半年度更新数据时间
  570. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  571. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
  572. funcIsNotice = true
  573. }
  574. case "年度":
  575. notifyFrequency = "每年"
  576. //提醒时间
  577. if tmpEdb.NoticeTime != "" {
  578. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  579. if len(noticeArr) >= 2 {
  580. noticeTime = noticeArr[1]
  581. }
  582. }
  583. //每年度更新数据时间
  584. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  585. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
  586. funcIsNotice = true
  587. }
  588. }
  589. //fmt.Println(tmpEdb.TradeCode, " funcIsNotice:", funcIsNotice)
  590. //如果还是要提醒
  591. if funcIsNotice {
  592. //用户微信openid列表数据
  593. openIdList := make([]*models.OpenIdList, 0)
  594. //获取用户信息
  595. isAdmin := true
  596. admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
  597. if err != nil {
  598. if err.Error() == utils.ErrNoRow() {
  599. isAdmin = false
  600. } else {
  601. return err
  602. }
  603. }
  604. if admin == nil {
  605. isAdmin = false
  606. }
  607. if isAdmin {
  608. if admin.Mobile == "" {
  609. } else {
  610. wxUser, err := models.GetWxUserByMobile(admin.Mobile)
  611. if err != nil {
  612. return err
  613. }
  614. if wxUser == nil {
  615. funcErr = errors.New("用户信息不存在:mobile:" + admin.Mobile)
  616. return err
  617. }
  618. tmpOpenidList, err := models.GetUserOpenidListByUserId(int(wxUser.UserId))
  619. if err != nil {
  620. return err
  621. }
  622. openIdList = tmpOpenidList
  623. }
  624. } else {
  625. tmpOpenidList, err := models.GetUserOpenidListByUserId(tmpEdb.UserId)
  626. if err != nil {
  627. return err
  628. }
  629. openIdList = tmpOpenidList
  630. }
  631. //发送消息
  632. if len(openIdList) <= 0 {
  633. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  634. return
  635. }
  636. first := "数据录入提醒"
  637. keyword1 := tmpEdb.SecName
  638. keyword2 := notifyFrequency + " " + tmpEdb.NoticeTime
  639. remark := tmpEdb.SecName + "该更新了"
  640. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  641. if err != nil {
  642. return err
  643. }
  644. //发送成功,记录发送日志
  645. {
  646. sendRecord := new(models.EdbinfoSendMsgRecord)
  647. sendRecord.UserId = tmpEdb.UserId
  648. sendRecord.TradeCode = tmpEdb.TradeCode
  649. sendRecord.CreateTime = time.Now()
  650. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  651. if err != nil {
  652. return err
  653. }
  654. }
  655. }
  656. return
  657. }
  658. //添加定时任务(没有设置通知时间就不进行定时任务通知了)
  659. spec := ``
  660. if noticeTime != "" {
  661. noticeArr := strings.Split(noticeTime, ":")
  662. if len(noticeArr) == 3 {
  663. //spec = ` */20 * * * * * `
  664. spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])
  665. //定时任务开始的时间
  666. tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)
  667. task.AddTask(taskName, tmpTask)
  668. tmpEdbTaskNameMap[taskName] = true
  669. }
  670. }
  671. }
  672. }
  673. //将当天的手工指标加入到手工指标池去
  674. EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
  675. //开启协程,用来清除定时任务
  676. go deleteTask()
  677. //如果当前定时任务执行次数大于0次,那么需要往手工指标定时任务停止channel写入数据,用来关闭昨天没有执行的的定时任务
  678. if EdbTaskRunNum > 0 {
  679. //清除昨天的数据
  680. EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  681. }
  682. //手工指标定时任务开始次数累加
  683. EdbTaskRunNum++
  684. return
  685. //fmt.Println(task.NewMapSorter())
  686. }
  687. // deleteTask 清除已通知的任务
  688. func deleteTask() {
  689. for {
  690. select {
  691. case taskName := <-EdbTaskNameChannel:
  692. task.DeleteTask(taskName)
  693. delete(EdbTaskNameMap, taskName)
  694. case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
  695. for taskName := range EdbTaskNameMap[dayStr] {
  696. task.DeleteTask(taskName)
  697. delete(EdbTaskNameMap, taskName)
  698. }
  699. break
  700. }
  701. }
  702. }