task.go 28 KB

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