task.go 23 KB

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