|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"insure/global"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GenerateNo(noType string) string {
|
|
|
|
now := time.Now()
|
|
|
|
key := "no_" + noType + ":" + now.Format("200601021504")
|
|
|
|
cxt := context.Background()
|
|
|
|
val := global.Redis.Incr(cxt, key).Val()
|
|
|
|
fmt.Println("val:", val)
|
|
|
|
global.Redis.ExpireAt(cxt, key, now.Add(time.Minute))
|
|
|
|
valStr := strconv.Itoa(int(val))
|
|
|
|
fmt.Println("valStr:", valStr)
|
|
|
|
l := len(valStr)
|
|
|
|
valStr = strings.Repeat("0", l) + valStr
|
|
|
|
return now.Format("20060102150405") + valStr
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateRandomString(length int) string {
|
|
|
|
charSet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
result := make([]byte, length)
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
result[i] = charSet[rand.Intn(len(charSet))]
|
|
|
|
}
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AmountToUpper(num float64) string {
|
|
|
|
strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
|
|
|
|
sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
|
|
|
|
|
|
|
|
s := sliceUnit[len(sliceUnit)-len(strNum) : len(sliceUnit)]
|
|
|
|
upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
|
|
|
|
str := ""
|
|
|
|
for k, v := range strNum[:] {
|
|
|
|
str = str + upperDigitUnit[string(v)] + s[k]
|
|
|
|
}
|
|
|
|
reg, err := regexp.Compile(`零角零分$`)
|
|
|
|
str = reg.ReplaceAllString(str, "整")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零角`)
|
|
|
|
str = reg.ReplaceAllString(str, "零")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零分$`)
|
|
|
|
str = reg.ReplaceAllString(str, "整")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零[仟佰拾]`)
|
|
|
|
str = reg.ReplaceAllString(str, "零")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零{2,}`)
|
|
|
|
str = reg.ReplaceAllString(str, "零")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零亿`)
|
|
|
|
str = reg.ReplaceAllString(str, "亿")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零万`)
|
|
|
|
str = reg.ReplaceAllString(str, "万")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零*元`)
|
|
|
|
str = reg.ReplaceAllString(str, "元")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`亿零{0, 3}万`)
|
|
|
|
str = reg.ReplaceAllString(str, "^元")
|
|
|
|
|
|
|
|
reg, err = regexp.Compile(`零元`)
|
|
|
|
str = reg.ReplaceAllString(str, "零")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|