task.go 25 KB

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