common.go
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package utils
import (
"fmt"
timeconv "github.com/Andrew-M-C/go.timeconv"
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"reflect"
"strings"
"time"
)
// ValidateCommand 验证输入参数
func ValidateCommand(commandType interface{}) error {
valid := validation.Validation{}
b, err := valid.Valid(commandType)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(commandType).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
if tag := field.Tag.Get("cname"); len(tag) > 0 {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, tag, -1))
} else {
return fmt.Errorf(validErr.Message)
}
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
// NextTime 0点时刻为标准计算
func NextTime(now0 time.Time, start *time.Time, kpiCycle int) time.Time {
// 起始时间0点时刻
start0 := time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, time.Local)
var nextTime time.Time
switch kpiCycle {
case domain.KpiCycleDay:
nextTime = timeconv.AddDate(now0, 0, 0, 1) // 当前时间的下一天0点开始发送
break
case domain.KpiCycleWeek:
offsetSeconds := int64(now0.Sub(start0).Seconds())
offsetDay := offsetSeconds / (24 * 60 * 60)
cycleCount := int(offsetDay)/7 + 1
nextTime = timeconv.AddDate(start0, 0, 0, cycleCount*7)
break
case domain.KpiCycleOneMonth:
nextTime = timeconv.AddDate(start0, 0, 1, 0)
break
case domain.KpiCycleTwoMonth:
nextTime = timeconv.AddDate(start0, 0, 2, 0)
break
case domain.KpiCycleThreeMonth:
nextTime = timeconv.AddDate(start0, 0, 3, 0)
break
case domain.KpiCycleSixMonth:
nextTime = timeconv.AddDate(start0, 0, 6, 0)
break
case domain.KpiCycleYear:
nextTime = timeconv.AddDate(start0, 1, 0, 0)
break
}
return nextTime
}
// NextTimeInc 0点时刻为标准计算
func NextTimeInc(start *time.Time, kpiCycle int) time.Time {
// 起始时间0点时刻
start0 := time.Date(start.Year(), start.Month(), start.Day(), 0, 0, 0, 0, time.Local)
var nextTime time.Time
switch kpiCycle {
case domain.KpiCycleDay:
nextTime = timeconv.AddDate(start0, 0, 0, 1) // 当前时间的下一天开始发送
break
case domain.KpiCycleWeek:
nextTime = timeconv.AddDate(start0, 0, 0, 7)
break
case domain.KpiCycleOneMonth:
nextTime = timeconv.AddDate(start0, 0, 1, 0)
break
case domain.KpiCycleTwoMonth:
nextTime = timeconv.AddDate(start0, 0, 2, 0)
break
case domain.KpiCycleThreeMonth:
nextTime = timeconv.AddDate(start0, 0, 3, 0)
break
case domain.KpiCycleSixMonth:
nextTime = timeconv.AddDate(start0, 0, 6, 0)
break
case domain.KpiCycleYear:
nextTime = timeconv.AddDate(start0, 1, 0, 0)
break
}
return nextTime
}