task.go 30 KB

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