“djh”
5 days ago b0be631d7800b2a35c4dfeb9332877946e361829
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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<FactorDiscernMapper, FactorDiscern> 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<FactorDiscern> factorDiscerns = factorDiscernMapper.selectFactorDiscernList(factorDiscern);
        for (FactorDiscern discern : factorDiscerns) {
            List<FactorContent> factorContents = factorContentMapper.selectByFactorId(discern.getId());
            discern.setFactorContents(factorContents);
        }
        return CommonPage.restPage(factorDiscerns);
    }
 
    @Override
    public CommonResult selectFactorDiscernListAll(FactorDiscern factorDiscern) {
        List<FactorDiscern> factorDiscerns = factorDiscernMapper.selectFactorDiscernList(factorDiscern);
        for (FactorDiscern discern : factorDiscerns) {
            List<FactorContent> 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<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){
            throw new ApiException("当前企业已有数据,请删除后重试");
        }
        factorDiscern.setCreateBy(SecurityUtils.getUsername());
        factorDiscern.setCreateTime(LocalDateTime.now());
        factorDiscernMapper.insert(factorDiscern);
        List<FactorContent> 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<FactorContent> 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();
    }
 
 
}