diff --git a/config/yamlConfig/app.yaml b/config/yamlConfig/app.yaml index 7702357..02cb1aa 100644 --- a/config/yamlConfig/app.yaml +++ b/config/yamlConfig/app.yaml @@ -1,6 +1,6 @@ #App主配置 appsetup: - port: 18888 #服务端口 + port: 18889 #服务端口 readtime: 3600 #请求的读取操作在超时前的最大持续时间 writetime : 3600 #回复的写入操作在超时前的最大持续时间 appkey: 'application_platform_qin_dong' #应用程序密钥 diff --git a/controller/stock/entry.go b/controller/stock/entry.go new file mode 100644 index 0000000..d1a6717 --- /dev/null +++ b/controller/stock/entry.go @@ -0,0 +1,9 @@ +package stock + +type ApiMethod struct{} + +// 获取天干地址 +type GainHeavenlyEarthly struct { + HeavenlyStems string `json:"heavenlyStems"` //天干 + EarthlyBranches string `json:"earthlyBranches"` //地支 +} diff --git a/controller/stock/heavenlyStemsEarthlyBranches.go b/controller/stock/heavenlyStemsEarthlyBranches.go new file mode 100644 index 0000000..021d3c1 --- /dev/null +++ b/controller/stock/heavenlyStemsEarthlyBranches.go @@ -0,0 +1,69 @@ +package stock + +import ( + "appNewPlatform/generalmethod" + "appNewPlatform/utils" + "appNewPlatform/utils/formatoutput" + + "github.com/gin-gonic/gin" +) + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 08:52:39 +@ 功能: 入口 +*/ +func (a *ApiMethod) Index(c *gin.Context) { + sendMap := generalmethod.MapOut[string]() + token, _ := utils.ReleaseToken("300450") + sendMap["token"] = token + sendMap["tokenStr"], sendMap["claims"], sendMap["err"] = utils.ParseToken(token) + formatoutput.Result(0, sendMap, c) +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 08:51:04 +@ 功能: 天干地址选股法 +@ 参数 + + # + +@ 返回值 + + # + +@ 方法原型 + + # +*/ + +func (a *ApiMethod) appNewPlatform(c *gin.Context) { + var request GainHeavenlyEarthly + err := c.ShouldBindJSON(&request) + if err != nil { + formatoutput.Result(10, err, c) + return + } + if request.HeavenlyStems == "" || request.EarthlyBranches == "" { + formatoutput.Result(102, request, c) + return + } + // HeavenlyStemsStarIndex := 0 //天干起始计算位置 + // for i, v := range formatoutput.HeavenlyStems { + // if v == request.HeavenlyStems { + // HeavenlyStemsStarIndex = i + // return + // } + // } + // EarthlyBranchesStarIndex := 0 //地支起始计算位置 + // for i, v := range formatoutput.EarthlyBranches { + // if v == request.EarthlyBranches { + // EarthlyBranchesStarIndex = i + // return + // } + // } + // theSameDay := time.Now().Unix() +} diff --git a/go.mod b/go.mod index de2a856..60fb962 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,11 @@ go 1.21.5 require ( github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b - github.com/fsnotify/fsnotify v1.7.0 + github.com/fsnotify/fsnotify v1.8.0 github.com/gin-gonic/gin v1.10.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gorilla/websocket v1.5.3 - github.com/redis/go-redis/v9 v9.6.1 + github.com/redis/go-redis/v9 v9.7.1 github.com/spf13/viper v1.19.0 gorm.io/driver/mysql v1.5.7 gorm.io/gorm v1.25.12 @@ -56,7 +56,7 @@ require ( golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect - golang.org/x/text v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index d134b10..3705e21 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -87,8 +87,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4= -github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA= +github.com/redis/go-redis/v9 v9.7.1 h1:4LhKRCIduqXqtvCUlaq9c8bdHOkICjDMrr1+Zb3osAc= +github.com/redis/go-redis/v9 v9.7.1/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= @@ -156,8 +156,8 @@ golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= -golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= diff --git a/publicmethod/publicControll.go b/publicmethod/publicControll.go new file mode 100644 index 0000000..4d5a847 --- /dev/null +++ b/publicmethod/publicControll.go @@ -0,0 +1,1287 @@ +package publicmethod + +import ( + "appNewPlatform/appConstant" + "appNewPlatform/utils/snowflake" + "crypto/md5" + "crypto/rand" + "crypto/sha1" + "encoding/hex" + "errors" + "fmt" + "math" + "math/big" + "reflect" + "strconv" + "strings" + "time" + + "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 +} + +// 获取UUID +func GainUUID(workId int64) (uuid int64) { + flake, err := snowflake.NewWorker(workId) + if err != nil { + uuid = TableNumber() + } else { + uuid = flake.GetId() + } + return +} + +// 获取随机数 +func GetRandNumber(max int) (num int64) { + maxVal := int64(math.Pow10(max)) - 1 + minVal := int64(math.Pow10(max - 1)) + + // fmt.Printf("max:%v\nmaxVal:%v\nminVal:%v\n", max, maxVal, minVal) + if maxVal < 1 { + maxVal = 9 + } + if minVal < 1 { + minVal = 1 + } + result, _ := rand.Int(rand.Reader, big.NewInt(int64(maxVal))) + num = result.Int64() + int64(minVal) + return +} + +// 初始化map(泛型) +func MapOut[T GenericityVariable]() (data map[T]interface{}) { + data = make(map[T]interface{}) //必可不少,分配内存 + return data +} + +// 时间戳转换成time格式 +func TimeUnixToTime(timeData int64, unit string) (res time.Time) { + timeStr := UnixTimeToDay(timeData, 11) + loc, _ := time.LoadLocation("Local") + layout, ok := timeLayoutMap[unit] + if !ok { + layout = timeLayoutMap["s"] + } + res, _ = time.ParseInLocation(layout, timeStr, loc) + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:06:03 +@ 功能: 日期字符串转换成time格式 +*/ +func StringToTimeIng(unit, str string) (res time.Time) { + loc, _ := time.LoadLocation("Local") + // str := t.ToString() + layout, ok := timeLayoutMap[unit] + if !ok { + layout = timeLayoutMap["s"] + } + res, _ = time.ParseInLocation(layout, str, loc) + return +} + +/* +时间搓转换成日期 +@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) + } else { + dateStr = "1" + } + dateStr = "1" + // fmt.Printf("获取是电话卡-------------->%v------------>%v------------>%v\n", dateStr, datMonthFloat, datMonthFloatErr) + case 20: + timeTemplate = "20060102" + case 21: + timeTemplate = "200601" + case 22: + timeTemplate = "01-02" + case 23: //当前日期为本月第几周 + monthFirstDayStr := DateToTimeStampOld(UnixTimeToDay(timeStamp, 10) + "-01 00:00:00") //获取指定月第一天时间戳 + dayTime := time.Unix(monthFirstDayStr, 0) + dayOfWeek := int(dayTime.Weekday()) //获取本月第一天是周几 + if dayOfWeek == 0 { + dayOfWeek = 7 + } + + dayNumber, dayNumberErr := strconv.ParseInt(UnixTimeToDay(timeStamp, 9), 10, 64) //获取今天是几号 + if dayNumberErr != nil { + dayNumber = 1 + } + daysAndWeeksDiff := dayNumber - (8 - int64(dayOfWeek)) + if daysAndWeeksDiff <= 0 { + dateStr = "1" + } else { + daysAndWeeksDiffFlot, daysAndWeeksDiffFloatErr := strconv.ParseFloat(strconv.FormatInt(daysAndWeeksDiff, 10), 10) + if daysAndWeeksDiffFloatErr == nil { + daysWeeksDiffVal := math.Ceil(daysAndWeeksDiffFlot/7) + 1 + dateStr = strconv.FormatFloat(daysWeeksDiffVal, 'f', -1, 64) + } else { + dateStr = "1" + } + } + case 24: + timeTemplate = "2006.01.02" + case 25: + timeTemplate = "2006.01" + case 26: + timeTemplate = "01.02" + case 27: + timeTemplate = "2006.01.02 15:04:05" + case 28: + timeTemplate = "2006012" + case 29: + timeTemplate = "200612" + case 30: + timeTemplate = "0612" + case 31: + timeTemplate = "20061" + case 32: + timeTemplate = "06" + case 33: + timeTemplate = "0612" + case 34: + timeTemplate = "061" + case 35: + timeTemplate = "0102" + case 36: + timeTemplate = "012" + case 37: + timeTemplate = "12" + case 38: + timeTemplate = "1" + case 39: + timeTemplate = "2" + case 40: + timeTemplate = "2006/01" + case 41: //当前年第几周 + timeDay := TimeUnixToTime(timeStamp, "s") + year, week := timeDay.ISOWeek() + dateStr = fmt.Sprintf("%v年第%v周", year, week) + return dateStr + 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 DateToTimeStampOld(dataStr string) (timeStamp int64) { + tmp := "2006-01-02 15:04:05" + res, _ := time.ParseInLocation(tmp, dataStr, time.Local) + timeStamp = res.Unix() + return +} + +// 数据库查询翻页 +func LimitPage[T GenericityVariableNumber](page, pageSize T) (offSet T) { + if page < 1 { + page = 1 + } + offSet = pageSize * (page - 1) + if offSet < 0 { + offSet = 0 + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:31:10 +@ 功能: 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 +} + +// sha1算法 +func Sha1Encryption(str string) string { + sha1 := sha1.New() + sha1.Write([]byte(str)) + return hex.EncodeToString(sha1.Sum(nil)) +} + +// 初始化程序 md5 +func (m *Md5Encryption) Md5EncryptionInit(code string) { + m.AppKey = appConstant.Constant_Config.Appsetup.AppKey + m.Code = code +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:32:09 +@ 功能: 加密算法 (md5) +*/ +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 +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:34:59 +@ 功能: 计算当前时间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 +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:35:38 +@ 功能: 判断字符串是否在切片中 泛型 +*/ +func IsInTrue[T GenericityVariable](key T, keyAry []T) (isTrue bool) { + isTrue = false + for _, v := range keyAry { + if v == key { + return true + } + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:35:52 +@ 功能: 获取两个切片得差集 +*/ +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 +} + +// 两个切片的交集 +func Intersect[T GenericityVariable](a, b []T) []T { + inter := make([]T, 0) + mp := make(map[T]bool) + + for _, s := range a { + if _, ok := mp[s]; !ok { + mp[s] = true + } + } + for _, s := range b { + if _, ok := mp[s]; ok { + inter = append(inter, s) + } + } + + return inter +} + +//判断类型转换成 + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:36:56 +@ 功能: 字符转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 +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:37:09 +@ 功能: 字符转int64 +*/ +func StringToInt64(val interface{}) (int64, error) { + switch valData := val.(type) { + case string: + return strconv.ParseInt(valData, 10, 64) + case int: + return int64(valData), nil + case int8: + return int64(valData), nil + case int16: + return int64(valData), nil + case int32: + return int64(valData), nil + case int64: + return valData, nil + case uint: + return int64(valData), nil + case uint8: + return int64(valData), nil + case uint16: + return int64(valData), nil + case uint32: + return int64(valData), nil + case uint64: + return int64(valData), nil + case float64: + val := strconv.FormatFloat(valData, 'f', -1, 64) + valInt, err := strconv.ParseInt(val, 10, 64) + return valInt, err + default: + return 0, nil + } + return 0, nil +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:38:15 +@ 功能: 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 +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:38:41 +@ 功能: 数据库翻页设置 +*/ +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 +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:39:08 +@ 功能: 浮点数保留小数 +*/ +func DecimalEs(value float64, weishu int) float64 { + switch weishu { + case 3: + value, _ = strconv.ParseFloat(fmt.Sprintf("%.3f", value), 64) + case 4: + value, _ = strconv.ParseFloat(fmt.Sprintf("%.4f", value), 64) + case 5: + value, _ = strconv.ParseFloat(fmt.Sprintf("%.5f", value), 64) + case 6: + value, _ = strconv.ParseFloat(fmt.Sprintf("%.6f", value), 64) + default: + value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64) + } + + return value +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:42:25 +@ 功能: 判断指定日期是周几 +*/ +func GetWeekday(date string) (string, error) { + layout := "2006-01-02" + t, err := time.Parse(layout, date) + if err != nil { + return fmt.Sprintf("%v", time.Sunday), err + } + dayOfWeek := t.Weekday() + + var weekdays = [...]string{ + "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", + } + + return fmt.Sprintf(weekdays[dayOfWeek]), nil +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 09:42:39 +@ 功能: 时间计算(年、季、月、周、日、时、分、秒) +*/ +func ComputingTime(timeStamp int64, timeType int) (timeDataInt int64) { + var timeData string = "1" + switch timeType { + case 1: + timeData = time.Unix(timeStamp, 0).Format("2006") //年 + case 2: + dayMonth := time.Unix(timeStamp, 0).Format("01") //季度 + datMonthFloat, datMonthFloatErr := strconv.ParseFloat(dayMonth, 10) + if datMonthFloatErr == nil { + timeData = strconv.FormatFloat(math.Ceil(datMonthFloat/3), 'f', -1, 64) + } else { + timeData = "1" + } + case 3: + timeData = time.Unix(timeStamp, 0).Format("01") //月份 + case 4: //当前日期为本月第几周 + monthFirstDayStr := DateToTimeStampOld(UnixTimeToDay(timeStamp, 15) + "-01 00:00:00") //获取指定月第一天时间戳 + dayTime := time.Unix(monthFirstDayStr, 0) + dayOfWeek := int(dayTime.Weekday()) //获取本月第一天是周几 + if dayOfWeek == 0 { + dayOfWeek = 7 + } + + dayNumber, dayNumberErr := strconv.ParseInt(UnixTimeToDay(timeStamp, 18), 10, 64) //获取今天是几号 + if dayNumberErr != nil { + dayNumber = 1 + } + daysAndWeeksDiff := dayNumber - (8 - int64(dayOfWeek)) + if daysAndWeeksDiff <= 0 { + timeData = "1" + } else { + daysAndWeeksDiffFlot, daysAndWeeksDiffFloatErr := strconv.ParseFloat(strconv.FormatInt(daysAndWeeksDiff, 10), 10) + if daysAndWeeksDiffFloatErr == nil { + daysWeeksDiffVal := math.Ceil(daysAndWeeksDiffFlot/7) + 1 + timeData = strconv.FormatFloat(daysWeeksDiffVal, 'f', -1, 64) + } else { + timeData = "1" + } + } + case 5: + timeData = time.Unix(timeStamp, 0).Format("02") //天 + case 7: + timeData = time.Unix(timeStamp, 0).Format("15") //时 + case 8: + timeData = time.Unix(timeStamp, 0).Format("04") //分 + case 9: + timeData = time.Unix(timeStamp, 0).Format("05") //秒 + default: + timeData = "0" + } + timeDataInt, timeDataIntErr := strconv.ParseInt(timeData, 10, 64) + if timeDataIntErr != nil { + timeDataInt = 0 + return + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:05:37 +@ 功能: 获取指定月的起止时间 +*/ +func GetAppointMonthStarAndEndTime(dayTime string) (startTime, endTime int64) { + dataTypeAll := "2006-01-02" + dayTime = fmt.Sprintf("%v-%v", dayTime, "01") + firstToTime := StringToTimeIng("d", dayTime) + startTimeStr := fmt.Sprintf("%v 00:00:00", firstToTime.AddDate(0, 0, 0).Format(dataTypeAll)) + endTimeStr := fmt.Sprintf("%v 23:59:59", firstToTime.AddDate(0, 1, -1).Format(dataTypeAll)) + // startTime = dayTime + startTime = DateToTimeStampOld(startTimeStr) + endTime = DateToTimeStampOld(endTimeStr) + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:44:39 +@ 功能: 获得月份最后一天,最后一秒 +#nextYear 年 +#nextMonth 月 +*/ +func GetDaysInMonth(nextYear int, nextMonth int) DateTimeTotimes { + firstDayOfNextMonth := time.Date(nextYear, time.Month(nextMonth), 1, 0, 0, 0, 0, time.UTC) + lastDayOfThisMonth := firstDayOfNextMonth.Add(-1 * time.Second) + //fmt.Println("本月最后一天时间:", lastDayOfThisMonth) + dates := lastDayOfThisMonth.Format("2006-01-02 15:04:05") + var creTimeAll DateTimeTotimes + creTimeAll.BaisStrToTime(dates) + return creTimeAll +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:45:17 +@ 功能: 获取指定时间的上个月的开始和结束时间 +#dayTime 时间 +*/ +func GetLastMonthStartEnd(dayTime string) (int64, int64) { + // dataTypeAll := "2006-01-02" + // dayTime = fmt.Sprintf("%v-%v", dayTime, "01") + now := StringToTimeIng("d", dayTime) + // now := time.Now() + lastMonthFirstDay := now.AddDate(0, -1, -now.Day()+1) + lastMonthStart := time.Date(lastMonthFirstDay.Year(), lastMonthFirstDay.Month(), lastMonthFirstDay.Day(), 0, 0, 0, 0, now.Location()).Unix() + lastMonthEndDay := lastMonthFirstDay.AddDate(0, 1, -1) + lastMonthEnd := time.Date(lastMonthEndDay.Year(), lastMonthEndDay.Month(), lastMonthEndDay.Day(), 23, 59, 59, 0, now.Location()).Unix() + return lastMonthStart, lastMonthEnd +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:07:07 +@ 功能: 获取指定日的起止时间副本 +*/ +func GetAppointMonthStarAndEndTimeEs(dayTime string) (startTime, endTime int64) { + dataTypeAll := "2006-01-02" + firstToTime := StringToTimeIng("d", dayTime) + startTimeStr := fmt.Sprintf("%v 00:00:00", firstToTime.AddDate(0, 0, 0).Format(dataTypeAll)) + endTimeStr := fmt.Sprintf("%v 23:59:59", firstToTime.AddDate(0, 1, -1).Format(dataTypeAll)) + // startTime = dayTime + startTime, _ = DateToTimeStamp(startTimeStr) + endTime, _ = DateToTimeStamp(endTimeStr) + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:12:42 +@ 功能: 一天的起止时间(时间戳版本) +*/ +func TimeUnixDayStartAndEnd(timeVal int64) (start, end int64) { + timeStr := UnixTimeToDay(timeVal, 14) + startTimeStr := fmt.Sprintf("%v 00:00:00", timeStr) + endTimeStr := fmt.Sprintf("%v 23:59:59", timeStr) + start = DateToTimeStampOld(startTimeStr) + end = DateToTimeStampOld(endTimeStr) + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:12:54 +@ 功能: 计算日期差 +*/ +func DayBetweenDate(oneDay, twoDay string) (int, error) { + const layout = "2006-01-02" + t1, err := time.Parse(layout, oneDay) + if err != nil { + return 0, err + } + t2, err := time.Parse(layout, twoDay) + if err != nil { + return 0, err + } + // 计算日期差 + duration := t1.Sub(t2) + // 转换为天数 + days := int(duration.Hours() / 24) + return days, nil +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:14:22 +@ 功能: 合并数组 +*/ +func MergeStruct[T GenericityVariable](aryOen, aryTwo []T) (structAry []T) { + if len(aryOen) >= len(aryTwo) { + structAry = aryOen + for i := 0; i < len(aryTwo); i++ { + if IsInTrue[T](aryTwo[i], structAry) == false { + structAry = append(structAry, aryTwo[i]) + } + } + } else { + structAry = aryTwo + for i := 0; i < len(aryOen); i++ { + if IsInTrue[T](aryOen[i], structAry) == false { + structAry = append(structAry, aryOen[i]) + } + } + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:14:42 +@ 功能: 一天的开始与结束时间 +@ 参数 + + #day 日期 + #class 1:返回时间戳,2:返回日期 + +@ 返回值 + + #startTime 起始时间 + #endTime 结束时间 + +@ 方法原型 + + # +*/ +func OenDayStartOrEndTime(day string, class int) (startTime, endTime interface{}) { + var creTimeAll DateTimeTotimes + creTimeAll.BaisStrToTime(day) + timeVal := UnixTimeToDay(creTimeAll.AllTime, 14) + if class == 1 { + startTime = DateToTimeStampOld(fmt.Sprintf("%v 00:00:00", timeVal)) + endTime = DateToTimeStampOld(fmt.Sprintf("%v 23:59:59", timeVal)) + } else { + startTime = fmt.Sprintf("%v 00:00:00", timeVal) + endTime = fmt.Sprintf("%v 23:59:59", timeVal) + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:16:07 +@ 功能: 根据字符串组合时间,补全时间 +#dateTime 日期 +*/ +func (d *DateTimeTotimes) BaisStrToTime(dateTime string) { + timeStrAry := strings.Split(dateTime, "-") + switch len(timeStrAry) { + case 1: + dateTime = fmt.Sprintf("%v-01-01 12:00:00", dateTime) + case 2: + if len(timeStrAry[1]) < 2 { + dateTime = fmt.Sprintf("%v-0%v-01 12:00:00", timeStrAry[0], timeStrAry[1]) + } + case 3: + monthStr := timeStrAry[1] + if len(timeStrAry[1]) < 2 { + monthStr = fmt.Sprintf("0%v", timeStrAry[1]) + } + dayAry := strings.Split(timeStrAry[2], " ") + // fmt.Printf("dayAry:%v------>%v\n", dayAry, len(dayAry)) + dayStr := dayAry[0] + if len(dayAry[0]) < 2 { + dayStr = fmt.Sprintf("0%v", dayAry[0]) + } + if len(dayAry) > 1 { + // fmt.Printf("dayAry[1]:%v------>%v\n", dayAry[1], len(dayAry[1])) + if len(dayAry[1]) > 0 { + Hours := "00" + minutes := "00" + seconds := "00" + hisAry := strings.Split(dayAry[1], ":") + switch len(hisAry) { + case 1: + if len(hisAry[0]) < 2 { + Hours = fmt.Sprintf("0%v", hisAry[0]) + } else { + Hours = hisAry[0] + } + case 2: + if len(hisAry[0]) < 2 { + Hours = fmt.Sprintf("0%v", hisAry[0]) + } else { + Hours = hisAry[0] + } + if len(hisAry[1]) < 2 { + minutes = fmt.Sprintf("0%v", hisAry[1]) + } else { + minutes = hisAry[1] + } + case 3: + if len(hisAry[0]) < 2 { + Hours = fmt.Sprintf("0%v", hisAry[0]) + } else { + Hours = hisAry[0] + } + if len(hisAry[1]) < 2 { + minutes = fmt.Sprintf("0%v", hisAry[1]) + } else { + minutes = hisAry[1] + } + if len(hisAry[2]) < 2 { + seconds = fmt.Sprintf("0%v", hisAry[2]) + } else { + seconds = hisAry[2] + } + default: + } + dayStr = fmt.Sprintf("%v %v:%v:%v", dayStr, Hours, minutes, seconds) + } + } + // dayStr := timeStrAry[2] + // if len(timeStrAry[2]) < 2 { + // dayStr = fmt.Sprintf("0%v", timeStrAry[2]) + // } + dateTime = fmt.Sprintf("%v-%v-%v", timeStrAry[0], monthStr, dayStr) + + default: + } + + // fmt.Printf("dateTime:%v---1--->%v\n", dateTime, len(dateTime)) + orgTime, orgTimeErr := DateToTimeStamp(fmt.Sprintf("%v-01-01 12:00:00", dateTime)) + if orgTimeErr { + d.AllTime = orgTime + d.AllTimeString = dateTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + } else { + + orgTime, orgTimeErr = DateToTimeStamp(fmt.Sprintf("%v-01 12:00:00", dateTime)) + if orgTimeErr { + d.AllTime = orgTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + + fmt.Printf("dateTime:%v---2--->%v--->%v\n", orgTime, d.AllTime, d.Quarter) + } else { + orgTime, orgTimeErr = DateToTimeStamp(fmt.Sprintf("%v 12:00:00", dateTime)) + if orgTimeErr { + d.AllTime = orgTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + } else { + orgTime, orgTimeErr = DateToTimeStamp(fmt.Sprintf("%v:00:00", dateTime)) + if orgTimeErr { + d.AllTime = orgTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + } else { + orgTime, orgTimeErr = DateToTimeStamp(fmt.Sprintf("%v:00", dateTime)) + if orgTimeErr { + d.AllTime = orgTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + } else { + orgTime, orgTimeErr = DateToTimeStamp(dateTime) + if orgTimeErr { + d.AllTime = orgTime + d.Years = strconv.FormatInt(ComputingTime(orgTime, 1), 10) + d.Quarter = strconv.FormatInt(ComputingTime(orgTime, 2), 10) + d.Months = strconv.FormatInt(ComputingTime(orgTime, 3), 10) + d.Week = strconv.FormatInt(ComputingTime(orgTime, 4), 10) + d.Days = strconv.FormatInt(ComputingTime(orgTime, 5), 10) + d.Hours = strconv.FormatInt(ComputingTime(orgTime, 7), 10) + d.Minutes = strconv.FormatInt(ComputingTime(orgTime, 8), 10) + d.Second = strconv.FormatInt(ComputingTime(orgTime, 9), 10) + } + } + } + } + } + d.AllTimeString = UnixTimeToDay(d.AllTime, 11) + } +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:20:40 +@ 功能: 获取指定符号中的数据 +@ 参数 + + #s 待处理字符串 + #starBracket 开始符号 + #endBracket 结束符号 + +@ 返回值 + + #目标字符串 + +@ 方法原型 + + # +*/ +func MatchBracket(s, starBracket, endBracket string) string { + i := strings.Index(s, starBracket) + if i >= 0 { + // fmt.Printf("match-i-->%v\n", i) + j := strings.Index(s[i:], endBracket) + if j >= 0 { + // fmt.Printf("match-j-->%v-->%v\n", j, s[i+1:i+j]) + return s[i+1 : i+j] + } + } + return "" +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:39:27 +@ 功能: 类型转换 +*/ +func TypeToClass(val interface{}, timeClass int) (str string) { + + if typeVal, ok := val.(uint); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(uint8); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(uint16); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(uint32); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(uint64); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(int); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(int8); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(int16); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(int32); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(int64); ok { + str = UnixTimeToDay(typeVal, timeClass) + } else if typeVal, ok := val.(float32); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(float64); ok { + str = UnixTimeToDay(int64(typeVal), timeClass) + } else if typeVal, ok := val.(byte); ok { + valInt, valErr := strconv.ParseInt(string(typeVal), 10, 64) + if valErr == nil { + str = UnixTimeToDay(valInt, timeClass) + } else { + str = "" + } + } else if typeVal, ok := val.(string); ok { + valInt, valErr := strconv.ParseInt(typeVal, 10, 64) + if valErr == nil { + str = UnixTimeToDay(valInt, timeClass) + } else { + str = "" + } + } else { + str = "" + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:40:43 +@ 功能: 根据身份证获取对应生日 +@ 参数 + + #cardId 身份证号码 + +@ 返回值 + + #生日 + +@ 方法原型 + + # +*/ +func GetBirthday(cardId string) string { + if len(cardId) < 15 { + return "" + } + // 兼容第一代15位身份证号码 + if len(cardId) == 15 { + // 第一代身份证年份都是19开头的 + return fmt.Sprintf("19%s", cardId[6:12]) + } else { + return cardId[6:14] + } +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:41:05 +@ 功能: 根据生日日期得到用户年龄 +*/ +func (b *BirthdayAge) GetAgeByBirthday(cardId string) { + if len(cardId) < 15 { + return + } + birthday := GetBirthday(cardId) + if len(birthday) < 1 { + b.Age = 0 + } + year, _ := strconv.Atoi(birthday[0:4]) + month, _ := strconv.Atoi(birthday[4:6]) + day, _ := strconv.Atoi(birthday[6:]) + var months string + if month < 10 { + months = fmt.Sprintf("0%v", month) + } else { + months = strconv.Itoa(month) + } + var days string + if day < 10 { + days = fmt.Sprintf("0%v", day) + } else { + days = strconv.Itoa(day) + } + b.Birthday = fmt.Sprintf("%v-%v-%v", year, months, days) + // 计算当前日期与生日中年月日都差值 + now := time.Now() + age := now.Year() - year + moreMonth := int(now.Month()) - month + moreDay := now.Day() - day + + // 未到生日月份,或者到了月份,但未到生日日期,年龄都要减1岁 + if moreMonth < 0 || (moreMonth == 0 && moreDay < 0) { + age-- + } + // 不要出现负值 + if age < 0 { + age = 0 + } + + b.Age = age +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:41:50 +@ 功能: 统计元素重复出现的次数 +#ary 数组切片 +*/ +func StatisticalDuplication[T GenericityVariable](ary []T) (mapAry map[T]int) { + mapAry = make(map[T]int) + count := 0 + for i := 0; i < len(ary); i++ { + for n := 0; n < len(ary); n++ { + if ary[i] == ary[n] { + count++ + } + } + mapAry[ary[i]] = count + count = 0 + } + return +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:42:16 +@ 功能: 泛型类型断言 +#val 位置类型数据 +@ 返回字符串类型值 +*/ +func TypeToInterface(val interface{}) string { + switch balData := val.(type) { + case int: + return strconv.Itoa(balData) + case int8: + return strconv.FormatInt(int64(balData), 10) + case int16: + return strconv.FormatInt(int64(balData), 10) + case int32: + return strconv.FormatInt(int64(balData), 10) + case int64: + return strconv.FormatInt(balData, 10) + case uint: + return strconv.FormatUint(uint64(balData), 10) + case uint8: + return strconv.FormatUint(uint64(balData), 10) + case uint16: + return strconv.FormatUint(uint64(balData), 10) + case uint32: + return strconv.FormatUint(uint64(balData), 10) + case uint64: + return strconv.FormatUint(balData, 10) + case float32: + return fmt.Sprintf("%v", float64(balData)) + case float64: + return fmt.Sprintf("%v", float64(balData)) + default: + if balDataStr, ok := balData.(string); ok { + if balDataStr != "" { + return balDataStr + } else { + return "" + } + } else { + return "" + } + + } +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:43:32 +@ 功能: 取最大值 +*/ +func GetMaxNum[T GenericityVariable](ary []T) T { + maxVal := ary[0] + if len(ary) == 0 { + return maxVal + } + + for i := 1; i < len(ary); i++ { + if maxVal < ary[i] { + maxVal = ary[i] + } + } + + return maxVal +} + +/* +* +@ 作者: 秦东 +@ 时间: 2025-02-26 10:43:50 +@ 功能: 泛型去重 +*/ +func RemoveDuplicate[T GenericityVariable](ary []T) []T { + m := make(map[T]bool) + uniqueNumbers := []T{} + for _, v := range ary { + if !m[v] { + m[v] = true + uniqueNumbers = append(uniqueNumbers, v) + } + } + return uniqueNumbers +} diff --git a/publicmethod/type.go b/publicmethod/type.go new file mode 100644 index 0000000..f6a75b3 --- /dev/null +++ b/publicmethod/type.go @@ -0,0 +1,61 @@ +package publicmethod + +// 全局函数处理 +// 时间相关处理类 +var ( + timeLayoutMap = map[string]string{ + "y": "2006", + "m": "2006-01", + "d": "2006-01-02", + "h": "2006-01-02 15", + "i": "2006-01-02 15:04", + "s": "2006-01-02 15:04:05", + } + + weekDay = map[string]int{ + "Monday": 1, + "Tuesday": 2, + "Wednesday": 3, + "Thursday": 4, + "Friday": 5, + "Saturday": 6, + "Sunday": 7, + } +) + +// 泛型基础变量类型 +type GenericityVariable interface { + int | int8 | int16 | int32 | int64 | float32 | float64 | string +} + +// 数值泛型基础变量类型 +type GenericityVariableNumber interface { + int | int8 | int16 | int32 | int64 | float32 | float64 +} + +/* +*加密算法 + */ +type Md5Encryption struct { + Code string `json:"code"` + AppKey string `json:"appKey"` +} + +//时间转换 +type DateTimeTotimes struct { + Years string `json:"years"` + Quarter string `json:"quarter"` + Months string `json:"months"` + Week string `json:"week"` + Days string `json:"days"` + Hours string `json:"hours"` + Minutes string `json:"minutes"` + Second string `json:"second"` + AllTime int64 `json:"alltime"` + AllTimeString string `json:"alltimeStr"` +} + +type BirthdayAge struct { + Birthday string + Age int +} diff --git a/utils/formatoutput/hseb.go b/utils/formatoutput/hseb.go new file mode 100644 index 0000000..6a07a72 --- /dev/null +++ b/utils/formatoutput/hseb.go @@ -0,0 +1,10 @@ +package formatoutput + +/** +@ 作者: 秦东 +@ 时间: 2025-02-26 08:41:30 +@ 功能: 天干地址 +*/ +var HeavenlyStems = []string{"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"} +var EarthlyBranches = []string{"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"} +var BackgroundColor = []string{"红", "绿"}