task.go 26 KB

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