package edb_refresh import ( "eta_gn/eta_index_lib/models/edb_refresh" "eta_gn/eta_index_lib/utils" "fmt" "time" ) // GetPreviousHalfHourDefaultConfigList // @Description: 获取上半个小时的默认配置列表 // @author: Roc // @datetime 2024-01-11 14:46:54 // @param source int // @param subSource int // @return list []*edb_refresh.EdbRefreshDefaultConfig // @return err error func GetPreviousHalfHourDefaultConfigList(source, subSource int) (list []*edb_refresh.EdbRefreshDefaultConfig, err error) { defer func() { if err != nil { fmt.Println(err) } }() //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年 refreshFrequencyList := []string{"每自然日", "每交易日", "每周", "每旬", "每月", "每季", "每半年", "每年"} now := time.Now() //now = time.Date(2023, 12, 31, 19, 10, 59, 0, time.Local) //now = time.Date(2023, 12, 31, 04, 10, 59, 0, time.Local) currTimeStr := getPreviousHalfHour(now) fmt.Println(currTimeStr) // 所有默认配置刷新项 list = make([]*edb_refresh.EdbRefreshDefaultConfig, 0) // 获取各个刷新频率的配置 for _, refreshFrequency := range refreshFrequencyList { // 获取刷新频率条件 condition, pars, isHandler := getRefreshFrequencyCondition(now, refreshFrequency) if !isHandler { // 可能是非交易日,所以过滤不处理 continue } condition += ` AND refresh_frequency = ? AND refresh_time = ? AND source = ? AND sub_source = ? ` pars = append(pars, refreshFrequency, currTimeStr, source, subSource) tmpList, tmpErr := edb_refresh.GetListByCondition(condition, pars) if tmpErr != nil { err = tmpErr return } list = append(list, tmpList...) } return } // Function to merge two maps func mergeMaps(dst map[string][]*edb_refresh.EdbInfoListAndRefreshConfig, src map[string][]*edb_refresh.EdbInfoListAndRefreshConfig) map[string][]*edb_refresh.EdbInfoListAndRefreshConfig { if dst == nil { return src } if src == nil { return dst } for k, v := range src { if dstk, ok := dst[k]; ok { dstk = append(dstk, v...) dst[k] = dstk } else { dst[k] = v } } return dst } // getRefreshFrequencyCondition // @Description: 根据时间和刷新频率获取条件 // @author: Roc // @datetime 2024-01-09 16:27:11 // @param now time.Time // @param refreshFrequency string // @return condition string // @return pars []interface{} // @return isHandler bool func getRefreshFrequencyCondition(now time.Time, refreshFrequency string) (condition string, pars []interface{}, isHandler bool) { isHandler = true var dayNum int var isLastDay bool //刷新频率,枚举值:每自然日、每交易日、每周、每旬、每月、每季、每半年、每年 switch refreshFrequency { case "每自然日": // 自然日不需要额外条件 return case "每交易日": // 周六日不处理 if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday { isHandler = false } return case "每周": currWeekDay := now.Weekday() if currWeekDay == time.Sunday { currWeekDay = 7 isLastDay = true } dayNum = int(currWeekDay) case "每旬": currDay := now.Day() if currDay <= 10 { dayNum = currDay // 如果是这旬的最后一天 if currDay == 10 { isLastDay = true } } else if currDay <= 20 { dayNum = currDay - 10 // 如果是这旬的最后一天 if currDay == 20 { isLastDay = true } } else { dayNum = currDay - 20 // 当月的最后一天 monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) // 如果是这旬的最后一天 if currDay == monthLastDay.Day() { isLastDay = true } } case "每月": // 当前日期 currDay := now.Day() dayNum = currDay // 当期的最后一天 monthLastDay := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) // 如果是这期的最后一天 if currDay == monthLastDay.Day() { isLastDay = true } case "每季": // 当期的第一天 ; 当期的最后一天 var startDay, endDay time.Time currMonth := now.Month() currDay := now.Day() if currMonth <= 3 { // 当季的第一天 startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local) // 当季的最后一天 endDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } else if currMonth <= 6 { // 当期的第一天 startDay = time.Date(now.Year(), 4, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } else if currMonth <= 9 { // 当期的第一天 startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } else { // 当期的第一天 startDay = time.Date(now.Year(), 10, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } // 计算这期的第一天和当日的天数 dayNum = utils.GetTimeSubDay(startDay, now) + 1 // 如果是这期的最后一天 if currMonth == endDay.Month() && currDay == endDay.Day() { isLastDay = true } case "每半年": // 当期的第一天 ; 当期的最后一天 var startDay, endDay time.Time currMonth := now.Month() currDay := now.Day() if currMonth <= 6 { // 当期的第一天 startDay = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } else { // 当期的第一天 startDay = time.Date(now.Year(), 7, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay = time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) } // 计算这期的第一天和当日的天数 dayNum = utils.GetTimeSubDay(startDay, now) + 1 // 如果是这期的最后一天 if currMonth == endDay.Month() && currDay == endDay.Day() { isLastDay = true } case "每年": currMonth := now.Month() currDay := now.Day() // 当期的第一天 startDay := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, time.Local) // 当期的最后一天 endDay := time.Date(now.Year()+1, 1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1) // 计算这期的第一天和当日的天数 dayNum = utils.GetTimeSubDay(startDay, now) + 1 // 如果是这期的最后一天 if currMonth == endDay.Month() && currDay == endDay.Day() { isLastDay = true } } // 如果是这期的最后一天,那么就是判断refresh_frequency_day是否配置为0,或者配置的天数大于这期的最大天数 if isLastDay { condition += ` AND ( refresh_frequency_day = ? OR refresh_frequency_day >= ? )` pars = append(pars, 0, dayNum) } else { // 如果不是这期的最后一天,那么就是判断refresh_frequency_day是否等于配置的天数 condition += ` AND refresh_frequency_day = ? ` pars = append(pars, dayNum) } return } // getPreviousHalfHour // @Description: 根据传入的时间获取该时间的前整半小时的时间字符串 // @author: Roc // @datetime 2024-01-09 14:27:34 // @param now time.Time // @return string func getPreviousHalfHour(now time.Time) string { minute := now.Minute() if minute >= 30 { return fmt.Sprintf("%02d:%02d", now.Hour(), 30) } return fmt.Sprintf("%02d:%02d", now.Hour(), 0) }