From 33c7d52d8c0c28601d88f99bd993d49bb1d782cc Mon Sep 17 00:00:00 2001 From: lixiaolong <702156524@qq.com> Date: Sat, 21 Nov 2026 21:50:22 +0800 Subject: [PATCH] feat: integrate barcode recognition with task workbench --- am-backend/sql/freescan/019_barcode.sql | 90 ++ .../admin/barcode/LabBarcodeController.java | 107 ++ .../barcode/vo/LabBarcodeAuditRespVO.java | 21 + .../admin/barcode/vo/LabBarcodeBindReqVO.java | 27 + .../barcode/vo/LabBarcodeDisableReqVO.java | 20 + .../vo/LabBarcodeHistoryPageReqVO.java | 18 + .../vo/LabBarcodePartOptionRespVO.java | 15 + .../barcode/vo/LabBarcodeProfileReqVO.java | 14 + .../barcode/vo/LabBarcodeProfileRespVO.java | 10 + .../barcode/vo/LabBarcodeRebindReqVO.java | 27 + .../barcode/vo/LabBarcodeRegisterReqVO.java | 29 + .../barcode/vo/LabBarcodeResolveReqVO.java | 27 + .../barcode/vo/LabBarcodeResolveRespVO.java | 89 ++ .../barcode/vo/LabBarcodeUnbindReqVO.java | 24 + .../barcode/LabBarcodeScanProfileDO.java | 19 + .../barcode/LabPieceBarcodeAuditDO.java | 27 + .../barcode/LabPieceBarcodeBindingDO.java | 33 + .../dataobject/barcode/LabPieceBarcodeDO.java | 26 + .../barcode/LabBarcodeScanProfileMapper.java | 19 + .../barcode/LabPieceBarcodeAuditMapper.java | 21 + .../barcode/LabPieceBarcodeBindingMapper.java | 31 + .../mysql/barcode/LabPieceBarcodeMapper.java | 19 + .../module/lab/enums/ErrorCodeConstants.java | 18 + .../barcode/LabBarcodeCommittedRead.java | 48 + .../service/barcode/LabBarcodeMatchRules.java | 130 +++ .../service/barcode/LabBarcodeService.java | 29 + .../barcode/LabBarcodeServiceImpl.java | 941 ++++++++++++++++++ am-web/src/api/lab/barcode/index.ts | 155 +++ .../views/lab/barcode/BarcodeScanPanel.vue | 539 ++++++++++ am-web/src/views/lab/barcode/index.vue | 9 + am-web/src/views/lab/dimension/index.vue | 1 + am-web/src/views/lab/dimension/workbench.vue | 30 +- am-web/src/views/lab/scan-task/index.vue | 13 + deploy/windows11/RELEASE-20261121-barcode.md | 28 + docs/barcode-integration-20261121.md | 46 + docs/barcode-source.json | 28 + scripts/bootstrap_db.py | 2 +- scripts/bootstrap_standalone.py | 9 +- scripts/upgrade_barcode.py | 31 + 39 files changed, 2760 insertions(+), 10 deletions(-) create mode 100644 am-backend/sql/freescan/019_barcode.sql create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/LabBarcodeController.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeAuditRespVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeBindReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeDisableReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeHistoryPageReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodePartOptionRespVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileRespVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRebindReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRegisterReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveRespVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeUnbindReqVO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabBarcodeScanProfileDO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeAuditDO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeBindingDO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeDO.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabBarcodeScanProfileMapper.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeAuditMapper.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeBindingMapper.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeMapper.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeCommittedRead.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeMatchRules.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeService.java create mode 100644 am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeServiceImpl.java create mode 100644 am-web/src/api/lab/barcode/index.ts create mode 100644 am-web/src/views/lab/barcode/BarcodeScanPanel.vue create mode 100644 am-web/src/views/lab/barcode/index.vue create mode 100644 deploy/windows11/RELEASE-20261121-barcode.md create mode 100644 docs/barcode-integration-20261121.md create mode 100644 docs/barcode-source.json create mode 100644 scripts/upgrade_barcode.py diff --git a/am-backend/sql/freescan/019_barcode.sql b/am-backend/sql/freescan/019_barcode.sql new file mode 100644 index 0000000..572e2c8 --- /dev/null +++ b/am-backend/sql/freescan/019_barcode.sql @@ -0,0 +1,90 @@ +-- RC7.2 实物条码登记与测量任务关联。 +-- 不改 Task / Selection / Execution / ResultBatch 状态机。 +-- 同一任务同一零部件只允许一件实物条码处于关联中:现有执行记录不能按实物拆分结果。 + +CREATE TABLE IF NOT EXISTS `lab_piece_barcode` ( + `id` bigint NOT NULL COMMENT '主键', + `barcode` varchar(128) NOT NULL COMMENT '实物条码原值', + `barcode_namespace` varchar(64) NOT NULL DEFAULT 'LOCAL' COMMENT '编码来源或业务域', + `sample_id` bigint NOT NULL COMMENT '零部件主数据 lab_sample.id', + `part_no` varchar(64) NOT NULL COMMENT '零部件编号,稳定键', + `physical_serial_no` varchar(128) DEFAULT NULL COMMENT '实物序列号', + `status` varchar(32) NOT NULL COMMENT 'ACTIVE/DISABLED', + `source` varchar(32) NOT NULL COMMENT 'MANUAL/IMPORT/EXTERNAL', + `active_flag` tinyint DEFAULT NULL COMMENT '1=当前有效;停用后置空以释放唯一约束', + `remark` varchar(500) DEFAULT NULL COMMENT '备注', + `creator` varchar(64) DEFAULT '', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(64) DEFAULT '', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted` bit(1) NOT NULL DEFAULT b'0', + `tenant_id` bigint NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_piece_barcode_active` (`tenant_id`, `barcode_namespace`, `barcode`, `active_flag`), + KEY `idx_piece_barcode_part` (`tenant_id`, `part_no`, `status`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='实物条码登记'; + +CREATE TABLE IF NOT EXISTS `lab_piece_barcode_binding` ( + `id` bigint NOT NULL COMMENT '主键', + `barcode_id` bigint NOT NULL COMMENT '条码登记', + `barcode` varchar(128) NOT NULL COMMENT '条码快照', + `task_id` bigint NOT NULL COMMENT '测量任务', + `part_no` varchar(64) NOT NULL COMMENT '零部件编号', + `sample_id` bigint NOT NULL COMMENT '零部件主数据', + `binding_status` varchar(32) NOT NULL COMMENT 'BOUND/UNBOUND', + `source` varchar(32) NOT NULL COMMENT 'SCAN/MANUAL', + `idempotency_key` varchar(128) DEFAULT NULL COMMENT '幂等键', + `bound_by` bigint DEFAULT NULL COMMENT '绑定人', + `bound_at` datetime DEFAULT NULL COMMENT '绑定时间', + `unbound_by` bigint DEFAULT NULL COMMENT '解除人', + `unbound_at` datetime DEFAULT NULL COMMENT '解除时间', + `unbind_reason` varchar(500) DEFAULT NULL COMMENT '解除或改绑原因', + `active_flag` tinyint DEFAULT NULL COMMENT '1=当前关联;解除后置空', + `creator` varchar(64) DEFAULT '', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(64) DEFAULT '', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted` bit(1) NOT NULL DEFAULT b'0', + `tenant_id` bigint NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_piece_binding_one` (`tenant_id`, `task_id`, `part_no`, `active_flag`), + UNIQUE KEY `uk_piece_binding_idem` (`tenant_id`, `idempotency_key`, `active_flag`), + KEY `idx_piece_binding_barcode` (`tenant_id`, `barcode_id`, `binding_status`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='实物条码与测量任务关联'; + +CREATE TABLE IF NOT EXISTS `lab_piece_barcode_audit` ( + `id` bigint NOT NULL COMMENT '主键', + `action` varchar(32) NOT NULL COMMENT '动作', + `barcode` varchar(128) DEFAULT NULL COMMENT '条码', + `barcode_namespace` varchar(64) DEFAULT NULL COMMENT '编码域', + `barcode_id` bigint DEFAULT NULL COMMENT '条码登记', + `task_id` bigint DEFAULT NULL COMMENT '任务', + `part_no` varchar(64) DEFAULT NULL COMMENT '零部件编号', + `resolution` varchar(32) DEFAULT NULL COMMENT '识别结论', + `message` varchar(500) DEFAULT NULL COMMENT '说明', + `detail_json` varchar(1000) DEFAULT NULL COMMENT '最小审计明细', + `operator_id` bigint DEFAULT NULL COMMENT '操作人', + `creator` varchar(64) DEFAULT '', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(64) DEFAULT '', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted` bit(1) NOT NULL DEFAULT b'0', + `tenant_id` bigint NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + KEY `idx_piece_barcode_audit` (`tenant_id`, `barcode`, `create_time`), + KEY `idx_piece_barcode_audit_task` (`tenant_id`, `task_id`, `create_time`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='实物条码识别与关联审计'; + +CREATE TABLE IF NOT EXISTS `lab_barcode_scan_profile` ( + `id` bigint NOT NULL COMMENT '主键', + `prefix` varchar(16) NOT NULL DEFAULT '' COMMENT '扫描枪前缀,识别时去掉一次', + `suffix` varchar(16) NOT NULL DEFAULT '' COMMENT '扫描枪后缀,识别时去掉一次', + `creator` varchar(64) DEFAULT '', + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updater` varchar(64) DEFAULT '', + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted` bit(1) NOT NULL DEFAULT b'0', + `tenant_id` bigint NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_barcode_scan_profile_tenant` (`tenant_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='扫码枪前后缀配置'; diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/LabBarcodeController.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/LabBarcodeController.java new file mode 100644 index 0000000..428d282 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/LabBarcodeController.java @@ -0,0 +1,107 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode; + +import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.lab.controller.admin.barcode.vo.*; +import cn.iocoder.yudao.module.lab.service.barcode.LabBarcodeService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import jakarta.validation.Valid; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.CREATE; +import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.UPDATE; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + +@Tag(name = "管理后台 - 实物条码") +@RestController +@RequestMapping("/lab/barcode") +@Validated +public class LabBarcodeController { + + @Resource + private LabBarcodeService barcodeService; + + @PostMapping("/resolve") + @Operation(summary = "识别实物条码并匹配测量任务,不写任务状态") + @PreAuthorize("@ss.hasPermission('lab:barcode:query')") + public CommonResult resolve(@Valid @RequestBody LabBarcodeResolveReqVO reqVO) { + return success(barcodeService.resolve(reqVO)); + } + + @PostMapping("/register") + @Operation(summary = "把未登记条码绑定到已有零部件") + @PreAuthorize("@ss.hasPermission('lab:barcode:register')") + @ApiAccessLog(operateType = CREATE) + public CommonResult register(@Valid @RequestBody LabBarcodeRegisterReqVO reqVO) { + return success(barcodeService.register(reqVO)); + } + + @PostMapping("/bind-task") + @Operation(summary = "把已登记条码关联到测量任务") + @PreAuthorize("@ss.hasPermission('lab:barcode:bind')") + @ApiAccessLog(operateType = CREATE) + public CommonResult bindTask(@Valid @RequestBody LabBarcodeBindReqVO reqVO) { + return success(barcodeService.bindTask(reqVO)); + } + + @PostMapping("/unbind-task") + @Operation(summary = "解除条码与测量任务的关联") + @PreAuthorize("@ss.hasPermission('lab:barcode:rebind')") + @ApiAccessLog(operateType = UPDATE) + public CommonResult unbindTask(@Valid @RequestBody LabBarcodeUnbindReqVO reqVO) { + return success(barcodeService.unbindTask(reqVO)); + } + + @PostMapping("/rebind") + @Operation(summary = "把条码改绑到另一个已有零部件") + @PreAuthorize("@ss.hasPermission('lab:barcode:rebind')") + @ApiAccessLog(operateType = UPDATE) + public CommonResult rebind(@Valid @RequestBody LabBarcodeRebindReqVO reqVO) { + return success(barcodeService.rebind(reqVO)); + } + + @PostMapping("/disable") + @Operation(summary = "停用实物条码") + @PreAuthorize("@ss.hasPermission('lab:barcode:rebind')") + @ApiAccessLog(operateType = UPDATE) + public CommonResult disable(@Valid @RequestBody LabBarcodeDisableReqVO reqVO) { + return success(barcodeService.disable(reqVO)); + } + + @GetMapping("/binding-history") + @Operation(summary = "查看条码登记和关联审计") + @PreAuthorize("@ss.hasPermission('lab:barcode:query')") + public CommonResult> history(@Valid LabBarcodeHistoryPageReqVO reqVO) { + return success(barcodeService.history(reqVO)); + } + + @GetMapping("/parts") + @Operation(summary = "搜索可登记的已有零部件") + @PreAuthorize("@ss.hasPermission('lab:barcode:register')") + public CommonResult> parts(@RequestParam(value = "keyword", required = false) String keyword) { + return success(barcodeService.searchParts(keyword)); + } + + @GetMapping("/profile") + @Operation(summary = "读取扫码前后缀") + @PreAuthorize("@ss.hasPermission('lab:barcode:query')") + public CommonResult getProfile() { + return success(barcodeService.getProfile()); + } + + @PutMapping("/profile") + @Operation(summary = "保存扫码前后缀") + @PreAuthorize("@ss.hasPermission('lab:barcode:register')") + @ApiAccessLog(operateType = UPDATE) + public CommonResult saveProfile(@Valid @RequestBody LabBarcodeProfileReqVO reqVO) { + barcodeService.saveProfile(reqVO); + return success(true); + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeAuditRespVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeAuditRespVO.java new file mode 100644 index 0000000..950d0c1 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeAuditRespVO.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class LabBarcodeAuditRespVO { + + private Long id; + private String action; + private String barcode; + private String barcodeNamespace; + private Long barcodeId; + private Long taskId; + private String partNo; + private String resolution; + private String message; + private Long operatorId; + private LocalDateTime createTime; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeBindReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeBindReqVO.java new file mode 100644 index 0000000..02b386e --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeBindReqVO.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeBindReqVO { + + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @NotNull(message = "请选择测量任务") + private Long taskId; + + @Size(max = 64) + private String barcodeNamespace; + + @Size(max = 128) + private String idempotencyKey; + + @Size(max = 64) + private String stationId; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeDisableReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeDisableReqVO.java new file mode 100644 index 0000000..0496950 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeDisableReqVO.java @@ -0,0 +1,20 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeDisableReqVO { + + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @Size(max = 64) + private String barcodeNamespace; + + @Size(max = 500) + private String reason; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeHistoryPageReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeHistoryPageReqVO.java new file mode 100644 index 0000000..75ee57c --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeHistoryPageReqVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +public class LabBarcodeHistoryPageReqVO extends PageParam { + + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + private String barcodeNamespace; + private Long taskId; + private String action; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodePartOptionRespVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodePartOptionRespVO.java new file mode 100644 index 0000000..0c57ed6 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodePartOptionRespVO.java @@ -0,0 +1,15 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import lombok.Data; + +@Data +public class LabBarcodePartOptionRespVO { + + private Long sampleId; + private String partNo; + private String partName; + private String sampleCode; + private String vehicleModel; + private String projectName; + private String status; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileReqVO.java new file mode 100644 index 0000000..c6d2c00 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileReqVO.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeProfileReqVO { + + @Size(max = 16) + private String prefix; + + @Size(max = 16) + private String suffix; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileRespVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileRespVO.java new file mode 100644 index 0000000..3e53e96 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeProfileRespVO.java @@ -0,0 +1,10 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import lombok.Data; + +@Data +public class LabBarcodeProfileRespVO { + + private String prefix; + private String suffix; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRebindReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRebindReqVO.java new file mode 100644 index 0000000..7f750ed --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRebindReqVO.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeRebindReqVO { + + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @NotNull(message = "请选择已有零部件") + private Long sampleId; + + @Size(max = 64) + private String barcodeNamespace; + + @Size(max = 128) + private String physicalSerialNo; + + @Size(max = 500) + private String reason; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRegisterReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRegisterReqVO.java new file mode 100644 index 0000000..5fa44f7 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeRegisterReqVO.java @@ -0,0 +1,29 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeRegisterReqVO { + + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @NotNull(message = "请选择已有零部件") + private Long sampleId; + + @Size(max = 64) + private String barcodeNamespace; + + @Size(max = 128) + private String physicalSerialNo; + + @Size(max = 32) + private String source; + + private Long currentTaskId; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveReqVO.java new file mode 100644 index 0000000..acd0fd0 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveReqVO.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeResolveReqVO { + + @Schema(description = "扫描枪或手工输入的条码原值") + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @Schema(description = "当前已打开的测量任务。为空时按全局任务匹配") + private Long currentTaskId; + + @Schema(description = "编码来源。为空时使用 LOCAL") + @Size(max = 64) + private String barcodeNamespace; + + @Schema(description = "工位号,只记入审计,不参与匹配") + @Size(max = 64) + private String stationId; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveRespVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveRespVO.java new file mode 100644 index 0000000..0ff63f8 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeResolveRespVO.java @@ -0,0 +1,89 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@Data +public class LabBarcodeResolveRespVO { + + private String resolution; + private String barcode; + private String barcodeNamespace; + /** ABSENT / ACTIVE / DISABLED / PART_DISABLED */ + private String registryStatus; + /** 已识别、已绑定、未登记等,和是否已写入任务关联是两件事 */ + private String recognitionLabel; + private Boolean recognized; + private Boolean bound; + private Boolean readOnly; + private Boolean bindAllowed; + private Boolean registeredNow; + private String pieceTraceMode; + private String message; + private String nextAction; + private String planHint; + private Long chosenTaskId; + private Long bindingId; + private Long auditId; + private String blockingBarcode; + private Part part; + private List candidateTasks = new ArrayList<>(); + private List historyTasks = new ArrayList<>(); + private List selectedPlans = new ArrayList<>(); + private Plan currentPlan; + private Plan nextPlan; + + @Data + public static class Part { + private Long partId; + private Long sampleId; + private String partNo; + private String partName; + private String sampleCode; + private String vehicleModel; + private String projectName; + private String status; + private String physicalSerialNo; + } + + @Data + public static class Task { + private Long taskId; + private String taskNo; + private String taskName; + private String taskStatus; + private String taskStatusName; + private LocalDateTime plannedFinishAt; + private String partNo; + private Boolean readOnly; + } + + @Data + public static class Plan { + private Long selectionId; + private Integer sequenceNo; + private String scanPlanName; + private String scanPlanCode; + private Boolean sequenceReady; + private String displayStatus; + private String blockedReason; + private Long executionId; + private Long unitId; + private String executionNo; + private String executionStatus; + private Boolean pointCloudReady; + private String inspectionScanId; + private List resultBatches = new ArrayList<>(); + private List reports = new ArrayList<>(); + } + + @Data + public static class Link { + private Long id; + private String no; + private Long executionId; + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeUnbindReqVO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeUnbindReqVO.java new file mode 100644 index 0000000..ee40151 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/controller/admin/barcode/vo/LabBarcodeUnbindReqVO.java @@ -0,0 +1,24 @@ +package cn.iocoder.yudao.module.lab.controller.admin.barcode.vo; + +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class LabBarcodeUnbindReqVO { + + @Size(max = 256) + private String barcode; + + /** True only when reusing a previously resolved canonical barcode. */ + private boolean barcodeNormalized; + + @NotNull(message = "请选择测量任务") + private Long taskId; + + @Size(max = 64) + private String barcodeNamespace; + + @Size(max = 500) + private String reason; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabBarcodeScanProfileDO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabBarcodeScanProfileDO.java new file mode 100644 index 0000000..727d416 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabBarcodeScanProfileDO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.lab.dal.dataobject.barcode; + +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@TableName("lab_barcode_scan_profile") +@Data +@EqualsAndHashCode(callSuper = true) +public class LabBarcodeScanProfileDO extends TenantBaseDO { + + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private String prefix; + private String suffix; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeAuditDO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeAuditDO.java new file mode 100644 index 0000000..bac01f5 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeAuditDO.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.module.lab.dal.dataobject.barcode; + +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@TableName("lab_piece_barcode_audit") +@Data +@EqualsAndHashCode(callSuper = true) +public class LabPieceBarcodeAuditDO extends TenantBaseDO { + + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private String action; + private String barcode; + private String barcodeNamespace; + private Long barcodeId; + private Long taskId; + private String partNo; + private String resolution; + private String message; + private String detailJson; + private Long operatorId; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeBindingDO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeBindingDO.java new file mode 100644 index 0000000..89ef1c5 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeBindingDO.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.module.lab.dal.dataobject.barcode; + +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +@TableName("lab_piece_barcode_binding") +@Data +@EqualsAndHashCode(callSuper = true) +public class LabPieceBarcodeBindingDO extends TenantBaseDO { + + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private Long barcodeId; + private String barcode; + private Long taskId; + private String partNo; + private Long sampleId; + private String bindingStatus; + private String source; + private String idempotencyKey; + private Long boundBy; + private LocalDateTime boundAt; + private Long unboundBy; + private LocalDateTime unboundAt; + private String unbindReason; + private Integer activeFlag; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeDO.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeDO.java new file mode 100644 index 0000000..7e7b37f --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/dataobject/barcode/LabPieceBarcodeDO.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.lab.dal.dataobject.barcode; + +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@TableName("lab_piece_barcode") +@Data +@EqualsAndHashCode(callSuper = true) +public class LabPieceBarcodeDO extends TenantBaseDO { + + @TableId(type = IdType.ASSIGN_ID) + private Long id; + private String barcode; + private String barcodeNamespace; + private Long sampleId; + private String partNo; + private String physicalSerialNo; + private String status; + private String source; + private Integer activeFlag; + private String remark; +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabBarcodeScanProfileMapper.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabBarcodeScanProfileMapper.java new file mode 100644 index 0000000..a2cfbfa --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabBarcodeScanProfileMapper.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.lab.dal.mysql.barcode; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabBarcodeScanProfileDO; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface LabBarcodeScanProfileMapper extends BaseMapperX { + + default LabBarcodeScanProfileDO selectCurrent() { + List list = selectList(new LambdaQueryWrapperX() + .orderByDesc(LabBarcodeScanProfileDO::getId) + .last("LIMIT 1")); + return list.isEmpty() ? null : list.get(0); + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeAuditMapper.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeAuditMapper.java new file mode 100644 index 0000000..77971e9 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeAuditMapper.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.lab.dal.mysql.barcode; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.lab.controller.admin.barcode.vo.LabBarcodeHistoryPageReqVO; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeAuditDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface LabPieceBarcodeAuditMapper extends BaseMapperX { + + default PageResult selectPage(LabBarcodeHistoryPageReqVO reqVO) { + return selectPage(reqVO, new LambdaQueryWrapperX() + .eqIfPresent(LabPieceBarcodeAuditDO::getBarcode, reqVO.getBarcode()) + .eqIfPresent(LabPieceBarcodeAuditDO::getBarcodeNamespace, reqVO.getBarcodeNamespace()) + .eqIfPresent(LabPieceBarcodeAuditDO::getTaskId, reqVO.getTaskId()) + .eqIfPresent(LabPieceBarcodeAuditDO::getAction, reqVO.getAction()) + .orderByDesc(LabPieceBarcodeAuditDO::getId)); + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeBindingMapper.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeBindingMapper.java new file mode 100644 index 0000000..e3dae5a --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeBindingMapper.java @@ -0,0 +1,31 @@ +package cn.iocoder.yudao.module.lab.dal.mysql.barcode; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeBindingDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface LabPieceBarcodeBindingMapper extends BaseMapperX { + + default LabPieceBarcodeBindingDO selectActive(Long taskId, String partNo) { + return selectOne(new LambdaQueryWrapperX() + .eq(LabPieceBarcodeBindingDO::getTaskId, taskId) + .eq(LabPieceBarcodeBindingDO::getPartNo, partNo) + .eq(LabPieceBarcodeBindingDO::getBindingStatus, "BOUND") + .eq(LabPieceBarcodeBindingDO::getActiveFlag, 1)); + } + + default LabPieceBarcodeBindingDO selectActiveByKey(String idempotencyKey) { + return selectOne(new LambdaQueryWrapperX() + .eq(LabPieceBarcodeBindingDO::getIdempotencyKey, idempotencyKey) + .eq(LabPieceBarcodeBindingDO::getActiveFlag, 1)); + } + + default Long countActiveByBarcode(Long barcodeId) { + return selectCount(new LambdaQueryWrapperX() + .eq(LabPieceBarcodeBindingDO::getBarcodeId, barcodeId) + .eq(LabPieceBarcodeBindingDO::getBindingStatus, "BOUND") + .eq(LabPieceBarcodeBindingDO::getActiveFlag, 1)); + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeMapper.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeMapper.java new file mode 100644 index 0000000..617edce --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/dal/mysql/barcode/LabPieceBarcodeMapper.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.lab.dal.mysql.barcode; + +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeDO; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface LabPieceBarcodeMapper extends BaseMapperX { + + default List selectByCode(String namespace, String barcode) { + return selectList(new LambdaQueryWrapperX() + .eq(LabPieceBarcodeDO::getBarcodeNamespace, namespace) + .eq(LabPieceBarcodeDO::getBarcode, barcode) + .orderByDesc(LabPieceBarcodeDO::getId)); + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/enums/ErrorCodeConstants.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/enums/ErrorCodeConstants.java index 1cfe3db..def41dc 100644 --- a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/enums/ErrorCodeConstants.java +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/enums/ErrorCodeConstants.java @@ -130,4 +130,22 @@ public interface ErrorCodeConstants { ErrorCode INTEGRATION_EXECUTION_FORBIDDEN = new ErrorCode(1_070_007_022, "执行不属于当前接入方"); ErrorCode INTEGRATION_TENANT_MISMATCH = new ErrorCode(1_070_007_023, "租户与接入方不一致"); ErrorCode INTEGRATION_IDEMPOTENCY_CONFLICT = new ErrorCode(1_070_007_024, "幂等键已被不同请求使用"); + + ErrorCode BARCODE_EMPTY = new ErrorCode(1_070_008_000, "请扫描或输入条码"); + ErrorCode BARCODE_INVALID = new ErrorCode(1_070_008_001, "条码格式不正确"); + ErrorCode BARCODE_ALREADY_REGISTERED = new ErrorCode(1_070_008_002, "该条码已登记到其他零部件。请使用改绑,不能直接覆盖。"); + ErrorCode BARCODE_NOT_REGISTERED = new ErrorCode(1_070_008_003, "条码尚未登记"); + ErrorCode BARCODE_DISABLED = new ErrorCode(1_070_008_004, "条码已停用,不能绑定"); + ErrorCode BARCODE_PART_DISABLED = new ErrorCode(1_070_008_005, "零部件已停用,不能绑定"); + ErrorCode BARCODE_PART_NOT_IN_TASK = new ErrorCode(1_070_008_006, "该零部件不属于当前任务"); + ErrorCode BARCODE_TASK_READONLY = new ErrorCode(1_070_008_007, "任务已结束,只能查看,不能通过扫码继续绑定"); + ErrorCode BARCODE_MULTI_PIECE_BLOCKED = new ErrorCode(1_070_008_008, "当前任务的该零部件已关联另一件实物。现有扫描执行不能按实物分开,已阻止继续绑定。"); + ErrorCode BARCODE_IDEMPOTENCY_CONFLICT = new ErrorCode(1_070_008_009, "幂等键已被不同的条码或任务使用"); + ErrorCode BARCODE_REASON_REQUIRED = new ErrorCode(1_070_008_010, "请填写原因"); + ErrorCode BARCODE_REBIND_BLOCKED = new ErrorCode(1_070_008_011, "条码仍关联测量任务,请先解除关联后再改绑零部件"); + ErrorCode BARCODE_BINDING_NOT_FOUND = new ErrorCode(1_070_008_012, "没有可解除的条码关联"); + ErrorCode BARCODE_PART_NOT_FOUND = new ErrorCode(1_070_008_013, "零部件不存在,请先维护零部件主数据"); + ErrorCode BARCODE_PART_KEY_MISSING = new ErrorCode(1_070_008_014, "该零部件主数据没有零部件编号,请先维护编号"); + ErrorCode BARCODE_PROFILE_INVALID = new ErrorCode(1_070_008_015, "扫码前后缀配置不正确"); + ErrorCode BARCODE_FORBIDDEN = new ErrorCode(1_070_008_016, "没有查看测量任务的权限"); } diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeCommittedRead.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeCommittedRead.java new file mode 100644 index 0000000..2b9e4d2 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeCommittedRead.java @@ -0,0 +1,48 @@ +package cn.iocoder.yudao.module.lab.service.barcode; + +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeBindingDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeDO; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabPieceBarcodeBindingMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabPieceBarcodeMapper; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * 冲突后在新事务里读取已提交的登记和关联,避免当前事务快照或会话缓存看不到并发写入。 + */ +@Component +public class LabBarcodeCommittedRead { + + @Resource + private LabPieceBarcodeBindingMapper bindingMapper; + @Resource + private LabPieceBarcodeMapper barcodeMapper; + + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true) + public LabPieceBarcodeBindingDO selectActiveBinding(Long taskId, String partNo) { + return bindingMapper.selectActive(taskId, partNo); + } + + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true) + public LabPieceBarcodeBindingDO selectActiveBindingByKey(String idempotencyKey) { + if (idempotencyKey == null) { + return null; + } + return bindingMapper.selectActiveByKey(idempotencyKey); + } + + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true) + public LabPieceBarcodeDO selectActiveBarcode(String namespace, String barcode) { + List rows = barcodeMapper.selectByCode(namespace, barcode); + for (LabPieceBarcodeDO row : rows) { + if ("ACTIVE".equals(row.getStatus()) && Integer.valueOf(1).equals(row.getActiveFlag())) { + return row; + } + } + return null; + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeMatchRules.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeMatchRules.java new file mode 100644 index 0000000..6c18fd3 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeMatchRules.java @@ -0,0 +1,130 @@ +package cn.iocoder.yudao.module.lab.service.barcode; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * 条码规范化与任务匹配。只根据已经查到的登记和任务做判断,不猜测条码前缀对应的零部件。 + */ +public final class LabBarcodeMatchRules { + + public static final Set TERMINAL = Set.of("COMPLETED", "CANCELLED", "FAILED"); + public static final String MULTI_PIECE_MESSAGE = "当前任务的该零部件已关联另一件实物。现有扫描执行不能按实物分开保存结果,已阻止继续绑定。"; + + private LabBarcodeMatchRules() { + } + + public record TaskSnap(long id, String status, boolean containsPart) { + } + + public record Decision(String resolution, Long chosenTaskId, boolean readOnly, boolean bindAllowed, + String bindBlockReason, String message) { + } + + public static String normalize(String raw, String prefix, String suffix) { + if (raw == null) { + return ""; + } + String value = raw.replace("\uFEFF", "").trim(); + while (!value.isEmpty() && isEdgeWhitespace(value.charAt(value.length() - 1))) { + value = value.substring(0, value.length() - 1).trim(); + } + while (!value.isEmpty() && isEdgeWhitespace(value.charAt(0))) { + value = value.substring(1).trim(); + } + if (prefix != null && !prefix.isEmpty() && value.startsWith(prefix) && value.length() > prefix.length()) { + value = value.substring(prefix.length()).trim(); + } + if (suffix != null && !suffix.isEmpty() && value.endsWith(suffix) && value.length() > suffix.length()) { + value = value.substring(0, value.length() - suffix.length()).trim(); + } + return value; + } + + public static String invalidReason(String normalized) { + if (normalized == null || normalized.isEmpty()) { + return "EMPTY"; + } + if (normalized.length() > 128) { + return "TOO_LONG"; + } + for (int i = 0; i < normalized.length(); i++) { + char c = normalized.charAt(i); + if (c < 32 || c == 127) { + return "CONTROL"; + } + } + return null; + } + + public static boolean terminal(String status) { + return status != null && TERMINAL.contains(status); + } + + public static Decision decide(String registry, boolean canReadTasks, Long currentTaskId, TaskSnap current, + List partTasks, boolean otherPieceBound) { + if ("ABSENT".equals(registry)) { + return new Decision("UNREGISTERED", null, false, false, null, + "这条码还没有登记。请选择已有零部件后再绑定,系统不会自动新建零部件。"); + } + if ("DISABLED".equals(registry)) { + return new Decision("DISABLED", null, true, false, "DISABLED", "条码已停用,不能自动绑定。"); + } + if ("PART_DISABLED".equals(registry)) { + return new Decision("PART_DISABLED", null, true, false, "PART_DISABLED", "零部件已停用,不能自动绑定。"); + } + if (!canReadTasks) { + return new Decision("FORBIDDEN", null, true, false, "FORBIDDEN", "没有查看测量任务的权限,条码未关联任务。"); + } + List tasks = partTasks == null ? List.of() : partTasks; + if (currentTaskId != null) { + if (current == null || !current.containsPart()) { + return new Decision("PART_NOT_IN_TASK", null, true, false, "PART_NOT_IN_TASK", + "该零部件不属于当前任务。可以打开其他任务查看,当前任务不会被修改。"); + } + if (terminal(current.status())) { + return new Decision("TASK_READONLY", current.id(), true, false, "TASK_READONLY", + "任务已结束,只能查看历史,不能通过扫码继续绑定或恢复执行。"); + } + if (otherPieceBound) { + return new Decision("MULTI_PIECE_BLOCKED", current.id(), true, false, "MULTI_PIECE_BLOCKED", MULTI_PIECE_MESSAGE); + } + return new Decision("UNIQUE_MATCH", current.id(), false, true, null, "已识别到当前测量任务。"); + } + List active = new ArrayList<>(); + for (TaskSnap task : tasks) { + if (task != null && task.containsPart() && !terminal(task.status())) { + active.add(task); + } + } + if (active.isEmpty()) { + boolean history = tasks.stream().anyMatch(task -> task != null && task.containsPart() && terminal(task.status())); + return new Decision("NO_ACTIVE_TASK", null, true, false, null, history + ? "已识别零部件。相关测量任务均已结束,只能查看历史,不会自动恢复。" + : "已识别零部件,但没有可关联的测量任务。"); + } + if (active.size() > 1) { + return new Decision("MULTIPLE_TASKS", null, false, false, null, "此零部件存在多个可关联测量任务,请选择后再关联。"); + } + TaskSnap only = active.get(0); + if (otherPieceBound) { + return new Decision("MULTI_PIECE_BLOCKED", only.id(), true, false, "MULTI_PIECE_BLOCKED", MULTI_PIECE_MESSAGE); + } + return new Decision("UNIQUE_MATCH", only.id(), false, true, null, "已识别到唯一测量任务。"); + } + + public static String bindRefusal(Decision decision, boolean alreadyBound) { + if (alreadyBound) { + return null; + } + if (decision == null || !decision.bindAllowed()) { + return decision == null || decision.bindBlockReason() == null ? decision == null ? "FORBIDDEN" : decision.resolution() : decision.bindBlockReason(); + } + return null; + } + + private static boolean isEdgeWhitespace(char c) { + return c == '\r' || c == '\n' || c == '\t' || c == ' '; + } +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeService.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeService.java new file mode 100644 index 0000000..560cc75 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeService.java @@ -0,0 +1,29 @@ +package cn.iocoder.yudao.module.lab.service.barcode; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.lab.controller.admin.barcode.vo.*; + +import java.util.List; + +public interface LabBarcodeService { + + LabBarcodeResolveRespVO resolve(LabBarcodeResolveReqVO reqVO); + + LabBarcodeResolveRespVO register(LabBarcodeRegisterReqVO reqVO); + + LabBarcodeResolveRespVO bindTask(LabBarcodeBindReqVO reqVO); + + LabBarcodeResolveRespVO unbindTask(LabBarcodeUnbindReqVO reqVO); + + LabBarcodeResolveRespVO rebind(LabBarcodeRebindReqVO reqVO); + + LabBarcodeResolveRespVO disable(LabBarcodeDisableReqVO reqVO); + + PageResult history(LabBarcodeHistoryPageReqVO reqVO); + + List searchParts(String keyword); + + LabBarcodeProfileRespVO getProfile(); + + void saveProfile(LabBarcodeProfileReqVO reqVO); +} diff --git a/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeServiceImpl.java b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeServiceImpl.java new file mode 100644 index 0000000..32a5148 --- /dev/null +++ b/am-backend/yudao-module-lab/src/main/java/cn/iocoder/yudao/module/lab/service/barcode/LabBarcodeServiceImpl.java @@ -0,0 +1,941 @@ +package cn.iocoder.yudao.module.lab.service.barcode; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONObject; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; +import cn.iocoder.yudao.module.lab.controller.admin.barcode.vo.*; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabBarcodeScanProfileDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeAuditDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeBindingDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.barcode.LabPieceBarcodeDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.data.LabArtifactFileDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.document.LabReportDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.execution.LabExecutionDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.master.LabSampleDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.result.LabMeasurementBatchDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.task.LabTaskSampleDO; +import cn.iocoder.yudao.module.lab.dal.dataobject.task.LabTestTaskDO; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabBarcodeScanProfileMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabPieceBarcodeAuditMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabPieceBarcodeBindingMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.barcode.LabPieceBarcodeMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.data.LabArtifactFileMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.document.LabReportMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.execution.LabExecutionMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.master.LabSampleMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.result.LabMeasurementBatchMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.task.LabTaskSampleMapper; +import cn.iocoder.yudao.module.lab.dal.mysql.task.LabTestTaskMapper; +import cn.iocoder.yudao.module.lab.enums.LabMasterStatusEnum; +import cn.iocoder.yudao.module.lab.enums.LabTaskStatusEnum; +import cn.iocoder.yudao.module.lab.service.task.LabTestTaskService; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import jakarta.annotation.Resource; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Isolation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.annotation.Validated; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.lab.enums.ErrorCodeConstants.*; + +@Service +@Validated +public class LabBarcodeServiceImpl implements LabBarcodeService { + + private static final Set SOURCES = Set.of("MANUAL", "IMPORT", "EXTERNAL"); + + @Resource + private LabPieceBarcodeMapper barcodeMapper; + @Resource + private LabPieceBarcodeBindingMapper bindingMapper; + @Resource + private LabPieceBarcodeAuditMapper auditMapper; + @Resource + private LabBarcodeScanProfileMapper profileMapper; + @Resource + private LabSampleMapper sampleMapper; + @Resource + private LabTestTaskMapper taskMapper; + @Resource + private LabTaskSampleMapper taskSampleMapper; + @Resource + private LabExecutionMapper executionMapper; + @Resource + private LabArtifactFileMapper artifactMapper; + @Resource + private LabMeasurementBatchMapper batchMapper; + @Resource + private LabReportMapper reportMapper; + @Resource + private org.springframework.jdbc.core.JdbcTemplate jdbc; + @Resource + private LabTestTaskService taskService; + @Resource + private SecurityFrameworkService securityFrameworkService; + @Resource + private LabBarcodeCommittedRead committedRead; + + @Override + @Transactional(rollbackFor = Exception.class) + public LabBarcodeResolveRespVO resolve(LabBarcodeResolveReqVO reqVO) { + return identify(reqVO, true, false); + } + + @Override + @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED) + public LabBarcodeResolveRespVO register(LabBarcodeRegisterReqVO reqVO) { + String barcode = requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized()); + String namespace = namespace(reqVO.getBarcodeNamespace()); + LabSampleDO sample = requireEnabledSample(reqVO.getSampleId()); + String source = source(reqVO.getSource()); + String serial = blankNull(reqVO.getPhysicalSerialNo(), 128); + Registry registry = loadRegistry(namespace, barcode); + if (registry.row() != null && "ACTIVE".equals(registry.row().getStatus())) { + if (sample.getId().equals(registry.row().getSampleId())) { + updateSerial(registry.row().getId(), serial); + Long auditId = audit("REGISTER_REPEAT", barcode, namespace, registry.row().getId(), null, sample.getPartNo(), + "UNIQUE_MATCH", "条码已登记到该零部件,未重复建档", null); + return finish(barcode, namespace, reqVO.getCurrentTaskId(), false, false, auditId); + } + throw exception(BARCODE_ALREADY_REGISTERED); + } + if (registry.row() != null && "DISABLED".equals(registry.row().getStatus())) { + if (!sample.getId().equals(registry.row().getSampleId())) { + throw exception(BARCODE_ALREADY_REGISTERED); + } + barcodeMapper.update(null, new LambdaUpdateWrapper() + .eq(LabPieceBarcodeDO::getId, registry.row().getId()) + .set(LabPieceBarcodeDO::getStatus, "ACTIVE") + .set(LabPieceBarcodeDO::getActiveFlag, 1) + .set(LabPieceBarcodeDO::getSource, source) + .set(serial != null, LabPieceBarcodeDO::getPhysicalSerialNo, serial)); + Long auditId = audit("REGISTER", barcode, namespace, registry.row().getId(), null, sample.getPartNo(), + "UNIQUE_MATCH", "已重新启用原条码登记", null); + return finish(barcode, namespace, reqVO.getCurrentTaskId(), false, true, auditId); + } + LabPieceBarcodeDO row = new LabPieceBarcodeDO(); + row.setBarcode(barcode); + row.setBarcodeNamespace(namespace); + row.setSampleId(sample.getId()); + row.setPartNo(sample.getPartNo().trim()); + row.setPhysicalSerialNo(serial); + row.setStatus("ACTIVE"); + row.setSource(source); + row.setActiveFlag(1); + try { + barcodeMapper.insert(row); + } catch (DataIntegrityViolationException ex) { + LabPieceBarcodeDO again = committedRead.selectActiveBarcode(namespace, barcode); + if (again != null && sample.getId().equals(again.getSampleId())) { + Long auditId = audit("REGISTER_REPEAT", barcode, namespace, again.getId(), null, sample.getPartNo(), + "UNIQUE_MATCH", "条码已登记到该零部件,未重复建档", null); + return finish(barcode, namespace, reqVO.getCurrentTaskId(), false, false, auditId); + } + throw exception(BARCODE_ALREADY_REGISTERED); + } + Long auditId = audit("REGISTER", barcode, namespace, row.getId(), null, sample.getPartNo(), + "UNIQUE_MATCH", "已把条码登记到零部件", null); + return finish(barcode, namespace, reqVO.getCurrentTaskId(), false, true, auditId); + } + + @Override + @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED) + public LabBarcodeResolveRespVO bindTask(LabBarcodeBindReqVO reqVO) { + String barcode = requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized()); + String namespace = namespace(reqVO.getBarcodeNamespace()); + String key = idempotency(reqVO.getIdempotencyKey()); + Registry registry = requireActive(namespace, barcode); + LabTestTaskDO task = requireTask(reqVO.getTaskId()); + String partNo = registry.sample().getPartNo().trim(); + boolean contains = taskContains(task, partNo); + LabBarcodeMatchRules.TaskSnap snap = new LabBarcodeMatchRules.TaskSnap(task.getId(), task.getStatus(), contains); + boolean other = contains && !LabBarcodeMatchRules.terminal(task.getStatus()) && otherPiece(task.getId(), partNo, barcode); + LabBarcodeMatchRules.Decision decision = LabBarcodeMatchRules.decide("ACTIVE", canReadTasks(), task.getId(), snap, List.of(snap), other); + LabPieceBarcodeBindingDO existing = bindingMapper.selectActive(task.getId(), partNo); + boolean already = existing != null && barcode.equals(existing.getBarcode()); + reject(LabBarcodeMatchRules.bindRefusal(decision, already)); + if (key != null) { + LabPieceBarcodeBindingDO byKey = bindingMapper.selectActiveByKey(key); + if (byKey != null && !sameBinding(byKey, barcode, task.getId(), partNo)) { + throw exception(BARCODE_IDEMPOTENCY_CONFLICT); + } + if (byKey != null) { + already = true; + existing = byKey; + } + } + if (already) { + Long auditId = audit("BIND_REPEAT", barcode, namespace, registry.row().getId(), task.getId(), partNo, + "UNIQUE_MATCH", "重复扫码,未新增任务关联", detail(reqVO.getStationId(), null)); + LabBarcodeResolveRespVO view = finish(barcode, namespace, task.getId(), false, false, auditId); + view.setMessage("该条码已关联此测量任务。"); + view.setRecognitionLabel("已绑定"); + view.setBound(true); + return view; + } + LabPieceBarcodeBindingDO row = new LabPieceBarcodeBindingDO(); + row.setBarcodeId(registry.row().getId()); + row.setBarcode(barcode); + row.setTaskId(task.getId()); + row.setPartNo(partNo); + row.setSampleId(registry.sample().getId()); + row.setBindingStatus("BOUND"); + row.setSource("SCAN"); + row.setIdempotencyKey(key); + row.setBoundBy(SecurityFrameworkUtils.getLoginUserId()); + row.setBoundAt(LocalDateTime.now()); + row.setActiveFlag(1); + try { + bindingMapper.insert(row); + } catch (DataIntegrityViolationException ex) { + LabPieceBarcodeBindingDO raced = committedRead.selectActiveBinding(task.getId(), partNo); + if (raced != null && barcode.equals(raced.getBarcode())) { + Long auditId = audit("BIND_REPEAT", barcode, namespace, registry.row().getId(), task.getId(), partNo, + "UNIQUE_MATCH", "重复扫码,未新增任务关联", null); + return repeatBinding(barcode, namespace, task.getId(), auditId); + } + if (raced != null) { + throw exception(BARCODE_MULTI_PIECE_BLOCKED); + } + if (key != null) { + LabPieceBarcodeBindingDO byKey = committedRead.selectActiveBindingByKey(key); + if (byKey != null && sameBinding(byKey, barcode, task.getId(), partNo)) { + return repeatBinding(barcode, namespace, task.getId(), null); + } + if (byKey != null) { + throw exception(BARCODE_IDEMPOTENCY_CONFLICT); + } + } + throw exception(BARCODE_MULTI_PIECE_BLOCKED); + } + Map content = new LinkedHashMap<>(); + content.put("barcode", barcode); + content.put("partNo", partNo); + taskService.recordEvent(task.getId(), "BARCODE_BOUND", content); + Long auditId = audit("BIND", barcode, namespace, registry.row().getId(), task.getId(), partNo, + "UNIQUE_MATCH", "已关联测量任务", detail(reqVO.getStationId(), null)); + LabBarcodeResolveRespVO view = finish(barcode, namespace, task.getId(), false, false, auditId); + view.setMessage("已关联测量任务。扫码没有改变任务状态,也没有发布扫描执行。"); + view.setRecognitionLabel("已绑定"); + view.setBound(true); + view.setNextAction("OPEN_TASK"); + return view; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public LabBarcodeResolveRespVO unbindTask(LabBarcodeUnbindReqVO reqVO) { + String barcode = requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized()); + String namespace = namespace(reqVO.getBarcodeNamespace()); + String reason = reason(reqVO.getReason()); + Registry registry = loadRegistry(namespace, barcode); + if (registry.row() == null) { + throw exception(BARCODE_NOT_REGISTERED); + } + LabTestTaskDO task = requireTask(reqVO.getTaskId()); + LabPieceBarcodeBindingDO binding = bindingMapper.selectActive(task.getId(), registry.row().getPartNo()); + if (binding == null || !barcode.equals(binding.getBarcode())) { + throw exception(BARCODE_BINDING_NOT_FOUND); + } + bindingMapper.update(null, new LambdaUpdateWrapper() + .eq(LabPieceBarcodeBindingDO::getId, binding.getId()) + .set(LabPieceBarcodeBindingDO::getBindingStatus, "UNBOUND") + .set(LabPieceBarcodeBindingDO::getActiveFlag, null) + .set(LabPieceBarcodeBindingDO::getUnbindReason, reason) + .set(LabPieceBarcodeBindingDO::getUnboundBy, SecurityFrameworkUtils.getLoginUserId()) + .set(LabPieceBarcodeBindingDO::getUnboundAt, LocalDateTime.now())); + Map content = new LinkedHashMap<>(); + content.put("barcode", barcode); + content.put("partNo", binding.getPartNo()); + content.put("reason", reason); + taskService.recordEvent(task.getId(), "BARCODE_UNBOUND", content); + Long auditId = audit("UNBIND", barcode, namespace, registry.row().getId(), task.getId(), binding.getPartNo(), + "UNBOUND", "已解除任务关联,原测量结果未改动", detail(null, reason)); + return finish(barcode, namespace, task.getId(), false, false, auditId); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public LabBarcodeResolveRespVO rebind(LabBarcodeRebindReqVO reqVO) { + String barcode = requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized()); + String namespace = namespace(reqVO.getBarcodeNamespace()); + String reason = reason(reqVO.getReason()); + LabSampleDO sample = requireEnabledSample(reqVO.getSampleId()); + Registry registry = loadRegistry(namespace, barcode); + if (registry.row() == null) { + throw exception(BARCODE_NOT_REGISTERED); + } + String oldPart = StrUtil.blankToDefault(registry.row().getPartNo(), ""); + String newPart = sample.getPartNo().trim(); + if (!oldPart.equals(newPart) && bindingMapper.countActiveByBarcode(registry.row().getId()) > 0) { + throw exception(BARCODE_REBIND_BLOCKED); + } + String serial = blankNull(reqVO.getPhysicalSerialNo(), 128); + LambdaUpdateWrapper update = new LambdaUpdateWrapper() + .eq(LabPieceBarcodeDO::getId, registry.row().getId()) + .set(LabPieceBarcodeDO::getSampleId, sample.getId()) + .set(LabPieceBarcodeDO::getPartNo, newPart) + .set(LabPieceBarcodeDO::getStatus, "ACTIVE") + .set(LabPieceBarcodeDO::getActiveFlag, 1); + if (serial != null) { + update.set(LabPieceBarcodeDO::getPhysicalSerialNo, serial); + } + barcodeMapper.update(null, update); + if (oldPart.equals(newPart)) { + bindingMapper.update(null, new LambdaUpdateWrapper() + .eq(LabPieceBarcodeBindingDO::getBarcodeId, registry.row().getId()) + .eq(LabPieceBarcodeBindingDO::getActiveFlag, 1) + .set(LabPieceBarcodeBindingDO::getSampleId, sample.getId())); + } + JSONObject extra = new JSONObject(); + extra.set("fromPartNo", oldPart); + extra.set("toPartNo", newPart); + extra.set("reason", reason); + Long auditId = audit("REBIND", barcode, namespace, registry.row().getId(), null, newPart, + "REBIND", "已改绑零部件,旧登记保留在审计中", StrUtil.sub(extra.toString(), 0, 1000)); + return finish(barcode, namespace, null, false, false, auditId); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public LabBarcodeResolveRespVO disable(LabBarcodeDisableReqVO reqVO) { + String barcode = requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized()); + String namespace = namespace(reqVO.getBarcodeNamespace()); + String reason = reason(reqVO.getReason()); + Registry registry = loadRegistry(namespace, barcode); + if (registry.row() == null) { + throw exception(BARCODE_NOT_REGISTERED); + } + if (!"DISABLED".equals(registry.row().getStatus())) { + barcodeMapper.update(null, new LambdaUpdateWrapper() + .eq(LabPieceBarcodeDO::getId, registry.row().getId()) + .set(LabPieceBarcodeDO::getStatus, "DISABLED") + .set(LabPieceBarcodeDO::getActiveFlag, null) + .set(LabPieceBarcodeDO::getRemark, reason)); + } + Long auditId = audit("DISABLE", barcode, namespace, registry.row().getId(), null, registry.row().getPartNo(), + "DISABLED", "条码已停用。若要换到另一件实物,请先解除原任务关联。", detail(null, reason)); + return finish(barcode, namespace, null, false, false, auditId); + } + + @Override + public PageResult history(LabBarcodeHistoryPageReqVO reqVO) { + if (StrUtil.isNotBlank(reqVO.getBarcode())) { + reqVO.setBarcode(requireBarcode(reqVO.getBarcode(), reqVO.isBarcodeNormalized())); + } + if (StrUtil.isNotBlank(reqVO.getBarcodeNamespace())) { + reqVO.setBarcodeNamespace(namespace(reqVO.getBarcodeNamespace())); + } + return BeanUtils.toBean(auditMapper.selectPage(reqVO), LabBarcodeAuditRespVO.class); + } + + @Override + public List searchParts(String keyword) { + String key = StrUtil.blankToDefault(keyword, "").trim(); + LambdaQueryWrapperX query = new LambdaQueryWrapperX() + .eq(LabSampleDO::getStatus, LabMasterStatusEnum.ENABLED.getStatus()) + .orderByDesc(LabSampleDO::getId) + .last("LIMIT 20"); + if (StrUtil.isNotBlank(key)) { + query.and(wrapper -> wrapper.like(LabSampleDO::getPartNo, key) + .or().like(LabSampleDO::getSampleName, key) + .or().like(LabSampleDO::getSampleCode, key)); + } + List result = new ArrayList<>(); + for (LabSampleDO sample : sampleMapper.selectList(query)) { + if (StrUtil.isBlank(sample.getPartNo())) { + continue; + } + LabBarcodePartOptionRespVO item = new LabBarcodePartOptionRespVO(); + item.setSampleId(sample.getId()); + item.setPartNo(sample.getPartNo().trim()); + item.setPartName(sample.getSampleName()); + item.setSampleCode(sample.getSampleCode()); + item.setVehicleModel(sample.getVehicleModel()); + item.setProjectName(sample.getProjectName()); + item.setStatus(sample.getStatus()); + result.add(item); + } + return result; + } + + @Override + public LabBarcodeProfileRespVO getProfile() { + LabBarcodeScanProfileDO row = profileMapper.selectCurrent(); + LabBarcodeProfileRespVO resp = new LabBarcodeProfileRespVO(); + resp.setPrefix(row == null ? "" : StrUtil.blankToDefault(row.getPrefix(), "")); + resp.setSuffix(row == null ? "" : StrUtil.blankToDefault(row.getSuffix(), "")); + return resp; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void saveProfile(LabBarcodeProfileReqVO reqVO) { + String prefix = affix(reqVO.getPrefix()); + String suffix = affix(reqVO.getSuffix()); + LabBarcodeScanProfileDO row = profileMapper.selectCurrent(); + if (row == null) { + row = new LabBarcodeScanProfileDO(); + row.setPrefix(prefix); + row.setSuffix(suffix); + profileMapper.insert(row); + return; + } + profileMapper.update(null, new LambdaUpdateWrapper() + .eq(LabBarcodeScanProfileDO::getId, row.getId()) + .set(LabBarcodeScanProfileDO::getPrefix, prefix) + .set(LabBarcodeScanProfileDO::getSuffix, suffix)); + } + + private LabBarcodeResolveRespVO repeatBinding(String barcode, String namespace, Long taskId, Long auditId) { + LabBarcodeResolveRespVO view = finish(barcode, namespace, taskId, false, false, auditId); + view.setMessage("该条码已关联此测量任务。"); + view.setRecognitionLabel("已绑定"); + view.setBound(true); + view.setNextAction("OPEN_TASK"); + return view; + } + + private LabBarcodeResolveRespVO finish(String barcode, String namespace, Long currentTaskId, boolean auditResolve, + boolean registeredNow, Long auditId) { + LabBarcodeResolveReqVO req = new LabBarcodeResolveReqVO(); + req.setBarcode(barcode); + req.setBarcodeNormalized(true); + req.setBarcodeNamespace(namespace); + req.setCurrentTaskId(currentTaskId); + LabBarcodeResolveRespVO view = identify(req, auditResolve, registeredNow); + if (auditId != null) { + view.setAuditId(auditId); + } + return view; + } + + private LabBarcodeResolveRespVO identify(LabBarcodeResolveReqVO reqVO, boolean auditResolve, boolean registeredNow) { + String[] affix = affixes(); + String barcode = LabBarcodeMatchRules.normalize(reqVO.getBarcode(), reqVO.isBarcodeNormalized() ? "" : affix[0], reqVO.isBarcodeNormalized() ? "" : affix[1]); + String invalid = LabBarcodeMatchRules.invalidReason(barcode); + LabBarcodeResolveRespVO resp = new LabBarcodeResolveRespVO(); + resp.setBarcode(barcode); + resp.setRegisteredNow(registeredNow); + if (invalid != null) { + resp.setResolution("INVALID"); + resp.setRegistryStatus("ABSENT"); + resp.setRecognized(false); + resp.setBound(false); + resp.setReadOnly(false); + resp.setBindAllowed(false); + resp.setRecognitionLabel("未识别"); + resp.setNextAction("NONE"); + resp.setMessage("EMPTY".equals(invalid) ? "请扫描或输入条码" : "条码格式不正确"); + if (auditResolve) { + resp.setAuditId(audit("RESOLVE_FAIL", StrUtil.sub(StrUtil.blankToDefault(reqVO.getBarcode(), ""), 0, 128), + null, null, reqVO.getCurrentTaskId(), null, "INVALID", resp.getMessage(), detail(reqVO.getStationId(), null))); + } + return resp; + } + String namespace = namespace(reqVO.getBarcodeNamespace()); + resp.setBarcodeNamespace(namespace); + LabTestTaskDO current = reqVO.getCurrentTaskId() == null ? null : requireTask(reqVO.getCurrentTaskId()); + Registry registry = loadRegistry(namespace, barcode); + boolean canRead = canReadTasks(); + String partNo = registry.sample() == null ? "" : StrUtil.blankToDefault(registry.sample().getPartNo(), "").trim(); + List tasks = canRead && "ACTIVE".equals(registry.state()) && StrUtil.isNotBlank(partNo) + ? tasksForPart(partNo) : List.of(); + boolean contains = current != null && canRead && "ACTIVE".equals(registry.state()) && taskContains(current, partNo); + LabBarcodeMatchRules.TaskSnap currentSnap = current == null ? null + : new LabBarcodeMatchRules.TaskSnap(current.getId(), current.getStatus(), contains); + List snaps = tasks.stream() + .map(task -> new LabBarcodeMatchRules.TaskSnap(task.getId(), task.getStatus(), true)) + .toList(); + LabBarcodeMatchRules.Decision draft = LabBarcodeMatchRules.decide(registry.state(), canRead, + current == null ? null : current.getId(), currentSnap, snaps, false); + boolean other = false; + if (draft.chosenTaskId() != null && "ACTIVE".equals(registry.state()) && canRead && StrUtil.isNotBlank(partNo)) { + other = otherPiece(draft.chosenTaskId(), partNo, barcode); + } + LabBarcodeMatchRules.Decision decision = other + ? LabBarcodeMatchRules.decide(registry.state(), canRead, current == null ? null : current.getId(), currentSnap, snaps, true) + : draft; + resp.setResolution(decision.resolution()); + resp.setRegistryStatus(registry.state()); + resp.setReadOnly(decision.readOnly()); + resp.setBindAllowed(decision.bindAllowed()); + resp.setMessage(decision.message()); + resp.setChosenTaskId(decision.chosenTaskId()); + resp.setRecognized("ACTIVE".equals(registry.state())); + fillPart(resp, registry); + if (registry.sample() == null && registry.row() != null && "PART_DISABLED".equals(registry.state())) { + resp.setMessage("关联的零部件主数据不存在或已停用,不能自动绑定。"); + } + fillTasks(resp, decision, tasks, partNo); + boolean bound = false; + if (decision.chosenTaskId() != null && StrUtil.isNotBlank(partNo)) { + LabPieceBarcodeBindingDO binding = bindingMapper.selectActive(decision.chosenTaskId(), partNo); + bound = binding != null && barcode.equals(binding.getBarcode()); + if (bound) { + resp.setBindingId(binding.getId()); + resp.setBound(true); + resp.setRecognitionLabel("已绑定"); + if ("UNIQUE_MATCH".equals(decision.resolution())) { + resp.setMessage("该条码已关联此测量任务。"); + resp.setNextAction("OPEN_TASK"); + } + } else if (binding != null && "MULTI_PIECE_BLOCKED".equals(decision.resolution())) { + resp.setBlockingBarcode(binding.getBarcode()); + } + } + resp.setBound(bound); + if (!bound) { + resp.setRecognitionLabel(label(registry.state(), decision.resolution())); + } + resp.setPieceTraceMode(!resp.getRecognized() ? null : "MULTI_PIECE_BLOCKED".equals(decision.resolution()) ? "BLOCKED" : "IDENTITY_ONLY"); + if (resp.getNextAction() == null) { + resp.setNextAction(nextAction(decision.resolution())); + } + if (decision.chosenTaskId() != null && resp.getRecognized() && canRead) { + fillPlans(resp, decision.chosenTaskId(), partNo); + } + if (auditResolve) { + String action = "INVALID".equals(decision.resolution()) || "UNREGISTERED".equals(decision.resolution()) + || "FORBIDDEN".equals(decision.resolution()) || "DISABLED".equals(decision.resolution()) + || "PART_DISABLED".equals(decision.resolution()) ? "RESOLVE_FAIL" : "RESOLVE"; + resp.setAuditId(audit(action, barcode, namespace, registry.row() == null ? null : registry.row().getId(), + decision.chosenTaskId(), StrUtil.blankToDefault(partNo, null), decision.resolution(), decision.message(), + detail(reqVO.getStationId(), null))); + } + return resp; + } + + private void fillPart(LabBarcodeResolveRespVO resp, Registry registry) { + LabSampleDO sample = registry.sample(); + if (sample == null && registry.row() == null) { + return; + } + LabBarcodeResolveRespVO.Part part = new LabBarcodeResolveRespVO.Part(); + if (sample != null) { + part.setPartId(sample.getId()); + part.setSampleId(sample.getId()); + part.setPartNo(StrUtil.blankToDefault(sample.getPartNo(), "").trim()); + part.setPartName(sample.getSampleName()); + part.setSampleCode(sample.getSampleCode()); + part.setVehicleModel(sample.getVehicleModel()); + part.setProjectName(sample.getProjectName()); + part.setStatus(sample.getStatus()); + } else { + part.setPartNo(registry.row().getPartNo()); + part.setSampleId(registry.row().getSampleId()); + part.setPartId(registry.row().getSampleId()); + } + if (registry.row() != null) { + part.setPhysicalSerialNo(registry.row().getPhysicalSerialNo()); + } + resp.setPart(part); + } + + private void fillTasks(LabBarcodeResolveRespVO resp, LabBarcodeMatchRules.Decision decision, + List tasks, String partNo) { + if ("FORBIDDEN".equals(decision.resolution()) || "UNREGISTERED".equals(decision.resolution()) + || "DISABLED".equals(decision.resolution()) || "PART_DISABLED".equals(decision.resolution()) + || "INVALID".equals(decision.resolution())) { + resp.setCandidateTasks(List.of()); + resp.setHistoryTasks(List.of()); + resp.setChosenTaskId("FORBIDDEN".equals(decision.resolution()) ? null : resp.getChosenTaskId()); + return; + } + List active = new ArrayList<>(); + List history = new ArrayList<>(); + for (LabTestTaskDO task : tasks) { + LabBarcodeResolveRespVO.Task item = toTask(task, partNo); + if (LabBarcodeMatchRules.terminal(task.getStatus())) { + history.add(item); + } else { + active.add(item); + } + } + if ("MULTIPLE_TASKS".equals(decision.resolution()) || "PART_NOT_IN_TASK".equals(decision.resolution())) { + resp.setCandidateTasks(active); + } else if (decision.chosenTaskId() != null) { + List chosen = new ArrayList<>(); + for (LabBarcodeResolveRespVO.Task item : active) { + if (decision.chosenTaskId().equals(item.getTaskId())) { + chosen.add(item); + } + } + for (LabBarcodeResolveRespVO.Task item : history) { + if (decision.chosenTaskId().equals(item.getTaskId())) { + chosen.add(item); + } + } + if (chosen.isEmpty()) { + LabTestTaskDO task = taskMapper.selectById(decision.chosenTaskId()); + if (task != null) { + chosen.add(toTask(task, partNo)); + } + } + resp.setCandidateTasks(chosen); + } else { + resp.setCandidateTasks(List.of()); + } + resp.setHistoryTasks(history); + } + + /** Barcode navigation reads existing executions; it never dispatches scans or computations. */ + private void fillPlans(LabBarcodeResolveRespVO resp, Long taskId, String partNo) { + long tenant = cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder.getRequiredTenantId(); + var task = taskMapper.selectById(taskId); + List plans = new ArrayList<>(); + var batches = batchMapper.selectListByTaskId(taskId); + var reports = reportMapper.selectListByTaskId(taskId); + for (var execution : executionMapper.selectListByTaskId(taskId)) { + if (!"SCAN_ONLY".equals(task.getProcessType()) && StrUtil.isNotBlank(execution.getPartTypeCode()) && !partNo.equals(execution.getPartTypeCode().trim())) continue; + var plan = new LabBarcodeResolveRespVO.Plan(); + plan.setExecutionId(execution.getId()); + plan.setExecutionNo(execution.getExecutionNo()); + plan.setExecutionStatus(execution.getStatus()); + plan.setUnitId(execution.getSampleUnitId()); + plan.setScanPlanName(StrUtil.blankToDefault(execution.getScanPlanName(), "扫描执行")); + plan.setScanPlanCode(execution.getScanPlanCode()); + plan.setSequenceReady(false); + plan.setDisplayStatus(taskStatusName(execution.getStatus())); + var clouds = artifactMapper.selectAvailableByExecutionId(execution.getId()); + plan.setPointCloudReady(clouds.stream().anyMatch(f -> "MATCHED".equals(f.getChecksumStatus()) && ("POINT_CLOUD".equals(f.getArtifactType()) || StrUtil.blankToDefault(f.getFileName(), "").toLowerCase(java.util.Locale.ROOT).endsWith(".ply")))); + var comparisons = jdbc.queryForList("SELECT engine_id FROM lab_dimension_comparison WHERE task_id=? AND source_execution_id=? AND tenant_id=? AND engine_id IS NOT NULL ORDER BY create_time DESC,id DESC LIMIT 1", taskId, execution.getId(), tenant); + if (!comparisons.isEmpty()) plan.setInspectionScanId(java.util.Objects.toString(comparisons.get(0).get("engine_id"), null)); + for (var batch : batches) if (execution.getId().equals(batch.getExecutionId())) { + var link = new LabBarcodeResolveRespVO.Link();link.setId(batch.getId());link.setNo(batch.getBatchNo());link.setExecutionId(execution.getId());plan.getResultBatches().add(link); + } + for (var report : reports) if (execution.getId().equals(report.getExecutionId())) { + var link = new LabBarcodeResolveRespVO.Link();link.setId(report.getId());link.setNo(report.getReportNo());link.setExecutionId(execution.getId());plan.getReports().add(link); + } + plans.add(plan); + } + resp.setSelectedPlans(plans); + resp.setCurrentPlan(plans.isEmpty() ? null : plans.get(0)); + resp.setPlanHint(plans.isEmpty() ? "尚无扫描执行,请进入任务工作台选择方案并准备扫描。" : "按原有任务工作台查看初次扫描、补扫与重试;扫码不改变方案顺序和执行状态。"); + } + + private LabBarcodeResolveRespVO.Task toTask(LabTestTaskDO task, String partNo) { + LabBarcodeResolveRespVO.Task item = new LabBarcodeResolveRespVO.Task(); + item.setTaskId(task.getId()); + item.setTaskNo(task.getTaskNo()); + item.setTaskName(task.getTaskName()); + item.setTaskStatus(task.getStatus()); + item.setTaskStatusName(taskStatusName(task.getStatus())); + item.setPlannedFinishAt(task.getPlannedFinishAt()); + item.setPartNo(partNo); + item.setReadOnly(LabBarcodeMatchRules.terminal(task.getStatus())); + return item; + } + + private List tasksForPart(String partNo) { + List samples = sampleMapper.selectList(LabSampleDO::getPartNo, partNo); + if (samples.isEmpty()) { + return List.of(); + } + Set sampleIds = new LinkedHashSet<>(); + for (LabSampleDO sample : samples) { + sampleIds.add(sample.getId()); + } + List links = taskSampleMapper.selectList(new LambdaQueryWrapperX() + .in(LabTaskSampleDO::getSampleId, sampleIds)); + Set taskIds = new LinkedHashSet<>(); + for (LabTaskSampleDO link : links) { + if (link.getTaskId() != null) { + taskIds.add(link.getTaskId()); + } + } + if (taskIds.isEmpty()) { + return List.of(); + } + List tasks = taskMapper.selectByIds(taskIds); + return tasks == null ? List.of() : tasks; + } + + private boolean taskContains(LabTestTaskDO task, String partNo) { + if (task == null || StrUtil.isBlank(partNo)) { + return false; + } + List links = taskSampleMapper.selectListByTaskId(task.getId()); + if (links.isEmpty()) { + return false; + } + Set sampleIds = new LinkedHashSet<>(); + for (LabTaskSampleDO link : links) { + if (link.getSampleId() != null) { + sampleIds.add(link.getSampleId()); + } + } + if (sampleIds.isEmpty()) { + return false; + } + List samples = sampleMapper.selectByIds(sampleIds); + if (samples == null) { + return false; + } + for (LabSampleDO sample : samples) { + if (partNo.equals(StrUtil.blankToDefault(sample.getPartNo(), "").trim())) { + return true; + } + } + return false; + } + + private boolean otherPiece(Long taskId, String partNo, String barcode) { + LabPieceBarcodeBindingDO existing = bindingMapper.selectActive(taskId, partNo); + return existing != null && !barcode.equals(existing.getBarcode()); + } + + private Registry requireActive(String namespace, String barcode) { + Registry registry = loadRegistry(namespace, barcode); + String refusal = switch (registry.state()) { + case "ABSENT" -> "UNREGISTERED"; + case "DISABLED" -> "DISABLED"; + case "PART_DISABLED" -> "PART_DISABLED"; + default -> null; + }; + reject(refusal); + return registry; + } + + private Registry loadRegistry(String namespace, String barcode) { + List rows = barcodeMapper.selectByCode(namespace, barcode); + for (LabPieceBarcodeDO row : rows) { + if ("ACTIVE".equals(row.getStatus()) && Integer.valueOf(1).equals(row.getActiveFlag())) { + LabSampleDO sample = sampleMapper.selectById(row.getSampleId()); + if (sample == null || StrUtil.isBlank(sample.getPartNo()) + || !LabMasterStatusEnum.ENABLED.getStatus().equals(sample.getStatus())) { + return new Registry("PART_DISABLED", row, sample); + } + return new Registry("ACTIVE", row, sample); + } + } + for (LabPieceBarcodeDO row : rows) { + if ("DISABLED".equals(row.getStatus())) { + return new Registry("DISABLED", row, sampleMapper.selectById(row.getSampleId())); + } + } + return new Registry("ABSENT", null, null); + } + + private LabSampleDO requireEnabledSample(Long sampleId) { + LabSampleDO sample = sampleMapper.selectById(sampleId); + if (sample == null) { + throw exception(BARCODE_PART_NOT_FOUND); + } + if (StrUtil.isBlank(sample.getPartNo())) { + throw exception(BARCODE_PART_KEY_MISSING); + } + if (!LabMasterStatusEnum.ENABLED.getStatus().equals(sample.getStatus())) { + throw exception(BARCODE_PART_DISABLED); + } + return sample; + } + + private LabTestTaskDO requireTask(Long taskId) { + LabTestTaskDO task = taskMapper.selectById(taskId); + if (task == null) { + throw exception(TASK_NOT_EXISTS); + } + return task; + } + + private void reject(String refusal) { + if (refusal == null) { + return; + } + throw exception(switch (refusal) { + case "PART_NOT_IN_TASK" -> BARCODE_PART_NOT_IN_TASK; + case "TASK_READONLY" -> BARCODE_TASK_READONLY; + case "MULTI_PIECE_BLOCKED" -> BARCODE_MULTI_PIECE_BLOCKED; + case "DISABLED" -> BARCODE_DISABLED; + case "PART_DISABLED" -> BARCODE_PART_DISABLED; + case "UNREGISTERED" -> BARCODE_NOT_REGISTERED; + case "FORBIDDEN" -> BARCODE_FORBIDDEN; + default -> BARCODE_INVALID; + }); + } + + private String requireBarcode(String raw, boolean normalized) { + String[] affix = affixes(); + String barcode = LabBarcodeMatchRules.normalize(raw, normalized ? "" : affix[0], normalized ? "" : affix[1]); + String reason = LabBarcodeMatchRules.invalidReason(barcode); + if ("EMPTY".equals(reason)) { + throw exception(BARCODE_EMPTY); + } + if (reason != null) { + throw exception(BARCODE_INVALID); + } + return barcode; + } + + private String namespace(String raw) { + String value = StrUtil.blankToDefault(raw, "LOCAL").trim(); + if (value.isEmpty()) { + return "LOCAL"; + } + if (LabBarcodeMatchRules.invalidReason(value) != null) { + throw exception(BARCODE_INVALID); + } + return value; + } + + private String source(String raw) { + String value = StrUtil.blankToDefault(raw, "MANUAL").trim().toUpperCase(); + if (!SOURCES.contains(value)) { + throw exception(BARCODE_INVALID); + } + return value; + } + + private String idempotency(String raw) { + if (StrUtil.isBlank(raw)) { + return null; + } + String value = raw.trim(); + if (value.length() > 128 || LabBarcodeMatchRules.invalidReason(value) != null) { + throw exception(BARCODE_INVALID); + } + return value; + } + + private String reason(String raw) { + String value = StrUtil.blankToDefault(raw, "").trim(); + if (value.isEmpty() || value.length() > 500) { + throw exception(BARCODE_REASON_REQUIRED); + } + return value; + } + + private String blankNull(String raw, int max) { + if (StrUtil.isBlank(raw)) { + return null; + } + String value = raw.trim(); + if (value.length() > max) { + throw exception(BARCODE_INVALID); + } + return value; + } + + private String affix(String raw) { + String value = raw == null ? "" : raw.trim(); + String invalid = LabBarcodeMatchRules.invalidReason(value); + if (invalid != null && !"EMPTY".equals(invalid)) { + throw exception(BARCODE_PROFILE_INVALID); + } + return value; + } + + private String[] affixes() { + LabBarcodeScanProfileDO profile = profileMapper.selectCurrent(); + if (profile == null) { + return new String[]{"", ""}; + } + return new String[]{StrUtil.blankToDefault(profile.getPrefix(), ""), StrUtil.blankToDefault(profile.getSuffix(), "")}; + } + + private void updateSerial(Long id, String serial) { + if (serial == null) { + return; + } + LabPieceBarcodeDO update = new LabPieceBarcodeDO(); + update.setId(id); + update.setPhysicalSerialNo(serial); + barcodeMapper.updateById(update); + } + + private boolean canReadTasks() { + return securityFrameworkService.hasPermission("lab:task:query"); + } + + private boolean sameBinding(LabPieceBarcodeBindingDO row, String barcode, Long taskId, String partNo) { + return row != null && barcode.equals(row.getBarcode()) && taskId.equals(row.getTaskId()) && partNo.equals(row.getPartNo()); + } + + private String label(String registry, String resolution) { + if ("ABSENT".equals(registry)) { + return "未登记"; + } + if ("DISABLED".equals(registry)) { + return "已停用"; + } + if ("PART_DISABLED".equals(registry)) { + return "零部件已停用"; + } + if ("FORBIDDEN".equals(resolution)) { + return "无权限"; + } + if ("ACTIVE".equals(registry)) { + return "已识别"; + } + return "未识别"; + } + + private String nextAction(String resolution) { + return switch (resolution) { + case "UNIQUE_MATCH" -> "BIND_TASK"; + case "MULTIPLE_TASKS" -> "CHOOSE_TASK"; + case "UNREGISTERED" -> "REGISTER"; + case "TASK_READONLY", "MULTI_PIECE_BLOCKED" -> "VIEW_TASK"; + case "PART_NOT_IN_TASK" -> "VIEW_OTHER_TASK"; + default -> "NONE"; + }; + } + + private String taskStatusName(String status) { + for (LabTaskStatusEnum item : LabTaskStatusEnum.values()) { + if (item.getStatus().equals(status)) { + return item.getName(); + } + } + return status; + } + + private Long audit(String action, String barcode, String namespace, Long barcodeId, Long taskId, String partNo, + String resolution, String message, String detail) { + LabPieceBarcodeAuditDO row = new LabPieceBarcodeAuditDO(); + row.setAction(action); + row.setBarcode(StrUtil.sub(StrUtil.blankToDefault(barcode, ""), 0, 128)); + row.setBarcodeNamespace(namespace); + row.setBarcodeId(barcodeId); + row.setTaskId(taskId); + row.setPartNo(StrUtil.sub(StrUtil.blankToDefault(partNo, ""), 0, 64)); + row.setResolution(resolution); + row.setMessage(StrUtil.sub(StrUtil.blankToDefault(message, ""), 0, 500)); + row.setDetailJson(StrUtil.sub(StrUtil.blankToDefault(detail, ""), 0, 1000)); + row.setOperatorId(SecurityFrameworkUtils.getLoginUserId()); + auditMapper.insert(row); + return row.getId(); + } + + private String detail(String stationId, String reason) { + if (StrUtil.isBlank(stationId) && StrUtil.isBlank(reason)) { + return null; + } + JSONObject json = new JSONObject(); + if (StrUtil.isNotBlank(stationId)) { + json.set("stationId", stationId); + } + if (StrUtil.isNotBlank(reason)) { + json.set("reason", reason); + } + return StrUtil.sub(json.toString(), 0, 1000); + } + + private record Registry(String state, LabPieceBarcodeDO row, LabSampleDO sample) { + } +} diff --git a/am-web/src/api/lab/barcode/index.ts b/am-web/src/api/lab/barcode/index.ts new file mode 100644 index 0000000..1ac7f1f --- /dev/null +++ b/am-web/src/api/lab/barcode/index.ts @@ -0,0 +1,155 @@ +import request from '@/config/axios' + +export interface LabBarcodePart { + partId?: number | string + sampleId?: number | string + partNo?: string + partName?: string + sampleCode?: string + vehicleModel?: string + projectName?: string + status?: string + physicalSerialNo?: string +} + +export interface LabBarcodeTask { + taskId: number | string + taskNo?: string + taskName?: string + taskStatus?: string + taskStatusName?: string + plannedFinishAt?: string + partNo?: string + readOnly?: boolean +} + +export interface LabBarcodeLink { + id: number | string + no?: string + executionId?: number | string +} + +export interface LabBarcodePlan { + selectionId?: number | string + sequenceNo?: number + scanPlanName?: string + scanPlanCode?: string + sequenceReady?: boolean + displayStatus?: string + blockedReason?: string + executionId?: number | string + executionNo?: string + unitId?: number | string + executionStatus?: string + pointCloudReady?: boolean + inspectionScanId?: string + resultBatches?: LabBarcodeLink[] + reports?: LabBarcodeLink[] +} + +export interface LabBarcodeResolution { + resolution?: string + barcode?: string + barcodeNamespace?: string + registryStatus?: string + recognitionLabel?: string + recognized?: boolean + bound?: boolean + readOnly?: boolean + bindAllowed?: boolean + registeredNow?: boolean + pieceTraceMode?: string + message?: string + nextAction?: string + planHint?: string + chosenTaskId?: number | string + bindingId?: number | string + auditId?: number | string + blockingBarcode?: string + part?: LabBarcodePart + candidateTasks?: LabBarcodeTask[] + historyTasks?: LabBarcodeTask[] + selectedPlans?: LabBarcodePlan[] + currentPlan?: LabBarcodePlan + nextPlan?: LabBarcodePlan +} + +export interface LabBarcodePartOption { + sampleId: number | string + partNo?: string + partName?: string + sampleCode?: string + vehicleModel?: string + projectName?: string + status?: string +} + +export const resolveBarcode = (data: { + barcode: string + currentTaskId?: number | string + barcodeNormalized?: boolean + barcodeNamespace?: string + stationId?: string +}) => { + return request.post({ url: '/lab/barcode/resolve', data }) +} + +export const registerBarcode = (data: { + barcode: string + sampleId: number | string + barcodeNamespace?: string + physicalSerialNo?: string + source?: string + currentTaskId?: number | string +}) => { + return request.post({ url: '/lab/barcode/register', data: { ...data, barcodeNormalized: true } }) +} + +export const bindBarcodeTask = (data: { + barcode: string + taskId: number | string + barcodeNamespace?: string + idempotencyKey?: string + stationId?: string +}) => { + return request.post({ url: '/lab/barcode/bind-task', data: { ...data, barcodeNormalized: true } }) +} + +export const unbindBarcodeTask = (data: { + barcode: string + taskId: number | string + barcodeNamespace?: string + reason: string +}) => { + return request.post({ url: '/lab/barcode/unbind-task', data: { ...data, barcodeNormalized: true } }) +} + +export const rebindBarcode = (data: { + barcode: string + sampleId: number | string + barcodeNamespace?: string + physicalSerialNo?: string + reason: string +}) => { + return request.post({ url: '/lab/barcode/rebind', data: { ...data, barcodeNormalized: true } }) +} + +export const disableBarcode = (data: { barcode: string; barcodeNamespace?: string; reason: string }) => { + return request.post({ url: '/lab/barcode/disable', data: { ...data, barcodeNormalized: true } }) +} + +export const getBarcodeHistory = (params) => { + return request.get({ url: '/lab/barcode/binding-history', params: { ...params, barcodeNormalized: !!params?.barcode } }) +} + +export const searchBarcodeParts = (keyword?: string) => { + return request.get({ url: '/lab/barcode/parts', params: { keyword } }) +} + +export const getBarcodeProfile = () => { + return request.get<{ prefix?: string; suffix?: string }>({ url: '/lab/barcode/profile' }) +} + +export const saveBarcodeProfile = (data: { prefix?: string; suffix?: string }) => { + return request.put({ url: '/lab/barcode/profile', data }) +} diff --git a/am-web/src/views/lab/barcode/BarcodeScanPanel.vue b/am-web/src/views/lab/barcode/BarcodeScanPanel.vue new file mode 100644 index 0000000..31edb6b --- /dev/null +++ b/am-web/src/views/lab/barcode/BarcodeScanPanel.vue @@ -0,0 +1,539 @@ + + + + + diff --git a/am-web/src/views/lab/barcode/index.vue b/am-web/src/views/lab/barcode/index.vue new file mode 100644 index 0000000..ab044a9 --- /dev/null +++ b/am-web/src/views/lab/barcode/index.vue @@ -0,0 +1,9 @@ + + + diff --git a/am-web/src/views/lab/dimension/index.vue b/am-web/src/views/lab/dimension/index.vue index 0e56a48..d68a6c5 100644 --- a/am-web/src/views/lab/dimension/index.vue +++ b/am-web/src/views/lab/dimension/index.vue @@ -26,6 +26,7 @@ @keyup.enter="load" />查询批量导入扫码识别 diff --git a/am-web/src/views/lab/dimension/workbench.vue b/am-web/src/views/lab/dimension/workbench.vue index 94b8ac3..c84e30e 100644 --- a/am-web/src/views/lab/dimension/workbench.vue +++ b/am-web/src/views/lab/dimension/workbench.vue @@ -26,6 +26,7 @@ {{ deliveryStages[data.stage] || data.stage }} · {{ ['DELIVERED', 'PENDING_CONFIRM'].includes(data.stage) ? '查看成果' : '查看待办' }} + 扫码关联 刷新
{{ snapshotText }}
+