feat: AGV点位锁与缓存迁移 + 工位终端去离线队列 + WMS出入库增强
【MES】 - AGV点位/线路点位/AGV任务/工单 ent schema 重构(全量重生成 ent 代码) - 新增派车锁 agv_point_lock.go:目标点位占用即拒车,JBT21 库房接驳台卸货后保持锁定待库管手动解锁 - 新增进程内 TTL 缓存 cache.go,由 Redis 看板缓存迁移至内存(singleflight 防击穿,去除外部依赖) - AGV 三段链安全机制、车辆状态/接驳台左右互换、补料损耗闭环完善 - 来料检/工序卡/工单/质检/日排产/AGV 前端与帮助文档更新 - 新增 migrations/、sql/ 目录 【WMS】 - 出库、Excel 导出、物料、路由处理增强 - 配置与 DB 初始化调整;基础数据/成品入库/入库前端调整 【工位终端】 - 移除离线同步队列(syncer 包、sync.go、SyncQueueDialog.vue),改为实时直连上报 MES(断网即失败报错),以本人身份操作 - 本地 SQLite 仅保留操作日志 + 内置账号 + 登录态;保留空闲自动登出 【文档】 - 删除 AGV海康litev1.5.md;案例.md 更新
This commit is contained in:
@@ -415,6 +415,25 @@ func deliveryDeliveredInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc
|
||||
}
|
||||
}
|
||||
|
||||
// deliveryStatusInternalHandler GET /api/internal/delivery/status?no=(MES 到位自动放行双重确认查询)
|
||||
// 返回配送单当前状态(PENDING/DISPATCHED/DONE/CANCELLED);单不存在返回空串(兼容旧叫料单直派链路)。
|
||||
func deliveryStatusInternalHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
no := r.URL.Query().Get("no")
|
||||
if no == "" {
|
||||
fail(w, http.StatusBadRequest, "缺少配送单号")
|
||||
return
|
||||
}
|
||||
o, err := ctx.EntClient.DeliveryOrder.Query().
|
||||
Where(deliveryorder.DeliveryNoEQ(no)).First(r.Context())
|
||||
if err != nil || o == nil {
|
||||
ok(w, map[string]any{"status": ""}) // 旧链路 BizNo=叫料单号:无配送单,空状态
|
||||
return
|
||||
}
|
||||
ok(w, map[string]any{"status": o.Status})
|
||||
}
|
||||
}
|
||||
|
||||
// callCreateProxyHandler POST /api/call/create WMS 代叫料(与工位终端同一套逻辑)
|
||||
func callCreateProxyHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -304,128 +304,8 @@ func excelInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
type planRow struct {
|
||||
row int
|
||||
code string
|
||||
sn string
|
||||
qty int
|
||||
prodDate string
|
||||
supplier string
|
||||
zone string
|
||||
remark string
|
||||
shelfNo string
|
||||
layerNo string
|
||||
positionNo string
|
||||
inspectionNo string
|
||||
productWorkNo string
|
||||
ownershipType string
|
||||
ownershipNo string
|
||||
}
|
||||
plans := make([]planRow, 0, len(rows)-1)
|
||||
errs := make([]importErr, 0)
|
||||
|
||||
// ---- 阶段1:解析 + 校验(不落库) ----
|
||||
snSeen := map[string]bool{}
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
continue // 表头
|
||||
}
|
||||
cell := func(idx int) string {
|
||||
if idx < len(row) {
|
||||
return strings.TrimSpace(row[idx])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
pr := planRow{row: i + 1}
|
||||
pr.code = cell(0)
|
||||
if mode == "sn" {
|
||||
// 图号|SN|区域|生产日期|供应商|架|层|位|备注|检测单号|产品工号|归属类型|归属编号
|
||||
pr.sn = cell(1)
|
||||
pr.zone = cell(2)
|
||||
pr.prodDate = cell(3)
|
||||
pr.supplier = cell(4)
|
||||
pr.shelfNo = cell(5)
|
||||
pr.layerNo = cell(6)
|
||||
pr.positionNo = cell(7)
|
||||
pr.remark = cell(8)
|
||||
pr.inspectionNo = cell(9)
|
||||
pr.productWorkNo = cell(10)
|
||||
pr.ownershipType = cell(11)
|
||||
pr.ownershipNo = cell(12)
|
||||
} else {
|
||||
// 图号|数量|生产日期|供应商|区域|架|层|位|备注|检测单号|产品工号|归属类型|归属编号(批次号由系统自动生成,模板不再含该列)
|
||||
pr.qty = atoi(cell(1), 0)
|
||||
pr.prodDate = cell(2)
|
||||
pr.supplier = cell(3)
|
||||
pr.zone = cell(4)
|
||||
pr.shelfNo = cell(5)
|
||||
pr.layerNo = cell(6)
|
||||
pr.positionNo = cell(7)
|
||||
pr.remark = cell(8)
|
||||
pr.inspectionNo = cell(9)
|
||||
pr.productWorkNo = cell(10)
|
||||
pr.ownershipType = cell(11)
|
||||
pr.ownershipNo = cell(12)
|
||||
}
|
||||
|
||||
if pr.code == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号为空"})
|
||||
continue
|
||||
}
|
||||
m, e := ctx.EntClient.Material.Query().Where(material.CodeEQ(pr.code)).Only(ctx0())
|
||||
if e != nil {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "物料不存在"})
|
||||
continue
|
||||
}
|
||||
if mode == "sn" {
|
||||
if m.SnMode != snModeSN {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "该物料序列号=否(按数量管理),SN 导入仅支持序列号=是(逐件SN)"})
|
||||
continue
|
||||
}
|
||||
if pr.sn == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 为空"})
|
||||
continue
|
||||
}
|
||||
if snSeen[pr.sn] {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 在文件中重复: " + pr.sn})
|
||||
continue
|
||||
}
|
||||
snSeen[pr.sn] = true
|
||||
exists, _ := ctx.EntClient.Inventory.Query().
|
||||
Where(inventory.SnModeEQ(snModeSN), inventory.SnCodeEQ(pr.sn)).Exist(ctx0())
|
||||
if exists {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 已存在: " + pr.sn})
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if m.SnMode != snModeBatch {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "该物料序列号=是(逐件SN管理),请使用 SN 导入"})
|
||||
continue
|
||||
}
|
||||
if pr.qty <= 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "数量必须大于 0"})
|
||||
continue
|
||||
}
|
||||
}
|
||||
if pr.zone == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "区域为空(必填)"})
|
||||
continue
|
||||
}
|
||||
// 货位必填(货架/层/位置):与手动录入(InboundEntry)口径一致,杜绝"有区域无货架"脏数据;任一为空则整批拒绝
|
||||
if pr.shelfNo == "" || pr.layerNo == "" || pr.positionNo == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "货位不完整(货架/层/位置均为必填)"})
|
||||
continue
|
||||
}
|
||||
// 归属校验(与手动录入口径一致:workorder/project 必填编号;other/空 不需要)
|
||||
pr.ownershipType = ownershipTypeOrNone(pr.ownershipType)
|
||||
if pr.ownershipType == "workorder" || pr.ownershipType == "project" {
|
||||
if pr.ownershipNo == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "归属类型为" + pr.ownershipType + "时归属编号必填"})
|
||||
continue
|
||||
}
|
||||
}
|
||||
plans = append(plans, pr)
|
||||
}
|
||||
// ---- 阶段1:解析 + 校验(不落库)——与预检端点 excelInboundPreviewHandler 共用同一函数,规则同源 ----
|
||||
plans, errs, _ := parseInboundImportPlan(ctx, rows, mode)
|
||||
|
||||
// ---- 阶段2:有错则整体拒绝,一行都不写 ----
|
||||
if len(errs) > 0 {
|
||||
@@ -569,6 +449,180 @@ func excelInboundHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// inboundImportPlan 入库导入单行写入计划(预检与写入共用同一解析/校验,规则同源不漂移)。
|
||||
type inboundImportPlan struct {
|
||||
row int
|
||||
code string
|
||||
sn string
|
||||
qty int
|
||||
prodDate string
|
||||
supplier string
|
||||
zone string
|
||||
remark string
|
||||
shelfNo string
|
||||
layerNo string
|
||||
positionNo string
|
||||
inspectionNo string
|
||||
productWorkNo string
|
||||
ownershipType string
|
||||
ownershipNo string
|
||||
}
|
||||
|
||||
// parseInboundImportPlan 入库导入阶段1:解析 + 逐行校验(只读不落库):
|
||||
// 物料存在/数量>0/SN 不重复/区域与货位必填/归属编号规则。返回行计划、逐行错误与总数据行数。
|
||||
func parseInboundImportPlan(ctx *svc.ServiceContext, rows [][]string, mode string) ([]inboundImportPlan, []importErr, int) {
|
||||
total := len(rows) - 1
|
||||
plans := make([]inboundImportPlan, 0, total)
|
||||
errs := make([]importErr, 0)
|
||||
|
||||
snSeen := map[string]bool{}
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
continue // 表头
|
||||
}
|
||||
cell := func(idx int) string {
|
||||
if idx < len(row) {
|
||||
return strings.TrimSpace(row[idx])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
pr := inboundImportPlan{row: i + 1}
|
||||
pr.code = cell(0)
|
||||
if mode == "sn" {
|
||||
// 图号|SN|区域|生产日期|供应商|架|层|位|备注|检测单号|产品工号|归属类型|归属编号
|
||||
pr.sn = cell(1)
|
||||
pr.zone = cell(2)
|
||||
pr.prodDate = cell(3)
|
||||
pr.supplier = cell(4)
|
||||
pr.shelfNo = cell(5)
|
||||
pr.layerNo = cell(6)
|
||||
pr.positionNo = cell(7)
|
||||
pr.remark = cell(8)
|
||||
pr.inspectionNo = cell(9)
|
||||
pr.productWorkNo = cell(10)
|
||||
pr.ownershipType = cell(11)
|
||||
pr.ownershipNo = cell(12)
|
||||
} else {
|
||||
// 图号|数量|生产日期|供应商|区域|架|层|位|备注|检测单号|产品工号|归属类型|归属编号(批次号由系统自动生成,模板不再含该列)
|
||||
pr.qty = atoi(cell(1), 0)
|
||||
pr.prodDate = cell(2)
|
||||
pr.supplier = cell(3)
|
||||
pr.zone = cell(4)
|
||||
pr.shelfNo = cell(5)
|
||||
pr.layerNo = cell(6)
|
||||
pr.positionNo = cell(7)
|
||||
pr.remark = cell(8)
|
||||
pr.inspectionNo = cell(9)
|
||||
pr.productWorkNo = cell(10)
|
||||
pr.ownershipType = cell(11)
|
||||
pr.ownershipNo = cell(12)
|
||||
}
|
||||
|
||||
if pr.code == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号为空"})
|
||||
continue
|
||||
}
|
||||
m, e := ctx.EntClient.Material.Query().Where(material.CodeEQ(pr.code)).Only(ctx0())
|
||||
if e != nil {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "物料不存在"})
|
||||
continue
|
||||
}
|
||||
if mode == "sn" {
|
||||
if m.SnMode != snModeSN {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "该物料序列号=否(按数量管理),SN 导入仅支持序列号=是(逐件SN)"})
|
||||
continue
|
||||
}
|
||||
if pr.sn == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 为空"})
|
||||
continue
|
||||
}
|
||||
if snSeen[pr.sn] {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 在文件中重复: " + pr.sn})
|
||||
continue
|
||||
}
|
||||
snSeen[pr.sn] = true
|
||||
exists, _ := ctx.EntClient.Inventory.Query().
|
||||
Where(inventory.SnModeEQ(snModeSN), inventory.SnCodeEQ(pr.sn)).Exist(ctx0())
|
||||
if exists {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "SN 已存在: " + pr.sn})
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if m.SnMode != snModeBatch {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "该物料序列号=是(逐件SN管理),请使用 SN 导入"})
|
||||
continue
|
||||
}
|
||||
if pr.qty <= 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "数量必须大于 0"})
|
||||
continue
|
||||
}
|
||||
}
|
||||
if pr.zone == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "区域为空(必填)"})
|
||||
continue
|
||||
}
|
||||
// 货位必填(货架/层/位置):与手动录入(InboundEntry)口径一致,杜绝"有区域无货架"脏数据;任一为空则整批拒绝
|
||||
if pr.shelfNo == "" || pr.layerNo == "" || pr.positionNo == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "货位不完整(货架/层/位置均为必填)"})
|
||||
continue
|
||||
}
|
||||
// 归属校验(与手动录入口径一致:workorder/project 必填编号;other/空 不需要)
|
||||
pr.ownershipType = ownershipTypeOrNone(pr.ownershipType)
|
||||
if pr.ownershipType == "workorder" || pr.ownershipType == "project" {
|
||||
if pr.ownershipNo == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "归属类型为" + pr.ownershipType + "时归属编号必填"})
|
||||
continue
|
||||
}
|
||||
}
|
||||
plans = append(plans, pr)
|
||||
}
|
||||
return plans, errs, total
|
||||
}
|
||||
|
||||
// excelInboundPreviewHandler POST /api/inbound/batch-excel/preview 入库导入预检(只读):
|
||||
// 复用写入端点的阶段1校验(parseInboundImportPlan,同一函数保证规则同源),不写任何数据。
|
||||
// 返回 {total 总行数, success 将成功行数, failed 失败行数, errors 逐行失败原因(行号+图号+原因)}。
|
||||
func excelInboundPreviewHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseMultipartForm(20 << 20); err != nil {
|
||||
fail(w, http.StatusBadRequest, "文件上传失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
mode := r.FormValue("mode")
|
||||
if mode != "sn" {
|
||||
mode = "batch"
|
||||
}
|
||||
f, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "缺少 file 字段")
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
xl, err := excelize.OpenReader(f)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "无法解析 Excel: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer xl.Close()
|
||||
sheet := xl.GetSheetList()[0]
|
||||
rows, err := xl.GetRows(sheet)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "读取工作表失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(rows) <= 1 {
|
||||
fail(w, http.StatusBadRequest, "文件无数据行(首行为表头,需至少一行数据)")
|
||||
return
|
||||
}
|
||||
plans, errs, total := parseInboundImportPlan(ctx, rows, mode)
|
||||
if errs == nil {
|
||||
errs = []importErr{}
|
||||
}
|
||||
ok(w, map[string]any{"total": total, "success": len(plans), "failed": len(errs), "errors": errs})
|
||||
}
|
||||
}
|
||||
|
||||
// ternary 小工具:condition 为真返回 a,否则 b
|
||||
func ternary(cond bool, a, b int) int {
|
||||
if cond {
|
||||
|
||||
@@ -860,116 +860,8 @@ func excelMaterialImportHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
type planRow struct {
|
||||
row int
|
||||
code string
|
||||
name string
|
||||
spec string
|
||||
unit string
|
||||
manageMode int
|
||||
snMode int // 序列号=是/否:按类型默认联动派生(电气件→1,其余→2)
|
||||
itemType int
|
||||
description string
|
||||
}
|
||||
plans := make([]planRow, 0, len(rows)-1)
|
||||
errs := make([]importErr, 0)
|
||||
|
||||
// 表头→列下标映射:容忍列顺序变化与多余列(如「物料类别」/旧「品类」插在单位与类型之间)。
|
||||
// 先按"去括号后的表头名"精确匹配,再按关键字包含兜底(顺序稳定),都找不到回退模板固定下标。
|
||||
normHeader := func(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
for _, op := range []string{"(", "("} {
|
||||
if i := strings.Index(s, op); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
headerNorm := make([]string, len(rows[0]))
|
||||
for j, h := range rows[0] {
|
||||
headerNorm[j] = normHeader(h)
|
||||
}
|
||||
colOf := func(names []string, fallback int) int {
|
||||
for _, n := range names {
|
||||
for j, hn := range headerNorm {
|
||||
if hn == n {
|
||||
return j
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, n := range names {
|
||||
for j, hn := range headerNorm {
|
||||
if hn != "" && strings.Contains(hn, n) {
|
||||
return j
|
||||
}
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
codeCol := colOf([]string{"图号", "物料编码", "编码"}, 0)
|
||||
nameCol := colOf([]string{"名称", "物料名称"}, 1)
|
||||
specCol := colOf([]string{"规格", "规格型号"}, 2)
|
||||
unitCol := colOf([]string{"单位"}, 3)
|
||||
typeCol := colOf([]string{"类型", "管理类型", "管理粒度"}, 4)
|
||||
catCol := colOf([]string{"物料类别", "品类", "类别"}, -1)
|
||||
descCol := colOf([]string{"说明", "备注", "描述"}, 5)
|
||||
|
||||
// 阶段1:解析 + 校验(不落库)
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
continue // 表头
|
||||
}
|
||||
cell := func(idx int) string {
|
||||
if idx < 0 || idx >= len(row) {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(row[idx])
|
||||
}
|
||||
pr := planRow{row: i + 1}
|
||||
pr.code = cell(codeCol)
|
||||
pr.name = cell(nameCol)
|
||||
pr.spec = cell(specCol)
|
||||
pr.unit = cell(unitCol)
|
||||
pr.manageMode = parseManageMode(cell(typeCol))
|
||||
pr.itemType = parseItemType(cell(catCol))
|
||||
pr.snMode = snModeByType(pr.manageMode)
|
||||
if pr.itemType == invCatFinished {
|
||||
pr.snMode = snModeSN // 成品强制「序列号=是」(与编辑/同步同口径,2026-10-19 拍板)
|
||||
}
|
||||
pr.description = cell(descCol)
|
||||
|
||||
if pr.code == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号为空"})
|
||||
continue
|
||||
}
|
||||
if pr.name == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "名称为空"})
|
||||
continue
|
||||
}
|
||||
if pr.spec == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "规格型号为空"})
|
||||
continue
|
||||
}
|
||||
if pr.unit == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "单位为空"})
|
||||
continue
|
||||
}
|
||||
if pr.manageMode == 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "类型无法识别,请填写「结构件/电气件/其他」(兼容 1/2/3)"})
|
||||
continue
|
||||
}
|
||||
if catCol >= 0 && cell(catCol) != "" && pr.itemType == 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "物料类别无法识别,请填写「原材料/成品」(兼容 1/3;半成品不是物料类别,「其他」已取消)"})
|
||||
continue
|
||||
}
|
||||
exists, _ := ctx.EntClient.Material.Query().
|
||||
Where(material.CodeEQ(pr.code)).Exist(ctx0())
|
||||
if exists {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号已存在: " + pr.code})
|
||||
continue
|
||||
}
|
||||
plans = append(plans, pr)
|
||||
}
|
||||
// 阶段1:解析 + 逐行校验(不落库)——与预检端点 excelMaterialImportPreviewHandler 共用同一函数,规则同源
|
||||
plans, errs := parseMaterialImportPlan(ctx, rows)
|
||||
|
||||
// 阶段2:有错则整体拒绝,一行都不写
|
||||
if len(errs) > 0 {
|
||||
@@ -1026,3 +918,160 @@ func excelMaterialImportHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
ok(w, map[string]any{"success": len(plans), "failed": 0, "errors": []importErr{}})
|
||||
}
|
||||
}
|
||||
|
||||
// materialImportPlan 物料档案导入单行写入计划(预检与写入共用同一解析/校验,规则同源不漂移)。
|
||||
type materialImportPlan struct {
|
||||
row int
|
||||
code string
|
||||
name string
|
||||
spec string
|
||||
unit string
|
||||
manageMode int
|
||||
snMode int // 序列号=是/否:按类型默认联动派生(电气件→1,其余→2)
|
||||
itemType int
|
||||
description string
|
||||
}
|
||||
|
||||
// parseMaterialImportPlan 阶段1:解析 + 逐行校验(只读不落库):图号必填且库内唯一 /
|
||||
// 名称、规格、单位必填 / 类型、物料类别合法。返回通过校验的行计划与逐行错误。
|
||||
func parseMaterialImportPlan(ctx *svc.ServiceContext, rows [][]string) ([]materialImportPlan, []importErr) {
|
||||
plans := make([]materialImportPlan, 0, len(rows)-1)
|
||||
errs := make([]importErr, 0)
|
||||
|
||||
// 表头→列下标映射:容忍列顺序变化与多余列(如「物料类别」/旧「品类」插在单位与类型之间)。
|
||||
// 先按"去括号后的表头名"精确匹配,再按关键字包含兜底(顺序稳定),都找不到回退模板固定下标。
|
||||
normHeader := func(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
for _, op := range []string{"(", "("} {
|
||||
if i := strings.Index(s, op); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
headerNorm := make([]string, len(rows[0]))
|
||||
for j, h := range rows[0] {
|
||||
headerNorm[j] = normHeader(h)
|
||||
}
|
||||
colOf := func(names []string, fallback int) int {
|
||||
for _, n := range names {
|
||||
for j, hn := range headerNorm {
|
||||
if hn == n {
|
||||
return j
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, n := range names {
|
||||
for j, hn := range headerNorm {
|
||||
if hn != "" && strings.Contains(hn, n) {
|
||||
return j
|
||||
}
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
codeCol := colOf([]string{"图号", "物料编码", "编码"}, 0)
|
||||
nameCol := colOf([]string{"名称", "物料名称"}, 1)
|
||||
specCol := colOf([]string{"规格", "规格型号"}, 2)
|
||||
unitCol := colOf([]string{"单位"}, 3)
|
||||
typeCol := colOf([]string{"类型", "管理类型", "管理粒度"}, 4)
|
||||
catCol := colOf([]string{"物料类别", "品类", "类别"}, -1)
|
||||
descCol := colOf([]string{"说明", "备注", "描述"}, 5)
|
||||
|
||||
for i, row := range rows {
|
||||
if i == 0 {
|
||||
continue // 表头
|
||||
}
|
||||
cell := func(idx int) string {
|
||||
if idx < 0 || idx >= len(row) {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(row[idx])
|
||||
}
|
||||
pr := materialImportPlan{row: i + 1}
|
||||
pr.code = cell(codeCol)
|
||||
pr.name = cell(nameCol)
|
||||
pr.spec = cell(specCol)
|
||||
pr.unit = cell(unitCol)
|
||||
pr.manageMode = parseManageMode(cell(typeCol))
|
||||
pr.itemType = parseItemType(cell(catCol))
|
||||
pr.snMode = snModeByType(pr.manageMode)
|
||||
if pr.itemType == invCatFinished {
|
||||
pr.snMode = snModeSN // 成品强制「序列号=是」(与编辑/同步同口径,2026-10-19 拍板)
|
||||
}
|
||||
pr.description = cell(descCol)
|
||||
|
||||
if pr.code == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号为空"})
|
||||
continue
|
||||
}
|
||||
if pr.name == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "名称为空"})
|
||||
continue
|
||||
}
|
||||
if pr.spec == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "规格型号为空"})
|
||||
continue
|
||||
}
|
||||
if pr.unit == "" {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "单位为空"})
|
||||
continue
|
||||
}
|
||||
if pr.manageMode == 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "类型无法识别,请填写「结构件/电气件/其他」(兼容 1/2/3)"})
|
||||
continue
|
||||
}
|
||||
if catCol >= 0 && cell(catCol) != "" && pr.itemType == 0 {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "物料类别无法识别,请填写「原材料/成品」(兼容 1/3;半成品不是物料类别,「其他」已取消)"})
|
||||
continue
|
||||
}
|
||||
exists, _ := ctx.EntClient.Material.Query().
|
||||
Where(material.CodeEQ(pr.code)).Exist(ctx0())
|
||||
if exists {
|
||||
errs = append(errs, importErr{pr.row, pr.code, "图号已存在: " + pr.code})
|
||||
continue
|
||||
}
|
||||
plans = append(plans, pr)
|
||||
}
|
||||
return plans, errs
|
||||
}
|
||||
|
||||
// excelMaterialImportPreviewHandler POST /api/material/import/preview 物料档案导入预检(只读):
|
||||
// 复用写入端点的阶段1校验(parseMaterialImportPlan,同一函数保证规则同源),不写任何数据。
|
||||
// 返回 {total 总行数, success 将成功行数, failed 失败行数, errors 逐行失败原因(行号+图号+原因)}。
|
||||
func excelMaterialImportPreviewHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseMultipartForm(20 << 20); err != nil {
|
||||
fail(w, http.StatusBadRequest, "文件上传失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
f, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "缺少 file 字段")
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
xl, err := excelize.OpenReader(f)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "无法解析 Excel: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer xl.Close()
|
||||
sheet := xl.GetSheetList()[0]
|
||||
rows, err := xl.GetRows(sheet)
|
||||
if err != nil {
|
||||
fail(w, http.StatusBadRequest, "读取工作表失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(rows) <= 1 {
|
||||
fail(w, http.StatusBadRequest, "文件无数据行(首行为表头,需至少一行数据)")
|
||||
return
|
||||
}
|
||||
plans, errs := parseMaterialImportPlan(ctx, rows)
|
||||
if errs == nil {
|
||||
errs = []importErr{}
|
||||
}
|
||||
ok(w, map[string]any{"total": len(rows) - 1, "success": len(plans), "failed": len(errs), "errors": errs})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodGet, Path: "/api/material/detail", Handler: getMaterialHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/material/export", Handler: requirePerm(ctx, "material:export", exportMaterialsHandler(ctx))},
|
||||
{Method: http.MethodPost, Path: "/api/material/import", Handler: requirePerm(ctx, "material:import", excelMaterialImportHandler(ctx))},
|
||||
{Method: http.MethodPost, Path: "/api/material/import/preview", Handler: requirePerm(ctx, "material:import", excelMaterialImportPreviewHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/material/import-template", Handler: requirePerm(ctx, "material:import", excelMaterialImportTemplateHandler(ctx))},
|
||||
{Method: http.MethodPost, Path: "/api/material/toggle-status", Handler: requirePerm(ctx, "material:edit", toggleMaterialStatusHandler(ctx))},
|
||||
{Method: http.MethodPost, Path: "/api/material/retry-sync", Handler: requirePerm(ctx, "material:edit", retryMaterialSyncHandler(ctx))},
|
||||
@@ -76,6 +77,7 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodGet, Path: "/api/inbound/details", Handler: inboundDetailsHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/inbound/export", Handler: exportInboundHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/inbound/batch-excel", Handler: excelInboundHandler(ctx)},
|
||||
{Method: http.MethodPost, Path: "/api/inbound/batch-excel/preview", Handler: excelInboundPreviewHandler(ctx)},
|
||||
{Method: http.MethodGet, Path: "/api/inbound/import-template", Handler: excelInboundImportTemplateHandler(ctx)},
|
||||
// 入库库位联动:选物料后回填最近一次入库库位
|
||||
{Method: http.MethodGet, Path: "/api/inbound/last-location", Handler: lastInboundLocationHandler(ctx)},
|
||||
@@ -372,6 +374,7 @@ func RegisterHandlers(server *rest.Server, ctx *svc.ServiceContext) {
|
||||
{Method: http.MethodPost, Path: "/api/internal/outbound/delivered", Handler: wrapInternal(ctx)(markOutboundDeliveredHandler(ctx))},
|
||||
// 配送单送达回写(2026-11-05 拍板:AGV 卸货放行后 MES 回写;配送单不存在时忽略,兼容旧链路)
|
||||
{Method: http.MethodPost, Path: "/api/internal/delivery/delivered", Handler: wrapInternal(ctx)(deliveryDeliveredInternalHandler(ctx))},
|
||||
{Method: http.MethodGet, Path: "/api/internal/delivery/status", Handler: wrapInternal(ctx)(deliveryStatusInternalHandler(ctx))},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user