task.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. notifyNeedAnswerQuestionToResearch := task.NewTask("notifyNeedAnswerQuestionToResearch", "0 0 10 * * *", NotifyNeedAnswerQuestionToResearch)
  237. task.AddTask("问答社区定时发送模版消息给研究员", notifyNeedAnswerQuestionToResearch)
  238. }
  239. //func TaskTest() {
  240. // fmt.Println("The task is start")
  241. //
  242. // //e, msg := data.InitTradePosition("shanghai", "2023-05-05", "2023-05-05")
  243. // //if e != nil {
  244. // // fmt.Println(e.Error())
  245. // // fmt.Println(msg)
  246. // //}
  247. //
  248. // //var ctx context.Context
  249. // //_ = EtaBusinessExpiredRemind(ctx)
  250. // //_ = EtaBusinessUpdateStatus(ctx)
  251. //
  252. // //task.StartTask()
  253. // fmt.Println("The task is end")
  254. //}
  255. func SendEmail(cont context.Context) (err error) {
  256. //报告历史访问次数
  257. go ReportViewTimes()
  258. //报告访问详情
  259. go ReportViewDetail()
  260. //用户权限统计
  261. go HongzeUsers()
  262. return
  263. }
  264. func OneMinute(cont context.Context) (err error) {
  265. //日度
  266. //FrequencyByDay()
  267. //周度
  268. FrequencyByWeek()
  269. //月度
  270. FrequencyByMonth()
  271. return
  272. }
  273. func RefreshData(cont context.Context) (err error) {
  274. wg := sync.WaitGroup{}
  275. wg.Add(16)
  276. //hour := time.Now().Hour()
  277. //if hour != 0 {
  278. //}
  279. go data.RefreshDataFromWind(&wg)
  280. //同花顺
  281. go data.RefreshDataFromThs(&wg)
  282. //彭博
  283. go data.RefreshDataFromPb(&wg)
  284. //彭博财务
  285. go data.RefreshDataFromPbFinance(&wg)
  286. //手工数据
  287. go data.RefreshDataFromManual(&wg)
  288. //隆众数据
  289. go data.RefreshDataFromLz(&wg)
  290. //有色
  291. go data.RefreshDataFromYs(&wg)
  292. //钢联
  293. go data.RefreshDataFromGl(&wg)
  294. //路透
  295. go data.RefreshDataFromLt(&wg)
  296. //煤炭
  297. go data.RefreshDataFromCoal(&wg)
  298. //谷歌出行数据
  299. go data.RefreshDataFromGoogleTravel(&wg)
  300. //钢联化工
  301. go data.RefreshDataFromMysteelChemical(&wg)
  302. //eia steo报告指标
  303. go data.RefreshDataFromEiaSteo(&wg)
  304. //UN报告指标
  305. go data.RefreshDataFromComTrade(&wg)
  306. //卓创报告指标
  307. go data.RefreshDataFromSci(&wg)
  308. //国家统计局指标
  309. go data.RefreshDataFromNationalStatistics(&wg)
  310. wg.Wait()
  311. ////计算指标
  312. data.RefreshDataFromCalculateAll()
  313. // 预测计算指标
  314. data.RefreshPredictDataFromCalculateAll()
  315. time.Sleep(5 * time.Second)
  316. data.RefreshNotice()
  317. fmt.Println("Refresh End")
  318. return
  319. }
  320. // RefreshTradeData 刷新交易所数据
  321. func RefreshTradeData(cont context.Context) (err error) {
  322. wg := sync.WaitGroup{}
  323. wg.Add(5)
  324. //郑商所
  325. go data.RefreshDataFromZz(&wg)
  326. //上期所
  327. go data.RefreshDataFromSh(&wg)
  328. //上期能源
  329. go data.RefreshDataFromShfe(&wg)
  330. //中金所
  331. go data.RefreshDataFromCffex(&wg)
  332. //大商所
  333. go data.RefreshDataFromDl(&wg)
  334. wg.Wait()
  335. //计算指标
  336. data.RefreshDataFromCalculateAll()
  337. // 计算预测指标
  338. data.RefreshPredictDataFromCalculateAll()
  339. return
  340. }
  341. // RefreshEicData 刷新欧洲天然气数据
  342. func RefreshEicData(cont context.Context) (err error) {
  343. wg := sync.WaitGroup{}
  344. wg.Add(1)
  345. //欧洲天然气
  346. go data.RefreshDataFromEic(&wg)
  347. wg.Wait()
  348. //计算指标
  349. data.RefreshDataFromCalculateAll()
  350. // 计算预测指标
  351. data.RefreshPredictDataFromCalculateAll()
  352. return
  353. }
  354. // 刷新基础数据
  355. func RefreshBaseData(cont context.Context) (err error) {
  356. now := time.Now()
  357. if now.Hour() == 0 || now.Hour() == 19 {
  358. return nil
  359. }
  360. //同步有色基础指标数据
  361. go data.SyncSmmIndexDataBase()
  362. return
  363. }
  364. // 刷新基础数据
  365. func SyncBaseData(cont context.Context) (err error) {
  366. now := time.Now()
  367. if now.Hour() == 0 || now.Hour() == 19 {
  368. return nil
  369. }
  370. //同步钢联基础数据
  371. go data.SyncGlDataBase()
  372. return
  373. }
  374. // 刷新基础数据
  375. func SyncBaseDataExt(cont context.Context) (err error) {
  376. now := time.Now()
  377. if now.Hour() == 0 || now.Hour() == 19 {
  378. return nil
  379. }
  380. //同步隆众基础数据
  381. go data.SyncLzDataBase()
  382. //同步手工数据
  383. go data.SyncManualDataBase()
  384. //同步有色基础数据
  385. go data.SyncSmmDataBase()
  386. //刷新图表中,指标的最新日期
  387. go data.SetChartEdbEndDate()
  388. return
  389. }
  390. // EdbTaskNameMap 手工指标定时任务名称map集合
  391. var EdbTaskNameMap map[string]map[string]bool
  392. // EdbTaskNameChannel 手工指标定时任务名称channel
  393. var EdbTaskNameChannel chan string
  394. // EdbTaskStopChannel 手工指标定时任务停止channel
  395. var EdbTaskStopChannel chan string
  396. // EdbTaskRunNum 手工指标定时任务开始次数
  397. var EdbTaskRunNum int
  398. // AddEdbTask 新增手工指标数据录入提醒
  399. func AddEdbTask(cont context.Context) (err error) {
  400. //失败列表
  401. failList := make([]string, 0)
  402. defer func() {
  403. if len(failList) > 0 {
  404. fmt.Println("提醒失败:")
  405. for _, v := range failList {
  406. fmt.Println(v)
  407. }
  408. }
  409. }()
  410. list, err := models.GetEdbInfoByFrequencyNotDay()
  411. if err != nil {
  412. fmt.Println("查询获取频度非日度 且 提醒时间不为空 的指标数据失败,Err:", err.Error())
  413. }
  414. //如果还没有初始化map,那么先初始
  415. if EdbTaskNameMap == nil {
  416. EdbTaskNameMap = make(map[string]map[string]bool)
  417. }
  418. tmpEdbTaskNameMap := make(map[string]bool)
  419. // 今天的日期字符串(格式:2021-10-25)
  420. todayStr := time.Now().Format(utils.FormatDate)
  421. //当前周的周一与周日
  422. nowWeekFirstDay := utils.GetNowWeekMonday()
  423. nowWeekLastDay := utils.GetNowWeekLastDay()
  424. //当前月的一号与最后一天
  425. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  426. nowMonthLastDay := utils.GetNowMonthLastDay()
  427. //当前季度的第一天与最后一天
  428. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  429. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  430. //当前半年的第一天与最后一天
  431. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  432. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  433. // 当前年的第一天与最后一天
  434. nowYearFirstDay := utils.GetNowYearFirstDay()
  435. nowYearLastDay := utils.GetNowYearLastDay()
  436. debugNoticeUserId := 0 //测试环境,需要发送消息的用户
  437. //测试环境也不发了
  438. //if utils.RunMode == "debug" {
  439. // tmpWxUser, tmpErr := models.GetWxUserByMobile("17634786714")
  440. // if tmpErr == nil && tmpWxUser != nil {
  441. // //debugNoticeUserId = 44078 //测试环境的话,发送邮箱给颜鹏
  442. // debugNoticeUserId = int(tmpWxUser.UserId) //测试环境的话,发送邮箱给嘉豪
  443. // }
  444. //}
  445. //task.globalTaskManager.adminTaskList
  446. for _, edb := range list {
  447. if edb.UserId <= 0 {
  448. continue //没有配置user_id的话,那么不需要提醒
  449. }
  450. tmpEdb := edb //指标信息
  451. isNotice := false //是否需要提醒
  452. noticeTime := "12:00:00" //提醒时间
  453. var dataDtTime time.Time
  454. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  455. if tmpErr != nil {
  456. if tmpErr.Error() != utils.ErrNoRow() {
  457. failList = append(failList, fmt.Sprint(tmpEdb.TradeCode, "失败,Err:", tmpErr.Error()))
  458. continue
  459. }
  460. }
  461. //如果确实是有数据的
  462. if edbData != nil {
  463. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  464. dataDtTime = tmpDataDtTime
  465. }
  466. switch tmpEdb.Frequency {
  467. case "周度":
  468. modifyDate := nowWeekLastDay //下次更新日期
  469. if tmpEdb.NoticeTime != "" {
  470. addDay := 7
  471. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  472. if len(noticeArr) >= 2 {
  473. noticeTime = noticeArr[1]
  474. }
  475. noticeWeek := noticeArr[0]
  476. switch noticeWeek {
  477. case "周一":
  478. addDay = 1
  479. case "周二":
  480. addDay = 2
  481. case "周三":
  482. addDay = 3
  483. case "周四":
  484. addDay = 4
  485. case "周五":
  486. addDay = 5
  487. case "周六":
  488. addDay = 6
  489. case "周日":
  490. addDay = 7
  491. }
  492. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  493. }
  494. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  495. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(dataDtTime) {
  496. isNotice = true
  497. }
  498. case "月度":
  499. addDay := 0
  500. modifyDate := nowMonthLastDay //下次更新日期
  501. if tmpEdb.NoticeTime != "" {
  502. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  503. if len(strArr) >= 2 {
  504. noticeTime = strArr[1]
  505. }
  506. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  507. if tmpErr != nil {
  508. continue
  509. }
  510. addDay = tmpAddDay - 1
  511. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  512. }
  513. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  514. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(dataDtTime) {
  515. isNotice = true
  516. }
  517. case "季度":
  518. //提醒时间
  519. if tmpEdb.NoticeTime != "" {
  520. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  521. if len(noticeArr) >= 2 {
  522. noticeTime = noticeArr[1]
  523. }
  524. }
  525. //每季度更新数据时间
  526. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  527. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(dataDtTime) {
  528. isNotice = true
  529. }
  530. case "半年度":
  531. //提醒时间
  532. if tmpEdb.NoticeTime != "" {
  533. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  534. if len(noticeArr) >= 2 {
  535. noticeTime = noticeArr[1]
  536. }
  537. }
  538. //每半年度更新数据时间
  539. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  540. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(dataDtTime) {
  541. isNotice = true
  542. }
  543. case "年度":
  544. //提醒时间
  545. if tmpEdb.NoticeTime != "" {
  546. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  547. if len(noticeArr) >= 2 {
  548. noticeTime = noticeArr[1]
  549. }
  550. }
  551. //每年度更新数据时间
  552. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  553. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(dataDtTime) {
  554. isNotice = true
  555. }
  556. }
  557. if isNotice {
  558. taskName := "edb_task_" + todayStr + ":" + fmt.Sprint(tmpEdb.TradeCode)
  559. //fmt.Println(taskName, ";", tmpEdb.SecName)
  560. //定时任务
  561. tmpTaskFunc := func(ctx context.Context) (funcErr error) {
  562. //方法执行结束后,移除定时任务
  563. defer func() {
  564. EdbTaskNameChannel <- taskName
  565. }()
  566. // 匿名方法内判断是否发送提醒,因为可能时间到的时候,发现
  567. funcIsNotice := false
  568. // 再次获取指标数据详情
  569. edbData, tmpErr := models.GetLastEdbdataInfo(tmpEdb.TradeCode)
  570. if tmpErr != nil {
  571. if tmpErr.Error() != utils.ErrNoRow() {
  572. funcErr = tmpErr
  573. return
  574. }
  575. }
  576. if utils.RunMode == "debug" && debugNoticeUserId > 0 {
  577. tmpEdb.UserId = debugNoticeUserId //测试环境的话,发送邮箱给嘉豪
  578. }
  579. //数据过期时间
  580. var funcDataDtTime time.Time
  581. //如果确实是有数据的
  582. if edbData != nil {
  583. tmpDataDtTime, _ := time.ParseInLocation(utils.FormatDate, edbData.Dt, time.Now().Location())
  584. funcDataDtTime = tmpDataDtTime
  585. }
  586. //提示频度文案
  587. notifyFrequency := "每日"
  588. switch tmpEdb.Frequency {
  589. case "周度":
  590. notifyFrequency = "每周"
  591. modifyDate := nowWeekLastDay //下次更新日期
  592. if tmpEdb.NoticeTime != "" {
  593. addDay := 7
  594. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  595. if len(noticeArr) >= 2 {
  596. noticeTime = noticeArr[1]
  597. }
  598. noticeWeek := noticeArr[0]
  599. switch noticeWeek {
  600. case "周一":
  601. addDay = 1
  602. case "周二":
  603. addDay = 2
  604. case "周三":
  605. addDay = 3
  606. case "周四":
  607. addDay = 4
  608. case "周五":
  609. addDay = 5
  610. case "周六":
  611. addDay = 6
  612. case "周日":
  613. addDay = 7
  614. }
  615. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  616. }
  617. //如果正好是提醒日,同时本周没有过记录,那么需要提醒
  618. if todayStr == modifyDate.Format(utils.FormatDate) && !nowWeekFirstDay.Before(funcDataDtTime) {
  619. funcIsNotice = true
  620. }
  621. case "月度":
  622. notifyFrequency = "每月"
  623. addDay := 0
  624. modifyDate := nowMonthLastDay //下次更新日期
  625. if tmpEdb.NoticeTime != "" {
  626. strArr := strings.Split(tmpEdb.NoticeTime, "日")
  627. if len(strArr) >= 2 {
  628. noticeTime = strArr[1]
  629. }
  630. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  631. if tmpErr != nil {
  632. funcErr = tmpErr
  633. }
  634. addDay = tmpAddDay - 1
  635. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  636. }
  637. //如果正好是提醒日,同时本月没有过记录,那么需要提醒
  638. if todayStr == modifyDate.Format(utils.FormatDate) && !nowMonthFirstDay.Before(funcDataDtTime) {
  639. funcIsNotice = true
  640. }
  641. case "季度":
  642. notifyFrequency = "每季度"
  643. //提醒时间
  644. if tmpEdb.NoticeTime != "" {
  645. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  646. if len(noticeArr) >= 2 {
  647. noticeTime = noticeArr[1]
  648. }
  649. }
  650. //每季度更新数据时间
  651. //如果正好是提醒日(每季度最后一天),同时本季度没有过记录,那么需要提醒
  652. if todayStr == nowQuarterLastDay.Format(utils.FormatDate) && !nowQuarterFirstDay.Before(funcDataDtTime) {
  653. funcIsNotice = true
  654. }
  655. case "半年度":
  656. notifyFrequency = "每半年度"
  657. //提醒时间
  658. if tmpEdb.NoticeTime != "" {
  659. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  660. if len(noticeArr) >= 2 {
  661. noticeTime = noticeArr[1]
  662. }
  663. }
  664. //每半年度更新数据时间
  665. //如果正好是提醒日(每半年度最后一天),同时本半年度没有过记录,那么需要提醒
  666. if todayStr == nowHalfYearLastDay.Format(utils.FormatDate) && !nowHalfYearFirstDay.Before(funcDataDtTime) {
  667. funcIsNotice = true
  668. }
  669. case "年度":
  670. notifyFrequency = "每年"
  671. //提醒时间
  672. if tmpEdb.NoticeTime != "" {
  673. noticeArr := strings.Split(tmpEdb.NoticeTime, " ")
  674. if len(noticeArr) >= 2 {
  675. noticeTime = noticeArr[1]
  676. }
  677. }
  678. //每年度更新数据时间
  679. //如果正好是提醒日(每年度最后一天),同时半年度没有过记录,那么需要提醒
  680. if todayStr == nowYearLastDay.Format(utils.FormatDate) && !nowYearFirstDay.Before(funcDataDtTime) {
  681. funcIsNotice = true
  682. }
  683. }
  684. //fmt.Println(tmpEdb.TradeCode, " funcIsNotice:", funcIsNotice)
  685. //如果还是要提醒
  686. if funcIsNotice {
  687. //用户微信openid列表数据
  688. openIdList := make([]*models.OpenIdList, 0)
  689. //获取用户信息
  690. admin, err := models.GetAdminByAdminId(tmpEdb.UserId)
  691. if err != nil {
  692. if err.Error() == utils.ErrNoRow() {
  693. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  694. return
  695. } else {
  696. return err
  697. }
  698. }
  699. if admin == nil {
  700. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  701. return
  702. }
  703. //发送消息
  704. if admin.OpenId == "" {
  705. funcErr = errors.New("openId 列表为空" + strconv.Itoa(tmpEdb.UserId))
  706. return
  707. }
  708. openIdTemp := new(models.OpenIdList)
  709. openIdTemp.OpenId = admin.OpenId
  710. openIdList = append(openIdList, openIdTemp)
  711. first := "数据录入提醒"
  712. keyword1 := tmpEdb.SecName + "该更新了"
  713. keyword2 := notifyFrequency + " " + tmpEdb.NoticeTime
  714. remark := tmpEdb.SecName + "该更新了"
  715. err = SendWxMsgWithFrequency(first, keyword1, keyword2, remark, openIdList)
  716. if err != nil {
  717. return err
  718. }
  719. //发送成功,记录发送日志
  720. {
  721. sendRecord := new(models.EdbinfoSendMsgRecord)
  722. sendRecord.UserId = tmpEdb.UserId
  723. sendRecord.TradeCode = tmpEdb.TradeCode
  724. sendRecord.CreateTime = time.Now()
  725. err = models.AddEdbinfoSendMsgRecord(sendRecord)
  726. if err != nil {
  727. return err
  728. }
  729. }
  730. }
  731. return
  732. }
  733. //添加定时任务(没有设置通知时间就不进行定时任务通知了)
  734. spec := ``
  735. if noticeTime != "" {
  736. noticeArr := strings.Split(noticeTime, ":")
  737. if len(noticeArr) == 3 {
  738. //spec = ` */20 * * * * * `
  739. spec = fmt.Sprintf(` %s %s %s * * * `, noticeArr[2], noticeArr[1], noticeArr[0])
  740. //定时任务开始的时间
  741. tmpTask := task.NewTask(taskName, spec, tmpTaskFunc)
  742. task.AddTask(taskName, tmpTask)
  743. tmpEdbTaskNameMap[taskName] = true
  744. }
  745. }
  746. }
  747. }
  748. //将当天的手工指标加入到手工指标池去
  749. EdbTaskNameMap[todayStr] = tmpEdbTaskNameMap
  750. //开启协程,用来清除定时任务
  751. go deleteTask()
  752. //如果当前定时任务执行次数大于0次,那么需要往手工指标定时任务停止channel写入数据,用来关闭昨天没有执行的的定时任务
  753. if EdbTaskRunNum > 0 {
  754. //清除昨天的数据
  755. EdbTaskStopChannel <- time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  756. }
  757. //手工指标定时任务开始次数累加
  758. EdbTaskRunNum++
  759. return
  760. //fmt.Println(task.NewMapSorter())
  761. }
  762. // deleteTask 清除已通知的任务
  763. func deleteTask() {
  764. for {
  765. select {
  766. case taskName := <-EdbTaskNameChannel:
  767. task.DeleteTask(taskName)
  768. delete(EdbTaskNameMap, taskName)
  769. case dayStr := <-EdbTaskStopChannel: //收到停止信号,先清除掉那一天的定时任务,
  770. for taskName := range EdbTaskNameMap[dayStr] {
  771. task.DeleteTask(taskName)
  772. delete(EdbTaskNameMap, taskName)
  773. }
  774. break
  775. }
  776. }
  777. }
  778. // 检测数据服务
  779. func checkDataServer(cont context.Context) (err error) {
  780. //检测wind新服务器
  781. go data.CheckWindDataInterface(cont)
  782. //检测同花顺数据服务器
  783. //go data.CheckThsDataInterface(cont)
  784. //检测路透数据服务器
  785. go data.CheckLtDataInterface(cont)
  786. //检测彭博
  787. go data.CheckPbDataInterface(cont)
  788. return
  789. }