From ca80f3681a9b3e7e210022e8201bcc27bdfecee1 Mon Sep 17 00:00:00 2001 From: "zangch@mesnac.com" Date: Tue, 29 Jul 2025 15:32:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(qms):=20=E5=AE=9E=E7=8E=B0=E8=B4=A8?= =?UTF-8?q?=E6=A3=80=E4=BB=BB=E5=8A=A1=E5=88=A0=E9=99=A4=E5=92=8C=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD:=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AD=90=E8=A1=A8=E7=9A=84=E7=BB=93=E6=9E=9C=E5=B0=B1?= =?UTF-8?q?=E4=BC=9A=E5=90=8C=E6=AD=A5=E4=B8=BB=E8=A1=A8=E7=9A=84=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=EF=BC=8C=E6=9C=89=E4=B8=80=E4=B8=AA=E8=B4=A8=E6=A3=80?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=AD=90=E8=A1=A8=E7=9A=84=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E4=B8=BA=E4=B8=8D=E5=90=88=E6=A0=BC=EF=BC=8C=E9=82=A3=E4=B9=88?= =?UTF-8?q?=E4=B8=BB=E8=A1=A8=E7=9A=84=E7=BB=93=E6=9E=9C=E5=B0=B1=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E6=9B=B4=E6=96=B0=E4=B8=BA=E4=B8=8D=E5=90=88=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 QcInspectionMainServiceImpl 中添加删除质检任务时同时删除关联子表记录的逻辑 - 在 QcInspectionResultServiceImpl 中添加插入和更新质检结果时同步更新主表状态的方法 - 优化了删除和插入操作的事务管理,确保数据一致性 --- .../impl/QcInspectionMainServiceImpl.java | 64 ++++++++++++++++--- .../impl/QcInspectionResultServiceImpl.java | 61 +++++++++++++++--- 2 files changed, 108 insertions(+), 17 deletions(-) diff --git a/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionMainServiceImpl.java b/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionMainServiceImpl.java index 2103138..dba33dd 100644 --- a/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionMainServiceImpl.java +++ b/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionMainServiceImpl.java @@ -1,26 +1,31 @@ package org.dromara.qms.service.impl; -import org.dromara.common.core.utils.MapstructUtils; -import org.dromara.common.core.utils.StringUtils; -import org.dromara.common.mybatis.core.page.TableDataInfo; -import org.dromara.common.mybatis.core.page.PageQuery; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.toolkit.JoinWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; +import org.dromara.common.core.exception.ServiceException; +import org.dromara.common.core.utils.MapstructUtils; +import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.mybatis.core.page.PageQuery; +import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.qms.domain.QcInspectionMain; +import org.dromara.qms.domain.QcInspectionResult; import org.dromara.qms.domain.QcInspectionTemplate; import org.dromara.qms.domain.QcInspectionType; -import org.springframework.stereotype.Service; import org.dromara.qms.domain.bo.QcInspectionMainBo; import org.dromara.qms.domain.vo.QcInspectionMainVo; -import org.dromara.qms.domain.QcInspectionMain; import org.dromara.qms.mapper.QcInspectionMainMapper; +import org.dromara.qms.mapper.QcInspectionResultMapper; import org.dromara.qms.service.IQcInspectionMainService; +import org.dromara.qms.service.IQcInspectionResultService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.Collection; /** * 质检任务Service业务层处理 @@ -34,6 +39,10 @@ public class QcInspectionMainServiceImpl implements IQcInspectionMainService { private final QcInspectionMainMapper baseMapper; + private final IQcInspectionResultService qcInspectionResultService; + private final QcInspectionResultMapper qcInspectionResultMapper; + + /** * 查询质检任务 * @@ -167,13 +176,52 @@ public class QcInspectionMainServiceImpl implements IQcInspectionMainService { * @return 是否删除成功 */ @Override + @Transactional(rollbackFor = Exception.class) public Boolean deleteWithValidByIds(Collection ids, Boolean isValid) { if (isValid) { //TODO 做一些业务上的校验,判断是否需要校验 } + + // 参数校验 + if (ids == null || ids.isEmpty()) { + return false; + } + + // 查询所有关联的子表记录ID + List resultIds = getResultIdsByMainIds(ids); + + // 删除子表记录 + if (!resultIds.isEmpty()) { + Boolean subDeleteResult = qcInspectionResultService.deleteWithValidByIds(resultIds, false); + if (!subDeleteResult) { + throw new ServiceException("删除质检结果子表记录失败"); + } + } + + // 删除主表记录 return baseMapper.deleteByIds(ids) > 0; } + /** + * 根据主表ID获取所有关联的子表记录ID + * + * @param mainIds 主表ID列表 + * @return 子表记录ID列表 + */ + private List getResultIdsByMainIds(Collection mainIds) { + // 使用JOIN查询优化性能 + MPJLambdaWrapper wrapper = JoinWrappers.lambda(QcInspectionResult.class) + .select(QcInspectionResult::getResultId) + .in(QcInspectionResult::getInspectionId, mainIds); + + List results = qcInspectionResultMapper.selectList(wrapper); + List resultIds = new ArrayList<>(); + for (QcInspectionResult result : results) { + resultIds.add(result.getResultId()); + } + return resultIds; + } + /** * PDA分页查询质检任务 * diff --git a/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionResultServiceImpl.java b/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionResultServiceImpl.java index 97b4a99..a90fe1e 100644 --- a/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionResultServiceImpl.java +++ b/ruoyi-modules/hwmom-qms/src/main/java/org/dromara/qms/service/impl/QcInspectionResultServiceImpl.java @@ -1,24 +1,26 @@ package org.dromara.qms.service.impl; -import org.dromara.common.core.utils.MapstructUtils; -import org.dromara.common.core.utils.StringUtils; -import org.dromara.common.mybatis.core.page.TableDataInfo; -import org.dromara.common.mybatis.core.page.PageQuery; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.toolkit.JoinWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; -import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; +import org.dromara.common.core.utils.MapstructUtils; +import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.mybatis.core.page.PageQuery; +import org.dromara.common.mybatis.core.page.TableDataInfo; +import org.dromara.qms.domain.QcInspectionMain; +import org.dromara.qms.domain.QcInspectionResult; import org.dromara.qms.domain.bo.QcInspectionResultBo; import org.dromara.qms.domain.vo.QcInspectionResultVo; -import org.dromara.qms.domain.QcInspectionResult; +import org.dromara.qms.mapper.QcInspectionMainMapper; import org.dromara.qms.mapper.QcInspectionResultMapper; import org.dromara.qms.service.IQcInspectionResultService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.Collection; /** * 质检结果子表Service业务层处理 @@ -32,6 +34,8 @@ public class QcInspectionResultServiceImpl implements IQcInspectionResultService private final QcInspectionResultMapper baseMapper; + private final QcInspectionMainMapper inspectionMainMapper; + /** * 查询质检结果子表 * @@ -110,12 +114,16 @@ public class QcInspectionResultServiceImpl implements IQcInspectionResultService * @return 是否新增成功 */ @Override + @Transactional(rollbackFor = Exception.class) public Boolean insertByBo(QcInspectionResultBo bo) { QcInspectionResult add = MapstructUtils.convert(bo, QcInspectionResult.class); validEntityBeforeSave(add); boolean flag = baseMapper.insert(add) > 0; if (flag) { bo.setResultId(add.getResultId()); + + // 同步主表状态 + syncMainInspectionResult(add.getInspectionId()); } return flag; } @@ -127,10 +135,45 @@ public class QcInspectionResultServiceImpl implements IQcInspectionResultService * @return 是否修改成功 */ @Override + @Transactional(rollbackFor = Exception.class) public Boolean updateByBo(QcInspectionResultBo bo) { QcInspectionResult update = MapstructUtils.convert(bo, QcInspectionResult.class); validEntityBeforeSave(update); - return baseMapper.updateById(update) > 0; + + // 更新子表记录 + boolean result = baseMapper.updateById(update) > 0; + if (result) { + syncMainInspectionResult(update.getInspectionId()); + } + return result; + } + + /** + * 同步主表质检结果状态 + * 根据子表中所有检测项的结果来确定主表的最终结果 + * + * @param inspectionId 质检任务ID + */ + private void syncMainInspectionResult(Long inspectionId) { + // 检查参数 + if (inspectionId == null) { + return; + } + + // 查询该质检任务下是否有不合格的检测项 + MPJLambdaWrapper wrapper = JoinWrappers.lambda(QcInspectionResult.class) + .eq(QcInspectionResult::getInspectionId, inspectionId) + .eq(QcInspectionResult::getDetectResult, "1"); // 检测结果(0合格,1不合格,2未判定) + + Long unqualifiedCount = baseMapper.selectCount(wrapper); + + // 更新主表状态 + QcInspectionMain main = new QcInspectionMain(); + main.setInspectionId(inspectionId); + // 如果存在不合格项,则主表为不合格,否则为合格 + main.setResult(unqualifiedCount > 0 ? "1" : "0");//质检结果(0合格/1不合格) + + inspectionMainMapper.updateById(main); } /**