From c26e227abe288476c11b0a8b7875045e71efa14c Mon Sep 17 00:00:00 2001
From: “djh” <“3298565835@qq.com”>
Date: Thu, 30 Apr 2026 17:30:00 +0800
Subject: [PATCH] 新增修改

---
 multi-system/src/main/java/com/gkhy/exam/system/service/impl/FactorDiscernServiceImpl.java |  138 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 138 insertions(+), 0 deletions(-)

diff --git a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/FactorDiscernServiceImpl.java b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/FactorDiscernServiceImpl.java
index e1d739a..6fe5fee 100644
--- a/multi-system/src/main/java/com/gkhy/exam/system/service/impl/FactorDiscernServiceImpl.java
+++ b/multi-system/src/main/java/com/gkhy/exam/system/service/impl/FactorDiscernServiceImpl.java
@@ -1,5 +1,6 @@
 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;
@@ -7,8 +8,11 @@
 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;
@@ -16,7 +20,9 @@
 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<FactorDiscernMapper, FactorDiscern> implements FactorDiscernService {
@@ -26,6 +32,9 @@
 
     @Autowired
     private FactorContentMapper factorContentMapper;
+
+    @Autowired
+    private FactorControlMapper factorControlMapper;
 
 
     @Override
@@ -56,6 +65,135 @@
 
     @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<FactorDiscern> 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<FactorDiscern> existingTargetData = factorDiscernMapper.selectFactorDiscernList(targetCheck);
+
+        if (existingTargetData != null && !existingTargetData.isEmpty()) {
+            // 收集所有需要删除的ID
+            List<Integer> 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<FactorDiscern> updateWrapper = new UpdateWrapper<>();
+            updateWrapper.in("id", deleteIds);
+            factorDiscernMapper.update(updateEntity, updateWrapper);
+
+            // 同时删除这些要素识别下的所有内容记录
+//            UpdateWrapper<FactorContent> 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<FactorControl>().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<FactorContent> sourceContents = factorContentMapper.selectByFactorId(sourceDiscern.getId());
+            //添加要素识别控制
+            List<FactorControl> 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<FactorContent> 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<FactorDiscern> factorDiscerns = factorDiscernMapper.selectByCompanyIdAndDeptId(factorDiscern);
         if (factorDiscerns.size()>0){

--
Gitblit v1.9.2