edb_refresh.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package edb_refresh
  2. import (
  3. "eta_gn/eta_index_lib/models/edb_refresh"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "time"
  7. )
  8. // GetPreviousHalfHourDefaultConfigList
  9. // @Description: 获取上半个小时的默认配置列表
  10. // @author: Roc
  11. // @datetime 2024-01-11 14:46:54
  12. // @param source int
  13. // @param subSource int
  14. // @return list []*edb_refresh.EdbRefreshDefaultConfig
  15. // @return err error
  16. func GetPreviousHalfHourDefaultConfigList(source, subSource int) (list []*edb_refresh.EdbRefreshDefaultConfig, err error) {
  17. defer func() {
  18. if err != nil {
  19. fmt.Println(err)
  20. }
  21. }()
  22. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  23. refreshFrequencyList := []string{"每自然日", "每交易日", "每周", "每旬", "每月", "每季", "每半年", "每年"}
  24. now := time.Now()
  25. //now = time.Date(2023, 12, 31, 19, 10, 59, 0, time.Local)
  26. //now = time.Date(2023, 12, 31, 04, 10, 59, 0, time.Local)
  27. currTimeStr := getPreviousHalfHour(now)
  28. fmt.Println(currTimeStr)
  29. // 所有默认配置刷新项
  30. list = make([]*edb_refresh.EdbRefreshDefaultConfig, 0)
  31. // 获取各个刷新频率的配置
  32. for _, refreshFrequency := range refreshFrequencyList {
  33. // 获取刷新频率条件
  34. condition, pars, isHandler := getRefreshFrequencyCondition(now, refreshFrequency)
  35. if !isHandler {
  36. // 可能是非交易日,所以过滤不处理
  37. continue
  38. }
  39. condition += ` AND refresh_frequency = ? AND refresh_time = ? AND source = ? AND sub_source = ? `
  40. pars = append(pars, refreshFrequency, currTimeStr, source, subSource)
  41. tmpList, tmpErr := edb_refresh.GetListByCondition(condition, pars)
  42. if tmpErr != nil {
  43. err = tmpErr
  44. return
  45. }
  46. list = append(list, tmpList...)
  47. }
  48. return
  49. }
  50. // Function to merge two maps
  51. func mergeMaps(dst map[string][]*edb_refresh.EdbInfoListAndRefreshConfig, src map[string][]*edb_refresh.EdbInfoListAndRefreshConfig) map[string][]*edb_refresh.EdbInfoListAndRefreshConfig {
  52. if dst == nil {
  53. return src
  54. }
  55. if src == nil {
  56. return dst
  57. }
  58. for k, v := range src {
  59. if dstk, ok := dst[k]; ok {
  60. dstk = append(dstk, v...)
  61. dst[k] = dstk
  62. } else {
  63. dst[k] = v
  64. }
  65. }
  66. return dst
  67. }
  68. // getRefreshFrequencyCondition
  69. // @Description: 根据时间和刷新频率获取条件
  70. // @author: Roc
  71. // @datetime 2024-01-09 16:27:11
  72. // @param now time.Time
  73. // @param refreshFrequency string
  74. // @return condition string
  75. // @return pars []interface{}
  76. // @return isHandler bool
  77. func getRefreshFrequencyCondition(now time.Time, refreshFrequency string) (condition string, pars []interface{}, isHandler bool) {
  78. isHandler = true
  79. var dayNum int
  80. var isLastDay bool
  81. //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年
  82. switch refreshFrequency {
  83. case "每自然日":
  84. // 自然日不需要额外条件
  85. return
  86. case "每交易日":
  87. // 周六日不处理
  88. if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {
  89. isHandler = false
  90. }
  91. return
  92. case "每周":
  93. currWeekDay := now.Weekday()
  94. if currWeekDay == time.Sunday {
  95. currWeekDay = 7
  96. isLastDay = true
  97. }
  98. dayNum = int(currWeekDay)
  99. case "每旬":
  100. currDay := now.Day()
  101. if currDay <= 10 {
  102. dayNum = currDay
  103. // 如果是这旬的最后一天
  104. if currDay == 10 {
  105. isLastDay = true
  106. }
  107. } else if currDay <= 20 {
  108. dayNum = currDay - 10
  109. // 如果是这旬的最后一天
  110. if currDay == 20 {
  111. isLastDay = true
  112. }
  113. } else {
  114. dayNum = currDay - 20
  115. // 当月的最后一天
  116. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  117. // 如果是这旬的最后一天
  118. if currDay == monthLastDay.Day() {
  119. isLastDay = true
  120. }
  121. }
  122. case "每月":
  123. // 当前日期
  124. currDay := now.Day()
  125. dayNum = currDay
  126. // 当期的最后一天
  127. monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  128. // 如果是这期的最后一天
  129. if currDay == monthLastDay.Day() {
  130. isLastDay = true
  131. }
  132. case "每季":
  133. // 当期的第一天 ; 当期的最后一天
  134. var startDay, endDay time.Time
  135. currMonth := now.Month()
  136. currDay := now.Day()
  137. if currMonth <= 3 {
  138. // 当季的第一天
  139. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  140. // 当季的最后一天
  141. endDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  142. } else if currMonth <= 6 {
  143. // 当期的第一天
  144. startDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local)
  145. // 当期的最后一天
  146. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  147. } else if currMonth <= 9 {
  148. // 当期的第一天
  149. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  150. // 当期的最后一天
  151. endDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  152. } else {
  153. // 当期的第一天
  154. startDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local)
  155. // 当期的最后一天
  156. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  157. }
  158. // 计算这期的第一天和当日的天数
  159. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  160. // 如果是这期的最后一天
  161. if currMonth == endDay.Month() && currDay == endDay.Day() {
  162. isLastDay = true
  163. }
  164. case "每半年":
  165. // 当期的第一天 ; 当期的最后一天
  166. var startDay, endDay time.Time
  167. currMonth := now.Month()
  168. currDay := now.Day()
  169. if currMonth <= 6 {
  170. // 当期的第一天
  171. startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  172. // 当期的最后一天
  173. endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  174. } else {
  175. // 当期的第一天
  176. startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local)
  177. // 当期的最后一天
  178. endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  179. }
  180. // 计算这期的第一天和当日的天数
  181. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  182. // 如果是这期的最后一天
  183. if currMonth == endDay.Month() && currDay == endDay.Day() {
  184. isLastDay = true
  185. }
  186. case "每年":
  187. currMonth := now.Month()
  188. currDay := now.Day()
  189. // 当期的第一天
  190. startDay := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  191. // 当期的最后一天
  192. endDay := time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  193. // 计算这期的第一天和当日的天数
  194. dayNum = utils.GetTimeSubDay(startDay, now) + 1
  195. // 如果是这期的最后一天
  196. if currMonth == endDay.Month() && currDay == endDay.Day() {
  197. isLastDay = true
  198. }
  199. }
  200. // 如果是这期的最后一天,那么就是判断refresh_frequency_day是否配置为0,或者配置的天数大于这期的最大天数
  201. if isLastDay {
  202. condition += ` AND ( refresh_frequency_day = ? OR refresh_frequency_day >= ? )`
  203. pars = append(pars, 0, dayNum)
  204. } else {
  205. // 如果不是这期的最后一天,那么就是判断refresh_frequency_day是否等于配置的天数
  206. condition += ` AND refresh_frequency_day = ? `
  207. pars = append(pars, dayNum)
  208. }
  209. return
  210. }
  211. // getPreviousHalfHour
  212. // @Description: 根据传入的时间获取该时间的前整半小时的时间字符串
  213. // @author: Roc
  214. // @datetime 2024-01-09 14:27:34
  215. // @param now time.Time
  216. // @return string
  217. func getPreviousHalfHour(now time.Time) string {
  218. minute := now.Minute()
  219. if minute >= 30 {
  220. return fmt.Sprintf("%02d:%02d", now.Hour(), 30)
  221. }
  222. return fmt.Sprintf("%02d:%02d", now.Hour(), 0)
  223. }