task.go 29 KB

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