吴羡
2 天以前 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
package com.ruoyi.personnel.service.impl;
 
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.config.ConfigureBuilder;
import com.deepoove.poi.data.FilePictureRenderData;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.personnel.dto.PersonPostAuthorizationRecordDto;
import com.ruoyi.personnel.mapper.PersonBasicInfoMapper;
import com.ruoyi.personnel.mapper.PersonPostAuthorizationRecordMapper;
import com.ruoyi.personnel.pojo.PersonBasicInfo;
import com.ruoyi.personnel.pojo.PersonPostAuthorizationRecord;
import com.ruoyi.personnel.service.PersonPostAuthorizationRecordService;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
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.time.LocalDateTime;
import java.util.HashMap;
 
/**
 * <p>
 * 任职授权记录 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-09 10:48:17
 */
@Service
public class PersonPostAuthorizationRecordServiceImpl extends ServiceImpl<PersonPostAuthorizationRecordMapper, PersonPostAuthorizationRecord> implements PersonPostAuthorizationRecordService {
 
    @Resource
    private UserMapper userMapper;
    @Resource
    private PersonBasicInfoMapper personBasicInfoMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public IPage<PersonPostAuthorizationRecordDto> personPostAuthorizationRecordPage(Page page, Integer departLimsId, Integer userId, String userName) {
        return baseMapper.personPostAuthorizationRecordPage(page, departLimsId, userId, userName);
    }
 
    @Override
    public void exportPersonPostAuthorizationRecord(Integer id, HttpServletResponse response) {
        if (id == null) {
            throw new ErrorException("缺少任职授权记录id");
        }
        PersonPostAuthorizationRecord personPostAuthorizationRecord = baseMapper.selectById(id);
        if (personPostAuthorizationRecord == null) {
            throw new ErrorException("任职授权记录不存在或已被删除");
        }
        Integer incumbentUserId;
        try {
            incumbentUserId = Integer.valueOf(personPostAuthorizationRecord.getUserId());
        } catch (Exception e) {
            throw new ErrorException("该记录没有选择被任职人员,无法导出上岗证");
        }
        //姓名
        User user = userMapper.selectById(incumbentUserId);
        if (user == null) {
            throw new ErrorException("被任职人员不存在或已被删除,无法导出上岗证");
        }
        String name = user.getName();
        //身份证号
        PersonBasicInfo basicInfo = personBasicInfoMapper.selectOne(Wrappers.<PersonBasicInfo>lambdaQuery()
                .eq(PersonBasicInfo::getUserId, incumbentUserId)
                .last("limit 1"));
        if (basicInfo == null || StringUtils.isBlank(basicInfo.getIdentityCard())) {
            throw new ErrorException(name + "还没有在人员基本信息中填写身份证号,无法导出上岗证");
        }
        String identityCard = basicInfo.getIdentityCard();
        //证书编号
        String code = personPostAuthorizationRecord.getCertificateNumber();
        if (StringUtils.isBlank(code)) {
            throw new ErrorException("该记录还没有填写证书编号,无法导出上岗证");
        }
        //任职岗位
        String post = personPostAuthorizationRecord.getPost();
        String finalPost = StringUtils.isBlank(post) ? "检测" : post;
        //发证时间
        LocalDateTime createTime = personPostAuthorizationRecord.getCreateTime();
        if (createTime == null) {
            createTime = LocalDateTime.now();
        }
        String year = createTime.getYear() + "";
        String mon = createTime.getMonthValue() + "";
        String day = createTime.getDayOfMonth() + "";
        //个人照片
        if (ObjectUtils.isEmpty(user.getPictureUrl())) {
            throw new ErrorException(name + "的个人照片没有上传,无法导出上岗证");
        }
        String pictureUrl = user.getPictureUrl();
        if (!new File(imgUrl + "/" + pictureUrl).exists()) {
            throw new ErrorException(name + "的个人照片文件不存在,请重新上传");
        }
 
        InputStream inputStream = this.getClass().getResourceAsStream("/static/credentials-deal.docx");
        if (inputStream == null) {
            throw new ErrorException("上岗证导出模板不存在");
        }
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("name", name);
                    put("identityCard", identityCard);
                    put("code", code);
                    put("post", finalPost);
                    put("year", year);
                    put("mon", mon);
                    put("day", day);
                    put("writeUrl", new FilePictureRenderData(100, 140, imgUrl + "/" + pictureUrl));
                }});
 
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    name + "的检测人员上岗证", "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("导出失败");
        }
 
    }
}