task.go 26 KB

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