task.go 30 KB

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