| | |
| | | import cn.iocoder.yudao.module.mes.service.pro.batch.MesProBatchService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.pallet.MesProPalletService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.scan.MesProScanEventService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.trace.consumer.MesTraceConsumerScanEventDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.trace.consumer.MesTraceConsumerScanEventMapper; |
| | | import cn.iocoder.yudao.module.mes.service.qc.seedquality.MesQcSeedQualityService; |
| | | import cn.iocoder.yudao.module.mes.service.trace.consumer.MesTraceConsumerScanEventService; |
| | | import cn.iocoder.yudao.module.mes.service.trace.crossregion.MesTraceCrossRegionService; |
| | | import cn.iocoder.yudao.module.mes.service.trace.integrity.MesTraceIntegrityService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.batch.MesWmBatchService; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private MesWmBatchService mesWmBatchService; |
| | | @Resource |
| | | private MesTraceConsumerScanEventService consumerScanEventService; |
| | | @Resource |
| | | private MesTraceCrossRegionService crossRegionService; |
| | | @Resource |
| | | private MesTraceIntegrityService integrityService; |
| | | @Resource |
| | | private MesTraceConsumerScanEventMapper consumerScanEventMapper; |
| | | |
| | | @Override |
| | | public MesProTracePublicRespVO queryTrace(String code) { |
| | | String rawCode = StrUtil.trim(StrUtil.nullToEmpty(code)); |
| | | String traceCode = normalize(rawCode); |
| | | if (StrUtil.isEmpty(traceCode)) { |
| | | recordConsumerScan(rawCode, traceCode, null, null, false); |
| | | throw exception(MES_TRACE_CODE_NOT_FOUND); |
| | | } |
| | | TraceEntry entry = null; |
| | | try { |
| | | // 1. 解析追溯入口,定位批次 |
| | | TraceEntry entry = resolveEntry(traceCode); |
| | | entry = resolveEntry(traceCode); |
| | | if (entry.batchId == null) { |
| | | throw exception(MES_TRACE_CODE_NOT_FOUND); |
| | | } |
| | | // 2. 聚合全程信息 |
| | | return buildRespVO(rawCode, traceCode, entry.sourceType, entry.batchId); |
| | | MesProTracePublicRespVO result = buildRespVO(rawCode, traceCode, entry.sourceType, entry.batchId); |
| | | recordConsumerScan(rawCode, traceCode, entry.sourceType, entry.batchId, true); |
| | | return result; |
| | | } catch (RuntimeException ex) { |
| | | recordConsumerScan(rawCode, traceCode, entry == null ? null : entry.sourceType, |
| | | entry == null ? null : entry.batchId, false); |
| | | throw ex; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 记录消费者扫码事件,并触发防伪防窜货扩展(窜货判定 + 扫码摘要),均容错不影响溯源主流程 |
| | | */ |
| | | private MesTraceConsumerScanEventDO recordConsumerScan(String rawCode, String traceCode, String sourceType, |
| | | Long batchId, boolean success) { |
| | | MesTraceConsumerScanEventDO event = consumerScanEventService.recordScan(rawCode, traceCode, sourceType, |
| | | batchId, success, success ? null : MES_TRACE_CODE_NOT_FOUND.getCode().toString()); |
| | | if (event == null) { |
| | | return null; |
| | | } |
| | | try { |
| | | crossRegionService.checkAndCreateWarning(event); |
| | | } catch (Exception ex) { |
| | | log.warn("[recordConsumerScan][窜货判定失败] eventId={}", event.getId(), ex); |
| | | } |
| | | try { |
| | | integrityService.recordScanIntegrity(event); |
| | | } catch (Exception ex) { |
| | | log.warn("[recordConsumerScan][扫码摘要生成失败] eventId={}", event.getId(), ex); |
| | | } |
| | | return event; |
| | | } |
| | | |
| | | // ==================== 追溯码解析 ==================== |
| | |
| | | resp.setInbound(buildInbound(batchId)); |
| | | // 质检区块 |
| | | resp.setQuality(buildQuality(batch)); |
| | | // 防伪验证区块 |
| | | fillAntiFake(resp, traceCode); |
| | | // 可选扩展区块(有数据才返回) |
| | | fillOptionalModules(resp, batch); |
| | | return resp; |
| | |
| | | return vo; |
| | | } |
| | | |
| | | /** |
| | | * 填充防伪验证区块:该追溯码累计扫码次数、首次/最近扫码时间(正品验证增强) |
| | | */ |
| | | private void fillAntiFake(MesProTracePublicRespVO resp, String traceCode) { |
| | | try { |
| | | List<MesTraceConsumerScanEventDO> events = consumerScanEventMapper.selectListByNormalizedCode(traceCode); |
| | | if (CollUtil.isEmpty(events)) { |
| | | return; |
| | | } |
| | | MesProTracePublicRespVO.AntiFakeVO vo = new MesProTracePublicRespVO.AntiFakeVO(); |
| | | vo.setScanCount((long) events.size()); |
| | | vo.setFirstScanTime(events.get(0).getEventTime()); |
| | | vo.setLastScanTime(events.get(events.size() - 1).getEventTime()); |
| | | resp.setAntiFake(vo); |
| | | } catch (Exception ex) { |
| | | log.warn("[fillAntiFake][追溯码 {} 防伪验证统计失败: {}]", traceCode, ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | private void fillOptionalModules(MesProTracePublicRespVO resp, MesProBatchDO batch) { |
| | | try { |
| | | List<MesWmBatchPurchaseSourceRespVO> purchaseSources = mesWmBatchService.getPurchaseSourceList(batch.getCode()); |