task.go 21 KB

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