zhuo
2025-05-20 61f34419e094f2363276d614a960b379cc456482
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
package com.ruoyi.manage.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.data.Pictures;
import com.ruoyi.common.core.domain.entity.User;
import com.ruoyi.common.utils.DateImageUtil;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.util.HackLoopTableRenderPolicy;
import com.ruoyi.manage.dto.InternalCheckDto;
import com.ruoyi.manage.mapper.InternalCheckMapper;
import com.ruoyi.manage.pojo.InternalCheck;
import com.ruoyi.manage.pojo.InternalCheckDetail;
import com.ruoyi.manage.service.InternalCheckDetailService;
import com.ruoyi.manage.service.InternalCheckService;
import com.ruoyi.system.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
 
/**
 * 内审检查表
 *
 * @author zhuo
 * @since 2024-11-11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InternalCheckServiceImpl extends ServiceImpl<InternalCheckMapper, InternalCheck> implements InternalCheckService {
 
    @Resource
    private InternalCheckDetailService internalCheckDetailService;
    @Resource
    private UserMapper userMapper;
    @Value("${file.path}")
    private String imgUrl;
 
    /**
     * 内审检查分页查询
     * @param page
     * @param internalCheck
     * @return
     */
    @Override
    public IPage<InternalCheckDto> pageInternalCheck(Page page, InternalCheck internalCheck) {
        return baseMapper.pageInternalCheck(page, QueryWrappers.queryWrappers(internalCheck));
    }
 
    /**
     * 内审检查新增
     * @param internalCheck
     * @return
     */
    @Override
    public boolean addInternalCheck(InternalCheckDto internalCheck) {
        Integer userId = SecurityUtils.getUserId().intValue();
        // 添加编制人
        User user = userMapper.selectById(userId);
        internalCheck.setWriteTime(LocalDateTime.now());
        internalCheck.setWriteUserId(user.getId());
        internalCheck.setWriteUserName(user.getName());
 
        baseMapper.insert(internalCheck);
        // 新增详情
        for (InternalCheckDetail internalCheckDetail : internalCheck.getCheckDetailList()) {
            internalCheckDetail.setCheckId(internalCheck.getCheckId());
        }
        internalCheckDetailService.saveBatch(internalCheck.getCheckDetailList());
        return true;
    }
 
    /**
     * 内审检查修改
     * @param internalCheck
     * @return
     */
    @Override
    public boolean updateInternalCheck(InternalCheckDto internalCheck) {
        baseMapper.updateById(internalCheck);
 
        // 删除之前的详情
        internalCheckDetailService.remove(Wrappers.<InternalCheckDetail>lambdaQuery()
                .eq(InternalCheckDetail::getCheckId, internalCheck.getCheckId()));
 
        // 新增详情
        for (InternalCheckDetail internalCheckDetail : internalCheck.getCheckDetailList()) {
            internalCheckDetail.setCheckId(internalCheck.getCheckId());
        }
        internalCheckDetailService.saveBatch(internalCheck.getCheckDetailList());
 
        return true;
    }
 
    /**
     * 内审检查删除
     * @param CheckId
     * @return
     */
    @Override
    public boolean delInternalCheck(Integer CheckId) {
        internalCheckDetailService.remove(Wrappers.<InternalCheckDetail>lambdaQuery()
                .eq(InternalCheckDetail::getCheckId, CheckId));
        baseMapper.deleteById(CheckId);
        return true;
    }
 
    /**
     * 内审检查查看详情
     * @param CheckId
     * @return
     */
    @Override
    public InternalCheckDto getInternalCheckOne(Integer CheckId) {
        InternalCheck internalCheck = baseMapper.selectById(CheckId);
        InternalCheckDto internalCheckDto = new InternalCheckDto();
        BeanUtils.copyProperties(internalCheck, internalCheckDto);
 
        // 查询详细信息
        internalCheckDto.setCheckDetailList(internalCheckDetailService.list(Wrappers.<InternalCheckDetail>lambdaQuery()
                .eq(InternalCheckDetail::getCheckId, CheckId)));
        return internalCheckDto;
    }
 
    /**
     * 内审检查批准
     * @param internalCheck
     * @return
     */
    @Override
    public boolean ratifyInternalCheck(InternalCheckDto internalCheck) {
        Integer userId = SecurityUtils.getUserId().intValue();
        User user = userMapper.selectById(userId);
        baseMapper.update(null, Wrappers.<InternalCheck>lambdaUpdate()
                .eq(InternalCheck::getCheckId, internalCheck.getCheckId())
                .set(InternalCheck::getRatifyUserId, userId)
                .set(InternalCheck::getRatifyUserName, user.getName())
                .set(InternalCheck::getRatifyRemark, internalCheck.getRatifyRemark())
                .set(InternalCheck::getRatifyStatus, internalCheck.getRatifyStatus())
                .set(InternalCheck::getRatifyTime, LocalDateTime.now())
        );
        return true;
    }
 
    /**
     * 导出内审检查
     * @param checkId
     * @param response
     */
    @Override
    public void exportInternalCheck(Integer checkId, HttpServletResponse response) {
        InternalCheck internalCheck = baseMapper.selectById(checkId);
 
        //获取提交人的签名地址
        String writeUrl = userMapper.selectById(internalCheck.getWriteUserId()).getSignatureUrl();
        if (ObjectUtils.isEmpty(writeUrl) || writeUrl.equals("")) {
            throw new ErrorException("找不到检验人的签名");
        }
 
        //获取批准人的签名地址
        String ratifyUrl = null;
        if (internalCheck.getRatifyUserId() != null) {
            ratifyUrl = userMapper.selectById(internalCheck.getRatifyUserId()).getSignatureUrl();
            if (StringUtils.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到复核人的签名");
            }
        }
 
        // 查询详情
        List<InternalCheckDetail> internalCheckDetails = internalCheckDetailService.list(Wrappers.<InternalCheckDetail>lambdaQuery()
                .eq(InternalCheckDetail::getCheckId, checkId));
 
        int index = 1;
        for (InternalCheckDetail detail : internalCheckDetails) {
            detail.setIndex(index);
            index++;
        }
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/internal-check.docx");
        String finalRatifyUrl = ratifyUrl;
        Configure configure = Configure.builder()
                .bind("checkDetailList", new HackLoopTableRenderPolicy())
                .build();
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("check", internalCheck);
                    put("checkDetailList", internalCheckDetails);
                    put("writeUrl", StringUtils.isNotBlank(writeUrl) ? Pictures.ofLocal(imgUrl + "/" + writeUrl).create() : null);
                    put("ratifyUrl", StringUtils.isNotBlank(finalRatifyUrl) ? Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create() : null);
                    put("writeDateUrl", internalCheck.getWriteTime() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(internalCheck.getWriteTime())).create() : null);
                    put("ratifyDateUrl", internalCheck.getRatifyTime() != null ?
                            Pictures.ofStream(DateImageUtil.createDateImage(internalCheck.getRatifyTime())).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();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
 
    }
 
}