郑永安
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
package com.gk.firework.Scheduls.GenerateCode;
 
import com.gk.firework.Domain.ContractLogInfo;
import com.gk.firework.Domain.ContractOrderInfo;
import com.gk.firework.Domain.ProductCodeInfo;
import com.gk.firework.Domain.ProductLocusInfo;
import com.gk.firework.Domain.Vo.ContractStatus;
import com.gk.firework.Domain.Vo.GenerateCode;
import com.gk.firework.Domain.Vo.ProductVo;
import com.gk.firework.Service.*;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
 
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class GenerateCodeTask {
    private Logger logger = LogManager.getLogger(GenerateCodeTask.class);
    @Autowired
    ContractDetailService contractDetailService;
    @Autowired
    ContractOrderService contractOrderService;
    @Autowired
    ContractLogService contractLogService;
    @Autowired
    ProductService productService;
    @Autowired
    ProductCodeService productCodeService;
    @Autowired
    ProductLocusService productLocusService;
 
 
    @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次
    private void GenerateCode() throws Exception {
        SimpleDateFormat sdfnow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ContractOrderInfo> orderInfoList = contractOrderService.selectByStatus(ContractStatus.Confirm_Product,sdfnow.format(new Date()));
        for (ContractOrderInfo contractOrderInfo : orderInfoList) {
            //生成电子标签号码
            List<ProductVo> productVoList = contractDetailService.selectByOrder(contractOrderInfo.getOrdercode());
            for (ProductVo productVo : productVoList) {
                //查询最新的箱码
                String directionCode = productVo.getDirectionCode();
                String four = GenerateCode.generateProduct();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                ProductCodeInfo productCodeInfo = productCodeService.selectByFourteen(directionCode + four,sdf.format(new Date()));
 
                List<ProductCodeInfo> productCodeInfoList = new ArrayList<>();
                Date now = new Date();
                for (int i = 0; i < productVo.getNum(); i++) {
                    int serial = 1;
                    if (productCodeInfoList.size() > 0){
                        serial = Integer.parseInt(productCodeInfoList.get(productCodeInfoList.size()-1).getOriginalcode().substring(14,19))
                                + productCodeInfoList.get(productCodeInfoList.size()-1).getBoxnumber();
                    }else if (productCodeInfo != null) {
                            serial = Integer.parseInt(productCodeInfo.getOriginalcode().substring(14,19))
                                    + productCodeInfo.getBoxnumber();
                    }
                    String serialNum = String.format("%05d", serial);
 
                    String boxNum = String.format("%03d", productVo.getBoxNumber());
                    ProductCodeInfo productCode = new ProductCodeInfo();
                    productCode.setOrdercode(contractOrderInfo.getOrdercode());
                    productCode.setOriginalcode(directionCode + four + serialNum + boxNum);
                    productCode.setItemname(productVo.getName());
                    productCode.setManufacturer(productVo.getManufacturer());
 
                    BigDecimal explosiveContent = productVo.getExplosiveContent();
                    Integer boxNumber = productVo.getBoxNumber();
                    BigDecimal sum = explosiveContent.multiply(new BigDecimal(boxNumber));
                    if (sum.compareTo(new BigDecimal(1000)) > 0) {
                        BigDecimal explosive = sum.divide(new BigDecimal(1000),2, RoundingMode.HALF_UP);
                        productCode.setExplosivecontent(explosive + "kg");
                    } else {
                        productCode.setExplosivecontent(sum + "g");
                    }
                    productCode.setBoxnumber(productVo.getBoxNumber());
                    productCode.setType(productVo.getType());
                    productCode.setLevel(productVo.getLevel());
                    productCode.setCreatedby("自动生成");
                    productCode.setCreateddate(new Date());
                    productCode.setPrice(productVo.getPrice());
                    productCodeInfoList.add(productCode);
                    //电子合同生成电子标签号插入流向轨迹表
                    ProductLocusInfo productLocusInfo = new ProductLocusInfo();
                    productLocusInfo.setDirectioncode(productCode.getOriginalcode());
                    productLocusInfo.setCreateddate(now);
                    productLocusInfo.setModifieddate(now);
                    //生产单位
                    productLocusInfo.setContent(productCode.getManufacturer());
                    productLocusInfo.setType(ProductLocusInfo.ELECTRONIC_LABEL_STATUS);
                    productLocusInfo.setBoxcode(Integer.valueOf(productCode.getOriginalcode().substring(19,22)).toString());
                    productLocusService.insertProductLocus(productLocusInfo);
                }
                productCodeService.insertBatch(productCodeInfoList);
            }
 
            contractOrderInfo.setStatus(ContractStatus.Generated);
            contractOrderInfo.setModifieddate(new Date());
            contractOrderService.updateById(contractOrderInfo);
 
            ContractLogInfo contractLogInfo = new ContractLogInfo();
            contractLogInfo.setOptlog("生成电子标签");
            contractLogInfo.setOperator("后台生成");
            contractLogInfo.setOrdercode(contractOrderInfo.getOrdercode());
            contractLogInfo.setOperatordate(new Date());
            contractLogService.save(contractLogInfo);
 
        }
    }
 
 
 
 
}