task.go 30 KB

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