吴羡
4 天以前 8ad863082097ffdcab4a8e7f426d344a1fe6f892
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
package com.ruoyi.personnel.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.config.ConfigureBuilder;
import com.deepoove.poi.data.Pictures;
import com.ruoyi.common.constant.MenuJumpPathConstants;
import com.ruoyi.common.core.domain.entity.InformationNotification;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.utils.DateImageUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WxCpUtils;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.personnel.dto.PersonPersonnelCapacityDto;
import com.ruoyi.personnel.dto.PersonPersonnelCapacityExportDto;
import com.ruoyi.personnel.pojo.PersonPersonnelCapacity;
import com.ruoyi.personnel.mapper.PersonPersonnelCapacityMapper;
import com.ruoyi.personnel.service.PersonPersonnelCapacityService;
import com.ruoyi.system.mapper.UserMapper;
import com.ruoyi.system.service.InformationNotificationService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
 
/**
 * <p>
 * 人员能力 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-10 11:26:18
 */
@Service
public class PersonPersonnelCapacityServiceImpl extends ServiceImpl<PersonPersonnelCapacityMapper, PersonPersonnelCapacity> implements PersonPersonnelCapacityService {
 
    @Resource
    private UserMapper userMapper;
    @Resource
    private InformationNotificationService informationNotificationService;
 
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<PersonPersonnelCapacityDto> personPersonnelCapacityPage(Page page, Integer departLimsId, Integer userId, String userName) {
        return baseMapper.personPersonnelCapacityPage(page, departLimsId, userId, userName);
    }
 
    /**
     * 导出人员能力
     * @param id
     * @param response
     */
    @Override
    public void exportPersonnelCapacity(Integer id, HttpServletResponse response) {
        if (id == null) {
            throw new ErrorException("缺少人员能力记录id");
        }
        PersonPersonnelCapacityExportDto capacityExportDto = baseMapper.selectExportPersonnelCapacity(id);
        if (capacityExportDto == null) {
            throw new ErrorException("人员能力记录不存在或已被删除");
        }
 
        // 确认人
        String confirmUrl = null;
        if (capacityExportDto.getConfirmOperatingPersonnelId() != null) {
            User confirmUser = userMapper.selectById(capacityExportDto.getConfirmOperatingPersonnelId());
            if (confirmUser == null) {
                throw new ErrorException("确认人不存在或已被删除,无法导出");
            }
            confirmUrl = confirmUser.getSignatureUrl();
            if (StringUtils.isBlank(confirmUrl)) {
                throw new ErrorException("确认人还没有上传签名,无法导出");
            }
            if (!new File(imgUrl + "/" + confirmUrl).exists()) {
                throw new ErrorException("确认人签名文件不存在,请让确认人重新上传");
            }
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/personnel-capacity.docx");
        if (inputStream == null) {
            throw new ErrorException("人员能力导出模板不存在");
        }
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        String finalConfirmUrl = confirmUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("capacity", capacityExportDto);
                    put("confirmUrl", StringUtils.isNotBlank(finalConfirmUrl) ? Pictures.ofLocal(imgUrl + "/" + finalConfirmUrl).create() : null);
                    put("confirmDateUrl", capacityExportDto.getConfirmDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(capacityExportDto.getConfirmDate())).create() : null);
                }});
 
        try {
            response.setContentType("application/msword");
            String userName = StringUtils.isBlank(capacityExportDto.getUserName()) ? "人员能力" : capacityExportDto.getUserName() + "人员能力认定";
            String fileName = URLEncoder.encode(userName, "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 提交确认人员能力
     * @param personPersonnelCapacity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitConfirmPersonnelCapability(PersonPersonnelCapacity personPersonnelCapacity) {
        if (personPersonnelCapacity.getUserId() == null) {
            throw new ErrorException("缺少人员");
        }
        if (personPersonnelCapacity.getConfirmOperatingPersonnelId() == null) {
            throw new ErrorException("缺少确认人");
        }
        User formUser = userMapper.selectById(personPersonnelCapacity.getUserId());
        if (formUser == null) {
            throw new ErrorException("人员不存在或已被删除,无法提交");
        }
        User confirmUser = userMapper.selectById(personPersonnelCapacity.getConfirmOperatingPersonnelId());
        if (confirmUser == null) {
            throw new ErrorException("确认人不存在或已被删除,无法提交");
        }
        if (personPersonnelCapacity.getId() != null) {
            PersonPersonnelCapacity existing = this.getById(personPersonnelCapacity.getId());
            if (existing == null) {
                throw new ErrorException("人员能力记录不存在或已被删除");
            }
            if (existing.getConfirmDate() != null) {
                throw new ErrorException("该记录已确认,不能重复提交");
            }
        }
 
        Integer userId = SecurityUtils.getUserId().intValue();
        User user = userMapper.selectById(userId);
        // 提交和确认日期分开:确认日期只在确认操作时由系统写入
        personPersonnelCapacity.setConfirmDate(null);
        this.saveOrUpdate(personPersonnelCapacity);
 
        // 消息发送
        InformationNotification info = new InformationNotification();
        // 发送人
        info.setCreateUser(user != null ? user.getName() : "");
        info.setMessageType("6");
        info.setTheme("CNAS人员能力确认通知");
        info.setContent(formUser.getName() + "的人员能力待确认");
        info.setSenderId(userId);
        // 接收人
        info.setConsigneeId(personPersonnelCapacity.getConfirmOperatingPersonnelId());
        info.setJumpPath(MenuJumpPathConstants.PERSONNEL);
        informationNotificationService.addInformationNotification(info);
 
        // 发送企业微信通知
        threadPoolTaskExecutor.execute(() -> {
            if (confirmUser.getAccount() == null || confirmUser.getAccount().trim().isEmpty()) {
                return;
            }
            String message = "";
            message += "CNAS人员能力确认通知";
            message += "\n请去资源要求-人员-人员管理-人员能力填写";
            message += "\n" + formUser.getName() + "的人员能力待确认";
            //发送企业微信消息通知
            try {
                WxCpUtils.inform(confirmUser.getAccount(), message, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
 
    }
 
}