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.
488 lines
12 KiB
488 lines
12 KiB
package publicmethod
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"crypto/sha1"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"key_performance_indicators/middleware/grocerystore"
|
|
"key_performance_indicators/models"
|
|
"key_performance_indicators/overall"
|
|
"math"
|
|
"math/big"
|
|
"reflect"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/mozillazg/go-pinyin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
//全局函数处理
|
|
//编号,纯数字
|
|
func TableNumber(class ...string) (number int64) {
|
|
result, _ := rand.Int(rand.Reader, big.NewInt(8999))
|
|
numberTeam := result.Int64() + 1000
|
|
numberStr := fmt.Sprintf("%v%v", time.Now().Unix(), numberTeam)
|
|
if len(class) > 0 {
|
|
resultLong, _ := rand.Int(rand.Reader, big.NewInt(8999999))
|
|
numberTeamLong := resultLong.Int64() + 1000000
|
|
numberStr = fmt.Sprintf("%v%v", time.Now().Unix(), numberTeamLong)
|
|
}
|
|
number, _ = strconv.ParseInt(numberStr, 10, 64)
|
|
return
|
|
}
|
|
|
|
//初始化map(泛型)
|
|
func MapOut[T GenericityVariable]() (data map[T]interface{}) {
|
|
data = make(map[T]interface{}) //必可不少,分配内存
|
|
return data
|
|
}
|
|
|
|
//时间搓转换成日期
|
|
/*
|
|
@timestamp 待转换的时间戳
|
|
@timeType 转换类型
|
|
*/
|
|
func UnixTimeToDay(timeStamp int64, timeType int) (dateStr string) {
|
|
timeTemplate := "2006-01-02 15:04:05" //常规类型
|
|
switch timeType {
|
|
case 1:
|
|
timeTemplate = "2006/01/02 15:04:05"
|
|
case 2:
|
|
timeTemplate = "2006/01/02 15:04"
|
|
case 3:
|
|
timeTemplate = "2006/01/02 15"
|
|
case 4:
|
|
timeTemplate = "2006/01/02"
|
|
case 5:
|
|
timeTemplate = "15:04:05"
|
|
case 6:
|
|
timeTemplate = "15:04"
|
|
case 7:
|
|
timeTemplate = "15"
|
|
case 8:
|
|
timeTemplate = "04:05"
|
|
case 9:
|
|
timeTemplate = "04"
|
|
case 10:
|
|
timeTemplate = "05"
|
|
case 11:
|
|
timeTemplate = "2006-01-02 15:04:05"
|
|
case 12:
|
|
timeTemplate = "2006-01-02 15:04"
|
|
case 13:
|
|
timeTemplate = "2006-01-02 15"
|
|
case 14:
|
|
timeTemplate = "2006-01-02"
|
|
case 15:
|
|
timeTemplate = "2006-01" //年月
|
|
case 16:
|
|
timeTemplate = "2006" //年
|
|
case 17:
|
|
timeTemplate = "01" //月
|
|
case 18:
|
|
timeTemplate = "02" //日
|
|
case 19: //季度
|
|
dayMonth := time.Unix(timeStamp, 0).Format("01")
|
|
datMonthFloat, datMonthFloatErr := strconv.ParseFloat(dayMonth, 10)
|
|
if datMonthFloatErr == nil {
|
|
dateStr = strconv.FormatFloat(math.Ceil(datMonthFloat/3), 'f', -1, 64)
|
|
}
|
|
dateStr = "1"
|
|
case 20:
|
|
timeTemplate = "20060102"
|
|
case 21:
|
|
timeTemplate = "200601"
|
|
default:
|
|
timeTemplate = "2006-01-02 15:04:05" //常规类型
|
|
}
|
|
dateStr = time.Unix(timeStamp, 0).Format(timeTemplate)
|
|
return
|
|
}
|
|
|
|
/*
|
|
日期转时间戳
|
|
*/
|
|
func DateToTimeStamp(dataStr string) (timeStamp int64, isTrue bool) {
|
|
isTrue = false
|
|
tmp := "2006-01-02 15:04:05"
|
|
res, err := time.ParseInLocation(tmp, dataStr, time.Local)
|
|
timeStamp = res.Unix()
|
|
if err == nil {
|
|
isTrue = true
|
|
}
|
|
return
|
|
}
|
|
|
|
//数据库查询翻页
|
|
func LimitPage(page, pageSize int) (offSet int) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
offSet = pageSize * (page - 1)
|
|
if offSet < 0 {
|
|
offSet = 0
|
|
}
|
|
return
|
|
}
|
|
|
|
//中文首字母大写
|
|
func ChineseFirstWordCapitalize(wordStr string) (firstWord string) {
|
|
pinYinSub := pinyin.NewArgs()
|
|
rows := pinyin.Pinyin(wordStr, pinYinSub)
|
|
for i := 0; i < len(rows); i++ {
|
|
if len(rows[i]) != 0 {
|
|
str := rows[i][0]
|
|
pi := str[0:1]
|
|
firstWord += string(bytes.ToUpper([]byte(pi)))
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// ZeroFillByStr
|
|
// @Description: 字符串补零
|
|
// @param str :需要操作的字符串
|
|
// @param resultLen 结果字符串的长度
|
|
// @param reverse true 为前置补零,false 为后置补零
|
|
// @return string
|
|
//
|
|
func ZeroFillByStr(str string, resultLen int, reverse bool) string {
|
|
if len(str) > resultLen || resultLen <= 0 {
|
|
return str
|
|
}
|
|
if reverse {
|
|
return fmt.Sprintf("%0*s", resultLen, str) //不足前置补零
|
|
}
|
|
result := str
|
|
for i := 0; i < resultLen-len(str); i++ {
|
|
result += "0"
|
|
}
|
|
return result
|
|
}
|
|
|
|
/*
|
|
*加密算法
|
|
*/
|
|
type Md5Encryption struct {
|
|
Code string `json:"code"`
|
|
AppKey string `json:"appKey"`
|
|
}
|
|
|
|
func (m *Md5Encryption) Md5EncryptionAlgorithm() (md5Val string) {
|
|
if m.AppKey == "" {
|
|
m.Md5EncryptionInit(m.Code)
|
|
}
|
|
// fmt.Printf("Code ====> %v\n", m.Code)
|
|
// fmt.Printf("AppKey ====> %v\n", m.AppKey)
|
|
|
|
mdNew := md5.New()
|
|
mdNew.Write([]byte(m.AppKey))
|
|
keyMd5 := fmt.Sprintf("%x", mdNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step1:--AppKey-加密->%v\n", keyMd5)
|
|
|
|
codeNewMd1 := md5.New()
|
|
codeNewMd1.Write([]byte(m.Code))
|
|
codeMd1 := fmt.Sprintf("%x", codeNewMd1.Sum(nil))
|
|
|
|
// fmt.Printf("Step2:--CodeString-加密->%v\n", codeMd1)
|
|
|
|
yiCeng := codeMd1 + keyMd5
|
|
|
|
// fmt.Printf("Step3:--CodeString+AppKey-->%v\n", yiCeng)
|
|
|
|
yiCengNew := md5.New()
|
|
yiCengNew.Write([]byte(yiCeng))
|
|
yiCengMd5 := fmt.Sprintf("%x", yiCengNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step4:--one-加密->%v\n", yiCengMd5)
|
|
|
|
erCeng := yiCengMd5 + m.AppKey
|
|
|
|
// fmt.Printf("Step5:--one + AppKey->%v\n", erCeng)
|
|
|
|
// fmt.Printf("AppKey ===2=> %v\n", m.AppKey)
|
|
|
|
erCengNew := md5.New()
|
|
erCengNew.Write([]byte(erCeng))
|
|
md5Val = fmt.Sprintf("%x", erCengNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step6:--therr--加密-->%v\n", md5Val)
|
|
|
|
// md5Val = codeMd1
|
|
return
|
|
}
|
|
|
|
func (m *Md5Encryption) Md5EncryptionAlgorithmFj() (md5Val string, outlog []string) {
|
|
if m.AppKey == "" {
|
|
m.Md5EncryptionInit(m.Code)
|
|
}
|
|
// fmt.Printf("Code ====> %v\n", m.Code)
|
|
// fmt.Printf("AppKey ====> %v\n", m.AppKey)
|
|
|
|
outlog = append(outlog, fmt.Sprintf("Code ====> %v", m.Code))
|
|
outlog = append(outlog, fmt.Sprintf("AppKey ====> %v", m.AppKey))
|
|
|
|
mdNew := md5.New()
|
|
mdNew.Write([]byte(m.AppKey))
|
|
keyMd5 := fmt.Sprintf("%x", mdNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step1:--AppKey-加密->%v\n", keyMd5)
|
|
outlog = append(outlog, fmt.Sprintf("Step1:--AppKey-加密->%v", keyMd5))
|
|
|
|
codeNewMd1 := md5.New()
|
|
codeNewMd1.Write([]byte(m.Code))
|
|
codeMd1 := fmt.Sprintf("%x", codeNewMd1.Sum(nil))
|
|
|
|
// fmt.Printf("Step2:--CodeString-加密->%v\n", codeMd1)
|
|
outlog = append(outlog, fmt.Sprintf("Step2:--CodeString-加密->%v", codeMd1))
|
|
|
|
yiCeng := codeMd1 + keyMd5
|
|
|
|
// fmt.Printf("Step3:--CodeString+AppKey-->%v\n", yiCeng)
|
|
outlog = append(outlog, fmt.Sprintf("Step3:--CodeString+AppKey-->%v", yiCeng))
|
|
|
|
yiCengNew := md5.New()
|
|
yiCengNew.Write([]byte(yiCeng))
|
|
yiCengMd5 := fmt.Sprintf("%x", yiCengNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step4:--one-加密->%v\n", yiCengMd5)
|
|
outlog = append(outlog, fmt.Sprintf("Step4:--one-加密->%v", yiCengMd5))
|
|
|
|
erCeng := yiCengMd5 + m.AppKey
|
|
|
|
// fmt.Printf("Step5:--one + AppKey->%v\n", erCeng)
|
|
outlog = append(outlog, fmt.Sprintf("Step5:--one + AppKey->%v", erCeng))
|
|
|
|
// fmt.Printf("AppKey ===2=> %v\n", m.AppKey)
|
|
|
|
erCengNew := md5.New()
|
|
erCengNew.Write([]byte(erCeng))
|
|
md5Val = fmt.Sprintf("%x", erCengNew.Sum(nil))
|
|
|
|
// fmt.Printf("Step6:--therr--加密-->%v\n", md5Val)
|
|
outlog = append(outlog, fmt.Sprintf("Step6:--therr--加密-->%v", md5Val))
|
|
|
|
// md5Val = codeMd1
|
|
return
|
|
}
|
|
|
|
//初始化程序
|
|
func (m *Md5Encryption) Md5EncryptionInit(code string) {
|
|
m.AppKey = overall.CONSTANT_CONFIG.Appsetup.AppKey
|
|
m.Code = code
|
|
}
|
|
|
|
//sha1算法
|
|
func Sha1Encryption(str string) string {
|
|
sha1 := sha1.New()
|
|
sha1.Write([]byte(str))
|
|
return hex.EncodeToString(sha1.Sum(nil))
|
|
}
|
|
|
|
//计算当前时间N个月后的时间
|
|
/*
|
|
@timeStamp 当前时间戳
|
|
@futureTime 正数多少时间后,负数多少时间前
|
|
*/
|
|
func GetFutureMonthTime(timeStamp int64, futureTime, class int) (dateStr int64) {
|
|
timeUnix := time.Unix(timeStamp, 0)
|
|
addDate := timeUnix.AddDate(0, 0, futureTime)
|
|
switch class {
|
|
case 2:
|
|
addDate = timeUnix.AddDate(0, futureTime, 0)
|
|
case 3:
|
|
addDate = timeUnix.AddDate(futureTime, 0, 0)
|
|
default:
|
|
addDate = timeUnix.AddDate(0, 0, futureTime)
|
|
}
|
|
|
|
dateStr = addDate.Unix()
|
|
return
|
|
}
|
|
|
|
//判断字符串是否在切片中 泛型
|
|
func IsInTrue[T GenericityVariable](key T, keyAry []T) (isTrue bool) {
|
|
isTrue = false
|
|
for _, v := range keyAry {
|
|
if v == key {
|
|
return true
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//获取两个切片得差集
|
|
func DifferenceSet[T GenericityVariable](one, two []T) []T {
|
|
var three []T
|
|
temp := map[T]struct{}{}
|
|
for _, val := range two {
|
|
if _, ok := temp[val]; !ok {
|
|
temp[val] = struct{}{}
|
|
}
|
|
}
|
|
for _, val := range one {
|
|
if _, ok := temp[val]; !ok {
|
|
three = append(three, val)
|
|
}
|
|
}
|
|
return three
|
|
}
|
|
|
|
//判断类型转换成
|
|
/*
|
|
字符转int
|
|
*/
|
|
func StringToInt(val interface{}) (int, error) {
|
|
switch val.(type) {
|
|
case string:
|
|
return strconv.Atoi(val.(string))
|
|
case int:
|
|
return val.(int), nil
|
|
default:
|
|
return 0, nil
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
/*
|
|
字符转int64
|
|
*/
|
|
func StringToInt64(val interface{}) (int64, error) {
|
|
switch val.(type) {
|
|
case string:
|
|
return strconv.ParseInt(val.(string), 10, 64)
|
|
case int:
|
|
return int64(val.(int)), nil
|
|
case int64:
|
|
return val.(int64), nil
|
|
default:
|
|
return 0, nil
|
|
}
|
|
return 0, nil
|
|
}
|
|
|
|
//获取人员TokenRedis 信息
|
|
func GetUserRedisToken(number string) (manCont models.ManCont, err error) {
|
|
redisMyContKey := fmt.Sprintf("ScanCode:Authentication:UserCont_%v_%v", overall.CONSTANT_CONFIG.RedisPrefixStr.Alias, number)
|
|
redisClient := grocerystore.RunRedis(overall.CONSTANT_REDIS5)
|
|
useMap, isTrue := redisClient.HashGetAll(redisMyContKey)
|
|
if isTrue == false {
|
|
err.Error()
|
|
return
|
|
}
|
|
// fmt.Printf("%v\n", useMap)
|
|
err = MapToStruct(useMap, &manCont, "json")
|
|
return
|
|
}
|
|
|
|
//获取人员Redis信息
|
|
func GetUserRedisCont(number string) (manCont models.ManCont, err error) {
|
|
redisMyContKey := fmt.Sprintf("ScanCode:Authentication:UserCont_%v_%v", overall.CONSTANT_CONFIG.RedisPrefixStr.Alias, number)
|
|
redisClient := grocerystore.RunRedis(overall.CONSTANT_REDIS5)
|
|
useMap, isTrue := redisClient.HashGetAll(redisMyContKey)
|
|
if isTrue == false {
|
|
err.Error()
|
|
return
|
|
}
|
|
// fmt.Printf("%v\n", useMap)
|
|
// var manInfo models.ManCont
|
|
err = MapToStruct(useMap, &manCont, "json")
|
|
// manCont = manInfo
|
|
return
|
|
}
|
|
|
|
//获取人员Redis信息返回指针
|
|
func GetUserRedisContPointer(number string) (manCont models.ManCont, err error) {
|
|
redisMyContKey := fmt.Sprintf("ScanCode:Authentication:UserCont_%v_%v", overall.CONSTANT_CONFIG.RedisPrefixStr.Alias, number)
|
|
redisClient := grocerystore.RunRedis(overall.CONSTANT_REDIS5)
|
|
useMap, isTrue := redisClient.HashGetAll(redisMyContKey)
|
|
if isTrue == false {
|
|
err.Error()
|
|
return
|
|
}
|
|
// fmt.Printf("%v\n", useMap)
|
|
// var manInfo models.ManCont
|
|
err = MapToStruct(useMap, &manCont, "json")
|
|
// manCont = manInfo
|
|
return
|
|
}
|
|
|
|
/*
|
|
map[string]string 转 struct
|
|
*/
|
|
func MapToStruct(mapString map[string]string, structInterface interface{}, tag string) (err error) {
|
|
strType := reflect.TypeOf(structInterface)
|
|
strValue := reflect.ValueOf(structInterface)
|
|
switch strType.Kind() {
|
|
case reflect.Ptr:
|
|
if strType.Elem().Kind() != reflect.Struct {
|
|
// fmt.Printf("1-->%v\n", strType.Elem().Kind().String())
|
|
return errors.New("需要*struct类型,却传入*" + strType.Elem().Kind().String() + "类型")
|
|
}
|
|
default:
|
|
// fmt.Printf("2-->%v\n", strType.Kind().String())
|
|
return errors.New("需要*struct类型,却传入" + strType.Kind().String() + "类型")
|
|
}
|
|
// fmt.Printf("strType--->%v---strValue---->%v\n", strType, strValue)
|
|
strTypeElem := strType.Elem()
|
|
strValueElem := strValue.Elem()
|
|
strTypeNum := strTypeElem.NumField()
|
|
// fmt.Printf("strTypeElem--->%v---strValueElem---->%v---------strTypeNum--------->%v\n", strTypeElem, strValueElem, strTypeNum)
|
|
for i := 0; i < strTypeNum; i++ {
|
|
fieldName := strTypeElem.Field(i).Name
|
|
if tag != "" {
|
|
fieldName = strTypeElem.Field(i).Tag.Get(tag)
|
|
}
|
|
value, ok := mapString[fieldName]
|
|
if !ok {
|
|
continue
|
|
}
|
|
// fmt.Printf("name--->%v---tag---->%v----value---->%v----ok---->%v\n", fieldName, tag, value, ok)
|
|
if strValueElem.Field(i).CanSet() {
|
|
// fmt.Printf("Type--->%v---value---->%v\n", strValueElem.Field(i).Type(), reflect.TypeOf(value))
|
|
valType := fmt.Sprintf("%v", strValueElem.Field(i).Type())
|
|
switch valType {
|
|
case "int":
|
|
valueInt, _ := strconv.Atoi(value)
|
|
strValueElem.Field(i).Set(reflect.ValueOf(valueInt))
|
|
case "int32":
|
|
valueInt32, _ := strconv.ParseInt(value, 10, 32)
|
|
strValueElem.Field(i).Set(reflect.ValueOf(int32(valueInt32)))
|
|
case "int64":
|
|
valueInt64, _ := strconv.ParseInt(value, 10, 64)
|
|
strValueElem.Field(i).Set(reflect.ValueOf(valueInt64))
|
|
case "float32":
|
|
valuefloat32, _ := strconv.ParseFloat(value, 32)
|
|
strValueElem.Field(i).Set(reflect.ValueOf(float32(valuefloat32)))
|
|
case "float64":
|
|
valuefloat64, _ := strconv.ParseFloat(value, 64)
|
|
strValueElem.Field(i).Set(reflect.ValueOf(valuefloat64))
|
|
case "string":
|
|
strValueElem.Field(i).Set(reflect.ValueOf(value))
|
|
default:
|
|
|
|
}
|
|
} else {
|
|
continue
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//翻页设置
|
|
func PageTurningSettings(gormDb *gorm.DB, page, pageSize int) *gorm.DB {
|
|
if page < 0 {
|
|
page = 1
|
|
}
|
|
if pageSize < 0 {
|
|
pageSize = 10
|
|
}
|
|
gormDb = gormDb.Limit(pageSize).Offset(LimitPage(page, pageSize))
|
|
return gormDb
|
|
}
|
|
|