task.go 25 KB

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