郑永安
2023-06-19 f65443d8abeaedc9d102324565e8368e7c9d90c8
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
package com.gk.firework.Service.ServiceImpl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Domain.*;
import com.gk.firework.Domain.Enum.CertificatePersonType;
import com.gk.firework.Domain.Enum.CertificateStatus;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.Utils.*;
import com.gk.firework.Domain.Utils.Properties;
import com.gk.firework.Domain.Vo.TransportArrivalVo;
import com.gk.firework.Mapper.TransportCertificateMapper;
import com.gk.firework.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
 
 
@Service("transportCertificateService")
public class TransportCertificateServiceImpl extends ServiceImpl<TransportCertificateMapper, TransportCertificate> implements TransportCertificateService {
 
    //最少行数限制
    private final static Integer rowMinSizeLimit = 12;
    private final static SimpleDateFormat  TransportDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
 
    @Autowired
    private TransportCertificateMapper transportCertificateMapper;
    @Autowired
    private TransportCertificatePersonService transportCertificatePersonService;
    @Autowired
    private TransportCertificateProductService transportCertificateProductService;
    @Autowired
    private TransportCertificateApproachService transportCertificateApproachService;
    @Autowired
    private TransportCertificateFileService transportCertificateFileService;
    @Autowired
    private TransportCertificateArrivalRecordService transportCertificateArrivalRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private EnterpriseService enterpriseService;
 
    @Override
    public IPage selectPages(Page<TransportCertificate> page, Map filter, UserInfo userInfo) {
        UserInfo user = userService.getById(userInfo.getId());
        Map<String, Object> params = new HashMap<>();
        //可见权限
        {
            params.put("enterprisenumber", user.getCompanynumber());
            params.put("province", user.getProvince());
            params.put("city", user.getCity());
            params.put("district", user.getArea());
            params.put("street", user.getTown());
            params.put("committee", user.getCommunity());
        }
        //基本
        params.put("filterProvince", filter.get("province"));
        params.put("filterCity", filter.get("city"));
        params.put("filterDistrict",filter.get("district"));
        params.put("filterStreet", filter.get("street"));
        params.put("filterCommittee", filter.get("committee"));
        params.put("code", filter.get("code"));
        params.put("enterprisename", filter.get("enterprisename"));
        params.put("page", filter.get("page"));
        params.put("tosubmmit", CertificateStatus.TOSUBMIT);
        List<TransportCertificate> list = transportCertificateMapper.selectPages(page, params);
        return page.setRecords(list);
    }
 
    /**
    * @Description: 新增运输证
    * @date 2021/4/1 13:02
    */
    @Override
    @Transactional
    public void addCertificateApply(TransportCertificate transportCertificate, UserInfo user) {
 
        UserInfo userInfo = userService.getById(user.getId());
        if (StringUtils.isBlank(userInfo.getCompanynumber())){
            throw new BusinessException("没有权限创建运输证");
        }
        //新增运输证基本信息
        {
            String code = "TC-" + System.currentTimeMillis();
            transportCertificate.setCode(code);
            transportCertificate.setCertstatus("未入库");
            transportCertificate.setCreateby(user.getId());
            transportCertificate.setCreatebyname(user.getUsername());
            transportCertificate.setEnterprisenumber(userInfo.getCompanynumber());
            transportCertificate.setCreatetime(new Date());
            transportCertificate.setValidflag(true);
            transportCertificate.setStatus(CertificateStatus.TOSUBMIT);
            this.save(transportCertificate);
        }
        //新增驾驶员和押运人
        {
 
            List<TransportCertificatePerson> person = transportCertificate.getTransportPerson();
            if (person != null && person.size() > 0) {
                for (TransportCertificatePerson p : person) {
                    p.setCertificatecode(transportCertificate.getCode());
                    p.setValidflag(true);
                }
                transportCertificatePersonService.saveBatch(person);
            }
        }
 
        //新建产品信息
        {
            List<TransportCertificateProduct> products = transportCertificate.getTransportProduct();
            if (products != null && products.size() > 0) {
                for (TransportCertificateProduct p : products) {
                    p.setCertificatecode(transportCertificate.getCode());
                    p.setArrivalnum(0);
                    p.setValidflag(true);
                }
                transportCertificateProductService.saveBatch(products);
            }
        }
        //新增途径地
 
        {
            List<TransportCertificateApproach> approach = transportCertificate.getTransportApproach();
            if (approach != null && approach.size() > 0) {
                for (TransportCertificateApproach a : approach) {
                    a.setCertificatecode(transportCertificate.getCode());
                    a.setValidflag(true);
                }
                transportCertificateApproachService.saveBatch(approach);
 
            }
        }
        //新增图片
        {
            TransportCertificateFile files = transportCertificate.getTransportFile();
            if (files != null) {
                files.setId(null);
                files.setCertificatecode(transportCertificate.getCode());
                files.setValidflag(true);
                transportCertificateFileService.save(files);
            }
        }
 
    }
 
