59 lines
2.0 KiB
Go
59 lines
2.0 KiB
Go
package upstreamapi
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
historicalMonthLayout = "200601"
|
|
historicalDailyLayout = "20060102"
|
|
historicalMonthFormatError = "queryDate格式错误,应为yyyyMM"
|
|
historicalMonthRangeError = "月流量仅支持查询已结束月份;本月累计流量请调用 /sim/query/sim-datausage"
|
|
historicalDailyFormatError = "queryDate格式错误,应为yyyyMMdd"
|
|
historicalDailyRangeError = "日流量仅支持查询已结束日期,不支持当天或未来日期"
|
|
)
|
|
|
|
var historicalPeriodLocation = time.FixedZone("UTC+8", 8*60*60)
|
|
|
|
const currentBatchMonthRangeError = "电信月批量接口仅支持当月;历史月份请调用 /sim/query/sim-datausage-monthly"
|
|
|
|
func validateCurrentBatchMonth(queryDate string, now time.Time) string {
|
|
queryDate = strings.TrimSpace(queryDate)
|
|
if _, err := time.ParseInLocation(historicalMonthLayout, queryDate, historicalPeriodLocation); err != nil {
|
|
return historicalMonthFormatError
|
|
}
|
|
if queryDate != now.In(historicalPeriodLocation).Format(historicalMonthLayout) {
|
|
return currentBatchMonthRangeError
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func validateHistoricalMonth(queryDate string, now time.Time) string {
|
|
queryDate = strings.TrimSpace(queryDate)
|
|
if len(queryDate) != len(historicalMonthLayout) {
|
|
return historicalMonthFormatError
|
|
}
|
|
if _, err := time.ParseInLocation(historicalMonthLayout, queryDate, historicalPeriodLocation); err != nil {
|
|
return historicalMonthFormatError
|
|
}
|
|
if queryDate >= now.In(historicalPeriodLocation).Format(historicalMonthLayout) {
|
|
return historicalMonthRangeError
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func validateHistoricalDay(queryDate string, now time.Time) string {
|
|
queryDate = strings.TrimSpace(queryDate)
|
|
if len(queryDate) != len(historicalDailyLayout) {
|
|
return historicalDailyFormatError
|
|
}
|
|
if _, err := time.ParseInLocation(historicalDailyLayout, queryDate, historicalPeriodLocation); err != nil {
|
|
return historicalDailyFormatError
|
|
}
|
|
if queryDate >= now.In(historicalPeriodLocation).Format(historicalDailyLayout) {
|
|
return historicalDailyRangeError
|
|
}
|
|
return ""
|
|
}
|