Administrator
2023-06-19 49588f5a462ae7425e7eb030438a35fd80c246fa
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
package com.gk.firework.Service.ServiceImpl;
import com.gk.firework.Domain.ContractFile;
 
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
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.EnterpriseSafetySupervision;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.Utils.PageInfo;
import com.gk.firework.Domain.Utils.Properties;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Utils.UploadUtil;
import com.gk.firework.Domain.Vo.*;
import com.gk.firework.Mapper.ContractDetailInfoMapper;
import com.gk.firework.Mapper.ContractOrderInfoMapper;
import com.gk.firework.Service.*;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
 
@Service("ContractOrderService")
public class ContractOrderServiceImpl extends ServiceImpl<ContractOrderInfoMapper, ContractOrderInfo> implements ContractOrderService {
    @Autowired
    ContractOrderInfoMapper contractOrderInfoMapper;
    @Autowired
    ContractDetailInfoMapper contractDetailInfoMapper;
    @Autowired
    ContractFileService contractFileService;
    @Autowired
    ContractDetailService contractDetailService;
    @Autowired
    ContractLogService contractLogService;
    @Autowired
    UserService userService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    ProductCodeService productCodeService;
    @Autowired
    ProductLocusService productLocusService;
    @Autowired
    ProductService productService;
    @Autowired
    ExcelExportService excelExportService;
    @Autowired
    ControlPrintParamService controlPrintParamService;
 
    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<ContractOrderVo> page = new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize());
        List<OrderItem> orderItems = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(pageInfo.getSort()) && StringUtils.isNotBlank(pageInfo.getOrder())) {
            orderItem.setAsc(pageInfo.getOrder().equalsIgnoreCase("ascending"));
            orderItem.setColumn(pageInfo.getSort());
        }else {
            orderItem.setAsc(false);
            orderItem.setColumn("createddate");
        }
        orderItems.add(orderItem);
        page.setOrders(orderItems);
        List<ContractOrderVo> list = contractOrderInfoMapper.selectDataGrid(page,pageInfo.getCondition());
        for (ContractOrderVo contractOrderVo : list) {
            List<ProductVo> productVoList = contractDetailInfoMapper.selectByOrder(contractOrderVo.getOrdercode());
            ContractFile contractFile =  contractFileService.selectByOrderCode(contractOrderVo.getOrdercode());
            contractOrderVo.setProductInfoList(productVoList);
            contractOrderVo.setContractFile(contractFile);
        }
        pageInfo.setResult(list);
        pageInfo.setTotalCount(page.getTotal());
    }
 
    @Override
    public List<ContractOrderInfo> selectByStatus(String status, String now) {
        return contractOrderInfoMapper.selectByStatus(status,now);
    }
 
    @Override
    public ContractOrderInfo selectByOrderCode(String ordercode) {
        return contractOrderInfoMapper.selectByOrderCode(ordercode);
    }
 
    @Override
    @Transactional
    public void addApply(ContractOrderVo contractOrderVo, UserInfo user) {
 
 
        UserInfo userInfo = userService.getById(user.getId());
        if (StringUtils.isBlank(userInfo.getCompanynumber())) {
            throw new BusinessException("没有权限新增");
        }
        Enterprise enterprise = enterpriseService.selectEnterpriseByNumber(userInfo.getCompanynumber());
        assert enterprise.getSafetysupervision() != null;
        if (enterprise.getSafetysupervision().equals(EnterpriseSafetySupervision.PRODUCE.getMsg())) {
            throw new BusinessException("生产企业不可以申请合同");
        }
        //单号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String ordercode = "CT"+sdf.format(new Date());
        String operator = user.getUsername();
        //规律的单号
        String regularCode = this.generateRegularCode();
 
 
        JSONArray jsonArray = JSONArray.parseArray(contractOrderVo.getProductList());
        List<ContractDetailInfo> contractDetails = jsonArray.toJavaList(ContractDetailInfo.class);
        if (contractDetails.size() <1) throw new BusinessException("产品不能为空");
        //上传附件
        MultipartFile file = contractOrderVo.getFile();
        if (file != null ) {
            try {
                ContractFile attachment = new ContractFile();
                String name = UploadUtil.uploadFile(file, Properties.contractPath);
                attachment.setValidflag(true);
                attachment.setOrdercode(ordercode);
                attachment.setFilename(file.getOriginalFilename());
                attachment.setUrl(Properties.contract + name);
                attachment.setCreateby(user.getId());
                attachment.setCreatebyname(user.getUsername());
                attachment.setCreatetime(new Date());
                contractFileService.save(attachment);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("上传文件失败");
            }
        }
        //甲方单位编号
        contractOrderVo.setPurchaseenterprisenumber(userInfo.getCompanynumber());
        contractOrderVo.setRegularcode(regularCode);
        contractOrderVo.setOrdercode(ordercode);
        contractOrderVo.setStatus(ContractStatus.WAIT_SUBMIT);
        contractOrderVo.setOperator(operator);
        contractOrderVo.setCreatedby(operator);
        contractOrderVo.setCreateddate(new Date());
        contractOrderVo.setModifiedby(operator);
        contractOrderVo.setModifieddate(new Date());
        contractOrderVo.setIsdel((byte)0);
        contractOrderVo.setManufacturer(contractOrderVo.getSupplyunitname());
        this.save(contractOrderVo);
 
        if (contractDetails.size() > 0) {
            for (ContractDetailInfo contractDetail : contractDetails) {
                ProductInfo productInfo = productService.selectByDirection(contractDetail.getDirectioncode());
                if (productInfo == null) {
                    throw new BusinessException(String.format("产品不存在,流向码:%s", contractDetail.getName()));
                }
                if (contractDetail.getNum() == null) {
                    throw new BusinessException(String.format("请输入%s"+"的产品数量",contractDetail.getName()));
                }
                if (contractDetail.getPrice() == null) {
                    throw new BusinessException(String.format("请输入%s"+"的进货单价",contractDetail.getName()));
                }
                contractDetail.setOrdercode(ordercode);
                contractDetailService.save(contractDetail);
            }
        }
        ContractLogInfo contractLogInfo = new ContractLogInfo();
        contractLogInfo.setOptlog("新增合同");
        contractLogInfo.setOperator(operator);
        contractLogInfo.setOrdercode(ordercode);
        contractLogInfo.setOperatordate(new Date());
        contractLogService.save(contractLogInfo);
    }
 
    @Override
    @Transactional
    public void putApply(ContractOrderVo contractOrderVo, UserInfo user) {
 
        UserInfo userInfo = userService.getById(user.getId());
        if (StringUtils.isBlank(userInfo.getCompanynumber())) {
            throw new BusinessException("没有权限修改");
        }
        String operator = user.getUsername();
        JSONArray jsonArray = JSONArray.parseArray(contractOrderVo.getProductList());
        List<ContractDetailInfo> contractDetails = jsonArray.toJavaList(ContractDetailInfo.class);
        if (contractDetails.size() <1) throw new BusinessException("产品不能为空");
 
        ContractOrderInfo contractOrderInfo = this.getById(contractOrderVo.getId());
        if (!contractOrderInfo.getStatus().equals(ContractStatus.WAIT_SUBMIT) &&
                !contractOrderInfo.getStatus().equals(ContractStatus.Refuse_Product)){
            throw new BusinessException("合同状态不为待提交、拒绝生产,无法修改");
        }
        //上传附件
        MultipartFile file = contractOrderVo.getFile();
        if (file != null ) {
            try {
                contractFileService.deleteAll(contractOrderVo.getOrdercode());
                ContractFile attachment = new ContractFile();
                String name = UploadUtil.uploadFile(file, Properties.contractPath);
                attachment.setValidflag(true);
                attachment.setOrdercode(contractOrderVo.getOrdercode());
                attachment.setFilename(file.getOriginalFilename());
                attachment.setUrl(Properties.contract + name);
                attachment.setCreateby(user.getId());
                attachment.setCreatebyname(user.getUsername());
                attachment.setCreatetime(new Date());
                contractFileService.save(attachment);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("上传文件失败");
            }
        }
 
        contractOrderVo.setManufacturer(contractOrderVo.getSupplyunitname());
        this.updateById(contractOrderVo);
 
        if (contractDetails != null && contractDetails.size() > 0) {
            contractDetailService.deleteByOrder(contractOrderInfo.getOrdercode());
            for (ContractDetailInfo contractDetail : contractDetails) {
                if (contractDetail.getNum() == null) {
                    throw new BusinessException(String.format("请输入%s"+"的产品数量",contractDetail.getName()));
                }
                if (contractDetail.getPrice() == null) {
                    throw new BusinessException(String.format("请输入%s"+"的进货单价",contractDetail.getName()));
                }
                contractDetail.setOrdercode(contractOrderVo.getOrdercode());
                contractDetailService.save(contractDetail);
            }
        }
 
        ContractLogInfo contractLogInfo = new ContractLogInfo();
        contractLogInfo.setOptlog("修改合同");
        contractLogInfo.setOperator(operator);
        contractLogInfo.setOrdercode(contractOrderInfo.getOrdercode());
        contractLogInfo.setOperatordate(new Date());
        contractLogService.save(contractLogInfo);
 
    }
 
    @Override
    @Transactional
    public void confirmProduct(ContractOrderVo contractOrderVo, UserInfo user) {
 
        String operator = user.getUsername();
        ContractOrderInfo contractOrderInfo = this.getById(contractOrderVo.getId());
        //上传附件
        MultipartFile file = contractOrderVo.getFile();
        if (file != null ) {
            try {
                ContractFile attachment = new ContractFile();
                String name = UploadUtil.uploadFile(file, Properties.contractPath);
                attachment.setValidflag(true);
                attachment.setFilename(file.getOriginalFilename());
                attachment.setUrl(Properties.contract + name);
                attachment.setCreateby(user.getId());
                attachment.setCreatebyname(user.getUsername());
                attachment.setCreatetime(new Date());
                contractFileService.save(attachment);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("上传文件失败");
            }
        }
 
        if (!contractOrderInfo.getStatus().equals(ContractStatus.WAIT_CONFIRM)) {
            throw new BusinessException("合同状态不为待确认,无法确认生产");
        }
 
        contractOrderInfo.setStatus(ContractStatus.Confirm_Product);
        contractOrderInfo.setModifiedby(operator);
        contractOrderInfo.setModifieddate(new Date());
        contractOrderInfo.setReturnstatus("未退回");
        contractOrderInfo.setSupplyunitaddress(contractOrderVo.getSupplyunitaddress());
        contractOrderInfo.setSupplyrepresentative(contractOrderVo.getSupplyrepresentative());
        contractOrderInfo.setSupplyrepresentativephone(contractOrderVo.getSupplyrepresentativephone());
        contractOrderInfo.setSupplybank(contractOrderVo.getSupplybank());
        contractOrderInfo.setSupplylicensenumber(contractOrderVo.getSupplylicensenumber());
        contractOrderInfo.setSupplyzipcode(contractOrderVo.getSupplyzipcode());
        contractOrderInfo.setSupplyagent(contractOrderVo.getSupplyagent());
        contractOrderInfo.setSupplyagentphone(contractOrderVo.getSupplyagentphone());
        contractOrderInfo.setSupplyagentemail(contractOrderVo.getSupplyagentemail());
        contractOrderInfo.setSupplyaccount(contractOrderVo.getSupplyaccount());
        this.updateById(contractOrderInfo);
 
        ContractLogInfo contractLogInfo = new ContractLogInfo();
        contractLogInfo.setOptlog("确认合同");
        contractLogInfo.setOperator(operator);
        contractLogInfo.setOrdercode(contractOrderInfo.getOrdercode());
        contractLogInfo.setOperatordate(new Date());
        contractLogService.save(contractLogInfo);
    }
 
    /**
    * @Description: 申请校验
    * @date 2021/5/11 13:55
    */
    @Override
    public void checkAdd(ContractOrderVo contractOrderVo) {
        if (StringUtils.isBlank(contractOrderVo.getSignlocation())) {
            throw new BusinessException("签订地点不能为空");
        }
 
        if (contractOrderVo.getSigntime() == null) {
            throw new BusinessException("签订时间不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getProductList())) {
            throw new BusinessException("产品列表不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyenterprisenumber())
                || StringUtils.isBlank(contractOrderVo.getSupplyunitname())) {
            throw new BusinessException("生产企业不能为空");
        }
 
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaseunitaddress())) {
            throw new BusinessException("供货单位地址不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaserepresentative())) {
            throw new BusinessException("供货单位法定代表人联系电话不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaserepresentativeemail())) {
            throw new BusinessException("供货单位法定代表人电子邮箱不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchasebank())) {
            throw new BusinessException("供货单位开户银行不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaselicensenumber())) {
            throw new BusinessException("供货单位许可证编号不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchasezipcode())) {
            throw new BusinessException("供货单位许可证编号不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaseagent())) {
            throw new BusinessException("供货单位委托代理人不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaseagentphone())) {
            throw new BusinessException("供货单位委托代理人联系电话不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPurchaseaccount())) {
            throw new BusinessException("供货单位账户不能为空");
        }
 
 
 
        //合同内容:
        if (StringUtils.isBlank(contractOrderVo.getQualitystandard())) {
            throw new BusinessException("产品质量标准及要求不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getProhibiteddrug())) {
            throw new BusinessException("禁(限)用药物要求不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getPackingstandard())) {
            throw new BusinessException("产品包装标准及要求不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getTimeandlocation())) {
            throw new BusinessException("提(交)货时间和地点不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getTransportandcost())) {
            throw new BusinessException("运输方式及费用负担不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getAcceptstandard())) {
            throw new BusinessException("验收标准与方法不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getObjectdeadline())) {
            throw new BusinessException("提出异议期限不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSettlemethod())) {
            throw new BusinessException("结算方式与期限不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getProductsafetyandquality())) {
            throw new BusinessException("产品安全与质量责任不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getTransportsafety())) {
            throw new BusinessException("运输安全责任不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getBreakcontract())) {
            throw new BusinessException("违约责任不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getContractdisputesfelid())) {
            throw new BusinessException("合同争议的解决方式不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getOtheragreedmatters())) {
            throw new BusinessException("其他约定事项不能为空");
        }
        //有效期
        if (contractOrderVo.getValidstarttime() == null || contractOrderVo.getValidendtime() == null) {
            throw new BusinessException("合同有效期不能为空");
        }
    }
 
 
    /**
     * @Description: 确认校验
     * @date 2021/5/11 13:55
     */
    @Override
    public void checkConfirm(ContractOrderVo contractOrderVo) {
 
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyunitaddress())) {
            throw new BusinessException("生产单位地址不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyrepresentative())) {
            throw new BusinessException("生产单位法定代表人联系电话不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyrepresentativeemail())) {
            throw new BusinessException("生产单位法定代表人电子邮箱不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplybank())) {
            throw new BusinessException("生产单位开户银行不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplylicensenumber())) {
            throw new BusinessException("生产单位许可证编号不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyzipcode())) {
            throw new BusinessException("生产单位邮政编码不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyagent())) {
            throw new BusinessException("生产单位委托代理人不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyagentphone())) {
            throw new BusinessException("生产单位委托代理人联系电话不能为空");
        }
 
        if (StringUtils.isBlank(contractOrderVo.getSupplyaccount())) {
            throw new BusinessException("生产单位账户不能为空");
        }
    }
 
    @Override
    @Transactional
    public void returnTag(String ordercode, MultipartFile file,UserInfo userInfo) {
        if (StringUtils.isBlank(ordercode)) {
            throw new BusinessException("参数传递错误");
        }
        List<ProductCodeInfo> productCodeInfos = productCodeService.selectByOrderCode(ordercode);
        if (productCodeInfos.size() < 1) {
            throw new BusinessException("订单编号或者产品出现问题");
        }
        byte [] byteArr;
        try {
            byteArr = file.getBytes();
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            assert originalFilename != null;
            boolean isExcel2007 = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).endsWith("xlsx");
        //解析
            List<String> parsedTags = excelExportService.parseProductCode(inputStream, userInfo, isExcel2007);
            assert parsedTags.size() > 0;
            Set<String> distinctSet = new HashSet<>(parsedTags);
            if (distinctSet.size() != parsedTags.size()) {
                throw new BusinessException("文件中有重复标签在不同行");
            }
            //更新电子标签isdel = 1
            {
                List<ProductCodeInfo> updatesTags = new ArrayList<>();
                Date now = new Date();
                for (String returnTag : parsedTags) {
                    boolean isError = true;
                    for (ProductCodeInfo realCode : productCodeInfos) {
                        if (realCode.getOriginalcode().equals(returnTag)) {
                            isError = false;
                            //开始更新计数
                            realCode.setIsdel((byte) 1);
                            updatesTags.add(realCode);
                            //流向
                            ProductLocusInfo flow = new ProductLocusInfo();
                            flow.setCreateddate(now);
                            flow.setModifieddate(now);
                            //流向内容为 退货人
                            flow.setContent(userInfo.getUsername());
                            flow.setDirectioncode(returnTag);
                            flow.setType(ProductLocusInfo.ELECTRONIC_LABEL_RETURN_STATUS);
                            flow.setBoxcode(Integer.valueOf(returnTag.substring(19,22)).toString());
                            productLocusService.insertProductLocus(flow);
                        }
                    }
                    //如果上传的标签不在合同编号的电子标签,则有问题
                    if (isError) {
                        throw new BusinessException(returnTag + ":上传的产品电子标签不是该订单的电子标签或者该标签已经退回");
                    }
                }
                //执行退回标签
                productCodeService.updateBatchById(updatesTags);
            }
            //变更数量统计
            {
                Map<String, Integer> changeMap = new HashMap<>();
 
                for (String parsedTag : parsedTags) {
                    String productCode = parsedTag.substring(0, 10);
                    if (changeMap.get(productCode) == null) {
                        changeMap.put(productCode, 1);
                    } else {
                        Integer count = changeMap.get(productCode);
                        changeMap.put(productCode, ++count);
                    }
                }
 
                for (Map.Entry<String, Integer> entry : changeMap.entrySet()) {
                    String productCode = entry.getKey();
                    Integer returnNum = entry.getValue();
                    //ordercode 和10位productCode 来更新一个变更数量
                    contractDetailService.updateChangNum(ordercode,productCode, returnNum);
                }
            }
 
            //更新单子状态-》有退回
 
            contractOrderInfoMapper.updateReturnStatus(ordercode,"有退回");
 
 
 
        } catch (IOException e) {
            e.printStackTrace();
            throw new BusinessException("文件传输发生错误");
        }
    }
 
    @Override
    public String generateZplString(ProductCodePrint productPrint, UserInfo userInfo) {
        List<ProductCodeVo>productCodeVos = productPrint.getProductCodeVos();
        //ZplUtil zplUtil = new ZplUtil();
        return generateInnerTag(productCodeVos, userInfo);
    }
 
    @Override
    public String generateInnerTag(List<ProductCodeVo> productCodeVos, UserInfo userInfo){
        StringBuilder builder = new StringBuilder();
        byte type = 1;
        ControlPrintParam controlPrintParam = controlPrintParamService.getParamsByUser(userInfo,type);
        for (int i = 0; i < productCodeVos.size(); i++){
            ProductCodeVo productCodeVo = productCodeVos.get(i);
            if (productCodeVo == null || (StringUtils.isBlank(productCodeVo.getOrdercode())&& StringUtils.isBlank(productCodeVo.getItemcode()))){
                if (i % 2 == 1){
                    if (!builder.toString().endsWith("^XZ")){
                        builder.append("^XZ");
                    }else {
                        builder.append("^XA^FD.").append("^XZ");
                    }
                }
                continue;
            }
            String code = productCodeVo.getItemcode();
            String title = "新疆流向专用";
            String date = DateFormatUtils.format(productCodeVo.getCreateddate(), "yyyy-MM-dd HH:mm:ss");
            Integer baseX = controlPrintParam.getLeftBaseX();
            Integer titleY = controlPrintParam.getTitleY();
            Integer titleOffsetX = controlPrintParam.getTitleOffsetX();
            Integer barCodeWidth = controlPrintParam.getBarcodeWidth();
            Integer barCodeWidthRatio = controlPrintParam.getBarcodeWidthRadio();
            Integer barCodeHeight = controlPrintParam.getBarcodeHeight();
            Integer barCodeY = controlPrintParam.getBarcodeY();
            Integer qrCodeModel = controlPrintParam.getQrCodeModel();
            Integer qrCodeMagnification = controlPrintParam.getQrCodeMagnification();
            Integer qrX = controlPrintParam.getLeftQrX();
            Integer qrY = controlPrintParam.getQrY();
            Integer fontSize = controlPrintParam.getFontsize1();
            Integer fontSize2 = controlPrintParam.getFontsize2();
            Integer fontSize3 = controlPrintParam.getFontsize3();
            Integer barCodeTextY = controlPrintParam.getBarcodeTextY();
            Integer itemNameY = controlPrintParam.getItemNameY();
            Integer typeY = controlPrintParam.getTypeY();
            Integer dateY = controlPrintParam.getDateY();
            Integer manufacturerY = controlPrintParam.getManufacturerY();
            Integer manufacturerY2 = controlPrintParam.getManufacturerY2();
            Integer frameX = controlPrintParam.getLeftFrameX();
            Integer frameY = controlPrintParam.getFrameY();
            Integer frameWidth = controlPrintParam.getFrameWidth();
            Integer frameHeight = controlPrintParam.getFrameHeight();
            Integer darkness = controlPrintParam.getDarkness();
 
 
            //左标签
            if (i % 2 == 0){
                //^XA 开始 ;^JMA 正常打印浓度 ;^SEE:GB18030.DAT^CW1,E:SIMSUN.FNT 支持中文编码
                builder.append("^XA").append("^JMA").append("^MD").append(darkness).append("^CI28^SEE:GB18030.DAT^CW1,E:SIMSUN.TTF");
            }else {
                baseX = controlPrintParam.getRightBaseX();
                qrX = controlPrintParam.getRightQrX();
                frameX = controlPrintParam.getRightFrameX();
            }
            //标签头
            //builder.append("^FO").append(baseX).append(",").append(titleY).append("^GFA,64896,64896,00156,").append(title);
            builder.append("^FO").append(baseX+titleOffsetX).append(",").append(titleY).append("^A1N,").append(fontSize3)
                    .append(",").append(fontSize3).append("^FD").append(title).append("^FS");
            //条形码
            builder.append("^BY").append(barCodeWidth).append(",").append(barCodeWidthRatio).append(",")
                    .append(barCodeHeight).append("^FO").append(baseX).append(",").append(barCodeY)
                    .append("^BCN,,N,N^FD").append(code).append("^FS");
            //二维码
            builder.append("^BQN,").append(qrCodeModel).append(",").append(qrCodeMagnification).append("^FO")
                    .append(qrX).append(",").append(qrY).append("^FDLA,").append(code).append("^FS");
            //条形码文字
            builder.append("^FO").append(baseX).append(",").append(barCodeTextY).append("^A1N,")
                    .append(fontSize).append("^FD").append(code).append("^FS");
            //其他信息
            //文字转换图片打印,但转换后数据太大
            // builder.append(zplUtil.getCharPicture(productCodeVo.getItemname(),baseX,itemNameY,30,34,1));
            // builder.append(zplUtil.getCharPicture(productCodeVo.getType(),baseX,typeY,30,34,1));
            // builder.append(zplUtil.getCharPicture(productCodeVo.getManufacturer(),baseX,manufacturerY,30,34,1));
            // builder.append("^FO").append(baseX).append(",").append(dateY).append("^A,").append(fontSize).append(",")
            // .append(fontSize).append("^FD").append(date).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(itemNameY).append("^A1N,").append(fontSize2)
                    .append(",").append(fontSize2).append("^FD").append(productCodeVo.getItemname()).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(typeY).append("^A1N,").append(fontSize2).append(",")
                    .append(fontSize2).append("^FD").append(productCodeVo.getType()).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(dateY).append("^A1N,").append(fontSize).append(",")
                    .append(fontSize).append("^FD").append(date).append("^FS");
            String manufacturer = productCodeVo.getManufacturer();
            if (StringUtils.isBlank(manufacturer)){
                throw new BusinessException("错误:生产厂家为空");
            }
            builder.append("^FO").append(baseX).append(",").append(manufacturerY).append("^A1N,").append(fontSize2)
                    .append(",").append(fontSize2).append("^FD").append(manufacturer.length()>16?manufacturer.substring(0,16):manufacturer).append("^FS");
            if (manufacturer.length()>16){
                builder.append("^FO").append(baseX).append(",").append(manufacturerY2).append("^A1N,").append(fontSize2)
                        .append(",").append(fontSize2).append("^FD").append(manufacturer.substring(16)).append("^FS");
            }
            builder.append("^FO").append(frameX).append(",").append(frameY).append("^GB").append(frameWidth)
                    .append(",").append(frameHeight).append(",4,B,1^FS");
            if (i % 2 != 0){
                builder.append("^XZ");
            }
 
        }
 
        return builder.toString();
    }
 
    @Override
    public String generateOuterTag(List<ProductCodeVo> productCodeVos, UserInfo userInfo){
        StringBuilder builder = new StringBuilder();
        byte type = 2;
        ControlPrintParam controlPrintParam = controlPrintParamService.getParamsByUser(userInfo,type);
        for (int i = 0; i < productCodeVos.size(); i++){
            ProductCodeVo productCodeVo = productCodeVos.get(i);
            if (productCodeVo == null || (StringUtils.isBlank(productCodeVo.getOrdercode())&& StringUtils.isBlank(productCodeVo.getItemcode()))){
                if (i % 2 == 1){
                    builder.append("^XZ");
                }
                builder.append("^XA").append("^XZ");
                continue;
            }
            String code = productCodeVo.getOriginalcode();
            if (code.length()!= 22){
                continue;
            }
            String codePrefix = code.substring(0,19);
            String codeSuffix = code.substring(19);
 
            String title = "新疆专用:包装识别码";
            String date = DateFormatUtils.format(productCodeVo.getCreateddate(), "yyyy/MM/dd");
            int baseX = controlPrintParam.getLeftBaseX();
            int titleY = controlPrintParam.getTitleY();
            int barCodeWidth = controlPrintParam.getBarcodeWidth();
            int barCodeWidthRatio = controlPrintParam.getBarcodeWidthRadio();
            int barCodeHeight = controlPrintParam.getBarcodeHeight();
            int barCodeY = controlPrintParam.getBarcodeY();
            int qrCodeModel = controlPrintParam.getQrCodeModel();
            int qrCodeMagnification = controlPrintParam.getQrCodeMagnification();
            int qrX = controlPrintParam.getLeftQrX();
            int qrY = controlPrintParam.getQrY();
            int fontSize = controlPrintParam.getFontsize1();
            int fontSize2 = controlPrintParam.getFontsize2();
            int barCodeTextY = controlPrintParam.getBarcodeTextY();
            int itemNameY = controlPrintParam.getItemNameY();
            int typeY = controlPrintParam.getTypeY();
            int dateY = controlPrintParam.getDateY();
            int totalY = controlPrintParam.getTotalY();
            int manufacturerY = controlPrintParam.getManufacturerY();
            int manufacturerY2 = controlPrintParam.getManufacturerY2();
            int darkness = controlPrintParam.getDarkness();
 
            //左标签
            if (i % 2 == 0){
                //^XA 开始 ;^JMA 正常打印浓度 ;^SEE:GB18030.DAT^CW1,E:SIMSUN.FNT 支持中文编码
                builder.append("^XA").append("^JMA").append("^MD").append(darkness).append("^CI28^SEE:GB18030.DAT^CW1,E:SIMSUN.TTF");
            }else {
                baseX = controlPrintParam.getRightBaseX();
                qrX = controlPrintParam.getRightQrX();
            }
            //标签头
            builder.append("^FO").append(baseX).append(",").append(titleY).append("^A1N,").append(fontSize2)
                    .append("^FD").append(title).append(productCodeVo.getBoxrange()).append("^FS");
            //条形码
            builder.append("^BY").append(barCodeWidth).append(",").append(barCodeWidthRatio).append(",")
                    .append(barCodeHeight).append("^FO").append(baseX-20).append(",").append(barCodeY)
                    .append("^BCN,,N,N^FD").append(code).append("^FS");
            //二维码
            builder.append("^BQN,").append(qrCodeModel).append(",").append(qrCodeMagnification).append("^FO")
                    .append(qrX).append(",").append(qrY).append("^FDLA,").append(code).append("^FS");
            //条形码文字
            builder.append("^FO").append(baseX).append(",").append(barCodeTextY).append("^A1,")
                    .append(fontSize).append("^FD").append(codePrefix).append("  ").append(codeSuffix).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(itemNameY).append("^A1N,").append(fontSize2)
                    .append(",").append(fontSize2).append("^FD").append(productCodeVo.getItemname()).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(typeY).append("^A1N,").append(fontSize2).append(",")
                    .append(fontSize2).append("^FD").append(productCodeVo.getType()).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(dateY).append("^A1N,").append(fontSize).append(",")
                    .append(fontSize).append("^FD").append(date).append("^FS");
            builder.append("^FO").append(baseX).append(",").append(totalY).append("^A1N,").append(fontSize).append(",")
                    .append(fontSize).append("^FD").append("总装药量:").append(productCodeVo.getExplosivecontent()).append("^FS");
            builder.append("^FO").append(qrX).append(",").append(totalY).append("^A1N,").append(fontSize).append(",")
                    .append(fontSize).append("^FD").append("箱含量:").append(productCodeVo.getBoxnumber()).append("^FS");
            String manufacturer = productCodeVo.getManufacturer();
            if (StringUtils.isBlank(manufacturer)){
                throw new BusinessException("错误:生产厂家为空");
            }
            builder.append("^FO").append(baseX).append(",").append(manufacturerY).append("^A1N,").append(fontSize2)
                    .append(",").append(fontSize2).append("^FD").append(manufacturer.length()>16?manufacturer.substring(0,16):manufacturer).append("^FS");
            if (manufacturer.length()>16){
                builder.append("^FO").append(baseX).append(",").append(manufacturerY2).append("^A1N,").append(fontSize2)
                        .append(",").append(fontSize2).append("^FD").append(manufacturer.substring(16)).append("^FS");
            }
            if (i % 2 != 0){
                builder.append("^XZ");
            }
        }
        if (!builder.toString().endsWith("^XZ")){
            builder.append("^XZ");
        }
        return builder.toString();
    }
 
    /**
    * @Description: 生成规律单号
    * @date 2021/5/10 10:11
    */
    private String generateRegularCode() {
        //查询当日合同总数
        int num = contractOrderInfoMapper.selectCountToday();
        String numStr = num + 1 + "";
        //超过3位 显示原位数据 不需要补0
        if (numStr.length() >= 3) {
            return numStr;
        }
        //不超过则需要补0
        //距离
        int i = 3 - numStr.length();
        return String.format("%03d", num + 1);
 
    }
 
}