| | |
| | | package cn.iocoder.yudao.module.mes.service.qc.oqc; |
| | | |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | 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.mes.controller.admin.qc.oqc.vo.MesQcOqcRetestReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.qc.defectrecord.MesQcDefectRecordDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.qc.oqc.MesQcOqcActionDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateItemDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.qc.oqc.MesQcOqcMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.qc.oqc.MesQcOqcActionMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.qc.MesQcDefectLevelEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.qc.MesQcStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.qc.MesQcTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.service.md.client.MesMdClientService; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; |
| | | import cn.iocoder.yudao.module.mes.service.qc.defectrecord.MesQcDefectRecordService; |
| | | import cn.iocoder.yudao.module.mes.service.qc.indicatorresult.MesQcIndicatorResultService; |
| | | import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productsales.MesWmProductSalesLineService; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.qc.ncr.vo.MesQcNcrSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.service.qc.ncr.MesQcNcrService; |
| | | import cn.iocoder.yudao.module.mes.enums.qc.MesQcCheckResultEnum; |
| | | import cn.iocoder.yudao.module.mes.service.wm.productsales.MesWmProductSalesService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productsales.MesWmProductSalesLineDO; |
| | | 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 jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | |
| | | import java.math.RoundingMode; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Set; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * MES 出货检验单(OQC) Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class MesQcOqcServiceImpl implements MesQcOqcService { |
| | | |
| | | private static final int WAIT_RETEST = 10, WAIT_SALES = 20, WAIT_COMPANY = 30, |
| | | RELEASED = 40, WAIT_SALES_FAIL = 50, WAIT_COMPANY_FAIL = 60, REJECTED = 70; |
| | | |
| | | @Resource |
| | | private MesQcOqcMapper oqcMapper; |
| | | @Resource |
| | | private MesQcOqcActionMapper actionMapper; |
| | | |
| | | @Resource |
| | | private MesQcTemplateItemService templateItemService; |
| | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcDefectRecordService defectRecordService; |
| | | @Resource |
| | | @Lazy |
| | | private MesMdClientService clientService; |
| | | @Resource |
| | | @Lazy |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | | @Lazy |
| | | private MesWmProductSalesLineService productSalesLineService; |
| | |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | |
| | | @Resource |
| | | @Lazy |
| | | private MesQcNcrService ncrService; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | |
| | | private void validateOqcSaveData(MesQcOqcSaveReqVO reqVO) { |
| | | // 校验编号唯一 |
| | | validateOqcCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验客户、物料、检测人员存在 |
| | | clientService.validateClientExists(reqVO.getClientId()); |
| | | itemService.validateItemExists(reqVO.getItemId()); |
| | | // 校验物料、检测人员存在 |
| | | // 注:物料 ID 来自 MDM,客户 ID 来自 CRM |
| | | mdmItemApi.validateItemExists(reqVO.getItemId()); |
| | | adminUserApi.validateUser(reqVO.getInspectorUserId()); |
| | | } |
| | | |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void finishOqc(Long id) { |
| | | // 1.1 校验存在 + 草稿状态 |
| | | MesQcOqcDO oqc = validateOqcStatusPrepare(id); |
| | | MesQcOqcDO oqc = lock(id); |
| | | if (!MesQcStatusEnum.DRAFT.getStatus().equals(oqc.getStatus())) throw exception(QC_OQC_NOT_PREPARE); |
| | | // 1.2 校验检测结论必填 |
| | | if (oqc.getCheckResult() == null) { |
| | | throw exception(QC_OQC_CHECK_RESULT_EMPTY); |
| | | } |
| | | if (oqc.getUnqualifiedQuantity() != null && oqc.getUnqualifiedQuantity().compareTo(BigDecimal.ZERO) > 0 |
| | | && MesQcCheckResultEnum.QUALIFIED.getType().equals(oqc.getCheckResult())) { |
| | | throw new IllegalArgumentException("存在不合格品时,出货检验结论不能为合格"); |
| | | } |
| | | if ((oqc.getUnqualifiedQuantity() == null || oqc.getUnqualifiedQuantity().compareTo(BigDecimal.ZERO) == 0) |
| | | && !MesQcCheckResultEnum.QUALIFIED.getType().equals(oqc.getCheckResult())) { |
| | | throw new IllegalArgumentException("不合格数量为 0 时,出货检验结论必须为合格"); |
| | | } |
| | | // 1.3 校验至少存在一条检测结果 |
| | | indicatorResultService.validateIndicatorResultExistsByQcIdAndType(id, MesQcTypeEnum.OQC.getType()); |
| | | |
| | | // 2. 更新状态为已完成 |
| | | MesQcOqcDO updateObj = new MesQcOqcDO() |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()); |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()) |
| | | .setWorkflowStatus(MesQcCheckResultEnum.QUALIFIED.getType().equals(oqc.getCheckResult()) ? WAIT_SALES : WAIT_RETEST); |
| | | oqcMapper.updateById(updateObj); |
| | | recordAction(id, "INITIAL", oqc.getCheckResult(), null, oqc); |
| | | // 销售出库明细仍为待检;公司质检审批通过后才回写并允许后续出库。 |
| | | } |
| | | |
| | | // 3. 回写来源单据 |
| | | writeBackSourceDoc(oqc); |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void retestOqc(MesQcOqcRetestReqVO req) { |
| | | MesQcOqcDO oqc = requireStatus(req.getId(), WAIT_RETEST); |
| | | if (req.getCheckQuantity().compareTo(req.getQualifiedQuantity().add(req.getUnqualifiedQuantity())) != 0) |
| | | throw new IllegalArgumentException("检测数量必须等于合格与不合格数量之和"); |
| | | if (req.getCheckQuantity().compareTo(oqc.getOutQuantity()) > 0) |
| | | throw new IllegalArgumentException("检测数量不能超过出货数量"); |
| | | if (!Set.of(1, 2).contains(req.getCheckResult())) throw new IllegalArgumentException("复检结论只能为合格或不合格"); |
| | | boolean passed = req.getCheckResult() == 1; |
| | | if (passed != (req.getUnqualifiedQuantity().compareTo(BigDecimal.ZERO) == 0)) |
| | | throw new IllegalArgumentException("复检结论与不合格数量不一致"); |
| | | if (!passed && (req.getOpinion() == null || req.getOpinion().isBlank())) |
| | | throw new IllegalArgumentException("复检不合格必须填写原因"); |
| | | MesQcOqcDO update = new MesQcOqcDO().setId(req.getId()) |
| | | .setCheckQuantity(req.getCheckQuantity()).setQualifiedQuantity(req.getQualifiedQuantity()) |
| | | .setUnqualifiedQuantity(req.getUnqualifiedQuantity()).setCheckResult(req.getCheckResult()) |
| | | .setInspectDate(LocalDateTime.now()).setInspectorUserId(getLoginUserId()) |
| | | .setWorkflowStatus(passed ? WAIT_SALES : WAIT_SALES_FAIL); |
| | | oqcMapper.updateById(update); |
| | | recordAction(req.getId(), "RETEST", req.getCheckResult(), req.getOpinion(), update); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void salesApprove(Long id, boolean approved, String opinion) { |
| | | MesQcOqcDO oqc = lock(id); |
| | | if (!Set.of(WAIT_SALES, WAIT_SALES_FAIL).contains(oqc.getWorkflowStatus())) |
| | | throw new IllegalArgumentException("当前状态不能进行销售部审批"); |
| | | requireOpinion(opinion); |
| | | int next = approved ? (oqc.getWorkflowStatus() == WAIT_SALES ? WAIT_COMPANY : WAIT_COMPANY_FAIL) : WAIT_RETEST; |
| | | oqcMapper.updateById(new MesQcOqcDO().setId(id).setWorkflowStatus(next)); |
| | | recordAction(id, approved ? "SALES_APPROVE" : "SALES_REJECT", oqc.getCheckResult(), opinion, oqc); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void companyApprove(Long id, boolean approved, String opinion) { |
| | | MesQcOqcDO oqc = lock(id); |
| | | if (!Set.of(WAIT_COMPANY, WAIT_COMPANY_FAIL).contains(oqc.getWorkflowStatus())) |
| | | throw new IllegalArgumentException("当前状态不能进行公司质检审批"); |
| | | requireOpinion(opinion); |
| | | if (approved) { |
| | | if (oqc.getWorkflowStatus() == WAIT_COMPANY_FAIL) generateNcrIfNeeded(oqc); |
| | | writeBackSourceDoc(oqc); |
| | | } |
| | | int next = approved ? (oqc.getWorkflowStatus() == WAIT_COMPANY ? RELEASED : REJECTED) : WAIT_RETEST; |
| | | oqcMapper.updateById(new MesQcOqcDO().setId(id).setWorkflowStatus(next)); |
| | | recordAction(id, approved ? "COMPANY_APPROVE" : "COMPANY_REJECT", oqc.getCheckResult(), opinion, oqc); |
| | | } |
| | | |
| | | private MesQcOqcDO lock(Long id) { |
| | | MesQcOqcDO oqc = oqcMapper.selectByIdForUpdate(id); |
| | | if (oqc == null) throw exception(QC_OQC_NOT_EXISTS); |
| | | return oqc; |
| | | } |
| | | |
| | | private MesQcOqcDO requireStatus(Long id, int status) { |
| | | MesQcOqcDO oqc = lock(id); |
| | | if (!Objects.equals(oqc.getWorkflowStatus(), status)) |
| | | throw new IllegalArgumentException("销售质检状态已变化,请刷新后重试"); |
| | | return oqc; |
| | | } |
| | | |
| | | private void requireOpinion(String opinion) { |
| | | if (opinion == null || opinion.isBlank()) throw new IllegalArgumentException("请填写审批意见"); |
| | | } |
| | | |
| | | private void recordAction(Long id, String action, Integer result, String opinion, MesQcOqcDO snapshot) { |
| | | MesQcOqcActionDO event = new MesQcOqcActionDO(); |
| | | event.setOqcId(id);event.setAction(action);event.setResult(result);event.setOpinion(opinion); |
| | | java.util.Map<String, BigDecimal> quantities = new java.util.HashMap<>(); |
| | | quantities.put("checkQuantity", snapshot.getCheckQuantity()); |
| | | quantities.put("qualifiedQuantity", snapshot.getQualifiedQuantity()); |
| | | quantities.put("unqualifiedQuantity", snapshot.getUnqualifiedQuantity()); |
| | | event.setQuantitiesJson(JSONUtil.toJsonStr(quantities)); |
| | | event.setOperatorId(getLoginUserId());event.setCreateTime(LocalDateTime.now()); |
| | | actionMapper.insert(event); |
| | | } |
| | | |
| | | @Override |
| | | public List<MesQcOqcActionDO> getActions(Long id) { return actionMapper.selectByOqcId(id); } |
| | | |
| | | /** |
| | | * 不合格时自动创建 NCR(不合格品处理单) |
| | | * |
| | | * @param oqc 出货检验单 |
| | | */ |
| | | private void generateNcrIfNeeded(MesQcOqcDO oqc) { |
| | | BigDecimal unqualifiedQty = oqc.getUnqualifiedQuantity(); |
| | | if (unqualifiedQty == null || unqualifiedQty.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return; |
| | | } |
| | | String ncrCode = "NCR" + java.time.LocalDateTime.now() |
| | | .format(java.time.format.DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")); |
| | | MesQcNcrSaveReqVO saveReqVO = new MesQcNcrSaveReqVO(); |
| | | saveReqVO.setCode(ncrCode); |
| | | saveReqVO.setName("不合格品处理单"); |
| | | saveReqVO.setQcType(MesQcTypeEnum.OQC.getType()); |
| | | saveReqVO.setQcId(oqc.getId()); |
| | | saveReqVO.setItemId(oqc.getItemId()); |
| | | saveReqVO.setUnqualifiedQuantity(unqualifiedQty); |
| | | saveReqVO.setDefectDescription("由出货检验(" + oqc.getCode() + ")自动生成"); |
| | | ncrService.createNcr(saveReqVO); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | @Override |
| | | public PageResult<MesQcOqcDO> getOqcPage(MesQcOqcPageReqVO pageReqVO) { |
| | | return oqcMapper.selectPage(pageReqVO); |
| | | // 获取用户有权限的客户 ID 列表 |
| | | List<Long> permittedClientIds = customerApi.getPermittedCustomerIds(); |
| | | return oqcMapper.selectPage(pageReqVO, permittedClientIds); |
| | | } |
| | | |
| | | @Override |
| | |
| | | oqcMapper.updateById(updateOqc); |
| | | } |
| | | |
| | | @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(); |
| | | |
| | | // 3. 创建 OQC 主表(待检验状态,初始数量为 0) |
| | | 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.setSourceDocCode(salesCode); |
| | | oqc.setClientId(clientId); |
| | | oqc.setItemId(line.getItemId()); |
| | | oqc.setBatchCode(line.getBatchCode()); |
| | | oqc.setOutQuantity(line.getQuantity()); |
| | | oqc.setCheckQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setQualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setUnqualifiedQuantity(BigDecimal.ZERO); // 待填写 |
| | | oqc.setOutDate(java.time.LocalDateTime.now()); |
| | | oqc.setStatus(MesQcStatusEnum.DRAFT.getStatus()); |
| | | oqcMapper.insert(oqc); |
| | | |
| | | // 4. 从模板指标自动生成检验行 |
| | | oqcLineService.createLinesFromTemplate(oqc.getId(), templateId); |
| | | |
| | | return oqc.getId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取物料信息(编码+名称) |
| | | */ |
| | | private String getItemInfo(Long itemId) { |
| | | if (itemId == null) { |
| | | return "未知物料"; |
| | | } |
| | | try { |
| | | cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO item = mdmItemApi.getItem(itemId).getCheckedData(); |
| | | if (item != null) { |
| | | return item.getCode() + "-" + item.getName(); |
| | | } |
| | | } catch (Exception e) { |
| | | // 忽略异常 |
| | | } |
| | | return "物料ID:" + itemId; |
| | | } |
| | | |
| | | } |