task.go 21 KB

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