edb_refresh.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_task/models/data_manage/edb_refresh"
  5. "eta/eta_task/services/alarm_msg"
  6. "eta/eta_task/services/data"
  7. "eta/eta_task/utils"
  8. "fmt"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. // ConfigRefreshData
  14. // @Description: 配置刷新数据
  15. // @author: Roc
  16. // @datetime 2024-01-10 13:55:05
  17. // @param cont context.Context
  18. // @return err error
  19. func ConfigRefreshData(cont context.Context) (err error) {
  20. errMsgList := make([]string, 0)
  21. defer func() {
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. }()
  26. // 一期是只做wind、同花顺、钢联、有色
  27. now := time.Now()
  28. //now = time.Date(2023, 12, 31, 19, 10, 59, 0, time.Local)
  29. //now = time.Date(2023, 12, 31, 16, 50, 59, 0, time.Local)
  30. defaultSourceEdbInfoListMap, err := getDefaultRefreshData(now)
  31. if err != nil {
  32. errMsgList = append(errMsgList, "获取默认刷新数据失败,Err:"+err.Error())
  33. }
  34. sourceEdbInfoListMap, err := getConfigRefreshData(now)
  35. if err != nil {
  36. errMsgList = append(errMsgList, "获取指标配置刷新数据失败,Err:"+err.Error())
  37. }
  38. // 将两个合并
  39. allSourceEdbInfoListMap := mergeMaps(defaultSourceEdbInfoListMap, sourceEdbInfoListMap)
  40. wgNum := len(allSourceEdbInfoListMap)
  41. if wgNum <= 0 {
  42. return
  43. }
  44. wg := sync.WaitGroup{}
  45. wg.Add(wgNum)
  46. for _, edbList := range allSourceEdbInfoListMap {
  47. go BaseRefreshData(&wg, edbList[0].Source, edbList[0].SubSource, edbList)
  48. }
  49. wg.Wait()
  50. fmt.Println("Refresh End")
  51. return
  52. }
  53. // Function to merge two maps
  54. func mergeMaps(dst map[string][]*edb_refresh.EdbInfoListAndRefreshConfig, src map[string][]*edb_refresh.EdbInfoListAndRefreshConfig) map[string][]*edb_refresh.EdbInfoListAndRefreshConfig {
  55. if dst == nil {
  56. return src
  57. }
  58. if src == nil {
  59. return dst
  60. }
  61. for k, v := range src {
  62. if dstk, ok := dst[k]; ok {
  63. dstk = append(dstk, v...)
  64. dst[k] = dstk
  65. } else {
  66. dst[k] = v
  67. }
  68. }
  69. return dst
  70. }
  71. // getDefaultRefreshData
  72. // @Description: 根据默认配置获取需要刷新的指标列表
  73. // @author: Roc
  74. // @datetime 2024-01-10 13:55:38
  75. // @param now time.Time
  76. // @return sourceEdbInfoListMap map[string][]*edb_refresh.EdbInfoListAndRefreshConfig
  77. // @return err error
  78. func getDefaultRefreshData(now time.Time) (sourceEdbInfoListMap map[string][]*edb_refresh.EdbInfoListAndRefreshConfig, err error) {
  79. errMsgList := make([]string, 0)
  80. defer func() {
  81. if err != nil {
  82. fmt.Println(err)
  83. }
  84. }()
  85. // 一期是只做wind、同花顺、钢联、有色
  86. sourceEdbInfoListMap = make(map[string][]*edb_refresh.EdbInfoListAndRefreshConfig)
  87. currTimeStr := getPreviousHalfHour(now)
  88. fmt.Println(currTimeStr)
  89. // 所有默认配置刷新项
  90. list := make([]*edb_refresh.EdbRefreshDefaultConfig, 0)
  91. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  92. refreshFrequencyList := []string{"每自然日", "每交易日", "每周", "每旬", "每月", "每季", "每半年", "每年"}
  93. // 获取各个刷新频率的配置
  94. for _, refreshFrequency := range refreshFrequencyList {
  95. // 获取刷新频率条件
  96. condition, pars, isHandler := getRefreshFrequencyCondition(now, refreshFrequency)
  97. if !isHandler {
  98. // 可能是非交易日,所以过滤不处理
  99. continue
  100. }
  101. condition += ` AND refresh_frequency = ? AND refresh_time = ?`
  102. pars = append(pars, refreshFrequency, currTimeStr)
  103. // 这两个是excel的数据源,他是从公共机更新的,需要过滤掉
  104. condition += ` AND source not in (?,?)`
  105. pars = append(pars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_YS)
  106. tmpList, tmpErr := edb_refresh.GetListByCondition(condition, pars)
  107. if tmpErr != nil {
  108. err = tmpErr
  109. return
  110. }
  111. list = append(list, tmpList...)
  112. }
  113. // 更新的单元格数
  114. refreshDataNumMap := make(map[string]*edb_refresh.EdbRefreshDefaultConfig)
  115. // 数据源刷新频度的列表数组
  116. refreshDataFrequencyListMap := make(map[int]map[int][]string)
  117. wgNum := 0
  118. // 处理待刷新的数据源,整理成数组,方便获取对应的指标
  119. for _, item := range list {
  120. // 更新的单元格数
  121. key := fmt.Sprintf("%d_%d_%s", item.Source, item.SubSource, item.Frequency)
  122. refreshDataNumMap[key] = item
  123. // 数据源刷新频度的列表数组
  124. subSourceFrequencyList, ok := refreshDataFrequencyListMap[item.Source]
  125. if !ok {
  126. subSourceFrequencyList = make(map[int][]string)
  127. }
  128. frequencyList, ok := subSourceFrequencyList[item.SubSource]
  129. if !ok {
  130. wgNum++
  131. frequencyList = make([]string, 0)
  132. }
  133. subSourceFrequencyList[item.SubSource] = append(frequencyList, item.Frequency)
  134. refreshDataFrequencyListMap[item.Source] = subSourceFrequencyList
  135. }
  136. for source, subSourceFrequencyListMap := range refreshDataFrequencyListMap {
  137. switch source {
  138. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_YS:
  139. // 这种不处理
  140. default:
  141. for subSource, frequencyList := range subSourceFrequencyListMap {
  142. edbList, tmpErr := edb_refresh.GetDefaultRefreshEdbInfoListBySourceAndSubSource(source, subSource, frequencyList)
  143. if tmpErr != nil {
  144. errMsgList = append(errMsgList, fmt.Sprint("source:", source, "subSource:", subSource, "frequencyList:", strings.Join(frequencyList, ","), "err:", tmpErr.Error()))
  145. }
  146. for _, v := range edbList {
  147. // 数据刷新的期数
  148. dataRefreshNum := utils.DATA_REFRESH
  149. key := fmt.Sprintf("%d_%d_%s", v.Source, v.SubSource, v.Frequency)
  150. if edbRefreshDefaultConfig, ok := refreshDataNumMap[key]; ok {
  151. if edbRefreshDefaultConfig.RefreshAllData == 1 { // 刷新所有数据期数
  152. dataRefreshNum = 0
  153. } else if edbRefreshDefaultConfig.RefreshDataNum > 0 { //
  154. dataRefreshNum = edbRefreshDefaultConfig.RefreshDataNum
  155. }
  156. }
  157. v.DataRefreshNum = dataRefreshNum
  158. }
  159. key := fmt.Sprint(source, "_", subSource)
  160. sourceEdbInfoListMap[key] = edbList
  161. }
  162. }
  163. }
  164. fmt.Println("Get Refresh End")
  165. return
  166. }
  167. // getConfigRefreshData
  168. // @Description: 根据指标配置获取需要刷新的指标列表
  169. // @author: Roc
  170. // @datetime 2024-01-10 13:55:59
  171. // @param now time.Time
  172. // @return sourceEdbInfoListMap map[string][]*edb_refresh.EdbInfoListAndRefreshConfig
  173. // @return err error
  174. func getConfigRefreshData(now time.Time) (sourceEdbInfoListMap map[string][]*edb_refresh.EdbInfoListAndRefreshConfig, err error) {
  175. defer func() {
  176. if err != nil {
  177. fmt.Println(err)
  178. }
  179. }()
  180. // 一期是只做wind、同花顺、钢联、有色
  181. sourceEdbInfoListMap = make(map[string][]*edb_refresh.EdbInfoListAndRefreshConfig)
  182. currTimeStr := getPreviousHalfHour(now)
  183. // 所有默认配置刷新项
  184. list := make([]*edb_refresh.EdbRefreshConfig, 0)
  185. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  186. refreshFrequencyList := []string{"每自然日", "每交易日", "每周", "每旬", "每月", "每季", "每半年", "每年"}
  187. // 获取各个刷新频率的配置
  188. for _, refreshFrequency := range refreshFrequencyList {
  189. // 获取刷新频率条件
  190. condition, pars, isHandler := getRefreshFrequencyCondition(now, refreshFrequency)
  191. if !isHandler {
  192. // 可能是非交易日,所以过滤不处理
  193. continue
  194. }
  195. condition += ` AND refresh_frequency = ? AND refresh_time = ?`
  196. pars = append(pars, refreshFrequency, currTimeStr)
  197. tmpList, tmpErr := edb_refresh.GetEdbRefreshConfigListByCondition(condition, pars)
  198. if tmpErr != nil {
  199. err = tmpErr
  200. return
  201. }
  202. list = append(list, tmpList...)
  203. }
  204. // 配置列表
  205. configIdEdbRefreshConfigMap := make(map[int]*edb_refresh.EdbRefreshConfig)
  206. configIdList := make([]int, 0)
  207. for _, v := range list {
  208. configIdList = append(configIdList, v.EdbRefreshConfigId)
  209. configIdEdbRefreshConfigMap[v.EdbRefreshConfigId] = v
  210. }
  211. edbInfoList, err := edb_refresh.GetConfigRefreshEdbInfoListBySourceAndSubSource([]int{utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_YS}, configIdList)
  212. if err != nil {
  213. return
  214. }
  215. for _, v := range edbInfoList {
  216. key := fmt.Sprint(v.Source, "_", v.SubSource)
  217. tmpList, ok := sourceEdbInfoListMap[key]
  218. if !ok {
  219. tmpList = make([]*edb_refresh.EdbInfoListAndRefreshConfig, 0)
  220. }
  221. // 数据刷新的期数
  222. dataRefreshNum := utils.DATA_REFRESH
  223. if edbRefreshConfig, ok2 := configIdEdbRefreshConfigMap[v.EdbRefreshConfigId]; ok2 {
  224. if edbRefreshConfig.RefreshAllData == 1 { // 刷新所有数据期数
  225. dataRefreshNum = 0
  226. } else if edbRefreshConfig.RefreshDataNum > 0 { //
  227. dataRefreshNum = edbRefreshConfig.RefreshDataNum
  228. }
  229. }
  230. v.DataRefreshNum = dataRefreshNum
  231. sourceEdbInfoListMap[key] = append(tmpList, v)
  232. }
  233. fmt.Println("Get ConfigRefreshData End")
  234. return
  235. }
  236. // BaseRefreshData
  237. // @Description: 基础数据刷新
  238. // @author: Roc
  239. // @datetime 2024-01-09 16:27:45
  240. // @param wg *sync.WaitGroup
  241. // @return err error
  242. func BaseRefreshData(wg *sync.WaitGroup, source, subSource int, items []*edb_refresh.EdbInfoListAndRefreshConfig) (err error) {
  243. errMsgList := make([]string, 0)
  244. defer func() {
  245. if err != nil {
  246. fmt.Println("来源:", source, ";子来源:", subSource, ";BaseRefreshData Err:"+err.Error())
  247. go alarm_msg.SendAlarmMsg(fmt.Sprint("来源:", source, ";子来源:", subSource, ";BaseRefreshData ErrMsg:"+err.Error()), 3)
  248. }
  249. if len(errMsgList) > 0 {
  250. errMsg := fmt.Sprint("来源:", source, ";子来源:", subSource, ";BaseRefreshData Err:"+strings.Join(errMsgList, "\n"))
  251. fmt.Println(errMsg)
  252. go alarm_msg.SendAlarmMsg(errMsg, 3)
  253. }
  254. wg.Done()
  255. }()
  256. // 数据刷新的期数
  257. dataRefreshNum := utils.DATA_REFRESH
  258. // 是否从最开始的日期更新
  259. var isRefreshByStartDate bool
  260. for _, v := range items {
  261. // 如果暂停更新,那就过滤
  262. if v.NoUpdate == 1 {
  263. continue
  264. }
  265. if v.DataRefreshNum > 0 {
  266. dataRefreshNum = v.DataRefreshNum
  267. }
  268. startDate := ""
  269. if isRefreshByStartDate {
  270. startDate = v.StartDate.Format(utils.FormatDate)
  271. } else {
  272. if v.Frequency == "日度" {
  273. startDate = v.EndDate.AddDate(0, 0, -dataRefreshNum).Format(utils.FormatDate)
  274. } else if v.Frequency == "周度" {
  275. startDate = v.EndDate.AddDate(0, 0, -(dataRefreshNum * 7)).Format(utils.FormatDate)
  276. } else if v.Frequency == "旬度" {
  277. startDate = v.EndDate.AddDate(0, 0, -(dataRefreshNum * 10)).Format(utils.FormatDate)
  278. } else if v.Frequency == "月度" {
  279. startDate = v.EndDate.AddDate(0, -dataRefreshNum, 0).Format(utils.FormatDate)
  280. } else if v.Frequency == "季度" {
  281. startDate = v.EndDate.AddDate(0, -dataRefreshNum*3, 0).Format(utils.FormatDate)
  282. } else if v.Frequency == "半年度" {
  283. startDate = v.EndDate.AddDate(0, -dataRefreshNum*6, 0).Format(utils.FormatDate)
  284. } else if v.Frequency == "年度" {
  285. startDate = v.EndDate.AddDate(-dataRefreshNum, 0, 0).Format(utils.FormatDate)
  286. } else {
  287. startDate = v.EndDate.AddDate(0, 0, -utils.DATA_REFRESH).Format(utils.FormatDate)
  288. }
  289. }
  290. fmt.Println(startDate)
  291. // 数据更新
  292. resp, tmpErr := data.RefreshEdbData(v.EdbInfoId, v.Source, v.SubSource, v.EdbCode, startDate)
  293. if tmpErr != nil {
  294. errMsgList = append(errMsgList, v.EdbCode+"RefreshEdbData Err:"+tmpErr.Error())
  295. continue
  296. }
  297. if resp.Ret != 200 {
  298. errMsgList = append(errMsgList, v.EdbCode+";RefreshEdbData Err:"+resp.Msg+";ErrMsg:"+resp.ErrMsg)
  299. continue
  300. }
  301. }
  302. fmt.Println("来源:", source, ";子来源:", subSource, "刷新结束")
  303. return err
  304. }
  305. // getRefreshFrequencyCondition
  306. // @Description: 根据时间和刷新频率获取条件
  307. // @author: Roc
  308. // @datetime 2024-01-09 16:27:11
  309. // @param now time.Time
  310. // @param refreshFrequency string
  311. // @return condition string
  312. // @return pars []interface{}
  313. // @return isHandler bool
  314. func getRefreshFrequencyCondition(now time.Time, refreshFrequency string) (condition string, pars []interface{}, isHandler bool) {
  315. isHandler = true
  316. var dayNum int
  317. var isLastDay bool
  318. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  319. switch refreshFrequency {
  320. case "每自然日":
  321. // 自然日不需要额外条件
  322. return
  323. case "每交易日":
  324. // 周六日不处理
  325. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  326. isHandler = false
  327. }
  328. return
  329. case "每周":
  330. currWeekDay := now.Weekday()
  331. if currWeekDay == time.Sunday {
  332. currWeekDay = 7
  333. isLastDay = true
  334. }
  335. dayNum = int(currWeekDay)
  336. case "每旬":
  337. currDay := now.Day()
  338. if currDay <= 10 {
  339. dayNum = currDay
  340. // 如果是这旬的最后一天
  341. if currDay == 10 {
  342. isLastDay = true
  343. }
  344. } else if currDay <= 20 {
  345. dayNum = currDay - 10
  346. // 如果是这旬的最后一天
  347. if currDay == 20 {
  348. isLastDay = true
  349. }
  350. } else {
  351. dayNum = currDay - 20
  352. // 当月的最后一天
  353. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  354. // 如果是这旬的最后一天
  355. if currDay == monthLastDay.Day() {
  356. isLastDay = true
  357. }
  358. }
  359. case "每月":
  360. // 当前日期
  361. currDay := now.Day()
  362. dayNum = currDay
  363. // 当期的最后一天
  364. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  365. // 如果是这期的最后一天
  366. if currDay == monthLastDay.Day() {
  367. isLastDay = true
  368. }
  369. case "每季":
  370. // 当期的第一天 ; 当期的最后一天
  371. var startDay, endDay time.Time
  372. currMonth := now.Month()
  373. currDay := now.Day()
  374. if currMonth <= 3 {
  375. // 当季的第一天
  376. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  377. // 当季的最后一天
  378. endDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  379. } else if currMonth <= 6 {
  380. // 当期的第一天
  381. startDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local)
  382. // 当期的最后一天
  383. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  384. } else if currMonth <= 9 {
  385. // 当期的第一天
  386. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  387. // 当期的最后一天
  388. endDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  389. } else {
  390. // 当期的第一天
  391. startDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local)
  392. // 当期的最后一天
  393. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  394. }
  395. // 计算这期的第一天和当日的天数
  396. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  397. // 如果是这期的最后一天
  398. if currMonth == endDay.Month() && currDay == endDay.Day() {
  399. isLastDay = true
  400. }
  401. case "每半年":
  402. // 当期的第一天 ; 当期的最后一天
  403. var startDay, endDay time.Time
  404. currMonth := now.Month()
  405. currDay := now.Day()
  406. if currMonth <= 6 {
  407. // 当期的第一天
  408. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  409. // 当期的最后一天
  410. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  411. } else {
  412. // 当期的第一天
  413. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  414. // 当期的最后一天
  415. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  416. }
  417. // 计算这期的第一天和当日的天数
  418. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  419. // 如果是这期的最后一天
  420. if currMonth == endDay.Month() && currDay == endDay.Day() {
  421. isLastDay = true
  422. }
  423. case "每年":
  424. currMonth := now.Month()
  425. currDay := now.Day()
  426. // 当期的第一天
  427. startDay := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  428. // 当期的最后一天
  429. endDay := time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  430. // 计算这期的第一天和当日的天数
  431. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  432. // 如果是这期的最后一天
  433. if currMonth == endDay.Month() && currDay == endDay.Day() {
  434. isLastDay = true
  435. }
  436. }
  437. // 如果是这期的最后一天,那么就是判断refresh_frequency_day是否配置为0,或者配置的天数大于这期的最大天数
  438. if isLastDay {
  439. condition += ` AND ( refresh_frequency_day = ? OR refresh_frequency_day >= ? )`
  440. pars = append(pars, 0, dayNum)
  441. } else {
  442. // 如果不是这期的最后一天,那么就是判断refresh_frequency_day是否等于配置的天数
  443. condition += ` AND refresh_frequency_day = ? `
  444. pars = append(pars, dayNum)
  445. }
  446. return
  447. }
  448. // getPreviousHalfHour
  449. // @Description: 根据传入的时间获取该时间的前整半小时的时间字符串
  450. // @author: Roc
  451. // @datetime 2024-01-09 14:27:34
  452. // @param now time.Time
  453. // @return string
  454. func getPreviousHalfHour(now time.Time) string {
  455. minute := now.Minute()
  456. if minute >= 30 {
  457. return fmt.Sprintf("%02d:%02d", now.Hour(), 30)
  458. }
  459. return fmt.Sprintf("%02d:%02d", now.Hour(), 0)
  460. }