| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.operatelog.core.util.OperateLogUtils; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.oqc.vo.MesQcOqcSaveReqVO; |
| | |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import com.mzt.logapi.starter.annotation.LogRecord; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | import static cn.iocoder.yudao.module.mes.enums.LogRecordConstants.*; |
| | | |
| | | /** |
| | | * MES 出货检验单(OQC) Service 实现类 |
| | |
| | | public class MesQcOqcServiceImpl implements MesQcOqcService { |
| | | |
| | | @Resource |
| | | private cn.iocoder.yudao.module.mes.service.qc.MesQcCompletionHelper completionHelper; |
| | | |
| | | @Resource |
| | | private MesQcOqcMapper oqcMapper; |
| | | |
| | | @Resource |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | |
| | | @Resource |
| | | private MesQcTemplateItemService templateItemService; |
| | |
| | | // 1.2 校验来源单据参数,并验证数据存在 |
| | | String sourceDocCode = validateAndGetSourceDocCode( |
| | | createReqVO.getSourceDocType(), createReqVO.getSourceLineId()); |
| | | // 1.3 根据产品 + OQC 类型查找可用质检方案(支持前端显式指定) |
| | | List<MesQcTemplateItemDO> templateItems = templateItemService.getTemplateItemList( |
| | | createReqVO.getItemId(), MesQcTypeEnum.OQC.getType(), null, null); |
| | | if (CollUtil.isEmpty(templateItems)) { |
| | | throw exception(QC_NO_TEMPLATE); |
| | | } |
| | | MesQcTemplateItemDO templateItem = templateItems.get(0); |
| | | if (createReqVO.getTemplateId() != null) { |
| | | templateItem = templateItems.stream() |
| | | .filter(available -> Objects.equals(available.getTemplateId(), createReqVO.getTemplateId())) |
| | | .findFirst().orElse(null); |
| | | if (templateItem == null) { |
| | | throw exception(QC_NO_TEMPLATE); // 指定了方案但不在匹配列表中,拒绝 |
| | | } |
| | | } |
| | | Long templateId = templateItem.getTemplateId(); |
| | | // 新建质检单不依赖质检方案。 |
| | | |
| | | // 2. 插入主表 |
| | | MesQcOqcDO oqc = BeanUtils.toBean(createReqVO, MesQcOqcDO.class) |
| | | .setTemplateId(templateId) |
| | | .setTemplateId(null) |
| | | .setSourceDocCode(sourceDocCode) |
| | | .setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | if (createReqVO.getSourceLineId() != null) { |
| | | oqc.setReelNo(resolveSalesReelNo(productSalesLineService |
| | | .validateProductSalesLineExists(createReqVO.getSourceLineId()))); |
| | | } |
| | | oqcMapper.insert(oqc); |
| | | |
| | | // 3. 从模板指标自动生成检验行 |
| | | oqcLineService.createLinesFromTemplate(oqc.getId(), templateId); |
| | | return oqc.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateOqc(MesQcOqcSaveReqVO updateReqVO) { |
| | | // 1.1 校验存在 + 草稿状态 |
| | | validateOqcStatusPrepare(updateReqVO.getId()); |
| | | MesQcOqcDO existingOqc = validateOqcStatusPrepare(updateReqVO.getId()); |
| | | // 1.2 校验数据 |
| | | validateOqcSaveData(updateReqVO); |
| | | |
| | |
| | | updateObj.setSourceDocType(null).setSourceDocId(null).setSourceLineId(null); // 不允许修改来源单据 |
| | | updateObj.setTemplateId(null); // 不允许修改模板 |
| | | updateObj.setItemId(null); // 不允许修改物料 |
| | | oqcMapper.updateById(updateObj); |
| | | if (existingOqc.getSourceLineId() != null) updateObj.setReelNo(null); // 来源盘号保持快照 |
| | | // 编辑时允许清空客户;完成检验等局部更新仍保留原客户。 |
| | | updateObj.setClientId(null); |
| | | oqcMapper.update(updateObj, new LambdaUpdateWrapper<MesQcOqcDO>() |
| | | .eq(MesQcOqcDO::getId, updateReqVO.getId()) |
| | | .set(MesQcOqcDO::getClientId, updateReqVO.getClientId())); |
| | | } |
| | | |
| | | private void validateOqcSaveData(MesQcOqcSaveReqVO reqVO) { |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void finishOqc(Long id) { |
| | | completionHelper.lockDocument("oqc", id); |
| | | // 1.1 校验存在 + 草稿状态 |
| | | MesQcOqcDO oqc = validateOqcStatusPrepare(id); |
| | | // 1.2 校验检测结论必填 |
| | | if (oqc.getCheckResult() == null) { |
| | | throw exception(QC_OQC_CHECK_RESULT_EMPTY); |
| | | } |
| | | // 1.3 校验至少存在一条检测结果 |
| | | indicatorResultService.validateIndicatorResultExistsByQcIdAndType(id, MesQcTypeEnum.OQC.getType()); |
| | | int result = completionHelper.validateAndGetResult("oqc", id, oqc.getQualifiedQuantity(), oqc.getUnqualifiedQuantity(), oqc.getCheckQuantity()); |
| | | oqc.setCheckResult(result); |
| | | |
| | | |
| | | // 2. 更新状态为已完成 |
| | | MesQcOqcDO updateObj = new MesQcOqcDO() |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()); |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()).setCheckResult(result); |
| | | oqcMapper.updateById(updateObj); |
| | | |
| | | // 3. 不合格时自动创建 NCR |
| | |
| | | |
| | | @Override |
| | | public PageResult<MesQcOqcDO> getOqcPage(MesQcOqcPageReqVO pageReqVO) { |
| | | // 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedClientIds = customerApi.getPermittedCustomerIds(); |
| | | return oqcMapper.selectPage(pageReqVO, permittedClientIds); |
| | | return oqcMapper.selectPage(pageReqVO, (List<Long>) null); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createOqcFromProductSales(Long salesId, MesWmProductSalesLineDO line, String salesCode, Long clientId) { |
| | | // 1. 校验物料是否配置了 OQC 检验模板 |
| | | MesQcTemplateItemDO templateItem; |
| | | try { |
| | | templateItem = templateItemService.getRequiredTemplateByItemIdAndType( |
| | | line.getItemId(), MesQcTypeEnum.OQC.getType()); |
| | | } catch (Exception e) { |
| | | // 获取物料信息,提供更明确的错误提示 |
| | | String itemInfo = getItemInfo(line.getItemId()); |
| | | throw exception(QC_NO_TEMPLATE_FOR_ITEM, itemInfo); |
| | | } |
| | | Long templateId = templateItem.getTemplateId(); |
| | | |
| | | // 2. 生成检验单编码 |
| | | String code = "OQC" + System.currentTimeMillis(); |
| | |
| | | MesQcOqcDO oqc = new MesQcOqcDO(); |
| | | oqc.setCode(code); |
| | | oqc.setName(salesCode + "-出货检验"); |
| | | oqc.setTemplateId(templateId); |
| | | |
| | | oqc.setSourceDocType(MesBizTypeConstants.WM_PRODUCT_SALES); |
| | | oqc.setSourceDocId(salesId); |
| | | oqc.setSourceLineId(line.getId()); |
| | |
| | | oqc.setClientId(clientId); |
| | | oqc.setItemId(line.getItemId()); |
| | | oqc.setBatchCode(line.getBatchCode()); |
| | | oqc.setReelNo(resolveSalesReelNo(line)); |
| | | oqc.setOutQuantity(line.getQuantity()); |
| | | oqc.setCheckQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setQualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setUnqualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setOutDate(java.time.LocalDateTime.now()); |
| | | MesWmProductSalesDO sales = productSalesService.getProductSales(salesId); |
| | | java.time.LocalDateTime businessDate = sales != null && sales.getSalesDate() != null |
| | | ? sales.getSalesDate() : java.time.LocalDateTime.now(); |
| | | oqc.setOutDate(businessDate); |
| | | oqc.setInspectDate(businessDate); |
| | | oqc.setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | oqcMapper.insert(oqc); |
| | | |
| | | // 4. 从模板指标自动生成检验行 |
| | | oqcLineService.createLinesFromTemplate(oqc.getId(), templateId); |
| | | |
| | | return oqc.getId(); |
| | | } |
| | | |
| | | private String resolveSalesReelNo(MesWmProductSalesLineDO line) { |
| | | if (line == null || line.getSaleOrderItemId() == null) return null; |
| | | var item = saleOrderApi.getSaleOrderItem(line.getSaleOrderItemId()).getCheckedData(); |
| | | return item == null ? null : item.getReelNo(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @LogRecord(type = MES_QC_OQC_TYPE, subType = MES_QC_OQC_IMPORT_SUB_TYPE, bizNo = "0", |
| | | success = MES_QC_OQC_IMPORT_SUCCESS) |
| | | public MesQcImportRespVO importOqcList(List<MesQcOqcImportExcelVO> importList) { |
| | | MesQcImportRespVO respVO = MesQcImportRespVO.builder() |
| | | .createList(new ArrayList<>()).failureList(new LinkedHashMap<>()).build(); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | // 空文件也要给日志文案兜底,避免 {{#importSummary}} 取不到值 |
| | | OperateLogUtils.putImportSummary(respVO.getCreateList(), null, respVO.getFailureList()); |
| | | return respVO; |
| | | } |
| | | qcImportHelper.startImport(); |
| | |
| | | respVO.getFailureList().put(label, MesQcImportHelper.resolveImportErrorMessage(ex)); |
| | | } |
| | | } |
| | | // 记录导入结果汇总,供 @LogRecord 的 success 文案引用 |
| | | OperateLogUtils.putImportSummary(respVO.getCreateList(), null, respVO.getFailureList()); |
| | | return respVO; |
| | | } |
| | | |
| | |
| | | reqVO.setName(row.getName().trim()); |
| | | reqVO.setItemId(qcImportHelper.resolveItemId(row.getItemCode())); |
| | | Long clientId = qcImportHelper.resolveClientId(row.getClientName()); |
| | | qcImportHelper.validateClientPermission(clientId, row.getClientName().trim()); |
| | | qcImportHelper.validateClientPermission(clientId, StrUtil.trimToEmpty(row.getClientName())); |
| | | reqVO.setClientId(clientId); |
| | | reqVO.setBatchCode(StrUtil.trimToNull(row.getBatchCode())); |
| | | reqVO.setTemplateId(qcImportHelper.resolveTemplateId(row.getTemplateName())); |