| | |
| | | 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 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.*; |
| | | |
| | | /** |
| | |
| | | @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; |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void finishOqc(Long id) { |
| | | // 1.1 校验存在 + 草稿状态 |
| | | MesQcOqcDO oqc = lock(id); |
| | | if (!MesQcStatusEnum.DRAFT.getStatus().equals(oqc.getStatus())) throw exception(QC_OQC_NOT_PREPARE); |
| | | MesQcOqcDO oqc = validateOqcStatusPrepare(id); |
| | | // 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()) |
| | | .setWorkflowStatus(MesQcCheckResultEnum.QUALIFIED.getType().equals(oqc.getCheckResult()) ? WAIT_SALES : WAIT_RETEST); |
| | | .setId(id).setStatus(MesQcStatusEnum.FINISHED.getStatus()); |
| | | oqcMapper.updateById(updateObj); |
| | | recordAction(id, "INITIAL", oqc.getCheckResult(), null, 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); |
| | | } |
| | | // 3. 不合格时自动创建 NCR |
| | | generateNcrIfNeeded(oqc); |
| | | |
| | | @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); |
| | | // 4. 回写来源单据 |
| | | writeBackSourceDoc(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(不合格品处理单) |