From ce496044ac23b030d7b86469e1b71b77c6ef07c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 20 Dec 2022 09:28:34 +0800 Subject: [PATCH] - update --- utils/only/custom.go | 203 ------------------------------------------- utils/only/only.go | 8 -- utils/only/uuid.go | 18 ---- utils/rmb/rmb.go | 27 ------ 4 files changed, 256 deletions(-) delete mode 100644 utils/only/custom.go delete mode 100644 utils/only/only.go delete mode 100644 utils/only/uuid.go delete mode 100644 utils/rmb/rmb.go diff --git a/utils/only/custom.go b/utils/only/custom.go deleted file mode 100644 index 375b3f4a..00000000 --- a/utils/only/custom.go +++ /dev/null @@ -1,203 +0,0 @@ -package only - -import ( - "errors" - "fmt" - "github.com/dtapps/go-library/utils/gorandom" - "github.com/dtapps/go-library/utils/gotime" -) - -// GenerateId 生成18一编号 -func GenerateId(customId string) string { - currentTime := gotime.Current().Format() - one, err := generateIdOne(customId, currentTime) - if err == nil { - return one - } - two, err := generateIdTwo(customId, currentTime) - if err == nil { - return two - } - three, err := generateIdThree(customId, currentTime) - if err == nil { - return three - } - four, err := generateIdFour(customId, currentTime) - if err == nil { - return four - } - five, err := generateIdFive(customId, currentTime) - if err == nil { - return five - } - six, err := generateIdSix(customId) - return six -} - -// GenerateIdAndTime 生成18一编号 -func GenerateIdAndTime(customId, customTime string) string { - one, err := generateIdOne(customId, customTime) - if err == nil { - return one - } - two, err := generateIdTwo(customId, customTime) - if err == nil { - return two - } - three, err := generateIdThree(customId, customTime) - if err == nil { - return three - } - four, err := generateIdFour(customId, customTime) - if err == nil { - return four - } - five, err := generateIdFive(customId, customTime) - if err == nil { - return five - } - six, err := generateIdSix(customId) - return six -} - -// 生成18位时间[年月日时分]唯一编号 -func generateIdOne(customId, setTime string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度 - dataLength = 18 // 默认数据长度 - dateFormat = "200601021504" // 时间格式 - dateFormatLength = len(dateFormat) // 时间格式长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - (dateFormatLength + customIdLength) - - if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil -} - -// 生成18位时间[年月日时]唯一编号 -func generateIdTwo(customId, setTime string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度 - dataLength = 18 // 默认数据长度 - dateFormat = "2006010215" // 时间格式 - dateFormatLength = len(dateFormat) // 时间格式长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - (dateFormatLength + customIdLength) - - if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil -} - -// 生成18位时间[年月日]唯一编号 -func generateIdThree(customId, setTime string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度 - dataLength = 18 // 默认数据长度 - dateFormat = "20060102" // 时间格式 - dateFormatLength = len(dateFormat) // 时间格式长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - (dateFormatLength + customIdLength) - - if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil -} - -// 生成18位时间[年月]唯一编号 -func generateIdFour(customId, setTime string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度 - dataLength = 18 // 默认数据长度 - dateFormat = "200601" // 时间格式 - dateFormatLength = len(dateFormat) // 时间格式长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - (dateFormatLength + customIdLength) - - if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil -} - -// 生成18位时间[年]唯一编号 -func generateIdFive(customId, setTime string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度` - dataLength = 18 // 默认数据长度 - dateFormat = "2006" // 时间格式 - dateFormatLength = len(dateFormat) // 时间格式长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - (dateFormatLength + customIdLength) - - if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil -} - -// 生成18位随机唯一编号 -func generateIdSix(customId string) (string, error) { - - var ( - newRandomLength = 0 // 随机数据长度 - customIdLength = len(customId) // 自定义长度 - ) - - const ( - randomLength = 5 // 随机数据长度 - dataLength = 18 // 默认数据长度 - ) - - // 重新计算随机数据长度 - newRandomLength = dataLength - customIdLength - - if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) { - return "", errors.New("没有满足条件") - } - return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil -} diff --git a/utils/only/only.go b/utils/only/only.go deleted file mode 100644 index aad79c4e..00000000 --- a/utils/only/only.go +++ /dev/null @@ -1,8 +0,0 @@ -package only - -type Only struct { -} - -func NewOnly() *Only { - return &Only{} -} diff --git a/utils/only/uuid.go b/utils/only/uuid.go deleted file mode 100644 index 40d57b8c..00000000 --- a/utils/only/uuid.go +++ /dev/null @@ -1,18 +0,0 @@ -package only - -import ( - "crypto/rand" - "fmt" - "time" -) - -// GetUuId 由 32 个十六进制数字组成,以 6 个组显示,由连字符 - 分隔 -func GetUuId() string { - unix32bits := uint32(time.Now().UTC().Unix()) - buff := make([]byte, 12) - numRead, err := rand.Read(buff) - if numRead != len(buff) || err != nil { - return "" - } - return fmt.Sprintf("%x-%x-%x-%x-%x-%x", unix32bits, buff[0:2], buff[2:4], buff[4:6], buff[6:8], buff[8:]) -} diff --git a/utils/rmb/rmb.go b/utils/rmb/rmb.go deleted file mode 100644 index 66ce217d..00000000 --- a/utils/rmb/rmb.go +++ /dev/null @@ -1,27 +0,0 @@ -package rmb - -import ( - "fmt" - "github.com/shopspring/decimal" -) - -// Fen2Yuan 一分钱转一块钱 分转元 -func Fen2Yuan(price int64) string { - d := decimal.New(1, 2) //分除以100得到元 - result := decimal.NewFromInt(price).DivRound(d, 2).String() - return result -} - -// Yuan2Fen 元转分,乘以100后,保留整数部分 -func Yuan2Fen(price float64) int64 { - d := decimal.New(1, 2) //分转元乘以100 - d1 := decimal.New(1, 0) //乘完之后,保留2为小数,需要这么一个中间参数 - //df := decimal.NewFromFloat(price).Mul(d).DivRound(d1,2).String() - df := decimal.NewFromFloat(price).Mul(d).IntPart() - - //如下是满足,当乘以100后,仍然有小数位,取四舍五入法后,再取整数部分 - dff := decimal.NewFromFloat(price).Mul(d).DivRound(d1, 0).IntPart() - fmt.Printf("输入值为:%f, 简单的元转分后,取整数部分:%d\n", price, df) - fmt.Printf("输入值为:%f, 元转分后,若还有小数,需做四舍五入后,再取整数:%d\n", price, dff) - return df -}