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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.ycl.platform.service.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.exception.job.TaskException;
import com.ycl.platform.base.BaseSelect;
import com.ycl.platform.domain.entity.CheckTemplate;
import com.ycl.platform.domain.entity.CheckTemplateRule;
import com.ycl.platform.domain.query.CheckTemplateQuery;
import com.ycl.platform.mapper.CheckTemplateMapper;
import com.ycl.platform.mapper.CheckTemplateRuleMapper;
import com.ycl.platform.service.ICheckTemplateRuleService;
import com.ycl.platform.service.ICheckTemplateService;
import com.ycl.system.AjaxResult;
import com.ycl.system.Result;
import com.ycl.system.domain.SysJob;
import com.ycl.system.entity.SysDept;
import com.ycl.system.service.ISysDeptService;
import com.ycl.system.service.ISysJobService;
import com.ycl.utils.SecurityUtils;
import constant.CheckConstants;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import utils.DateUtils;
import utils.StringUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 考核模板Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@Service
public class CheckTemplateServiceImpl extends ServiceImpl<CheckTemplateMapper, CheckTemplate> implements ICheckTemplateService {
    @Autowired
    private CheckTemplateMapper checkTemplateMapper;
    @Autowired
    private ICheckTemplateRuleService templateRuleServicee;
    @Autowired
    private CheckTemplateRuleMapper checkTemplateRuleMapper;
    @Autowired
    private ISysJobService jobService;
 
    /**
     * 查询考核模板
     *
     * @param id 考核模板主键
     * @return 考核模板
     */
    @Override
    public CheckTemplateQuery selectCheckTemplateById(Integer id) {
        CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(id);
        CheckTemplateQuery checkTemplateQuery = new CheckTemplateQuery();
        BeanUtils.copyProperties(checkTemplate, checkTemplateQuery);
        List<Integer> deptIds = JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class);
        checkTemplateQuery.setDeptId(deptIds)
                .setAdjustCoefficient(checkTemplate.getAdjustCoefficient() + "")
                .setAlarmScore(checkTemplate.getAlarmScore() + "");
        //查询规则权重
        List<CheckTemplateRule> templateRuleList = checkTemplateRuleMapper.selectListByTemplateId(checkTemplate.getId());
        List<Map<String, Object>> list = new ArrayList<>();
        for (CheckTemplateRule checkTemplateRule : templateRuleList) {
            Map<String, Object> map = new HashMap<>();
            map.put("ruleId", checkTemplateRule.getCheckRuleId());
            map.put("weight", checkTemplateRule.getWeight());
            list.add(map);
        }
        checkTemplateQuery.setRuleFormList(list);
 
