重构排产/叫料配送/时钟/终端事件驱动,清理 SHIPPED 废弃状态
时钟与 JWT:验签按「谁签发谁的时间为准」,向签发方取 /api/server-time, 对端地址由既有 BaseURL 自动推导;不再要求现场人工 w32tm 对时。 排产 C 方案:工单状态不再回写排产,排产只表示启用开关;启用排产是唯一 开工开关,未启用不允许建档上件;完工/取消后工单进度从工单派生、叫料池 按工单状态重算;逐日手改后合计为准;日期筛选默认空=全部日期。 叫料配送 B 方案:新增 DELIVERED 待签收态,货真到工位才从 PREPARED 转 DELIVERED、工人签收再转 DONE;PREPARED 在途不可签收/退库;叫料队列一屏 显示 PENDING+PREPARED、支持批量完成备料;配送链改按工位凑车,一张配送单 =一个托盘=一个工位,只在配送单页新建,未派车可删可改;新建弹窗直列有货 工位;派车硬门禁要求接驳台传感器检测到托盘;删除改为事务内复核状态、 带 PENDING 条件删单头,防止并发派车时误删。 装配记账:新增已装量 installedQty 与追溯绑定 traceBind,落实「总账=分账 之和」;追溯件绑定/撤销回补、非序列号件报工按单台用量自动扣已装;报工前 按工位×物料核对累计已签收 ≥ 已消耗,不够拦下。 工件与工位终端:建档/重上线增加上料位可达性校验与 PLC 送件、半截补齐防 重复扣库;终端去轮询改事件驱动(报警/工件到位/点位/停单即时推送),铃铛 只挂消息类预警、与待办角标分家;刷新保留拧紧录入;跨服务连线失败不再 误报成数据库故障;新增 BJ_POWER_DEV=1 单进程调试模式(不起守护/托盘)。 告警:新增处置字段 resolved_at/resolved_by,红点=待处置数,确认关闭才消。 清理:删除 migrate 中 SHIPPED→DELIVERED 兼容补丁;场景1测试断言改为 现行 PREPARED / DELIVERED;同步更新案例.md、现场对时操作卡.md 与 ent 生成代码。
This commit is contained in:
@@ -6,16 +6,55 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/callpool"
|
||||
"bj_power_mes/ent/dock"
|
||||
"bj_power_mes/ent/materialrequest"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
"bj_power_mes/ent/workpiecebind"
|
||||
"bj_power_mes/ent/workpieceprocess"
|
||||
)
|
||||
|
||||
// adjustInstalledQty 调整工位叫料池「已装量」(2026-11-26 用户铁律「总账=分账之和」)。
|
||||
// delta>0=装配占用(追溯件绑定/非追溯件报工),delta<0=撤销回补。
|
||||
// 只改 installed_qty 一列,用 ent 原子增减(AddInstalledQty)避免并发丢更新;
|
||||
// 工位号来自绑定记录的 stationNo(字符串),无效或池子无对应行时静默跳过——
|
||||
// 记账是追溯的附属,不能因池子缺行卡死装配主流程(无行场景:虚拟位/未启用排产)。
|
||||
func (s *Service) adjustInstalledQty(ctx context.Context, client *ent.Client, stationNo, materialCode string, delta int) {
|
||||
if delta == 0 || stationNo == "" || materialCode == "" {
|
||||
return
|
||||
}
|
||||
stNo, err := strconv.Atoi(stationNo)
|
||||
if err != nil || stNo <= 0 {
|
||||
return
|
||||
}
|
||||
_, err = client.CallPool.Update().
|
||||
Where(callpool.StationNo(stNo), callpool.MaterialCode(materialCode)).
|
||||
AddInstalledQty(delta).Save(ctx)
|
||||
if err != nil {
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.bind.installed_fail", "", "system", "call_pool", stationNo,
|
||||
"已装量记账失败", map[string]any{"stationNo": stationNo, "materialCode": materialCode, "delta": delta, "err": err.Error()})
|
||||
}
|
||||
}
|
||||
|
||||
// installedPerBind 每条绑定分账对应的已装量(2026-11-26 用户拍板:按实际数量):
|
||||
// SN 件逐颗绑定,每颗计 1(一台绑 ceil(单台用量) 颗合计正好=单台用量);
|
||||
// 批次件只扫 1 个批次号,已装按单台用量整包计(数量自动带出,工人不用管)。
|
||||
func installedPerBind(snMode int, unitQty float64) int {
|
||||
if snMode == 1 {
|
||||
return 1
|
||||
}
|
||||
n := int(math.Ceil(unitQty))
|
||||
if n < 1 {
|
||||
n = 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// RemoveBind 撤销一条绑定(仅允许未报工前的误扫纠正;报工后不可撤销)
|
||||
func (s *Service) RemoveBind(ctx context.Context, sn string, flowId int, materialCode, bindValue, operator string) error {
|
||||
if sn == "" || materialCode == "" || bindValue == "" {
|
||||
@@ -30,6 +69,13 @@ func (s *Service) RemoveBind(ctx context.Context, sn string, flowId int, materia
|
||||
if done {
|
||||
return errors.New("该工艺已报工,不能撤销装配绑定")
|
||||
}
|
||||
// 先取回待删绑定(拿工位号用于已装量回补)
|
||||
row, err := s.ctx.EntClient.WorkpieceBind.Query().
|
||||
Where(workpiecebind.Sn(sn), workpiecebind.FlowId(flowId),
|
||||
workpiecebind.MaterialCode(materialCode), workpiecebind.BindValue(bindValue)).Only(ctx)
|
||||
if err != nil {
|
||||
return errors.New("未找到该绑定记录")
|
||||
}
|
||||
n, err := s.ctx.EntClient.WorkpieceBind.Delete().
|
||||
Where(workpiecebind.Sn(sn), workpiecebind.FlowId(flowId),
|
||||
workpiecebind.MaterialCode(materialCode), workpiecebind.BindValue(bindValue)).Exec(ctx)
|
||||
@@ -39,8 +85,21 @@ func (s *Service) RemoveBind(ctx context.Context, sn string, flowId int, materia
|
||||
if n == 0 {
|
||||
return errors.New("未找到该绑定记录")
|
||||
}
|
||||
// 回补已装量(与绑定扣账同口径 installedPerBind:SN颗=1、批次件按单台用量整包回补)
|
||||
unitQty := 1.0
|
||||
if _, wo, werr := s.workpieceProduct(ctx, s.ctx.EntClient, sn); werr == nil && wo != nil {
|
||||
if dm, derr := s.MaterialsByFlowForWO(ctx, s.ctx.EntClient, wo, []int{flowId}); derr == nil {
|
||||
for _, d := range dm[flowId] {
|
||||
if d != nil && d.MaterialCode == materialCode {
|
||||
unitQty = d.UnitQty
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s.adjustInstalledQty(ctx, s.ctx.EntClient, row.StationNo, materialCode, -installedPerBind(row.SnMode, unitQty))
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.bind.remove", "", operator, "workpiece_bind", sn,
|
||||
"撤销装配绑定", map[string]any{"flowId": flowId, "materialCode": materialCode, "bindValue": bindValue})
|
||||
"撤销装配绑定", map[string]any{"flowId": flowId, "materialCode": materialCode, "bindValue": bindValue, "stationNo": row.StationNo})
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -113,6 +172,12 @@ func (s *Service) stationBindTargets(ctx context.Context, client *ent.Client, sn
|
||||
}
|
||||
out := make([]bindTarget, 0, len(fmByFlow[flowId]))
|
||||
for _, it := range fmByFlow[flowId] {
|
||||
// 追溯绑定开关(2026-11-26 拍板):产品总账未勾选「追溯绑定」的物料(C类标准件)不进
|
||||
// 绑定清单/齐套校验——绑定是关键件的追溯动作,不是全物料的记账负担。绑定方式由
|
||||
// 物料档案 sn_mode 自动判定(序列号=是逐件扫SN / 序列号=否记批次),无需再选档位。
|
||||
if !it.TraceBind {
|
||||
continue
|
||||
}
|
||||
out = append(out, bindTarget{
|
||||
MaterialCode: it.MaterialCode, MaterialName: it.MaterialName, Spec: it.Spec,
|
||||
Unit: it.Unit, SnMode: it.SnMode, UnitQty: it.UnitQty,
|
||||
@@ -121,10 +186,50 @@ func (s *Service) stationBindTargets(ctx context.Context, client *ent.Client, sn
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// deliveredOutboundNos 本工位「已签收叫料单」对应的 WMS 出库单号集合(SN 绑定的合法来源范围)。
|
||||
// 签收=PREPARED→DONE,代表料确实送到本工位;叫料单 outboundNo 可能多个逗号分隔。
|
||||
func (s *Service) deliveredOutboundNos(ctx context.Context, client *ent.Client, stationNo string) []string {
|
||||
n, err := strconv.Atoi(strings.TrimSpace(stationNo))
|
||||
if err != nil || n <= 0 {
|
||||
return nil
|
||||
}
|
||||
rows, err := client.MaterialRequest.Query().
|
||||
Where(materialrequest.StationNo(n), materialrequest.StatusEQ("DONE")).All(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
set := map[string]bool{}
|
||||
out := make([]string, 0)
|
||||
for _, mr := range rows {
|
||||
for _, no := range strings.Split(mr.OutboundNo, ",") {
|
||||
no = strings.TrimSpace(no)
|
||||
if no != "" && !set[no] {
|
||||
set[no] = true
|
||||
out = append(out, no)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// bindNeed 单个物料在本工件上允许绑定的最大数量(与齐套判定同口径):
|
||||
// 序列号=是 按单台用量向上取整(至少1颗);序列号=否 只认1个批次号。
|
||||
func bindNeed(snMode int, unitQty float64) int {
|
||||
if snMode == 1 {
|
||||
n := int(math.Ceil(unitQty))
|
||||
if n < 1 {
|
||||
n = 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// applyBinds 报工时应用装机绑定(落库 + 防错校验):
|
||||
// ① 料必须在该工件来源产品工艺的分账中;
|
||||
// ② 电气件 SN 全局防重:已被其他工件绑定则拒绝(防错拿/防重复装机);
|
||||
// ③ 同一工件同一料同一绑定值幂等(重复扫码不报错,静默去重)。
|
||||
// ③ 同一工件同一料同一绑定值幂等(重复扫码不报错,静默去重);
|
||||
// ④ 绑定数量卡死:本工件该物料已绑数达到需要量后再绑直接拒绝(2026-11-26 用户拍板)。
|
||||
// 返回绑定成功条数。
|
||||
func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderNo string, flowId int, stationNo string, operator string, items []BindItemReq) (int, error) {
|
||||
if len(items) == 0 {
|
||||
@@ -150,6 +255,44 @@ func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderN
|
||||
SnMode: targets[i].SnMode, UnitQty: targets[i].UnitQty,
|
||||
}
|
||||
}
|
||||
// 绑定值校验(2026-11-26 用户定案):
|
||||
// 序列号=是 → SN 必须是「发到本工位」的料:本工位已签收叫料单对应出库明细里的 SN;
|
||||
// 序列号=否 → 批次件放开,信任现场实物标签,不校验。
|
||||
// 按物料分组一次校验;WMS 不可达 → 放行并写 event_log 留痕(不因 WMS 故障卡产线装配)。
|
||||
outNos := s.deliveredOutboundNos(ctx, client, stationNo)
|
||||
chk := map[string][]string{} // materialCode → SN 列表(只有序列号=是的进这里)
|
||||
for _, it := range items {
|
||||
mc := strings.TrimSpace(it.MaterialCode)
|
||||
bv := strings.TrimSpace(it.BindValue)
|
||||
fm, ok := fmMap[mc]
|
||||
if !ok || bv == "" {
|
||||
continue // 留给下方逐条校验报错
|
||||
}
|
||||
if fm.SnMode == 1 {
|
||||
chk[mc] = append(chk[mc], bv)
|
||||
}
|
||||
}
|
||||
for mc, vals := range chk {
|
||||
missing, err := s.ctx.Wms.BindValuesMissing(ctx, mc, 1, vals, outNos)
|
||||
if err != nil {
|
||||
// WMS 不可达:fail-open + 留痕。校验能力依赖 WMS,但装配不能被 WMS 故障卡死。
|
||||
s.ctx.EventLog.Write(ctx, "workpiece.bind.check_skip", sn, operator, "workpiece_bind", sn,
|
||||
fmt.Sprintf("SN 到料校验跳过(WMS 不可达):%s %v", mc, vals),
|
||||
map[string]any{"sn": sn, "materialCode": mc, "err": err.Error()})
|
||||
continue
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return 0, fmt.Errorf("SN 校验不通过:%s 不是发到本工位的物料,不能用在本工件上;请核对实物是否本工位叫料,错拿请退回库房",
|
||||
strings.Join(missing, "、"))
|
||||
}
|
||||
}
|
||||
// 本工件各物料已绑数(数量卡死用,一次查全,循环内增量维护)
|
||||
have := map[string]int{}
|
||||
existingRows, _ := client.WorkpieceBind.Query().
|
||||
Where(workpiecebind.Sn(sn)).All(ctx)
|
||||
for _, rb := range existingRows {
|
||||
have[rb.MaterialCode]++
|
||||
}
|
||||
bound := 0
|
||||
for _, it := range items {
|
||||
mc := strings.TrimSpace(it.MaterialCode)
|
||||
@@ -159,7 +302,7 @@ func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderN
|
||||
}
|
||||
fm, ok := fmMap[mc]
|
||||
if !ok {
|
||||
return bound, fmt.Errorf("物料 %s 未配置在工艺「%s」的物料清单,不能绑定", mc, s.flowNameById(ctx, flowId))
|
||||
return bound, fmt.Errorf("物料 %s 不在本工艺的追溯绑定清单(总账未勾选「追溯绑定」或未分配到该工艺),不能绑定", mc)
|
||||
}
|
||||
snMode := fm.SnMode
|
||||
if snMode != 1 {
|
||||
@@ -171,6 +314,17 @@ func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderN
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
// 数量卡死:已达需要量仍未绑过这个新值 → 拒绝(防超装多算追溯)
|
||||
need := bindNeed(snMode, fm.UnitQty)
|
||||
if have[mc] >= need {
|
||||
kind := "批次号"
|
||||
if snMode == 1 {
|
||||
kind = fmt.Sprintf("%d颗SN", need)
|
||||
}
|
||||
return bound, fmt.Errorf("物料 %s(%s)本工件只需绑定 %s,已绑满 %d 个,不能再绑;如装错请先在追溯明细移除后再绑",
|
||||
mc, fm.MaterialName, kind, have[mc])
|
||||
}
|
||||
have[mc]++
|
||||
// 序列号=是 SN 全局防重
|
||||
if snMode == 1 {
|
||||
other, _ := client.WorkpieceBind.Query().
|
||||
@@ -192,6 +346,9 @@ func (s *Service) applyBinds(ctx context.Context, client *ent.Client, sn, orderN
|
||||
Exec(ctx); err != nil {
|
||||
return bound, err
|
||||
}
|
||||
// 已装量扣账(总账=分账之和,按实际数量):SN件每颗+1;批次件扫1个批次号按单台用量整包+。
|
||||
// 失败不卡装配,仅留痕。
|
||||
s.adjustInstalledQty(ctx, client, stationNo, mc, installedPerBind(snMode, fm.UnitQty))
|
||||
bound++
|
||||
}
|
||||
return bound, nil
|
||||
@@ -276,10 +433,9 @@ func (s *Service) validateStationBinds(ctx context.Context, client *ent.Client,
|
||||
for _, t := range targets {
|
||||
set := boundMap[t.MaterialCode]
|
||||
boundCount := len(set)
|
||||
need := int(math.Ceil(t.UnitQty))
|
||||
if need < 1 {
|
||||
need = 1
|
||||
}
|
||||
// 需绑量与 applyBinds 数量卡死同口径(bindNeed):SN件=ceil(单台用量)、批次件=1个批次号。
|
||||
// 2026-11-26 审计修正:原 ceil(UnitQty) 不分模式,批次件用量>1 时与卡死上限矛盾 → 死锁绑不齐。
|
||||
need := bindNeed(t.SnMode, t.UnitQty)
|
||||
if t.SnMode == 1 { // 序列号=是 SN:需绑满单台用量
|
||||
if boundCount < need {
|
||||
missing = append(missing, fmt.Sprintf("%s(%s):需绑定%d颗SN,已绑%d颗", t.MaterialCode, t.MaterialName, need, boundCount))
|
||||
@@ -507,10 +663,8 @@ func (s *Service) StationBindPanelData(ctx context.Context, sn string, flowId in
|
||||
if !hasDock {
|
||||
break // 无接驳台工位不展示「需绑批次 / 需绑SN」应绑清单
|
||||
}
|
||||
need := int(math.Ceil(t.UnitQty))
|
||||
if need < 1 {
|
||||
need = 1
|
||||
}
|
||||
// 需绑量与卡死/报工齐套同口径(bindNeed):批次件=1,避免面板显示"需绑2"实际绑1就齐的误导
|
||||
need := bindNeed(t.SnMode, t.UnitQty)
|
||||
bound := make([]string, 0, len(boundMap[t.MaterialCode]))
|
||||
for v := range boundMap[t.MaterialCode] {
|
||||
bound = append(bound, v)
|
||||
|
||||
Reference in New Issue
Block a user