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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.dto.PersonTrainingRecordDto;
import com.yuanchu.mom.dto.PersonTrainingRecordListDto;
import com.yuanchu.mom.dto.TrainingRecordPersonDetailedDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.PersonTrainingRecordMapper;
import com.yuanchu.mom.pojo.PersonTrainingDetailed;
import com.yuanchu.mom.pojo.PersonTrainingRecord;
import com.yuanchu.mom.service.PersonTrainingDetailedService;
import com.yuanchu.mom.service.PersonTrainingRecordService;
import com.yuanchu.mom.utils.HackLoopTableRenderPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 培训记录 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-12 04:50:48
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class PersonTrainingRecordServiceImpl extends ServiceImpl<PersonTrainingRecordMapper, PersonTrainingRecord> implements PersonTrainingRecordService {
 
    @Autowired
    private GetLook getLook;
 
    @Autowired
    private PersonTrainingDetailedService personTrainingDetailedService;
 
    @Override
    public List<PersonTrainingRecordDto> trainingAndAssessmentRecordsPage(Integer trainingDetailedId, String userName) {
        return baseMapper.trainingAndAssessmentRecordsPage(trainingDetailedId, userName);
    }
 
    @Override
    public void deleteTrainingAndAssessmentRecords(String ids) {
        String[] split = ids.split(",");
        if (split.length > 0) {
            for (String s : split) {
                baseMapper.deleteById(s);
            }
        }
    }
 
    @Override
    public IPage<PersonTrainingRecordListDto> personnelTrainingPersonnel(Page page, String userName, Integer userId, Integer departLimsId) {
        return baseMapper.personnelTrainingPersonnel(page, userName, userId, departLimsId);
    }
 
    @Override
    public IPage<TrainingRecordPersonDetailedDto> queryPersonnelDetails(Page page, Integer userId) {
        return baseMapper.queryPersonnelDetails(page, userId);
    }
 
    @Override
    public void claimOfTrainingAndAssessmentRecords(Boolean claimAndClaim, Integer courseId) {
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectInsOrderParameter");
        // 1、查询培训课程,取培训日期与开始日期,与当前时间比较,判断是否超出,如果超出不允许认领
        Boolean doesItExceedState = doesItExceed(courseId);
        if (!doesItExceedState) {
            throw new ErrorException("当前课程状态无法认领!");
        }
        // 2、判断是否重复
        PersonTrainingRecord personTrainingRecord1 = baseMapper.selectOne(Wrappers.<PersonTrainingRecord>lambdaQuery()
                .eq(PersonTrainingRecord::getCourseId, courseId)
                .eq(PersonTrainingRecord::getUserId, map1.get("userId")));
        if (ObjectUtils.isNotEmpty(personTrainingRecord1)) {
            throw new ErrorException("请勿重复认领!");
        }
        // 3、如果未重复新增培训记录
        // true 认领
        if (claimAndClaim) {
            PersonTrainingRecord personTrainingRecord = new PersonTrainingRecord();
            personTrainingRecord.setUserId(map1.get("userId"));
            personTrainingRecord.setCourseId(courseId);
            baseMapper.insert(personTrainingRecord);
        // 取消认领
        } else {
            baseMapper.delete(Wrappers.<PersonTrainingRecord>lambdaQuery()
                    .eq(PersonTrainingRecord::getUserId, map1.get("userId"))
                    .eq(PersonTrainingRecord::getCourseId, courseId));
        }
    }
 
    /**
     * 导出人员培训记录
     * @param userId
     * @param response
     */
    @Override
    public void exportTrainingRecord(Integer userId, HttpServletResponse response) {
        // 查询人员人信息
        PersonTrainingRecordListDto trainingRecordListDto = baseMapper.selectUserTraining(userId);
 
        // 查询培训记录
        List<TrainingRecordPersonDetailedDto> personDetailedDtos = baseMapper.selectPersonDetailedDtos(userId);
 
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/training-record.docx");
        Configure configure = Configure.builder()
                .bind("personnelDetailsLisat", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("traning", trainingRecordListDto);
                    put("personnelDetailsLisat", personDetailedDtos);
                }});
        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("导出失败");
        }
 
 
    }
 
    @Override
    public IPage<TrainingRecordPersonDetailedDto> queryPersonnelDetailsOfUserIdAndYear(Page page, Integer userId, Integer year) {
        return baseMapper.queryPersonnelDetailsOfUserIdAndYear(page, userId, year);
    }
 
    @Override
    public void exportTrainingRecordAddTrainingDate(Integer userId, Integer trainingDate, HttpServletResponse response) {
        // 查询人员人信息
        PersonTrainingRecordListDto trainingRecordListDto = baseMapper.selectUserTraining(userId);
 
        // 查询培训记录
        List<TrainingRecordPersonDetailedDto> personDetailedDtos = baseMapper.selectPersonDetailedDtosByTrainingDate(userId, trainingDate);
 
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/training-record.docx");
        Configure configure = Configure.builder()
                .bind("personnelDetailsLisat", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("traning", trainingRecordListDto);
                    put("personnelDetailsLisat", personDetailedDtos);
                }});
        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 courseId 该课程的id
     * @return
     */
    public Boolean doesItExceed(Integer courseId) {
        PersonTrainingDetailed personTrainingDetailed = personTrainingDetailedService.getById(courseId);
        // 只有状态为3可以认领
        if (!personTrainingDetailed.getState().equals(3)) {
            return false;
        }
        SimpleDateFormat sdfWithoutTime = new SimpleDateFormat("yyyy-MM-dd");
        String originalDateStr = sdfWithoutTime.format(personTrainingDetailed.getTrainingDate());
        SimpleDateFormat sdfWithTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 添加时分秒后的日期
            Date addedDate = sdfWithTime.parse(originalDateStr + " " + personTrainingDetailed.getOpeningTime());
            // 获取当前时间
            Date currentDate = new Date();
            // 日期未超出当前时间。
            if (addedDate.after(currentDate) || addedDate.equals(currentDate)) {
                return true;
            // 日期已超出当前时间
            } else {
                // 如果日期超出 更新课程状态为已结束
                personTrainingDetailedService.update(Wrappers.<PersonTrainingDetailed>lambdaUpdate()
                        .eq(PersonTrainingDetailed::getId, courseId)
                        .set(PersonTrainingDetailed::getState, 2));
                return false;
            }
        } catch (Exception e) {
            throw new ErrorException("时间格式错误!");
        }
    }
}