feat(qms): 实现质检任务删除和结果同步功能:修改子表的结果就会同步主表的结果,有一个质检任务子表的结果为不合格,那么主表的结果就应该更新为不合格

- 在 QcInspectionMainServiceImpl 中添加删除质检任务时同时删除关联子表记录的逻辑
- 在 QcInspectionResultServiceImpl 中添加插入和更新质检结果时同步更新主表状态的方法
- 优化了删除和插入操作的事务管理,确保数据一致性
master
zangch@mesnac.com 6 days ago
parent 52a6e5542c
commit ca80f3681a

@ -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<Long> ids, Boolean isValid) {
if (isValid) {
//TODO 做一些业务上的校验,判断是否需要校验
}
// 参数校验
if (ids == null || ids.isEmpty()) {
return false;
}
// 查询所有关联的子表记录ID
List<Long> resultIds = getResultIdsByMainIds(ids);
// 删除子表记录
if (!resultIds.isEmpty()) {
Boolean subDeleteResult = qcInspectionResultService.deleteWithValidByIds(resultIds, false);
if (!subDeleteResult) {
throw new ServiceException("删除质检结果子表记录失败");
}
}
// 删除主表记录
return baseMapper.deleteByIds(ids) > 0;
}
/**
* IDID
*
* @param mainIds ID
* @return ID
*/
private List<Long> getResultIdsByMainIds(Collection<Long> mainIds) {
// 使用JOIN查询优化性能
MPJLambdaWrapper<QcInspectionResult> wrapper = JoinWrappers.lambda(QcInspectionResult.class)
.select(QcInspectionResult::getResultId)
.in(QcInspectionResult::getInspectionId, mainIds);
List<QcInspectionResult> results = qcInspectionResultMapper.selectList(wrapper);
List<Long> resultIds = new ArrayList<>();
for (QcInspectionResult result : results) {
resultIds.add(result.getResultId());
}
return resultIds;
}
/**
* PDA
*

@ -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<QcInspectionResult> 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);
}
/**

Loading…
Cancel
Save