资产增加批次库存管理模式

This commit is contained in:
2026-01-04 10:35:15 +08:00
parent a068e95017
commit cf27eee0ea

View File

@@ -3,8 +3,12 @@ package utils
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"reflect" "reflect"
"sort" "sort"
"strconv"
"strings"
"sync/atomic"
"time" "time"
"gitee.com/red-future---jilin-g/common/beans" "gitee.com/red-future---jilin-g/common/beans"
@@ -59,6 +63,7 @@ func GetMonthToday(t time.Time, month int) time.Time {
} }
return target.AddDate(0, 0, t.Day()-1) return target.AddDate(0, 0, t.Day()-1)
} }
func GetUserInfo(ctx context.Context) (user beans.User, err error) { func GetUserInfo(ctx context.Context) (user beans.User, err error) {
r := g.RequestFromCtx(ctx) r := g.RequestFromCtx(ctx)
if r != nil { if r != nil {
@@ -102,6 +107,7 @@ func GetUserInfo(ctx context.Context) (user beans.User, err error) {
} }
return return
} }
func OrderMap(m map[string]interface{}) map[string]interface{} { func OrderMap(m map[string]interface{}) map[string]interface{} {
// 提取所有key // 提取所有key
keys := make([]string, 0, len(m)) keys := make([]string, 0, len(m))
@@ -121,3 +127,146 @@ func OrderMap(m map[string]interface{}) map[string]interface{} {
return orderedMap return orderedMap
} }
// ParseIntSlice 解析整数切片 - 通用字符串处理工具
func ParseIntSlice(str string) []int {
parts := strings.Split(str, ",")
result := make([]int, 0, len(parts))
for _, part := range parts {
if val, err := strconv.Atoi(strings.TrimSpace(part)); err == nil {
result = append(result, val)
}
}
return result
}
// ParseStrings 解析字符串切片 - 通用字符串处理工具
func ParseStrings(str string) []string {
if str == "" {
return nil
}
parts := strings.Split(str, ",")
result := make([]string, 0, len(parts))
for _, part := range parts {
if trimmed := strings.TrimSpace(part); trimmed != "" {
result = append(result, trimmed)
}
}
return result
}
// FilterServiceNames 过滤服务名 - 通用映射处理工具
func FilterServiceNames(services map[string]interface{}, excludeKeys ...string) []string {
excludeMap := make(map[string]bool)
for _, key := range excludeKeys {
excludeMap[key] = true
}
result := make([]string, 0, len(services))
for key := range services {
if !excludeMap[key] {
result = append(result, key)
}
}
return result
}
// FormatUnixTime 格式化Unix时间戳 - 通用时间处理工具
func FormatUnixTime(timestamp int64) string {
if timestamp <= 0 {
return ""
}
return time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")
}
// ParseDurationWithDefault 解析持续时间,失败时使用默认值 - 通用时间处理工具
func ParseDurationWithDefault(ctx context.Context, durationStr, defaultStr, fieldName string) (time.Duration, string) {
durationParsed, err := time.ParseDuration(durationStr)
if err != nil {
// 这里不能直接使用g.Log()因为这是utils包没有直接的日志访问
// 调用方应该处理日志
// g.Log().Warningf(ctx, "解析%s失败: %s, 使用默认值 %s, error: %v", fieldName, durationStr, defaultStr, err)
durationParsed, _ = time.ParseDuration(defaultStr)
return durationParsed, defaultStr
}
return durationParsed, durationStr
}
// AtomicUpdateMin 原子更新最小值 - 通用数值处理工具
func AtomicUpdateMin(minValue *atomic.Int64, newValue int64) {
for {
currentMin := minValue.Load()
if newValue >= currentMin {
break
}
if minValue.CompareAndSwap(currentMin, newValue) {
break
}
}
}
// AtomicUpdateMax 原子更新最大值 - 通用数值处理工具
func AtomicUpdateMax(maxValue *atomic.Int64, newValue int64) {
for {
currentMax := maxValue.Load()
if newValue <= currentMax {
break
}
if maxValue.CompareAndSwap(currentMax, newValue) {
break
}
}
}
// ParseCIDRs 解析CIDR列表 - 通用网络处理工具
func ParseCIDRs(strs []string) ([]*net.IPNet, error) {
nets := make([]*net.IPNet, 0, len(strs))
for _, s := range strs {
if s == "*" {
if _, ipv4Net, err := net.ParseCIDR("0.0.0.0/0"); err == nil {
nets = append(nets, ipv4Net)
}
if _, ipv6Net, err := net.ParseCIDR("::/0"); err == nil {
nets = append(nets, ipv6Net)
}
continue
}
if _, ipNet, err := net.ParseCIDR(s); err == nil {
nets = append(nets, ipNet)
}
}
return nets, nil
}
// UrlDecode 简单的URL解码 - 通用编码解码工具
func UrlDecode(s string) string {
result := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
if s[i] == '%' && i+2 < len(s) {
if high := HexDigit(s[i+1]); high != 0xFF {
if low := HexDigit(s[i+2]); low != 0xFF {
result = append(result, (high<<4)|low)
i += 2
continue
}
}
}
result = append(result, s[i])
}
return string(result)
}
// HexDigit 十六进制字符转数字 - 通用编码解码工具
func HexDigit(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
default:
return 0xFF
}
}