zss
6 天以前 51ec98113c6d49d0f7eec4e3c030e55e337e97db
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.yuanchu.mom.service.impl;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.Pictures;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.dto.*;
import com.yuanchu.mom.excel.PersonSupervisePlanDetailsUpload;
import com.yuanchu.mom.excel.PersonTrainingDetailedListener;
import com.yuanchu.mom.excel.PersonTrainingDetailedUpload;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.PersonTrainingDetailedMapper;
import com.yuanchu.mom.mapper.PersonTrainingMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.mapper.PersonTrainingRecordMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.PersonTraining;
import com.yuanchu.mom.pojo.User;
import com.yuanchu.mom.pojo.PersonTrainingDetailed;
import com.yuanchu.mom.service.PersonTrainingDetailedService;
import com.yuanchu.mom.service.PersonTrainingService;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import com.yuanchu.mom.utils.XWPFDocumentUtils;
import com.yuanchu.mom.utils.DateImageUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.Optional;
 
/**
 * <p>
 * 培训计划 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-11 01:11:49
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PersonTrainingServiceImpl extends ServiceImpl<PersonTrainingMapper, PersonTraining> implements PersonTrainingService {
 
    @Autowired
    private GetLook getLook;
 
    @Autowired
    private PersonTrainingDetailedService personTrainingDetailedService;
 
    @Autowired
    private UserMapper userMapper;
 
 
    @Autowired
    private PersonTrainingDetailedMapper personTrainingDetailedMapper;
 
    @Autowired
    private PersonTrainingRecordMapper personTrainingRecordMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<PersonTrainingDto> personTrainingSelect(Page page, String compilerName, String departmentId) {
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        User user = Optional.ofNullable(userMapper.selectById(userId)).orElse(new User());
        if(3==user.getRoleId()){
            departmentId=null;
        }
        return baseMapper.personTrainingSelect(page, compilerName, departmentId);
    }
 
    @Override
    public void personTrainingImport(MultipartFile file) {
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
        // 年度计划父级新增数据
        PersonTraining personSupervisePlan = new PersonTraining();
        String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
        personSupervisePlan.setFileName(fileName);
        personSupervisePlan.setCompilerId(map1.get("userId"));
        personSupervisePlan.setCompilationDate(LocalDateTime.now());
        baseMapper.insert(personSupervisePlan);
        // 年度计划详情 新增
        try {
            PersonTrainingDetailedListener personSupervisePlanDetailsListener = new PersonTrainingDetailedListener(personTrainingDetailedService);
            personSupervisePlanDetailsListener.setPlanId(personSupervisePlan.getId());
            EasyExcel.read(file.getInputStream(), PersonTrainingDetailedUpload.class, personSupervisePlanDetailsListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    @Override
    public void personTrainingDelete(Integer id) {
        personTrainingDetailedService.removeById(id);
        baseMapper.deleteById(id);
    }
 
    @Override
    public void reviewAnnualPersonnelTraining(PersonTrainingUpdateDto personTrainingUpdateDto) {
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
        PersonTraining personTraining = new PersonTraining();
        personTraining.setId(personTrainingUpdateDto.getId());
        personTraining.setReviewerId(map1.get("userId"));
        personTraining.setAuditDate(LocalDateTime.now());
        personTraining.setAuditRemarks(personTrainingUpdateDto.getRemarks());
        personTraining.setReviewerStatus(personTrainingUpdateDto.getStatus());
        baseMapper.updateById(personTraining);
    }
 
    @Override
    public void approveAnnualPersonnelTraining(PersonTrainingUpdateDto personTrainingUpdateDto) {
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
        LambdaUpdateWrapper<PersonTraining> wrapper = Wrappers.<PersonTraining>lambdaUpdate()
                .eq(PersonTraining::getId, personTrainingUpdateDto.getId())
                .set(PersonTraining::getApproverId, map1.get("userId"))
                .set(PersonTraining::getApprovalDate, LocalDateTime.now())
                .set(PersonTraining::getApprovalRemarks, personTrainingUpdateDto.getRemarks())
                .set(PersonTraining::getApprovalStatus, personTrainingUpdateDto.getStatus());
        if (personTrainingUpdateDto.getStatus().equals(2)) {
            wrapper.set(PersonTraining::getReviewerStatus, null);
        }
        baseMapper.update(new PersonTraining(), wrapper);
    }
 
    /**
     * 导出人员培训计划
     * @param id
     * @param response
     */
    @Override
    public void exportPersonTraining(Integer id, HttpServletResponse response) {
 
        // 查询详情
        PersonTraining personTraining = baseMapper.selectById(id);
 
        //获取提交人的签名地址
        String writeUrl = userMapper.selectById(personTraining.getCompilerId()).getSignatureUrl();
        if (ObjectUtils.isEmpty(writeUrl) || writeUrl.equals("")) {
            throw new ErrorException("找不到检验人的签名");
        }
 
        //获取复核人的签名地址
        String examineUrl = null;
        if (personTraining.getReviewerId() != null) {
            examineUrl = userMapper.selectById(personTraining.getReviewerId()).getSignatureUrl();
            if (StringUtils.isBlank(examineUrl)) {
                throw new ErrorException("找不到复核人的签名");
            }
        }
 
        //获取批准人的签名地址
        String ratifyUrl = null;
        if (personTraining.getApproverId() != null) {
            ratifyUrl = userMapper.selectById(personTraining.getApproverId()).getSignatureUrl();
            if (StringUtils.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到复核人的签名");
            }
        }
 
        // 查询详情
        List<PersonTrainingDetailedDto> detailedDtos = personTrainingDetailedMapper.selectTrainingList(id);
 
        int index = 1;
        for (PersonTrainingDetailedDto detailedDto : detailedDtos) {
            if (detailedDto.getTrainingDate() != null) {
                SimpleDateFormat sdfWithoutTime = new SimpleDateFormat("yyyy-MM-dd");
                detailedDto.setTrainingDateString(sdfWithoutTime.format(detailedDto.getTrainingDate()));
            }
 
            detailedDto.setIndex(index);
            index++;
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/person-training.docx");
        String finalExamineUrl = examineUrl;
        String finalRatifyUrl = ratifyUrl;
        Configure configure = Configure.builder()
                .bind("trainingDetailedList", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("year", personTraining.getCreateTime().getYear());
                    put("trainingDetailedList", detailedDtos);
                    put("writeUrl", StringUtils.isNotBlank(writeUrl) ? Pictures.ofLocal(imgUrl + "/" + writeUrl).create() : null);
                    put("examineUrl", StringUtils.isNotBlank(finalExamineUrl) ? Pictures.ofLocal(imgUrl + "/" + finalExamineUrl).create() : null);
                    put("ratifyUrl", StringUtils.isNotBlank(finalRatifyUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create() : null);
                    put("writeDateUrl", personTraining.getCompilationDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(personTraining.getCompilationDate())).create() : null);
                    put("examineDateUrl", personTraining.getAuditDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(personTraining.getAuditDate())).create() : null);
                    put("ratifyDateUrl", personTraining.getApprovalDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(personTraining.getApprovalDate())).create() : null);
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "人员培训计划导出", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
 
    }
 
    /**
     * 导出人员培训与考核记录
     * @param id
     * @param response
     */
    @Override
    public void exportPersonTrainingRecord(Integer id, HttpServletResponse response) {
        // 查询人员培训明细
        PersonTrainingDetailedDto detailedDto = personTrainingDetailedMapper.selectTrainingDetail(id);
 
        // 查询培训的人员
        List<PersonTrainingRecordDto> recordDtos = personTrainingRecordMapper.selectListByTrainingDetailedId(id);
 
        List<TrainingRecordExportDto> exportDtoList = new ArrayList<>();
        TrainingRecordExportDto exportDto = new TrainingRecordExportDto();
 
        int count = 0;
        for (PersonTrainingRecordDto recordDto : recordDtos) {
            switch (count) {
                case 0:
                    exportDto.setUserName1(recordDto.getUserName());
                    exportDto.setDepartment1(recordDto.getDepartment());
                    exportDto.setExaminationResults1(recordDto.getExaminationResults());
                    count ++;
                    break;
                case 1:
                    exportDto.setUserName2(recordDto.getUserName());
                    exportDto.setDepartment2(recordDto.getDepartment());
                    exportDto.setExaminationResults2(recordDto.getExaminationResults());
                    exportDtoList.add(exportDto);
                    exportDto = new TrainingRecordExportDto();
                    count = 0;
                    break;
            }
        }
        exportDtoList.add(exportDto);
 
        // 质量负责人
        String assessmentUserUrl = null;
        if (detailedDto.getAssessmentUserId() != null) {
            assessmentUserUrl = userMapper.selectById(detailedDto.getAssessmentUserId()).getSignatureUrl();
            if (StringUtils.isBlank(assessmentUserUrl)) {
                throw new ErrorException("找不到评价人的签名");
            }
        }
 
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/person-training-record.docx");
        Configure configure = Configure.builder()
                .bind("trainingRecordsList", new HackLoopTableRenderPolicy())
                .build();
        String finalAssessmentUserUrl = assessmentUserUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("trainingDetail", detailedDto);
                    put("trainingRecordsList", exportDtoList);
                    put("assessmentUserUrl", StringUtils.isNotBlank(finalAssessmentUserUrl) ? Pictures.ofLocal(imgUrl + "/" + finalAssessmentUserUrl).create() : null);
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    "培训与考核记录导出", "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
 
 
    }
}