郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
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
package com.gk.firework.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.firework.Config.DataSource.MybatisPlusConfig;
import com.gk.firework.Domain.Exception.BusinessException;
import com.gk.firework.Domain.ProductLocusInfo;
import com.gk.firework.Domain.Utils.StringUtils;
import com.gk.firework.Domain.Vo.DirectionDetail;
import com.gk.firework.Domain.Vo.FireworkDeal;
import com.gk.firework.Domain.Vo.ProductLocusVo;
import com.gk.firework.Domain.Vo.ProductVo;
import com.gk.firework.Mapper.ProductLocusInfoMapper;
import com.gk.firework.Service.ProductLocusService;
import com.gk.firework.Service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author : jingjy
 * @date : 2021/3/24 14:34
 */
@Service("ProductLocusService")
public class ProductLocusServiceImpl extends ServiceImpl<ProductLocusInfoMapper, ProductLocusInfo> implements ProductLocusService {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductLocusInfoMapper productLocusInfoMapper;
 
    @Override
    public int insertProductLocus(ProductLocusInfo productLocusInfo) {
        int i = 0;
        if (productLocusInfo == null){
            return i;
        }
        String slice = productService.getSlice(productLocusInfo.getDirectioncode());
        i = productLocusInfoMapper.insertProductLocus(productLocusInfo, slice);
        return i;
    }
 
    @Override
    public int insertBatch(List<ProductLocusInfo> productLocusInfos) {
        int i = 0;
        if (productLocusInfos == null || productLocusInfos.size() == 0){
            return i;
        }
        for (ProductLocusInfo productLocusInfo : productLocusInfos){
            i = insertProductLocus(productLocusInfo);
        }
        return i;
    }
 
    /**
    * @Description: 条码流向查询
    * @date 2021/4/14 16:01
    */
    @Override
    public List<ProductLocusVo> getFlow(String directionCode) {
 
        ArrayList<ProductLocusVo> result = new ArrayList<>();
        //判空和长度判断
        if (StringUtils.isBlank(directionCode)) throw new BusinessException("流向码不能为空");
        if (FireworkDeal.isNotDirectionCode(directionCode)) throw new BusinessException("流向码非法");
        //根据创建时间倒叙排序
        DirectionDetail directionDetail = FireworkDeal.dealDirectionCode(directionCode);
        List<ProductVo> productVos = new ArrayList<>();
        ProductVo productVo = productService.selectVoByDirection(directionCode);
        if (productVo == null)  throw new BusinessException("产品不存在");
        String slice = productService.getSlice(directionCode);
        //22位码
        if (FireworkDeal.is22Characters(directionCode)){
            FireworkDeal.getProductVos(directionCode,directionDetail,directionDetail,productVos,productVo);
            if (productVos.size() < 1) throw new BusinessException("产品流向码出现问题");
            List<String>  codes= new ArrayList<>();
            for (ProductVo product : productVos) {
                codes.add(product.getDirectionCode());
            }
            //22位查询
            List<ProductLocusVo> flow_22 = productLocusInfoMapper.selectLists(directionCode, slice);
            if (flow_22.size() < 1) throw new BusinessException("查询结果不存在");
            result.addAll(flow_22);
            //19位查询
            List<ProductLocusVo> flow_19 = productLocusInfoMapper.selectFlowByCodes(codes, slice);
            assert flow_19.size() > 0;
            result.addAll(flow_19);
        }else{
            //19位查询
            List<ProductLocusVo> flow_19 =  productLocusInfoMapper.selectLists(directionCode,slice);
            //19找22位箱
            Integer boxNumber = productVo.getBoxNumber();
            String serialNo = directionDetail.getSerialNo();
            Integer serial = Integer.parseInt(serialNo);
            int left_open_interval = serial - boxNumber;
            int right_close_interval = serial;
            List<String> codes = new ArrayList<>();
            for (int i = left_open_interval + 1; i <= right_close_interval; i++) {
                int abs = Math.abs(i);
                String itemCode = directionDetail.getItemCode();
                String dateCode = directionDetail.getDateCode();
                String serials = String.format("%05d", abs);
                String boxNumberCode = String.format("%03d", boxNumber);
                String code = String.format("%s%s%s%s", itemCode, dateCode, serials, boxNumberCode);
                codes.add(code);
            }
            List<ProductLocusVo> flow_22 =  productLocusInfoMapper.selectFlowByCodes(codes,slice);
            assert flow_22.size() > 0;
            result.addAll(flow_22);
            result.addAll(flow_19);
        }
 
        if (result.size() >0 )
            return result.stream().sorted(Comparator.comparing(ProductLocusVo::getCreateddate).reversed()).collect(Collectors.toList());
 
        return result;
    }
 
    @Override
    public int saveBatchLocus(List<ProductLocusInfo> productLocuses) {
        if (productLocuses == null || productLocuses.size() == 0) {
            throw new BusinessException("系统入参为空");
        }
        return productLocusInfoMapper.insertBatch(productLocuses);
    }
}