You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB
Go

5 months ago
package utils
import (
"context"
3 months ago
"fmt"
5 months ago
"insure/global"
4 months ago
"log"
5 months ago
"math/rand"
4 months ago
"regexp"
5 months ago
"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()
3 months ago
fmt.Println("val:", val)
5 months ago
global.Redis.ExpireAt(cxt, key, now.Add(time.Minute))
valStr := strconv.Itoa(int(val))
3 months ago
fmt.Println("valStr:", valStr)
5 months ago
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)
}
4 months ago
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
}