task.go 28 KB

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