    /**
    * @Description: 修改运输证
    * @date 2021/4/2 14:49
    */
    @Override
    @Transactional
    public void modCertificateApply(TransportCertificate transportCertificate, UserInfo user) {
 
 
        //1.判断单子状态
        TransportCertificate certificate = this.getById(transportCertificate.getId());
        if (certificate.getStatus() != CertificateStatus.TOSUBMIT){
            throw new BusinessException("非待提交状态不能修改");
        }
        //修改运输证基本信息
        {
            transportCertificate.setUpdateby(user.getId());
            transportCertificate.setUpdatebyname(user.getUsername());
            transportCertificate.setUpdatetime(new Date());
            this.updateById(transportCertificate);
        }
 
        //修改驾驶员和押运人
        {
            //1.删除所有已关联驾驶员和押运人
            transportCertificatePersonService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联驾驶员和押运人
            List<TransportCertificatePerson> person = transportCertificate.getTransportPerson();
            if (person != null && person.size() > 0) {
                for (TransportCertificatePerson p : person) {
                    p.setId(null);
                    p.setCertificatecode(transportCertificate.getCode());
                    p.setValidflag(true);
                }
                transportCertificatePersonService.saveBatch(person);
            }
        }
 
        //修改产品信息
        {
 
            //1.删除所有关联产品信息
            transportCertificateProductService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联驾驶员和押运人
            List<TransportCertificateProduct> products = transportCertificate.getTransportProduct();
            if (products != null && products.size() > 0) {
                for (TransportCertificateProduct p : products) {
                    p.setId(null);
                    p.setCertificatecode(transportCertificate.getCode());
                    p.setValidflag(true);
                }
                transportCertificateProductService.saveBatch(products);
            }
        }
 
        //修改产品信息
        {
 
            //1.删除所有关联产品信息
            transportCertificateProductService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联产品信息
            List<TransportCertificateProduct> products = transportCertificate.getTransportProduct();
            if (products != null && products.size() > 0) {
                for (TransportCertificateProduct p : products) {
                    p.setCertificatecode(transportCertificate.getCode());
                    p.setValidflag(true);
                }
                transportCertificateProductService.saveBatch(products);
            }
        }
 
        //修改途径地
        {
            //1.删除所有途径地
            transportCertificateApproachService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联途径地
            List<TransportCertificateApproach> approach = transportCertificate.getTransportApproach();
            if (approach != null && approach.size() > 0) {
                for (TransportCertificateApproach a : approach) {
                    a.setId(null);
                    a.setCertificatecode(transportCertificate.getCode());
                    a.setValidflag(true);
                }
                transportCertificateApproachService.saveBatch(approach);
            }
        }
 
        //修改途径地
        {
            //1.删除所有途径地
            transportCertificateApproachService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联途径地
            List<TransportCertificateApproach> approach = transportCertificate.getTransportApproach();
            if (approach != null && approach.size() > 0) {
                for (TransportCertificateApproach a : approach) {
                    a.setId(null);
                    a.setCertificatecode(transportCertificate.getCode());
                    a.setValidflag(true);
                }
                transportCertificateApproachService.saveBatch(approach);
 
            }
        }
 
 
        //修改图片
        {
            //1.删除所有图片
            transportCertificateFileService.deleteByCertificateCode(transportCertificate.getCode());
            //2.新增关联图片
            TransportCertificateFile files = transportCertificate.getTransportFile();
            if (files != null) {
                files.setId(null);
                files.setCertificatecode(transportCertificate.getCode());
                files.setValidflag(true);
                transportCertificateFileService.save(files);
            }
 
        }
 
    }
 
