审查视图

pkg/utils/common.go 4.8 KB
1 2 3
package utils

import (
tangxvhui authored
4
	"errors"
郑周 authored
5
	"math"
6
	"reflect"
7
	"strconv"
8
	"strings"
郑周 authored
9
	"time"
tangxvhui authored
10 11 12 13

	timeconv "github.com/Andrew-M-C/go.timeconv"
	"github.com/beego/beego/v2/core/validation"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
)

// 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 {
tangxvhui authored
29
					return errors.New(strings.Replace(validErr.Message, validErr.Field, tag, -1))
30
				} else {
tangxvhui authored
31
					return errors.New(validErr.Message)
32 33
				}
			} else {
tangxvhui authored
34
				return errors.New(validErr.Message)
35 36 37 38 39
			}
		}
	}
	return nil
}
郑周 authored
40
41 42 43 44 45 46 47 48 49 50
// FormatFloatDecimal 格式化小数点位数
func FormatFloatDecimal(num float64, decimal int) string {
	d := float64(1)
	if decimal > 0 {
		d = math.Pow10(decimal) // 10的N次方
	}
	// math.trunc作用就是返回浮点数的整数部分
	return strconv.FormatFloat(math.Trunc(num*d)/d, 'f', -1, 64)
}
郑周 authored
51
// NextTime 0点时刻为标准计算
52
func NextTime(now0 time.Time, start time.Time, kpiCycle int) time.Time {
郑周 authored
53
	year, month, day := start.Date()
郑周 authored
54
	// 起始时间0点时刻
郑周 authored
55
	start0 := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
郑周 authored
56 57 58 59

	var nextTime time.Time
	switch kpiCycle {
	case domain.KpiCycleDay:
郑周 authored
60
		nextTime = now0 // 当前时间的0点开始发送
郑周 authored
61 62 63 64 65 66 67 68
		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:
郑周 authored
69 70 71 72 73
		offsetMonth := SubMonth(now0, start0)
		nextTime = timeconv.AddDate(start0, 0, offsetMonth, 0)
		if nextTime.Before(now0) {
			nextTime = timeconv.AddDate(nextTime, 0, 1, 0)
		}
郑周 authored
74 75
		break
	case domain.KpiCycleTwoMonth:
郑周 authored
76 77 78 79 80 81 82 83 84 85 86
		offsetMonth := SubMonth(now0, start0)
		multiple := float64(offsetMonth) / 2
		if multiple == 0 {
			multiple = 1
		} else {
			multiple = math.Ceil(multiple)
		}
		nextTime = timeconv.AddDate(start0, 0, int(multiple)*2, 0)
		if nextTime.Before(now0) {
			nextTime = timeconv.AddDate(nextTime, 0, 2, 0)
		}
郑周 authored
87 88
		break
	case domain.KpiCycleThreeMonth:
郑周 authored
89 90 91 92 93 94 95 96 97 98 99
		offsetMonth := SubMonth(now0, start0)
		multiple := float64(offsetMonth) / 3
		if multiple == 0 {
			multiple = 1
		} else {
			multiple = math.Ceil(multiple)
		}
		nextTime = timeconv.AddDate(start0, 0, int(multiple)*3, 0)
		if nextTime.Before(now0) {
			nextTime = timeconv.AddDate(nextTime, 0, 3, 0)
		}
郑周 authored
100 101
		break
	case domain.KpiCycleSixMonth:
郑周 authored
102 103 104 105 106 107 108 109 110 111 112
		offsetMonth := SubMonth(now0, start0)
		multiple := float64(offsetMonth) / 6
		if multiple == 0 {
			multiple = 1
		} else {
			multiple = math.Ceil(multiple)
		}
		nextTime = timeconv.AddDate(start0, 0, int(multiple)*6, 0)
		if nextTime.Before(now0) {
			nextTime = timeconv.AddDate(nextTime, 0, 6, 0)
		}
郑周 authored
113 114
		break
	case domain.KpiCycleYear:
郑周 authored
115 116 117 118 119 120 121 122 123 124 125
		offsetMonth := SubMonth(now0, start0)
		multiple := float64(offsetMonth) / 12
		if multiple == 0 {
			multiple = 1
		} else {
			multiple = math.Ceil(multiple)
		}
		nextTime = timeconv.AddDate(start0, 0, int(multiple)*12, 0)
		if nextTime.Before(now0) {
			nextTime = timeconv.AddDate(nextTime, 0, 12, 0)
		}
郑周 authored
126 127 128 129 130
		break
	}
	return nextTime
}
郑周 authored
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
// SubMonth 计算日期相差多少月
func SubMonth(t1, t2 time.Time) (month int) {
	y1 := t1.Year()
	y2 := t2.Year()
	m1 := int(t1.Month())
	m2 := int(t2.Month())
	d1 := t1.Day()
	d2 := t2.Day()

	yearInterval := y1 - y2
	// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
	if m1 < m2 || (m1 == m2 && d1 < d2) {
		yearInterval--
	}
	// 获取月数差值
	monthInterval := (m1 + 12) - m2
	if d1 < d2 {
		monthInterval--
	}
	monthInterval %= 12
	month = yearInterval*12 + monthInterval
	return
}
郑周 authored
155
// NextTimeInc 0点时刻为标准计算
156
func NextTimeInc(start time.Time, kpiCycle int) time.Time {
郑周 authored
157
	year, month, day := start.Date()
郑周 authored
158
	// 起始时间0点时刻
郑周 authored
159
	start0 := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
郑周 authored
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	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
}