You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.0 KiB
90 lines
2.0 KiB
|
1 year ago
|
package util
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"server/modules/backend/values"
|
||
|
|
"server/util"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 将查询日期的字符串转化成实际查询日期
|
||
|
|
func GetQueryDate(start, end string) (string, string) {
|
||
|
|
if start == "" {
|
||
|
|
start = values.StartDate
|
||
|
|
}
|
||
|
|
if end == "" {
|
||
|
|
end = time.Now().Format("2006-01-02")
|
||
|
|
}
|
||
|
|
e, _ := time.ParseInLocation("2006-01-02", end, time.Local)
|
||
|
|
return start, e.AddDate(0, 0, 1).Format("2006-01-02")
|
||
|
|
}
|
||
|
|
|
||
|
|
// 将查询日期的字符串转化成实际查询时间戳
|
||
|
|
func GetQueryUnix(start, end string) (int64, int64) {
|
||
|
|
var s, e int64
|
||
|
|
if start == "" {
|
||
|
|
// s = values.StartTime
|
||
|
|
s = util.GetZeroTime(time.Now().AddDate(0, -2, 0)).Unix() // 设定默认最多查询两个月
|
||
|
|
} else {
|
||
|
|
st, _ := time.ParseInLocation("2006-01-02", start, time.Local)
|
||
|
|
s = st.Unix()
|
||
|
|
}
|
||
|
|
if end == "" {
|
||
|
|
e = util.GetZeroTime(time.Now().AddDate(0, 0, 1)).Unix()
|
||
|
|
} else {
|
||
|
|
st, _ := time.ParseInLocation("2006-01-02", end, time.Local)
|
||
|
|
e = st.AddDate(0, 0, 1).Unix()
|
||
|
|
}
|
||
|
|
if s < values.StartTime {
|
||
|
|
s = values.StartTime
|
||
|
|
}
|
||
|
|
now := util.GetZeroTime(time.Now().AddDate(0, 0, 1)).Unix()
|
||
|
|
if e > now {
|
||
|
|
e = now
|
||
|
|
}
|
||
|
|
return s, e
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取分页查询参数,返回开始时间,结束时间,总数
|
||
|
|
func GetPageQuery(start, end string, page, num int) (int64, int64, int64) {
|
||
|
|
su, eu := GetQueryUnix(start, end)
|
||
|
|
e := eu - int64(((page-1)*num)*24*60*60)
|
||
|
|
s := e - int64((num-1)*24*60*60)
|
||
|
|
if s < su {
|
||
|
|
s = su
|
||
|
|
}
|
||
|
|
count := (eu - su) / (24 * 60 * 60)
|
||
|
|
return s, e, count
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取百分比
|
||
|
|
func GetPer(a, b int64) string {
|
||
|
|
if b == 0 {
|
||
|
|
return fmt.Sprintf("%d", a) + "%"
|
||
|
|
}
|
||
|
|
return util.FormatFloat(float64(a*100)/float64(b), 2) + "%"
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取比例小数点
|
||
|
|
func GetPoint(a, b int64) string {
|
||
|
|
if b == 0 {
|
||
|
|
return "0"
|
||
|
|
}
|
||
|
|
return util.FormatFloat(float64(a)/float64(b), 2)
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获取比例小数点
|
||
|
|
func GetPoint2(a, b int64) string {
|
||
|
|
if b == 0 {
|
||
|
|
return "0"
|
||
|
|
}
|
||
|
|
return util.FormatFloat(float64(a)/float64(b), 5)
|
||
|
|
}
|
||
|
|
|
||
|
|
func AddPerFloat(f float64, s string) float64 {
|
||
|
|
num, _ := strconv.ParseFloat(strings.ReplaceAll(s, "%", ""), 64)
|
||
|
|
return f + num
|
||
|
|
}
|