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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.yuanchu.mom.dto.PersonSupervisionRecordDto;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.PersonSupervisionControlSheet;
import com.yuanchu.mom.pojo.PersonSupervisionProcessingSheet;
import com.yuanchu.mom.pojo.PersonSupervisionRecord;
import com.yuanchu.mom.mapper.PersonSupervisionRecordMapper;
import com.yuanchu.mom.service.PersonSupervisionControlSheetService;
import com.yuanchu.mom.service.PersonSupervisionProcessingSheetService;
import com.yuanchu.mom.service.PersonSupervisionRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.utils.DateImageUtil;
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 javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
 
/**
 * <p>
 * 监督记录 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-10 04:16:49
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PersonSupervisionRecordServiceImpl extends ServiceImpl<PersonSupervisionRecordMapper, PersonSupervisionRecord> implements PersonSupervisionRecordService {
 
    @Autowired
    private PersonSupervisionControlSheetService personSupervisionControlSheetService;
 
    @Autowired
    private PersonSupervisionProcessingSheetService personSupervisionProcessingSheetService;
 
    @Autowired
    private UserMapper userMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
    @Override
    public void deletePersonSupervisionRecord(List<Integer> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            ids.forEach(id -> {
                personSupervisionControlSheetService.remove(Wrappers.<PersonSupervisionControlSheet>lambdaQuery()
                        .eq(PersonSupervisionControlSheet::getSupervisionRecordId, id));
                personSupervisionProcessingSheetService.remove(Wrappers.<PersonSupervisionProcessingSheet>lambdaQuery()
                        .eq(PersonSupervisionProcessingSheet::getSupervisionRecordId, id));
                baseMapper.deleteById(id);
            });
        }
    }
 
    @Override
    public IPage<PersonSupervisionRecordDto> personSupervisionRecordPage(Page page, Integer userId, String userName, Integer departLimsId) {
        return baseMapper.personSupervisionRecordPage(page, userId, departLimsId, userName);
    }
 
    /**
     * 导出人员监督记录
     * @param id
     * @param response
     */
    @Override
    public void exportPersonSupervisionRecord(Integer id, HttpServletResponse response) {
        PersonSupervisionRecordDto recordDto =  baseMapper.selectPersonSupervisionRecord(id);
 
        //获取技术负责人的签名地址
        String ratifyUrl = null;
        if (recordDto.getTechnicalDirector() != null) {
            ratifyUrl = userMapper.selectById(recordDto.getTechnicalDirector()).getSignatureUrl();
            if (StringUtils.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到技术负责人的签名");
            }
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/supervision-record.docx");
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        String finalRatifyUrl = ratifyUrl;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("supervision", recordDto);
                    put("technicalDirectorUrl", StringUtils.isNotBlank(finalRatifyUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create() : null);
                    put("technicalDirectorDateUrl", recordDto.getTechnicalDirectorDate() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(recordDto.getTechnicalDirectorDate())).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("导出失败");
        }
 
 
    }
}