zhuo
2025-02-20 3fb8061a9fb85629885ae8383db247946ede7eaf
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package com.ruoyi.inspect.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.DateImageUtil;
import com.ruoyi.common.utils.IfsApiUtils;
import com.ruoyi.common.utils.QueryWrappers;
import com.ruoyi.framework.exception.ErrorException;
import com.ruoyi.inspect.dto.IfsStockQueryDTO;
import com.ruoyi.inspect.dto.SpotCheckQuarterDto;
import com.ruoyi.inspect.dto.SpotCheckYearDto;
import com.ruoyi.inspect.mapper.SpotCheckQuarterMapper;
import com.ruoyi.inspect.mapper.SpotCheckYearMapper;
import com.ruoyi.inspect.pojo.SpotCheckQuarter;
import com.ruoyi.inspect.pojo.SpotCheckQuarterItem;
import com.ruoyi.inspect.pojo.SpotCheckYear;
import com.ruoyi.inspect.pojo.SpotCheckYearItem;
import com.ruoyi.inspect.service.FinishProductSpotCheckService;
import com.ruoyi.inspect.service.SpotCheckQuarterItemService;
import com.ruoyi.inspect.service.SpotCheckYearItemService;
import com.ruoyi.system.mapper.UserMapper;
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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 成品抽样
 *
 * @Author zhuo
 * @Date 2024/9/29
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class FinishProductSpotCheckServiceImpl implements FinishProductSpotCheckService {
 
    @Resource
    private IfsApiUtils ifsApiUtils;
    @Resource
    private SpotCheckQuarterMapper spotCheckQuarterMapper;
    @Resource
    private SpotCheckQuarterItemService spotCheckQuarterItemService;
    @Resource
    private SpotCheckYearMapper spotCheckYearMapper;
    @Resource
    private SpotCheckYearItemService spotCheckYearItemService;
    @Value("${wordUrl}")
    private String wordUrl;
    @Value("${file.path}")
    private String imgUrl;
    @Resource
    private UserMapper userMapper;
 
 
    public Result getIfsStockReport(IfsStockQueryDTO ifsStockQueryDTO) {
        JSONObject jresult = new JSONObject();
        jresult.put("total", 0);
        jresult.put("count", 0);
        jresult.put("data", new JSONArray());
        JSONObject jsonObject = new JSONObject();
        generateIfsStockQueryParams(jsonObject, ifsStockQueryDTO, ifsStockQueryDTO.getPage(), ifsStockQueryDTO.getLimit());
        Result result = ifsApiUtils.getIfsStock(jsonObject.toJSONString());
        if (result.getCode() == 200) {
            JSONObject data = (JSONObject) JSON.toJSON(result.getData());
            JSONArray array = new JSONArray();
            for (int i = 0; i < data.getJSONArray("LIST_INFO").size(); i++) {
                JSONObject listInfo = data.getJSONArray("LIST_INFO").getJSONObject(i);
                array.add(listInfo);
            }
            //获取总数量
            int count = data.getIntValue("TOTAL_RECORD");
            //获取页数
            int total = (int) Math.ceil((double) count / (double) ifsStockQueryDTO.getLimit());
            jresult.put("total", total);
            jresult.put("count", count);
            jresult.put("data", array);
        }
        return Result.success(jresult);
    }
 
    public void generateIfsStockQueryParams(JSONObject result, IfsStockQueryDTO ifsStockQueryDTO, Long pageNo, Long pageSize) {
        result.put("PAGE", pageNo);
        result.put("LIMIT", pageSize);
        result.put("WAREHOUSE", "成品仓库");
        result.put("QUANTITY_FLAG", ">0");
        //表里面的OTC行号
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getCotcOrderNo())) {
            result.put("C_OTC_ORDER_NO", ifsStockQueryDTO.getCotcOrderNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getCotcLineNo())) {
            result.put("C_OTC_LINE_NO", ifsStockQueryDTO.getCotcLineNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getPartNo())) {
            result.put("PART_NO", ifsStockQueryDTO.getPartNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getPartDescription())) {
            result.put("PART_DESC", "%" + ifsStockQueryDTO.getPartDescription() + "%");
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLocNo())) {
            result.put("LOCATION_NO", ifsStockQueryDTO.getLocNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLocDesc())) {
            result.put("LOCATION_DESC", ifsStockQueryDTO.getLocDesc());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLotBatchNo())) {
            result.put("LOT_BATCH_NO", ifsStockQueryDTO.getLotBatchNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getWdrNo())) {
            result.put("WDR_NO", ifsStockQueryDTO.getWdrNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getWarehouse())) {
            result.put("WAREHOUSE", ifsStockQueryDTO.getWarehouse());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLocationGroup())) {
            result.put("LOCATION_GROUP", ifsStockQueryDTO.getLocationGroup());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLocationGroupDesc())) {
            result.put("LOCATION_GROUP_DESC", ifsStockQueryDTO.getLocationGroupDesc());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getReelNumber())) {
            result.put("ATTR1", ifsStockQueryDTO.getReelNumber());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getStartMeter())) {
            result.put("ATTR2", ifsStockQueryDTO.getStartMeter());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getEndMeter())) {
            result.put("ATTR3", ifsStockQueryDTO.getEndMeter());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getOuterColor())) {
            result.put("ATTR4", ifsStockQueryDTO.getOuterColor());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getInsulationColor())) {
            result.put("ATTR5", ifsStockQueryDTO.getInsulationColor());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getOtcOrderNo())) {
            result.put("ATTR6", ifsStockQueryDTO.getOtcOrderNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getMpsNo())) {
            result.put("ATTR7", ifsStockQueryDTO.getMpsNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLetteringInfo())) {
            result.put("ATTR8", ifsStockQueryDTO.getLetteringInfo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getSStockQuantity())) {
            result.put("ATTR9", ifsStockQueryDTO.getSStockQuantity());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getSAvailableStockQuantity())) {
            result.put("ATTR10", ifsStockQueryDTO.getSAvailableStockQuantity());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getSunit())) {
            result.put("ATTR11", ifsStockQueryDTO.getSunit());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getStockSource())) {
            result.put("ATTR12", ifsStockQueryDTO.getStockSource());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getInspInstorageTime())) {
            result.put("ATTR13", ifsStockQueryDTO.getInspInstorageTime());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getTestRemark())) {
            result.put("ATTR14", ifsStockQueryDTO.getTestRemark());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getGrossWeight())) {
            result.put("ATTR15", ifsStockQueryDTO.getGrossWeight());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getNetWeight())) {
            result.put("ATTR16", ifsStockQueryDTO.getNetWeight());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getPackingManner())) {
            result.put("ATTR17", ifsStockQueryDTO.getPackingManner());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getCylinderNumber())) {
            result.put("ATTR18", ifsStockQueryDTO.getCylinderNumber());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getRemark())) {
            result.put("ATTR19", ifsStockQueryDTO.getRemark());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getLengthRequirement())) {
            result.put("ATTR20", ifsStockQueryDTO.getLengthRequirement());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getCustomerName())) {
            result.put("ATTR21", ifsStockQueryDTO.getCustomerName());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getSystemNo())) {
            result.put("ATTR22", ifsStockQueryDTO.getSystemNo());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getInSource())) {
            result.put("ATTR23", ifsStockQueryDTO.getInSource());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getSplitQuality())) {
            result.put("ATTR24", ifsStockQueryDTO.getSplitQuality());
        }
        if (StrUtil.isNotBlank(ifsStockQueryDTO.getReserveQuantity())) {
            result.put("QTY_RESERVED", ifsStockQueryDTO.getReserveQuantity());
        }
    }
 
    /************************************************* 季度抽样 ***********************************************************/
 
    /**
     * 新增季度抽检
     * @param spotCheckQuarterDto
     * @return
     */
    @Override
    public boolean addQuarter(SpotCheckQuarterDto spotCheckQuarterDto) {
        // 根据编号查询之前的
        SpotCheckQuarter spotCheckQuarter = spotCheckQuarterMapper.selectOne(Wrappers.<SpotCheckQuarter>lambdaQuery()
                .eq(SpotCheckQuarter::getQuarterNo, spotCheckQuarterDto.getQuarterNo())
                .orderByDesc(SpotCheckQuarter::getCreateTime)
                .last("limit 1"));
        if (spotCheckQuarter == null) {
            spotCheckQuarterMapper.insert(spotCheckQuarterDto);
            // 添加详情
            for (SpotCheckQuarterItem quarterItem : spotCheckQuarterDto.getQuarterItems()) {
                quarterItem.setQuarterId(spotCheckQuarterDto.getQuarterId());
            }
            spotCheckQuarterItemService.saveBatch(spotCheckQuarterDto.getQuarterItems());
        } else {
            // 累加详情
            for (SpotCheckQuarterItem quarterItem : spotCheckQuarterDto.getQuarterItems()) {
                quarterItem.setQuarterId(spotCheckQuarter.getQuarterId());
            }
            spotCheckQuarterItemService.saveBatch(spotCheckQuarterDto.getQuarterItems());
        }
 
        return true;
    }
 
    /**
     * 查询季度抽样详情
     * @param quarterId
     * @return
     */
    @Override
    public SpotCheckQuarterDto getQuarter(Integer quarterId) {
        SpotCheckQuarter spotCheckQuarter = spotCheckQuarterMapper.selectById(quarterId);
 
        // 查询详情
        List<SpotCheckQuarterItem> list = spotCheckQuarterItemService.list(Wrappers.<SpotCheckQuarterItem>lambdaQuery()
                .eq(SpotCheckQuarterItem::getQuarterId, spotCheckQuarter.getQuarterId()));
        SpotCheckQuarterDto spotCheckQuarterDto = new SpotCheckQuarterDto();
        BeanUtil.copyProperties(spotCheckQuarter, spotCheckQuarterDto);
        spotCheckQuarterDto.setQuarterItems(list);
 
        return spotCheckQuarterDto;
    }
 
    /**
     * 季度抽样分页查询
     * @param page
     * @param spotCheckQuarter
     * @return
     */
    @Override
    public IPage<SpotCheckQuarterDto> getQuarterPage(Page page, SpotCheckQuarterDto spotCheckQuarter) {
        return spotCheckQuarterMapper.getQuarterPage(page, QueryWrappers.queryWrappers(spotCheckQuarter));
    }
 
    /**
     * 删除季度抽样
     * @param quarterId
     * @return
     */
    @Override
    public boolean deleteQuarter(Integer quarterId) {
        spotCheckQuarterMapper.deleteById(quarterId);
        spotCheckQuarterItemService.remove(Wrappers.<SpotCheckQuarterItem>lambdaQuery()
                .eq(SpotCheckQuarterItem::getQuarterId, quarterId));
        return true;
    }
 
    /**
     * 成品下单界面查询季度信息
     * @return
     */
    @Override
    public List<Map<String, Object>> getQuarterOnOrder() {
        List<Map<String, Object>> quarterOnOrder = spotCheckQuarterMapper.getQuarterOnOrder();
        for (Map<String, Object> stringMap : quarterOnOrder) {
            List<SpotCheckQuarterItem> list = spotCheckQuarterItemService.list(Wrappers.<SpotCheckQuarterItem>lambdaQuery()
                    .eq(SpotCheckQuarterItem::getQuarterId, (Integer) stringMap.get("value")));
            List<Map<String, Object>> itemMaps = list.stream().map(spotCheckQuarterItem -> {
                Map<String, Object> map = new HashMap<>();
                map.put("value", spotCheckQuarterItem.getQuarterItemId());
                map.put("label", spotCheckQuarterItem.getProductModel() + " "
                        + spotCheckQuarterItem.getProductType() + " "
                        + spotCheckQuarterItem.getSpotCheckNumber());
                return map;
            }).collect(Collectors.toList());
            stringMap.put("children", itemMaps);
 
        }
 
        return quarterOnOrder;
    }
 
    /**
     * 修改季度检验
     * @param spotCheckQuarterDto
     * @return
     */
    @Override
    public boolean updateQuarterOnOrder(SpotCheckQuarterDto spotCheckQuarterDto) {
        if (CollectionUtils.isNotEmpty(spotCheckQuarterDto.getQuarterItems())) {
            // 清除之前详情
            spotCheckQuarterItemService.remove(Wrappers.<SpotCheckQuarterItem>lambdaQuery()
                    .eq(SpotCheckQuarterItem::getQuarterId, spotCheckQuarterDto.getQuarterId()));
            // 添加详情
            for (SpotCheckQuarterItem quarterItem : spotCheckQuarterDto.getQuarterItems()) {
                quarterItem.setQuarterId(spotCheckQuarterDto.getQuarterId());
            }
            spotCheckQuarterItemService.saveBatch(spotCheckQuarterDto.getQuarterItems());
        }
 
        spotCheckQuarterMapper.updateById(spotCheckQuarterDto);
        return true;
    }
 
    /**
     * 生成最终报告
     * @param quarterId
     * @return
     */
    @Override
    public boolean finalReportQuarter(Integer quarterId, HttpServletResponse response) {
        // 添加报告信息
        SpotCheckQuarter spotCheckQuarter = spotCheckQuarterMapper.selectById(quarterId);
 
        // 查询详情
        List<SpotCheckQuarterItem> itemList = spotCheckQuarterItemService.list(Wrappers.<SpotCheckQuarterItem>lambdaQuery()
                .eq(SpotCheckQuarterItem::getQuarterId, spotCheckQuarter.getQuarterId()));
 
        for (SpotCheckQuarterItem spotCheckQuarterItem : itemList) {
            // 拼接型号和数量
            spotCheckQuarterItem.setSpotCheckNumber((StringUtils.isBlank(spotCheckQuarterItem.getProductModel()) ? "" : spotCheckQuarterItem.getProductModel()) +
                    (StringUtils.isBlank(spotCheckQuarterItem.getSpotCheckNumber()) ? "" : spotCheckQuarterItem.getSpotCheckNumber()));
        }
 
        //编制人
        String writeUrl = null;
        if (spotCheckQuarter.getWriteUser() != null) {
            try {
                writeUrl = userMapper.selectById(spotCheckQuarter.getWriteUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到编制人的签名");
            }
            if (StrUtil.isBlank(writeUrl)) {
                throw new ErrorException("找不到编制人的签名");
            }
        }
 
        // 初始化所有的 countersignUrl 变量
        String[] countersignUrls = new String[7];
 
        // 如果 spotCheckQuarter.getCountersignUser() 不是空或空白字符串
        if (StrUtil.isNotBlank(spotCheckQuarter.getCountersignUser())) {
            String[] split = spotCheckQuarter.getCountersignUser().split(",");
 
            // 遍历 split 数组,并将元素依次赋值给 countersignUrls 数组
            for (int i = 0; i < Math.min(split.length, countersignUrls.length); i++) {
 
                String countersignUrl = null;
                try {
                    countersignUrl = userMapper.selectById(Integer.parseInt(split[i])).getSignatureUrl();
                } catch (Exception e) {
                    throw new ErrorException("找不到编制人的签名");
                }
                if (StrUtil.isBlank(countersignUrl)) {
                    throw new ErrorException("找不到编制人的签名");
                }
 
                countersignUrls[i] = countersignUrl;
            }
        }
 
        // 将 countersignUrls 数组中的值依次赋值给 countersignUrl1 到 countersignUrl7
        String countersignUrl1 = countersignUrls[0];
        String countersignUrl2 = countersignUrls[1];
        String countersignUrl3 = countersignUrls[2];
        String countersignUrl4 = countersignUrls[3];
        String countersignUrl5 = countersignUrls[4];
        String countersignUrl6 = countersignUrls[5];
        String countersignUrl7 = countersignUrls[6];
 
        //审核人
        String examineUrl = null;
        if (spotCheckQuarter.getExamineUser() != null) {
            try {
                examineUrl = userMapper.selectById(spotCheckQuarter.getExamineUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到审核人的签名");
            }
            if (StrUtil.isBlank(examineUrl)) {
                throw new ErrorException("找不到审核人的签名");
            }
        }
 
        //批准人
        String ratifyUrl = null;
        if (spotCheckQuarter.getRatifyUser() != null) {
            try {
                ratifyUrl = userMapper.selectById(spotCheckQuarter.getRatifyUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到批准人的签名");
            }
            if (StrUtil.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到批准人的签名");
            }
        }
 
        String finalWriteUrl = writeUrl;
        String finalExamineUrl = examineUrl;
        String finalRatifyUrl = ratifyUrl;
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/spot_check_quarter.docx");
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
 
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("quarter", spotCheckQuarter);
                    put("quarterItems", itemList);
                    put("writeUrl", StrUtil.isBlank(finalWriteUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalWriteUrl).create());
                    put("countersignUrl1", StrUtil.isBlank(countersignUrl1) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl1).create());
                    put("countersignUrl2", StrUtil.isBlank(countersignUrl2) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl2).create());
                    put("countersignUrl3", StrUtil.isBlank(countersignUrl3) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl3).create());
                    put("countersignUrl4", StrUtil.isBlank(countersignUrl4) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl4).create());
                    put("countersignUrl5", StrUtil.isBlank(countersignUrl5) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl5).create());
                    put("countersignUrl6", StrUtil.isBlank(countersignUrl6) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl6).create());
                    put("countersignUrl7", StrUtil.isBlank(countersignUrl7) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl7).create());
                    put("examineUrl", StrUtil.isBlank(finalExamineUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalExamineUrl).create());
                    put("ratifyUrl", StrUtil.isBlank(finalRatifyUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create());
                    put("createDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(spotCheckQuarter.getCreateTime())).create());
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    spotCheckQuarter.getQuarterNo() + "抽样计划", "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("导出失败");
        }
        return true;
    }
 
 
    /******************************************************* 年度抽样  *************************************************************/
 
    @Override
    public boolean addSpotCheckYear(SpotCheckYearDto spotCheckYearDto) {
        spotCheckYearMapper.insert(spotCheckYearDto);
        // 详情
        for (SpotCheckYearItem yearItem : spotCheckYearDto.getYearItems()) {
            yearItem.setYearId(spotCheckYearDto.getYearId());
        }
        spotCheckYearItemService.saveBatch(spotCheckYearDto.getYearItems());
        return true;
    }
 
    @Override
    public SpotCheckYearDto getSpotCheckYear(Integer yearId) {
        SpotCheckYear spotCheckYear = spotCheckYearMapper.selectById(yearId);
 
        // 查询详情
        List<SpotCheckYearItem> list = spotCheckYearItemService.list(Wrappers.<SpotCheckYearItem>lambdaQuery()
                .eq(SpotCheckYearItem::getYearId, spotCheckYear.getYearId()));
        SpotCheckYearDto spotCheckYearDto = new SpotCheckYearDto();
        BeanUtil.copyProperties(spotCheckYear, spotCheckYearDto);
        spotCheckYearDto.setYearItems(list);
 
        return spotCheckYearDto;
    }
 
    @Override
    public IPage<SpotCheckYearDto> getSpotCheckYearPage(Page page, SpotCheckYearDto spotCheckYear) {
        return spotCheckYearMapper.getSpotCheckYearPage(page, QueryWrappers.queryWrappers(spotCheckYear));
    }
 
    @Override
    public boolean deleteSpotCheckYear(Integer yearId) {
        spotCheckYearMapper.deleteById(yearId);
        spotCheckYearItemService.remove(Wrappers.<SpotCheckYearItem>lambdaQuery()
                .eq(SpotCheckYearItem::getYearId, yearId));
        return true;
    }
 
    /**
     * 修改年度检验
     * @param spotCheckYearDto
     * @return
     */
    @Override
    public boolean updateSpotCheckYear(SpotCheckYearDto spotCheckYearDto) {
        spotCheckYearMapper.updateById(spotCheckYearDto);
 
        // 删除详情
        spotCheckYearItemService.remove(Wrappers.<SpotCheckYearItem>lambdaQuery()
                .eq(SpotCheckYearItem::getYearId, spotCheckYearDto.getYearId()));
 
        // 重新新增
        for (SpotCheckYearItem yearItem : spotCheckYearDto.getYearItems()) {
            yearItem.setYearId(spotCheckYearDto.getYearId());
        }
        spotCheckYearItemService.saveBatch(spotCheckYearDto.getYearItems());
        return false;
    }
 
    @Override
    public boolean finalReportSpotCheckYear(Integer yearId, HttpServletResponse response) {
        SpotCheckYear spotCheckYear = spotCheckYearMapper.selectById(yearId);
 
        // 查询详情
        List<SpotCheckYearItem> itemList = spotCheckYearItemService.list(Wrappers.<SpotCheckYearItem>lambdaQuery()
                .eq(SpotCheckYearItem::getYearId, spotCheckYear.getYearId()));
 
        //编制人
        String writeUrl = null;
        if (spotCheckYear.getWriteUser() != null) {
            try {
                writeUrl = userMapper.selectById(spotCheckYear.getWriteUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到编制人的签名");
            }
            if (StrUtil.isBlank(writeUrl)) {
                throw new ErrorException("找不到编制人的签名");
            }
        }
 
        // 初始化所有的 countersignUrl 变量
        String[] countersignUrls = new String[7];
 
        // 如果不是空或空白字符串
        if (StrUtil.isNotBlank(spotCheckYear.getCountersignUser())) {
            String[] split = spotCheckYear.getCountersignUser().split(",");
 
            // 遍历 split 数组,并将元素依次赋值给 countersignUrls 数组
            for (int i = 0; i < Math.min(split.length, countersignUrls.length); i++) {
 
                String countersignUrl = null;
                try {
                    countersignUrl = userMapper.selectById(Integer.parseInt(split[i])).getSignatureUrl();
                } catch (Exception e) {
                    throw new ErrorException("找不到编制人的签名");
                }
                if (StrUtil.isBlank(countersignUrl)) {
                    throw new ErrorException("找不到编制人的签名");
                }
 
                countersignUrls[i] = countersignUrl;
            }
        }
 
        // 将 countersignUrls 数组中的值依次赋值给 countersignUrl1 到 countersignUrl7
        String countersignUrl1 = countersignUrls[0];
        String countersignUrl2 = countersignUrls[1];
        String countersignUrl3 = countersignUrls[2];
        String countersignUrl4 = countersignUrls[3];
        String countersignUrl5 = countersignUrls[4];
        String countersignUrl6 = countersignUrls[5];
        String countersignUrl7 = countersignUrls[6];
 
        //审核人
        String examineUrl = null;
        if (spotCheckYear.getExamineUser() != null) {
            try {
                examineUrl = userMapper.selectById(spotCheckYear.getExamineUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到审核人的签名");
            }
            if (StrUtil.isBlank(examineUrl)) {
                throw new ErrorException("找不到审核人的签名");
            }
        }
 
        //批准人
        String ratifyUrl = null;
        if (spotCheckYear.getRatifyUser() != null) {
            try {
                ratifyUrl = userMapper.selectById(spotCheckYear.getRatifyUser()).getSignatureUrl();
            } catch (Exception e) {
                throw new ErrorException("找不到批准人的签名");
            }
            if (StrUtil.isBlank(ratifyUrl)) {
                throw new ErrorException("找不到批准人的签名");
            }
        }
 
        String finalWriteUrl = writeUrl;
        String finalExamineUrl = examineUrl;
        String finalRatifyUrl = ratifyUrl;
 
        // 获取路径
        InputStream inputStream = this.getClass().getResourceAsStream("/static/spot_check_year.docx");
        ConfigureBuilder builder = Configure.builder();
        builder.useSpringEL(true);
        XWPFTemplate template = XWPFTemplate.compile(inputStream, builder.build()).render(
                new HashMap<String, Object>() {{
                    put("year", spotCheckYear);
                    put("yearItems", itemList);
                    put("writeUrl", StrUtil.isBlank(finalWriteUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalWriteUrl).create());
                    put("countersignUrl1", StrUtil.isBlank(countersignUrl1) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl1).create());
                    put("countersignUrl2", StrUtil.isBlank(countersignUrl2) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl2).create());
                    put("countersignUrl3", StrUtil.isBlank(countersignUrl3) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl3).create());
                    put("countersignUrl4", StrUtil.isBlank(countersignUrl4) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl4).create());
                    put("countersignUrl5", StrUtil.isBlank(countersignUrl5) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl5).create());
                    put("countersignUrl6", StrUtil.isBlank(countersignUrl6) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl6).create());
                    put("countersignUrl7", StrUtil.isBlank(countersignUrl7) ? null :
                            Pictures.ofLocal(imgUrl + "/" + countersignUrl7).create());
                    put("examineUrl", StrUtil.isBlank(finalExamineUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalExamineUrl).create());
                    put("ratifyUrl", StrUtil.isBlank(finalRatifyUrl) ? null :
                            Pictures.ofLocal(imgUrl + "/" + finalRatifyUrl).create());
                    put("createDateUrl", Pictures.ofStream(DateImageUtil.createDateImage(spotCheckYear.getCreateTime())).create());
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode(
                    spotCheckYear.getYearHead(), "UTF-8");
            response.setHeader("Content-disposition",
                    "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
 
 
}