task.go 23 KB

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