    /**
    * @Description: 删除运输证
    * @date 2021/4/2 16:16
    */
    @Override
    public void delCertificate(Long id, UserInfo user) {
        TransportCertificate certificate = new TransportCertificate();
        certificate.setId(id);
        certificate.setUpdatetime(new Date());
        certificate.setUpdatebyname(user.getUsername());
        certificate.setUpdateby(user.getId());
        certificate.setValidflag(false);
        this.updateById(certificate);
    }
 
 
    /**
    * @Description: 根据id获取运输证信息
    * @date 2021/5/11 17:48
    */
    @Override
    public TransportCertificate getOneById(Long id) {
        return transportCertificateMapper.getOneById(id);
    }
 
 
    /**
    * @Description: 提交单子
    * @date 2021/5/11 16:05
    */
    @Override
    public void summitApply(Long id, UserInfo user) {
        TransportCertificate certificate = new TransportCertificate();
        certificate.setId(id);
        certificate.setUpdatetime(new Date());
        certificate.setUpdatebyname(user.getUsername());
        certificate.setUpdateby(user.getId());
        certificate.setValidflag(true);
        certificate.setStatus(CertificateStatus.PENDING);
        this.updateById(certificate);
    }
 
 
    /**
    * @Description: 监管部门审批
    * @date 2021/5/11 16:10
    */
    @Override
    public void approveApply(JSONObject params, UserInfo user) {
        Long id = params.getLong("id");
        String signperson = params.getString("signperson");
        String issueperson = params.getString("issueperson");
        String issuepersonphone = params.getString("issuepersonphone");
        Date deadline = params.getDate("deadline");
        //简单校验
        {
            if (id == null) {
                throw new BusinessException("审批错误,请联系管理员");
            }
 
            if (StringUtils.isBlank(signperson)) {
                throw new BusinessException("请填写签发人员");
            }
 
            if (StringUtils.isBlank(issueperson)) {
                throw new BusinessException("请填写填发人员");
            }
 
            if (StringUtils.isBlank(issuepersonphone)) {
                throw new BusinessException("请填写填发人员联系电话");
            }
 
            if (deadline == null) {
                throw new BusinessException("请填写填发预期到达日期");
            }
        }
 
        //判断托运人和收货人的许可证是否过期
        TransportCertificate transportCertificate = this.getOneById(id);
        Enterprise shipper = enterpriseService.selectEnterpriseByName(transportCertificate.getShippercompanyname());
        Enterprise receiver = enterpriseService.selectEnterpriseByName(transportCertificate.getReceivercompanyname());
        Date date = new Date();
        if (shipper == null){
            throw new BusinessException("托运单位不存在");
        }
        if (receiver == null){
            throw new BusinessException("收货单位不存在");
        }
        if (shipper.getValidendtime().getTime() < date.getTime()){
            throw new BusinessException("托运单位运输证已过期");
        }
        if (receiver.getValidendtime().getTime() < date.getTime()){
            throw new BusinessException("收货单位运输证已过期");
        }
 
 
        //修改信息
        TransportCertificate certificate = new TransportCertificate();
        certificate.setId(id);
        certificate.setUpdatetime(new Date());
        certificate.setUpdatebyname(user.getUsername());
        certificate.setUpdateby(user.getId());
        certificate.setValidflag(true);
        certificate.setStatus(CertificateStatus.APPROVED);
        certificate.setSignperson(signperson);
        certificate.setIssueperson(issueperson);
        certificate.setIssuepersonphone(issuepersonphone);
        certificate.setDeadline(deadline);
        this.updateById(certificate);
    }
 
