task.go 24 KB

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