task.go 20 KB

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