    /**
    * @Description: 监管部门回拒单子-》待提交状态
    * @date 2021/5/11 16:14
    */
    @Override
    public void refuseApply(Long id, UserInfo user) {
        TransportCertificate certificate = new TransportCertificate();
        certificate.setId(id);
        certificate.setUpdatetime(new Date());
        certificate.setUpdatebyname(user.getUsername());
        certificate.setUpdateby(user.getId());
        certificate.setValidflag(true);
        certificate.setStatus(CertificateStatus.TOSUBMIT);
        this.updateById(certificate);
    }
 
    /**
     * @Description: 导出运输许可证 三联
     * @date 2021/5/11 17:14
     */
    @Override
    public String exportCertificate(Long id) {
 
        TransportCertificate certificate = this.getOneById(id);
        if (certificate.getStatus() != CertificateStatus.APPROVED) {
            throw new BusinessException("未审批不能导出相关许可证");
        }
        //如果已经生成旧不需要生成了
        String fileUrl = Properties.transportCertificatePath + certificate.getCode()+".docx";
        String fileUrlReturn = Properties.transportCertificate+ certificate.getCode()+".docx";
        if (new File(fileUrl).exists()) {
            return fileUrlReturn;
        }
 
        try {
//            File file = ResourceUtils.getFile("classpath:printTemplate/certificatelicense.docx");
            ClassPathResource classPathResource = new ClassPathResource("printTemplate/certificatelicense.docx");
            InputStream inputStream = classPathResource.getInputStream();
            WordTemplate wordTemplate = new WordTemplate();
            wordTemplate.loadTemplate(null,inputStream);
            //参数
            Map<String, String> params = new HashMap<>();
            //编号
            params.put("ordercode",certificate.getCode());
            //托运人:单位名称
            params.put("shippercompanyname",certificate.getShippercompanyname());
            //收货人:单位名称
            params.put("receivercompanyname",certificate.getReceivercompanyname());
            //承运人:单位名称
            params.put("carriercompanyname",certificate.getCarriercompanyname());
            //承运人:联系电话
            params.put("carrierprincipalphone",certificate.getCarrierprincipalphone());
            //截止日期
            Date deadline = certificate.getDeadline();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(deadline);
            //年
            params.put("endyear", calendar.get(Calendar.YEAR) + "");
            //月
            params.put("endmonth", calendar.get(Calendar.MONTH) + "");
            //日
            params.put("endday", calendar.get(Calendar.DATE) + "");
            //运输车辆牌号
            params.put("carriercarnumber",certificate.getCarriercarnumber());
            //运输起始地
            params.put("startplace", certificate.getOtherdeparture());
            //运输目的地
            params.put("endplace", certificate.getOtherdestination());
            //运输途径地  任意一处
            params.put("byway", certificate.getOtherdeparture());
            List<TransportCertificateApproach> approachList = certificate.getTransportApproach();
            if (approachList != null && approachList.size() > 0) {
                params.put("byway", approachList.get(0).getCity());
            }
            //签发人
            params.put("signperson", certificate.getSignperson());
            //填发人
            params.put("issueperson", certificate.getIssueperson());
            //填发人联系方式
            params.put("issuepersonphone", certificate.getIssuepersonphone());
            params.put("first", "第一联 公安局存查联");
            params.put("second", "第二联 托运人存查联");
            params.put("third", "第三联 承运人存查联");
 
            //产品信息
            List<String[]> productPrintList= new ArrayList<>();
            List<TransportCertificateProduct> productList = certificate.getTransportProduct();
            assert productList.size() > 0;
 
            for (TransportCertificateProduct product : productList) {
                productPrintList.add(new String[]
                        {
                        //种类
                        product.getType(),
                        //规格
                        product.getSpecification(),
                        //数量
                        product.getNum()+"(箱)",
                        //备注 暂无字段,默认为空
                        ""
                        });
            }
            //至少10行
            int size = productList.size();
            if (size < rowMinSizeLimit) {
                for (int i = 0; i < rowMinSizeLimit - size; i++) {
                    productPrintList.add(new String[]{"","","",""});
                }
            }
            String[] rowParams = {"type", "specification", "num", "memo"};
            HashMap<String,WordTemplate.Table> tables = new HashMap<>();
            WordTemplate.Table t1 = new WordTemplate.Table(rowParams,productPrintList);
            tables.put("mainTable", t1);
            wordTemplate.replaceDocument(tables, params);
            //存一份到本地
            wordTemplate.saveFileAndQRCode(fileUrl,certificate.getCode());
            return fileUrlReturn;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到模板文件路径");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return fileUrlReturn;
    }
 
    /**
    * @Description: 校验运输证信息
    * @date 2021/5/12 14:09
    */
    @Override
    public void checkAddApply(TransportCertificate transportCertificate) {
 
        if (transportCertificate.getTransportProduct() == null || transportCertificate.getTransportProduct().size() < 1) {
            throw new BusinessException("产品不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getOtherdeparture())) {
            throw new BusinessException("运输起始地不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getOtherdestination())) {
            throw new BusinessException("运输到达地不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getReceivercompanyname())) {
            throw new BusinessException("收货单位不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getShippercompanyname())) {
            throw new BusinessException("托运单位不能为空");
        }
 
        //承运人负责人电话
        if (StringUtils.isBlank(transportCertificate.getCarrierprincipalname())) {
            throw new BusinessException("承运单位负责联系电话不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getCarriercarnumber())) {
            throw new BusinessException("承运单位运输车牌号不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getCarriernumber())) {
            throw new BusinessException("承运单位资格证编号不能为空");
        }
 
        if (StringUtils.isBlank(transportCertificate.getCarrierroadnumber())) {
            throw new BusinessException("承运单位道路运输证编号不能为空");
        }
 
 
 
    }
 
    @Override
    public String exportCertificateApply(Long id) {
 
        TransportCertificate certificate = this.getOneById(id);
        if (certificate.getStatus() == CertificateStatus.PENDING) {
            throw new BusinessException("未提交单子不能导出");
        }
        //如果已经生成旧不需要生成了
        String fileUrl = Properties.transportCertificatePath + certificate.getCode()+"-apply.docx";
        String fileUrlReturn =  Properties.transportCertificate+certificate.getCode()+"-apply.docx";
        if (new File(fileUrl).exists()) {
            return fileUrlReturn;
        }
 
        try {
//            File file = ResourceUtils.getFile("classpath:printTemplate/certificatelicenseApply.docx");
            ClassPathResource classPathResource = new ClassPathResource("printTemplate/certificatelicenseapply.docx");
            InputStream inputStream =classPathResource.getInputStream();;
            WordTemplate wordTemplate = new WordTemplate();
            wordTemplate.loadTemplate(null,inputStream);
            //参数
            Map<String, String> params = new HashMap<>();
            //编号
            params.put("ordercode",certificate.getCode());
            //托运人
            {
                //托运人:单位名称
                params.put("shippercompanyname",certificate.getShippercompanyname());
                //托运人:单位地址
                params.put("shipperaddress",certificate.getShipperaddress());
                //托运人:负责人姓名
                params.put("shipperprincipalname",certificate.getShipperprincipalname());
                //托运人:负责人联系电话
                params.put("shipperprincipalphone",certificate.getShipperprincipalphone());
                //托运人:经办人姓名
                params.put("shippermanagername",certificate.getShippermanagername());
                //托运人:经办人身份证号
                params.put("shippermanageridentify",certificate.getShippermanageridentify());
                //托运人:经办人联系电话
                params.put("shippermanagerphone",certificate.getShippermanagerphone());
            }
 
            //销售单位
            params.put("salecompanyname",certificate.getSalecompanyname());
            //收货人
            {
                //收货人:单位名称
                params.put("receivercompanyname",certificate.getReceivercompanyname());
                //收货人:地址
                params.put("receiveraddress",certificate.getReceiveraddress());
                //收货人:负责人姓名
                params.put("receiverprincipalname",certificate.getReceiverprincipalname());
                //收货人:负责人联系电话
                params.put("receiverprincipalphone",certificate.getReceiverprincipalphone());
            }
            //承运人
            {
                //承运人:单位名称
                params.put("carriercompanyname",certificate.getCarriercompanyname());
                //承运人:单位地址
                params.put("carrieraddress",certificate.getCarrieraddress());
                //承运人:单位地址
                params.put("carriernumber",certificate.getCarriernumber());
                //承运人:负责人
                params.put("carrierprincipalname",certificate.getCarrierprincipalname());
                //承运人:负责人联系电话
                params.put("carrierprincipalphone",certificate.getCarrierprincipalphone());
                //承运人:负责人联系电话
                params.put("carriercarnumber",certificate.getCarriercarnumber());
            }
            //运输
            {
                //运输起始地
                params.put("startplace", certificate.getOtherdeparture());
                //运输目的地
                params.put("endplace", certificate.getOtherdestination());
                //运输开始时间
                params.put("othershipstarttime", TransportDateFormat.format(certificate.getOthershipstarttime()));
                //运输结束时间
                params.put("othershipendtime", TransportDateFormat.format(certificate.getOthershipendtime()));
                //承运人:资质证明编号没有这个字段
                params.put("carrierroadnumber", certificate.getCarrierroadnumber());
                //运输途径地  任意一处
                params.put("byway", certificate.getOtherdeparture());
                List<TransportCertificateApproach> approachList = certificate.getTransportApproach();
                if (approachList != null && approachList.size() > 0) {
                    params.put("byway", approachList.get(0).getCity());
                }
                //
            }
            //驾驶员和押运员
            {
                List<TransportCertificatePerson> transportPersons = certificate.getTransportPerson();
                params.put("driver1", "");
                params.put("driver1id", "");
                params.put("driver1license", "");
                params.put("driver2", "");
                params.put("driver2id", "");
                params.put("driver2license", "");
                params.put("escort1", "");
                params.put("escort1id", "");
                params.put("escort1license", "");
                params.put("escort2", "");
                params.put("escort2id", "");
                params.put("escort2license", "");
                List<TransportCertificatePerson> drivers = new ArrayList<>();
                List<TransportCertificatePerson> escorts = new ArrayList<>();
                if (transportPersons != null && transportPersons.size() > 0) {
                    for (TransportCertificatePerson person : transportPersons) {
                        if (person.getType().equals(CertificatePersonType.DRIVER.getMsg())) {
                            drivers.add(person);
                        }
                        if (person.getType().equals(CertificatePersonType.ESCORT.getMsg())) {
                            escorts.add(person);
                        }
 
                    }
                }
                if (drivers.size() == 1) {
                    params.put("driver1", drivers.get(0).getPersonname());
                    params.put("driver1id", drivers.get(0).getPersonidentify());
                    params.put("driver1license", drivers.get(0).getNumber());
                }
                if (drivers.size() > 1) {
                    params.put("driver2", drivers.get(1).getPersonname());
                    params.put("driver2id", drivers.get(1).getPersonidentify());
                    params.put("driver2license", drivers.get(1).getNumber());
                }
                if (escorts.size() == 1) {
                    params.put("escort1", escorts.get(0).getPersonname());
                    params.put("escort2id", escorts.get(0).getPersonidentify());
                    params.put("escort2license", escorts.get(0).getNumber());
                }
                if (escorts.size() > 1) {
                    params.put("escort1", escorts.get(1).getPersonname());
                    params.put("escort2id", escorts.get(1).getPersonidentify());
                    params.put("escort2license", escorts.get(1).getNumber());
                }
            }
            //产品信息
            {
                List<String[]> productPrintList= new ArrayList<>();
                List<TransportCertificateProduct> productList = certificate.getTransportProduct();
                assert productList.size() > 0;
 
                for (TransportCertificateProduct product : productList) {
                    productPrintList.add(new String[]
                            {
                                    //种类
                                    product.getType(),
                                    //规格
                                    product.getSpecification(),
                                    //数量
                                    product.getNum()+"(箱)",
                            });
                }
 
                String[] rowParams = {"type", "specification", "num"};
                HashMap<String,WordTemplate.Table> tables = new HashMap<>();
                WordTemplate.Table t1 = new WordTemplate.Table(rowParams,productPrintList);
                tables.put("mainTable", t1);
                wordTemplate.replaceDocument2(tables, params);
            }
            //存一份到本地
            wordTemplate.saveFile(fileUrl);
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new BusinessException("找不到模板文件路径");
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("发生错误,请联系管理员");
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return fileUrlReturn;
    }
 
 
    /**
     * @Description: 获取运输证信息
     * @date 2021/5/14 19:04
     */
    @Override
    public TransportCertificate getByCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new BusinessException("运输证编号不能为空");
        }
        return transportCertificateMapper.selectByCode(code);
    }
 
    /**
    * @Description: 修改运输证产品的到货数量
    * @date 2021/5/14 19:54
    */
    @Override
    @Transactional
    public synchronized  void modTransportProductArrivalNum(TransportArrivalVo transportArrivalVo, UserInfo userInfo) {
 
        if (StringUtils.isBlank(transportArrivalVo.getCode())) {
            throw new BusinessException("运输证的单位编号不能为空");
        }
        List<TransportCertificateProduct> productList = transportArrivalVo.getProductList();
        if (productList == null || productList.size() < 1) {
            throw new BusinessException("产品信息不能为空");
        }
 
        for (TransportCertificateProduct product : productList) {
            //更新产品到货量信息
            TransportCertificateProduct oldProduct = transportCertificateProductService.getById(product.getId());
            Integer oldArrivalNum = oldProduct.getArrivalnum();
            Integer newArrivalNum = oldArrivalNum + product.getArrivalnum();
            oldProduct.setArrivalnum(newArrivalNum);
            if (oldProduct.getNum() < newArrivalNum) {
                throw new BusinessException("该次操作使得总到货量超过产品预计到货量,操作失败。");
            }
            transportCertificateProductService.updateById(oldProduct);
            //插入发送到货数的记录信息
            TransportCertificateArrivalRecord record = new TransportCertificateArrivalRecord();
            record.setCertificatecode(transportArrivalVo.getCode());
            record.setProductcode(oldProduct.getProductcode());
            record.setProductname(oldProduct.getName());
            record.setCreateby(userInfo.getId());
            record.setCreatebyname(userInfo.getUsername());
            record.setCreatetime(new Date());
            record.setArrivalnum(product.getArrivalnum());
            record.setValidflag(true);
            transportCertificateArrivalRecordService.save(record);
        }
 
        //获取单子所有产品
       List<TransportCertificateProduct> list = transportCertificateProductService.getListByTransportCode(transportArrivalVo.getCode());
        boolean flag = true;
        if (list.size() > 0) {
            for (TransportCertificateProduct product : list) {
                if (!product.getNum().equals(product.getArrivalnum())) {
                    flag = false;
                }
            }
        }
 
        //数量全相等
        if (flag) {
            this.updateByCode(transportArrivalVo.getCode(),"全部入库");
        }else{
            this.updateByCode(transportArrivalVo.getCode(),"部分入库");
        }
    }
 
    @Override
    public void updateByCode(String code,String certstatus) {
        LambdaUpdateWrapper<TransportCertificate> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(TransportCertificate::getCertstatus, certstatus).eq(TransportCertificate::getCode,code);
        this.update(updateWrapper);
    }
 
    @Override
    public List<TransportCertificate> selectWarnList(Date start, Date end) {
        return transportCertificateMapper.selectWarnList(start,end);
    }
 
 
//    @Override
//    public IPage selectTransportArrivalPages(Page<Object> page, UserInfo user) {
//        return null;
//    }
}