zss
2025-04-22 dfd3d024da6b3966d11d6e8619ce0fb19a58264b
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
233
234
235
236
237
238
239
240
package com.ruoyi.process.service.impl;
 
import cn.hutool.core.lang.UUID;
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.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.*;
import com.ruoyi.common.core.domain.entity.User;
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.process.mapper.InspectionOrderMapper;
import com.ruoyi.process.mapper.ProcessDealMapper;
import com.ruoyi.process.mapper.ProcessSampleMapper;
import com.ruoyi.process.mapper.ProcessTotaldealMapper;
import com.ruoyi.process.pojo.InspectionOrder;
import com.ruoyi.process.pojo.ProcessDeal;
import com.ruoyi.process.pojo.ProcessSample;
import com.ruoyi.process.pojo.ProcessTotaldeal;
import com.ruoyi.process.service.ProcessDealService;
import com.ruoyi.process.service.ProcessTotaldealService;
import com.ruoyi.system.mapper.UserMapper;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.SAAJMetaFactory;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 检测或校准物品的处置总表(历史) 服务实现类
 * </p>
 *
 * @author
 * @since 2024-11-02 03:59:09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProcessTotaldealServiceImpl extends ServiceImpl<ProcessTotaldealMapper, ProcessTotaldeal> implements ProcessTotaldealService {
 
    @Resource
    private ProcessTotaldealMapper processTotaldealMapper;
 
    @Resource
    private ProcessSampleMapper processSampleMapper;
 
    @Resource
    private ProcessDealService processDealService;
 
    @Resource
    private InspectionOrderMapper inspectionOrderMapper;
 
   @Resource
   private ProcessDealMapper processDealMapper;
 
    @Value("${file.path}")
    private String imgUrl;
 
 
    @Resource
    private UserMapper userMapper;
 
    @Override
    public IPage<ProcessTotaldeal> pageProcessTotaldeal(Page page, ProcessTotaldeal processTotaldeal) {
        return processTotaldealMapper.pageProcessTotaldeal(page, QueryWrappers.queryWrappers(processTotaldeal));
 
    }
 
    @Override
    public int submitProcessTotaldeal(Integer id) {
        Integer userId = SecurityUtils.getUserId().intValue();
        User user = userMapper.selectById(userId);
        if (ObjectUtils.isEmpty(user.getSignatureUrl())) {
            throw new ErrorException("未找到填表人的电子签名,请上传自己的电子签名!");
        }
        ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
        processTotaldeal.setSubmitUser(userId);
        processTotaldeal.setSubmitState("已提交");
        return processTotaldealMapper.updateById(processTotaldeal);
    }
 
    @Override
    public int checkProcessTotaldeal(Integer id, String state) {
        Integer userId = SecurityUtils.getUserId().intValue();
        User user = userMapper.selectById(userId);
        if (ObjectUtils.isEmpty(user.getSignatureUrl())) {
            throw new ErrorException("未找到审核人的电子签名,请上传自己的电子签名!");
        }
        ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
        processTotaldeal.setExamineUser(userId);
        processTotaldeal.setExamineState(state);
        if (state.equals("不通过")) {
            processTotaldeal.setSubmitState("待提交");
        }
        return processTotaldealMapper.updateById(processTotaldeal);
    }
 
    @Override
    public int ratifyProcessTotaldeal(Integer id, String state) {
        Integer userId = SecurityUtils.getUserId().intValue();
        User user = userMapper.selectById(userId);
        if (ObjectUtils.isEmpty(user.getSignatureUrl())) {
            throw new ErrorException("未找到批准人的电子签名,请上传自己的电子签名!");
        }
        ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
        processTotaldeal.setRatifyUser(userId);
        processTotaldeal.setRatifyState(state);
        if (state.equals("不通过")) {
            processTotaldeal.setExamineState(state);
            processTotaldeal.setSubmitState("待提交");
        }
        return processTotaldealMapper.updateById(processTotaldeal);
    }
 
    @Override
    public int addProcessTotaldeal(String month) {
        //新增总表
        ProcessTotaldeal processTotaldeal = new ProcessTotaldeal();
        List<ProcessTotaldeal> processTotaldeals = processTotaldealMapper.selectList(Wrappers.<ProcessTotaldeal>lambdaQuery()
                .eq(ProcessTotaldeal::getMonth, month));
        if (processTotaldeals.size()<=0) {
            processTotaldeal.setMonth(month);//月份
            processTotaldeal.setSubmitState("待提交");
            processTotaldealMapper.insert(processTotaldeal);
        }else {
            processTotaldeal=processTotaldeals.get(0);
        }
        //新增详情表
        //1.获取该月份的第一天和最后一天
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        YearMonth yearMonth = YearMonth.parse(month, formatter);
        LocalDate firstDay = yearMonth.atDay(1);
        LocalDate lastDay = yearMonth.atEndOfMonth();
        //2.查询在这个时间范围内的所有样品登记的数据
        List<ProcessSample> processSamples = processSampleMapper.selectList(Wrappers.<ProcessSample>lambdaQuery()
                .between(ProcessSample::getLeaveDate, firstDay, lastDay));
        if (processSamples.size()>0){
            //获取处理方式
            InspectionOrder inspectionOrder = inspectionOrderMapper.selectById(processSamples.get(0).getInspectionOrderId());
            List<ProcessDeal> processDeals = new ArrayList<>();
            for (ProcessSample processSample : processSamples) {
                long count = processDealService.count(Wrappers.<ProcessDeal>lambdaQuery().eq(ProcessDeal::getProcessSampleId, processSample.getId()));
                if (count>0){
                    continue;
                }
                ProcessDeal processDeal = new ProcessDeal();
                processDeal.setSampleName(processSample.getSampleName());//样品名称
                processDeal.setSampleCode(processSample.getSampleCode());//样品编号
                processDeal.setSampleSupplier(processSample.getSampleSupplier());//供样单位
                processDeal.setNum(processSample.getNum());//样品数量
                processDeal.setDealMethod(inspectionOrder.getProcessing()==1?"实验室处理":"委托单位取回");//处理方式
                processDeal.setDealTime(LocalDate.now());//时间=当前时间
                processDeal.setTotaldealId(processTotaldeal.getId());//总表id
                processDeal.setProcessSampleId(processSample.getId());//样品接收id
                processDeals.add(processDeal);
            }
            processDealService.saveBatch(processDeals);
        }
        return 0;
    }
 
    @Override
    public void exportProcessTotaldeal(Integer id, HttpServletResponse response) {
        List<ProcessDeal> sampleDealDtos = processDealMapper.selectDeal(id);
        ProcessTotaldeal processTotaldeal = processTotaldealMapper.selectById(id);
        String submitUserUrl="1";
        String examineUserUrl="1";
        String ratifyUserUrl="1";
        //获取提交人签名
        if (ObjectUtils.isNotNull(processTotaldeal.getSubmitUser())) {
            User user1 = userMapper.selectById(processTotaldeal.getSubmitUser());
            if (ObjectUtils.isEmpty(user1.getSignatureUrl())) throw new ErrorException("未找到提交人的电子签名,请上传自己的电子签名!");
            submitUserUrl = user1.getSignatureUrl();
        }
        //获取审核人签名
        if (ObjectUtils.isNotNull(processTotaldeal.getExamineUser())) {
            User user2 = userMapper.selectById(processTotaldeal.getExamineUser());
            if (ObjectUtils.isEmpty(user2.getSignatureUrl())) throw new ErrorException("未找到审核人的电子签名,请上传自己的电子签名!");
            examineUserUrl = user2.getSignatureUrl();
        }
        //获取批准人签名
        if (ObjectUtils.isNotNull(processTotaldeal.getRatifyUser())) {
            User user3 = userMapper.selectById(processTotaldeal.getRatifyUser());
            if (ObjectUtils.isEmpty(user3.getSignatureUrl())) throw new ErrorException("未找到批准人的电子签名,请上传自己的电子签名!");
            ratifyUserUrl = user3.getSignatureUrl();
        }
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/sample-deal.docx");
        Configure configure = Configure.builder()
                .bind("sampleList", new HackLoopTableRenderPolicy())
                .build();
        FilePictureRenderData finalSubmitUserUrl = submitUserUrl!="1" ? new FilePictureRenderData(100, 50, imgUrl + "/" + submitUserUrl) : null;
        FilePictureRenderData finalExamineUserUrl = examineUserUrl!="1" ? new FilePictureRenderData(100, 50, imgUrl + "/" + examineUserUrl) : null;
        FilePictureRenderData finalRatifyUserUrl = ratifyUserUrl!="1" ? new FilePictureRenderData(100, 50, imgUrl + "/" + ratifyUserUrl) : null;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("sampleList", sampleDealDtos);
                    put("examineUrl", finalExamineUserUrl);//审核人
                    put("ratifyUrl", finalRatifyUserUrl);//批准人
                    put("writeUrl",finalSubmitUserUrl);//提交人
                }});
        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("导出失败");
        }
    }
}