task.go 24 KB

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