package com.gkhy.exam.system.service.impl; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gkhy.exam.common.api.CommonPage; import com.gkhy.exam.common.api.CommonResult; import com.gkhy.exam.common.exception.ApiException; import com.gkhy.exam.common.utils.PageUtils; import com.gkhy.exam.common.utils.SecurityUtils; import com.gkhy.exam.system.domain.FactorContent; import com.gkhy.exam.system.domain.FactorControl; import com.gkhy.exam.system.domain.FactorDiscern; import com.gkhy.exam.system.domain.req.CopyReq; import com.gkhy.exam.system.mapper.FactorContentMapper; import com.gkhy.exam.system.mapper.FactorControlMapper; import com.gkhy.exam.system.mapper.FactorDiscernMapper; import com.gkhy.exam.system.service.FactorDiscernService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Service public class FactorDiscernServiceImpl extends ServiceImpl implements FactorDiscernService { @Autowired private FactorDiscernMapper factorDiscernMapper; @Autowired private FactorContentMapper factorContentMapper; @Autowired private FactorControlMapper factorControlMapper; @Override public CommonPage selectFactorDiscernList(FactorDiscern factorDiscern) { if (!SecurityUtils.adminUser()){ if (factorDiscern.getCompanyId()==null){ throw new ApiException("非管理员操作,企业id不可为空"); } } PageUtils.startPage(); List factorDiscerns = factorDiscernMapper.selectFactorDiscernList(factorDiscern); for (FactorDiscern discern : factorDiscerns) { List factorContents = factorContentMapper.selectByFactorId(discern.getId()); discern.setFactorContents(factorContents); } return CommonPage.restPage(factorDiscerns); } @Override public CommonResult selectFactorDiscernListAll(FactorDiscern factorDiscern) { List factorDiscerns = factorDiscernMapper.selectFactorDiscernList(factorDiscern); for (FactorDiscern discern : factorDiscerns) { List factorContents = factorContentMapper.selectByFactorId(discern.getId()); discern.setFactorContents(factorContents); } return CommonResult.success(factorDiscerns); } @Override @Transactional public CommonResult copyFactorDiscern(CopyReq copyReq) { // 参数校验 if (copyReq.getCompanyId() == null) { throw new ApiException("企业ID不能为空"); } if (copyReq.getCopyYear() == null || copyReq.getCopyYear().isEmpty()) { throw new ApiException("选择年份不能为空"); } if (copyReq.getTargetYear() == null || copyReq.getTargetYear().isEmpty()) { throw new ApiException("目标年份不能为空"); } // 查询源数据:根据企业ID和选择年份查询要素识别记录 FactorDiscern queryParam = new FactorDiscern(); queryParam.setCompanyId(Long.valueOf(copyReq.getCompanyId())); queryParam.setYear(copyReq.getCopyYear()); List sourceFactorDiscerns = factorDiscernMapper.selectFactorDiscernList(queryParam); if (sourceFactorDiscerns == null || sourceFactorDiscerns.isEmpty()) { throw new ApiException("未找到源年份[" + copyReq.getCopyYear() + "]的要素识别数据"); } // 检查目标年份是否已存在数据 FactorDiscern targetCheck = new FactorDiscern(); targetCheck.setCompanyId(Long.valueOf(copyReq.getCompanyId())); targetCheck.setYear(copyReq.getTargetYear()); List existingTargetData = factorDiscernMapper.selectFactorDiscernList(targetCheck); if (existingTargetData != null && !existingTargetData.isEmpty()) { // 收集所有需要删除的ID List deleteIds = existingTargetData.stream() .map(FactorDiscern::getId) .collect(Collectors.toList()); // 批量逻辑删除这些ID对应的要素识别记录 FactorDiscern updateEntity = new FactorDiscern(); updateEntity.setDelFlag(2); updateEntity.setUpdateTime(LocalDateTime.now()); updateEntity.setUpdateBy(SecurityUtils.getUsername()); UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.in("id", deleteIds); factorDiscernMapper.update(updateEntity, updateWrapper); // 同时删除这些要素识别下的所有内容记录 // UpdateWrapper contentWrapper = new UpdateWrapper<>(); // contentWrapper.in("factor_discern_id", deleteIds); // factorContentMapper.delete(contentWrapper); //删除要素识别控制对应数据 FactorControl factorControl = new FactorControl(); factorControl.setDelFlag(2); factorControl.setUpdateTime(LocalDateTime.now()); factorControl.setUpdateBy(SecurityUtils.getUsername()); factorControlMapper.update(factorControl, new UpdateWrapper().in("factor_discern_id", deleteIds)); } // 遍历源数据进行复制 for (FactorDiscern sourceDiscern : sourceFactorDiscerns) { // 创建新的要素识别记录 FactorDiscern newFactorDiscern = new FactorDiscern(); newFactorDiscern.setCompanyId(sourceDiscern.getCompanyId()); newFactorDiscern.setDeptId(sourceDiscern.getDeptId()); newFactorDiscern.setYear(copyReq.getTargetYear()); newFactorDiscern.setFictionId(sourceDiscern.getFictionId()); newFactorDiscern.setCheckId(sourceDiscern.getCheckId()); newFactorDiscern.setRatifyId(sourceDiscern.getRatifyId()); newFactorDiscern.setFictionTime(sourceDiscern.getFictionTime()); newFactorDiscern.setDelFlag(1); newFactorDiscern.setCreateBy(SecurityUtils.getUsername()); newFactorDiscern.setCreateTime(LocalDateTime.now()); // 插入新的要素识别记录 factorDiscernMapper.insert(newFactorDiscern); // 查询源要素识别的内容 List sourceContents = factorContentMapper.selectByFactorId(sourceDiscern.getId()); //添加要素识别控制 List factorControls = factorControlMapper.selectFactorDiscernId(sourceDiscern.getId()); for (FactorControl factorControl : factorControls) { FactorControl newControl = new FactorControl(); newControl.setCompanyId(factorControl.getCompanyId()); newControl.setFactorDiscernId(newFactorDiscern.getId()); newControl.setNumber(factorControl.getNumber()); newControl.setFrequency(factorControl.getFrequency()); newControl.setFictionId(factorControl.getFictionId()); newControl.setCheckId(factorControl.getCheckId()); newControl.setRatifyId(factorControl.getRatifyId()); newControl.setDelFlag(1); newControl.setCreateBy(SecurityUtils.getUsername()); newControl.setCreateTime(LocalDateTime.now()); factorControlMapper.insert(newControl); } if (sourceContents != null && !sourceContents.isEmpty()) { List newContents = new ArrayList<>(); // 遍历内容进行复制 for (FactorContent sourceContent : sourceContents) { FactorContent newContent = new FactorContent(); newContent.setFactorDiscernId(newFactorDiscern.getId()); newContent.setFactorName(sourceContent.getFactorName()); newContent.setFactorMess(sourceContent.getFactorMess()); newContent.setInfluence(sourceContent.getInfluence()); newContent.setMonitorMethod(sourceContent.getMonitorMethod()); newContent.setReview(sourceContent.getReview()); newContent.setSolutions(sourceContent.getSolutions()); newContent.setResult(sourceContent.getResult()); newContent.setRemark(sourceContent.getRemark()); newContent.setType(sourceContent.getType()); newContents.add(newContent); } // 批量插入内容记录 if (!newContents.isEmpty()) { factorContentMapper.insertBatch(newContents); } } } return CommonResult.success("成功从年份[" + copyReq.getCopyYear() + "]复制到年份[" + copyReq.getTargetYear() + "]"); } @Override @Transactional public CommonResult insertFactorDiscern(FactorDiscern factorDiscern) { List factorDiscerns = factorDiscernMapper.selectByCompanyIdAndDeptId(factorDiscern); if (factorDiscerns.size()>0){ throw new ApiException("当前企业已有数据,请删除后重试"); } factorDiscern.setCreateBy(SecurityUtils.getUsername()); factorDiscern.setCreateTime(LocalDateTime.now()); factorDiscernMapper.insert(factorDiscern); List factorContents = factorDiscern.getFactorContents(); for (FactorContent factorContent : factorContents) { factorContent.setFactorDiscernId(factorDiscern.getId()); } factorContentMapper.insertBatch(factorContents); return CommonResult.success(); } @Override public CommonResult updateFactorDiscern(FactorDiscern factorDiscern) { factorDiscern.setUpdateBy(SecurityUtils.getUsername()); factorDiscern.setUpdateTime(LocalDateTime.now()); factorDiscernMapper.updateById(factorDiscern); List factorContents = factorDiscern.getFactorContents(); for (FactorContent factorContent : factorContents) { factorContent.setFactorDiscernId(factorDiscern.getId()); } factorContentMapper.deletedByFactorId(factorDiscern.getId()); factorContentMapper.insertBatch(factorContents); return CommonResult.success(); } @Override public CommonResult deletedFactorDiscern(Integer factorDiscrenId) { FactorDiscern factorDiscern = new FactorDiscern(); factorDiscern.setId(factorDiscrenId); factorDiscern.setUpdateBy(SecurityUtils.getUsername()); factorDiscern.setUpdateTime(LocalDateTime.now()); factorDiscern.setDelFlag(2); factorDiscernMapper.updateById(factorDiscern); return CommonResult.success(); } }