task.go 23 KB

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