task.go 21 KB

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