task.go 25 KB

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