task.go 28 KB

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