        return checkTemplateQuery;
    }
 
    /**
     * 查询考核模板列表
     *
     * @param checkTemplateDTO 考核模板
     * @return 考核模板
     */
    @Override
    public List<CheckTemplateQuery> selectCheckTemplateList(CheckTemplateQuery checkTemplateDTO) {
        List<CheckTemplate> checkTemplates = checkTemplateMapper.selectCheckTemplateList(checkTemplateDTO);
        List<CheckTemplateQuery> checkTemplateList = new ArrayList<>();
        //转换部门id为集合,转换alarmScore为string
        for (CheckTemplate template : checkTemplates) {
            List<Integer> deptIds = JSONArray.parseArray(template.getDeptId(), Integer.class);
            CheckTemplateQuery checkTemplateQuery = new CheckTemplateQuery();
            BeanUtils.copyProperties(template, checkTemplateQuery);
            checkTemplateQuery.setDeptId(deptIds)
                    .setAdjustCoefficient(template.getAdjustCoefficient() + "");
            checkTemplateList.add(checkTemplateQuery);
        }
 
        return checkTemplateList;
    }
 
    /**
     * 新增考核模板
     *
     * @param checkTemplateDTO 考核模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException {
        String username = SecurityUtils.getUsername();
        /** 插入t_template */
        CheckTemplate checkTemplate = new CheckTemplate();
        BeanUtils.copyProperties(checkTemplateDTO, checkTemplate);
        //从小到大排序
        List<Integer> deptId = checkTemplateDTO.getDeptId();
        Collections.sort(deptId);
        Date nowDate = DateUtils.getNowDate();
        checkTemplate.setDeptId(JSONArray.toJSONString(deptId))
                .setUpdateUserName(username)
                .setCreateUserName(username)
                .setAdjustCoefficient(new BigDecimal(checkTemplateDTO.getAdjustCoefficient() + ""))
                //不填报警分数---->零分---->不报警
                .setAlarmScore(new BigDecimal(checkTemplateDTO.getAlarmScore() == null ? "" : checkTemplateDTO.getAlarmScore()));
        int i = checkTemplateMapper.insertCheckTemplate(checkTemplate);
 
        //新增定时任务,状态同模板,然后赋值jobId给template
        SysJob sysJob = addJob(checkTemplateDTO.setId(checkTemplate.getId()));
        checkTemplate.setJobId(Integer.parseInt(sysJob.getJobId() + ""));
        checkTemplateMapper.updateCheckTemplate(checkTemplate);
 
        /** t_template_rule新增权重 */
        insertTemlpateRule(checkTemplateDTO, checkTemplate);
 
        return i;
    }
 
    /**
     * 复制考核模板
     *
     * @param checkTemplate 考核模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int copyCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException {
        //插入模板表
        CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(checkTemplateDTO.getId());
        String username = SecurityUtils.getUsername();
        checkTemplate.setUpdateUserName(username)
                .setCreateUserName(username)
                .setStatus(CheckConstants.Status_Stop)
                .setId(null);
        int i = checkTemplateMapper.insertCheckTemplate(checkTemplate);
        //插入template_rule表
        List<CheckTemplateRule> templateRuleList = checkTemplateRuleMapper.selectListByTemplateId(checkTemplateDTO.getId());
        templateRuleList.forEach(checkTemplateRule -> checkTemplateRule.setCheckTemplateId(checkTemplate.getId()).setId(null));
        templateRuleServicee.saveBatch(templateRuleList);
        //新增定时任务,状态同模板,然后赋值jobId给template
        SysJob sysJob = addJob(checkTemplateDTO.setId(checkTemplate.getId()));
        checkTemplate.setJobId(Integer.parseInt(sysJob.getJobId() + ""));
        checkTemplateMapper.updateCheckTemplate(checkTemplate);
        return i;
    }
 
    /**
     * 修改考核模板
     *
     * @param checkTemplate 考核模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateCheckTemplate(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException {
        CheckTemplate checkTemplate = new CheckTemplate();
        BeanUtils.copyProperties(checkTemplateDTO, checkTemplate);
        checkTemplate.setUpdateUserName(SecurityUtils.getUsername());
        List<Integer> deptId = checkTemplateDTO.getDeptId();
        Collections.sort(deptId);
        checkTemplate.setDeptId(JSONArray.toJSONString(deptId))
                .setAdjustCoefficient(new BigDecimal(checkTemplateDTO.getAdjustCoefficient() + ""));
        checkTemplateMapper.updateCheckTemplate(checkTemplate);
 
        /** t_template_rule修改权重 */
        if (!CollectionUtils.isEmpty(checkTemplateDTO.getRuleFormList())) {
            //先删除原数据
            checkTemplateRuleMapper.deleteByTemplateId(checkTemplate.getId());
            //插入新规则数据
            insertTemlpateRule(checkTemplateDTO, checkTemplate);
        }
        //调整job表里的状态
        SysJob job = jobService.selectJobById(Long.valueOf(checkTemplateDTO.getJobId()));
        job.setStatus(checkTemplateDTO.getStatus());
        jobService.updateJob(job);
        return AjaxResult.success();
    }
 
    /**
     * 删除考核模板信息
     *
     * @param id 考核模板主键
     * @return 结果
     */
    @Override
    public int deleteCheckTemplateById(Integer id) throws SchedulerException {
        CheckTemplate checkTemplate = checkTemplateMapper.selectCheckTemplateById(id);
        //删除定时任务
        SysJob job = new SysJob();
        if (checkTemplate.getJobId() != null) {
            job.setJobId(Long.valueOf(checkTemplate.getJobId()));
            job.setJobGroup("CHECK");
            jobService.deleteJob(job);
        }
        return checkTemplateMapper.deleteCheckTemplateById(id);
    }
 
    @Override
    public Result pullList() {
        List<CheckTemplate> checkTemplates = checkTemplateMapper.selectCheckTemplateList(new CheckTemplateQuery());
 
        return Result.ok().data(checkTemplates);
    }
 
 
    private void insertTemlpateRule(CheckTemplateQuery checkTemplateDTO, CheckTemplate checkTemplate) {
        List<Map<String, Object>> ruleFormList = checkTemplateDTO.getRuleFormList();
        if (!CollectionUtils.isEmpty(ruleFormList)) {
            List<CheckTemplateRule> templateRuleList = new ArrayList<>();
            for (Map<String, Object> map : ruleFormList) {
                templateRuleList.add(new CheckTemplateRule()
                        .setCheckRuleId((Integer) map.get("ruleId"))
                        .setCheckTemplateId(checkTemplate.getId())
                        .setWeight(new BigDecimal(map.get("weight").toString())));
            }
            //批量插入数据库
            templateRuleServicee.saveBatch(templateRuleList);
        }
    }
 
    private SysJob addJob(CheckTemplateQuery checkTemplateDTO) throws SchedulerException, TaskException {
        SysJob job = new SysJob();
        job.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        //调用方法名
        job.setInvokeTarget("checkScoreTask.executeTemplate(" + checkTemplateDTO.getId() + ")");
        job.setConcurrent("1");
        job.setStatus(checkTemplateDTO.getStatus());
        job.setJobGroup("CHECK");
        job.setCronExpression("0 0 8 * * ?");
        job.setJobName(checkTemplateDTO.getTemplateName());
        int i = jobService.insertJob(job);
        return job;
    }
 
    //    /**
//     * 批量删除考核模板
//     *
//     * @param ids 需要删除的考核模板主键
//     * @return 结果
//     */
//    @Override
//    public int deleteCheckTemplateByIds(Integer[] ids) {
//        return checkTemplateMapper.deleteCheckTemplateByIds(ids);
//    }
}