#
Junjie
2024-08-30 48278d6051b6e58d648178964585d35ae000ec91
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
package com.zy.asrs.wms.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.wms.asrs.entity.LocDetl;
import com.zy.asrs.wms.asrs.entity.dto.OutDetlDto;
import com.zy.asrs.wms.asrs.entity.dto.OutLocDto;
import com.zy.asrs.wms.asrs.entity.param.OutParam;
import com.zy.asrs.wms.asrs.service.LocDetlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class OutUtils {
 
    @Autowired
    private LocDetlService locDetlService;
 
    public List<OutLocDto> merge(OutParam outParam) {
        HashMap<Long, OutLocDto> map = new HashMap<>();
 
        for (OutDetlDto detl : outParam.getDetls()) {
            LocDetl locDetl = locDetlService.getById(detl.getDetlId());
            if (locDetl == null) {
                continue;
            }
            detl.setStock(locDetl.getAnfme());
 
            if (map.containsKey(locDetl.getLocId())) {
                OutLocDto locDto = map.get(locDetl.getLocId());
                List<OutDetlDto> detlDtos = locDto.getDetls();
                detlDtos.add(detl);
 
                locDto.setDetls(detlDtos);
            }else {
                OutLocDto locDto = new OutLocDto();
                map.put(locDetl.getLocId(), locDto);
 
                List<OutDetlDto> detlDtos = new ArrayList<>();
                detlDtos.add(detl);
 
                locDto.setLocId(locDetl.getLocId());
                locDto.setDetls(detlDtos);
            }
        }
 
        List<OutLocDto> locDtos = new ArrayList<>();
        for (Map.Entry<Long, OutLocDto> entry : map.entrySet()) {
            OutLocDto locDto = entry.getValue();
            locDtos.add(locDto);
 
            List<LocDetl> list = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, locDto.getLocId()));
 
            Double sum = 0D;
            for (LocDetl locDetl : list) {
                sum += locDetl.getAnfme();
            }
 
            for (OutDetlDto detl : locDto.getDetls()) {
                sum -= detl.getAnfme();
            }
 
            locDto.setAll(sum == 0);
            locDto.setOperationPort(outParam.getOperationPort());
        }
 
        //add zero stock
        for (OutLocDto locDto : locDtos) {
            List<OutDetlDto> detls = locDto.getDetls();
            List<Long> detlIds = new ArrayList<>();
            for (OutDetlDto detl : detls) {
                detlIds.add(detl.getDetlId());
            }
 
            List<LocDetl> list = locDetlService.list(new LambdaQueryWrapper<LocDetl>().eq(LocDetl::getLocId, locDto.getLocId()).notIn(LocDetl::getId, detlIds));
            if (!list.isEmpty()) {
                List<OutDetlDto> detlDtos = locDto.getDetls();
                for (LocDetl locDetl : list) {
                    OutDetlDto outDetlDto = new OutDetlDto();
                    outDetlDto.setDetlId(locDetl.getId());
                    outDetlDto.setAnfme(0D);
                    outDetlDto.setStock(locDetl.getAnfme());
                    detlDtos.add(outDetlDto);
                }
                locDto.setDetls(detlDtos);
            }
        }
 
        return locDtos;
    }
 
}