95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package logic
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"bj_power_mes/ent"
|
||
|
|
"bj_power_mes/ent/materialrequest"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ===== 短号规则(2026-11-27 用户拍板)=====
|
||
|
|
// 叫料单号 MR、叫料包号 PK、转产退库单号 TR:前缀 + YYMMDD + 3位当天序号(每天从 001 归零,超 999 自然进 4 位)。
|
||
|
|
// 例:MR271127001 / PK271127001 / TR271127001。老号(时间戳)前缀不匹配,自动排除;历史数据一律不动。
|
||
|
|
// MR 与 TR 同写 material_request.request_no,但前缀不同、各自独立计数,不撞号;DB 唯一索引同列兜底。
|
||
|
|
// 唯一性:进程内互斥串行分配(单实例零撞号)+ DB 唯一索引兜底(异常/多实例)。
|
||
|
|
// 跨端:配送单号 DL 由 WMS 侧同规则实现(见 bj_power_wms/internal/handler/delivery.go)。
|
||
|
|
var (
|
||
|
|
shortNoMu sync.Mutex
|
||
|
|
shortNoSeq = map[string]int{} // key = 前缀+YYMMDD → 当天已分配的最大序号
|
||
|
|
)
|
||
|
|
|
||
|
|
func shortNoDay() string { return time.Now().Format("060102") }
|
||
|
|
|
||
|
|
// nextShortNo 分配一个短号。dbMax 只在当天该前缀首次分配时调用一次(从 DB 查当天同前缀最大序号续接),
|
||
|
|
// 之后从内存 counter 递增——故一次批量提交内多行号连续、并发请求也不重号。
|
||
|
|
func nextShortNo(prefix string, dbMax func() int) string {
|
||
|
|
day := shortNoDay()
|
||
|
|
key := prefix + day
|
||
|
|
shortNoMu.Lock()
|
||
|
|
defer shortNoMu.Unlock()
|
||
|
|
if _, ok := shortNoSeq[key]; !ok {
|
||
|
|
shortNoSeq[key] = dbMax()
|
||
|
|
}
|
||
|
|
shortNoSeq[key]++
|
||
|
|
return fmt.Sprintf("%s%s%03d", prefix, day, shortNoSeq[key])
|
||
|
|
}
|
||
|
|
|
||
|
|
// parseMaxSeq 从号列表里解析当天同前缀的最大序号(禁正则:前缀匹配 + strconv)。
|
||
|
|
func parseMaxSeq(nos []string, prefix string) int {
|
||
|
|
full := prefix + shortNoDay()
|
||
|
|
max := 0
|
||
|
|
for _, no := range nos {
|
||
|
|
if !strings.HasPrefix(no, full) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if n, err := strconv.Atoi(strings.TrimPrefix(no, full)); err == nil && n > max {
|
||
|
|
max = n
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return max
|
||
|
|
}
|
||
|
|
|
||
|
|
// maxRequestNoSeq 查当天 MR 号已用的最大序号(供 nextShortNo 初始化)。
|
||
|
|
func maxRequestNoSeq(ctx context.Context, q *ent.MaterialRequestQuery) int {
|
||
|
|
rows, err := q.Where(materialrequest.RequestNoHasPrefix("MR" + shortNoDay())).All(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
nos := make([]string, 0, len(rows))
|
||
|
|
for _, r := range rows {
|
||
|
|
nos = append(nos, r.RequestNo)
|
||
|
|
}
|
||
|
|
return parseMaxSeq(nos, "MR")
|
||
|
|
}
|
||
|
|
|
||
|
|
// maxPackageNoSeq 查当天 PK 号已用的最大序号(供 nextShortNo 初始化)。
|
||
|
|
func maxPackageNoSeq(ctx context.Context, q *ent.MaterialRequestQuery) int {
|
||
|
|
rows, err := q.Where(materialrequest.PackageNoHasPrefix("PK" + shortNoDay())).All(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
nos := make([]string, 0, len(rows))
|
||
|
|
for _, r := range rows {
|
||
|
|
nos = append(nos, r.PackageNo)
|
||
|
|
}
|
||
|
|
return parseMaxSeq(nos, "PK")
|
||
|
|
}
|
||
|
|
|
||
|
|
// maxTransferNoSeq 查当天 TR 号已用的最大序号(转产退库单,与 MR 同列 request_no,前缀区分)。
|
||
|
|
func maxTransferNoSeq(ctx context.Context, q *ent.MaterialRequestQuery) int {
|
||
|
|
rows, err := q.Where(materialrequest.RequestNoHasPrefix("TR" + shortNoDay())).All(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
nos := make([]string, 0, len(rows))
|
||
|
|
for _, r := range rows {
|
||
|
|
nos = append(nos, r.RequestNo)
|
||
|
|
}
|
||
|
|
return parseMaxSeq(nos, "TR")
|
||
|
|
}
|