- update string

master
李光春 1 year ago
parent 6961abb4c7
commit 4a419f409b

@ -1,5 +1,5 @@
package go_library
func Version() string {
return "1.0.87"
return "1.0.88"
}

@ -1,203 +1,58 @@
package gostring
import (
"errors"
"fmt"
"github.com/dtapps/go-library/utils/gorandom"
"github.com/dtapps/go-library/utils/gotime"
)
// GenerateId 生成18编号
// GenerateId 生成18编号
func GenerateId(customId string) string {
currentTime := gotime.Current().Format()
one, err := generateIdOne(customId, currentTime)
one, err := generateIdOne(customId, currentTime, 18)
if err == nil {
return one
}
two, err := generateIdTwo(customId, currentTime)
two, err := generateIdTwo(customId, currentTime, 18)
if err == nil {
return two
}
three, err := generateIdThree(customId, currentTime)
three, err := generateIdThree(customId, currentTime, 18)
if err == nil {
return three
}
four, err := generateIdFour(customId, currentTime)
four, err := generateIdFour(customId, currentTime, 18)
if err == nil {
return four
}
five, err := generateIdFive(customId, currentTime)
five, err := generateIdFive(customId, currentTime, 18)
if err == nil {
return five
}
six, err := generateIdSix(customId)
six, err := generateIdSix(customId, 18)
return six
}
// GenerateIdAndTime 生成18编号
// GenerateIdAndTime 生成18编号
func GenerateIdAndTime(customId, customTime string) string {
one, err := generateIdOne(customId, customTime)
one, err := generateIdOne(customId, customTime, 18)
if err == nil {
return one
}
two, err := generateIdTwo(customId, customTime)
two, err := generateIdTwo(customId, customTime, 18)
if err == nil {
return two
}
three, err := generateIdThree(customId, customTime)
three, err := generateIdThree(customId, customTime, 18)
if err == nil {
return three
}
four, err := generateIdFour(customId, customTime)
four, err := generateIdFour(customId, customTime, 18)
if err == nil {
return four
}
five, err := generateIdFive(customId, customTime)
five, err := generateIdFive(customId, customTime, 18)
if err == nil {
return five
}
six, err := generateIdSix(customId)
six, err := generateIdSix(customId, 18)
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
}

@ -0,0 +1,144 @@
package gostring
import (
"errors"
"fmt"
"github.com/dtapps/go-library/utils/gorandom"
"github.com/dtapps/go-library/utils/gotime"
)
// 生成18位时间[年月日时分]唯一编号
func generateIdOne(customId, setTime string, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度`
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
)
// 重新计算随机数据长度
newRandomLength = dataLength - customIdLength
if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) {
return "", errors.New("没有满足条件")
}
return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil
}

@ -0,0 +1,56 @@
package gostring
import "github.com/dtapps/go-library/utils/gotime"
// GenerateIdLength 生成自定义长度编号
func GenerateIdLength(customId string, dataLength int) string {
currentTime := gotime.Current().Format()
one, err := generateIdOne(customId, currentTime, dataLength)
if err == nil {
return one
}
two, err := generateIdTwo(customId, currentTime, dataLength)
if err == nil {
return two
}
three, err := generateIdThree(customId, currentTime, dataLength)
if err == nil {
return three
}
four, err := generateIdFour(customId, currentTime, dataLength)
if err == nil {
return four
}
five, err := generateIdFive(customId, currentTime, dataLength)
if err == nil {
return five
}
six, err := generateIdSix(customId, dataLength)
return six
}
// GenerateIdAndTimeLength 生成自定义长度编号
func GenerateIdAndTimeLength(customId, customTime string, dataLength int) string {
one, err := generateIdOne(customId, customTime, dataLength)
if err == nil {
return one
}
two, err := generateIdTwo(customId, customTime, dataLength)
if err == nil {
return two
}
three, err := generateIdThree(customId, customTime, dataLength)
if err == nil {
return three
}
four, err := generateIdFour(customId, customTime, dataLength)
if err == nil {
return four
}
five, err := generateIdFive(customId, customTime, dataLength)
if err == nil {
return five
}
six, err := generateIdSix(customId, dataLength)
return six
}
Loading…
Cancel
Save