task.go 29 KB

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