task.go 24 KB

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