#
Junjie
2024-09-20 053e6028bd20e599aea103401137816c6296a9ef
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
package com.zy.asrs.wms.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.enums.LocStsType;
import com.zy.asrs.wms.asrs.entity.param.FieldParam;
import com.zy.asrs.wms.asrs.entity.param.FieldSortParam;
import com.zy.asrs.wms.asrs.mapper.LocDetlMapper;
import com.zy.asrs.wms.asrs.mapper.ViewLocDetlMapper;
import com.zy.asrs.wms.asrs.service.LanewayRuleService;
import com.zy.asrs.wms.asrs.service.LocDetlFieldService;
import com.zy.asrs.wms.asrs.service.LocDetlService;
import com.zy.asrs.wms.asrs.service.LocService;
import com.zy.asrs.wms.common.domain.BaseParam;
import com.zy.asrs.wms.common.domain.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
@Service("locDetlService")
public class LocDetlServiceImpl extends ServiceImpl<LocDetlMapper, LocDetl> implements LocDetlService {
 
    @Autowired
    private ViewLocDetlMapper viewLocDetlMapper;
    @Autowired
    private LocDetlFieldService locDetlFieldService;
    @Autowired
    private LocService locService;
    @Autowired
    private LanewayRuleService lanewayRuleService;
 
    @Override
    public PageParam<ViewLocDetl, BaseParam> getPage(PageParam<ViewLocDetl, BaseParam> pageParam, QueryWrapper<ViewLocDetl> buildWrapper) {
        PageParam<ViewLocDetl, BaseParam> result = viewLocDetlMapper.selectPage(pageParam, buildWrapper);
 
        //解析动态字段
        JSONObject data = JSON.parseObject(JSON.toJSONString(result));
        List<ViewLocDetl> records = result.getRecords();
        data.put("records", records);
        for (ViewLocDetl locDetl : records) {
            Map<String, Object> resultMap = viewLocDetlMapper.getById(locDetl.getId());
            locDetl.syncFieldMap(resultMap);
        }
        return result;
    }
 
    @Override
    public List<LocDetl> getLocDetlList(Map<String, Object> map) {
        String matnr = null;
        String batch = null;
        if (map.containsKey("matnr")) {
            matnr = map.get("matnr").toString();
            map.remove("matnr");
        }
        if (map.containsKey("batch")) {
            batch = map.get("batch").toString();
            map.remove("batch");
        }
        ArrayList<FieldParam> param = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }
            FieldParam fieldParam = new FieldParam();
            fieldParam.setName(entry.getKey());
            fieldParam.setValue(entry.getValue());
            param.add(fieldParam);
        }
 
        List<Map<String, Object>> list2 = viewLocDetlMapper.getListLike(matnr, batch, param);
        List<LocDetl> locDetls = new ArrayList<>();
        for (Map<String, Object> objectMap : list2) {
            LocDetl locDetl = JSON.parseObject(JSON.toJSONString(objectMap), LocDetl.class);
            locDetl.syncFieldMap(objectMap);
            locDetls.add(locDetl);
        }
        return locDetls;
    }
 
    @Override
    public List<LocDetl> parseLocDetl(List<LocDetl> list) {
        for (LocDetl locDetl : list) {
            List<LocDetlField> locDetlFieldList = locDetlFieldService.list(new LambdaQueryWrapper<LocDetlField>().eq(LocDetlField::getDetlId, locDetl.getId()));
            locDetl.syncField(locDetlFieldList);
        }
        return list;
    }
 
    @Override
    public List<LocDetl> queryStock(String matnr, String batch, List<FieldParam> param) {
        return queryStock(matnr, batch, param, null);
    }
 
    @Override
    public List<LocDetl> queryStock(String matnr, String batch, List<FieldParam> param, List<FieldSortParam> sortParam) {
        List<Map<String, Object>> list = viewLocDetlMapper.getList(matnr, batch, param, sortParam);
        List<LocDetl> locDetlsSort = resortDetls(list);
        return locDetlsSort;
    }
 
    private List<LocDetl> resortDetls(List<Map<String, Object>> list) {
        List<LocDetl> locDetls = new ArrayList<>();
 
        for (Map<String, Object> map : list) {
            LocDetl locDetl = JSON.parseObject(JSON.toJSONString(map), LocDetl.class);
            locDetls.add(locDetl);
        }
 
        locDetls = parseLocDetl(locDetls);
 
        List<Long> sortDirctLoc = new ArrayList<>();
        HashMap<Long, List<LocDetl>> sortMap = new HashMap<>();
        for (LocDetl locDetl : locDetls) {
            List<LocDetl> detls = sortMap.get(locDetl.getLocId());
            if (detls == null) {
                detls = new ArrayList<>();
                detls.add(locDetl);
                sortMap.put(locDetl.getLocId(), detls);
            }else {
                detls.add(locDetl);
                sortMap.put(locDetl.getLocId(), detls);
            }
 
            Loc loc = locService.getById(locDetl.getLocId());
            if (loc == null) {
                continue;
            }
 
            if(!loc.getLocStsId().equals(LocStsType.F.val())){
                continue;
            }
 
            //获取库位所在巷道
            LanewayRule lanewayRule = lanewayRuleService.getLaneByLoc(loc);
            if(lanewayRule == null) {
                throw new CoolException("库位未配置巷道");
            }
 
            //获取库位方向
            List<Integer> direction = null;
            if (lanewayRule.getLaneX$().contains(loc.getRow1())) {
                direction = lanewayRule.getLaneX$();
            }else {
                direction = lanewayRule.getLaneY$();
            }
            Collections.reverse(direction);
 
            for (Integer row : direction) {
                Loc one = locService.getOne(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getRow1, row)
                        .eq(Loc::getBay1, loc.getBay1())
                        .eq(Loc::getLev1, loc.getLev1()));
                if (one == null) {
                    continue;
                }
 
                if (!sortDirctLoc.contains(one.getId())) {
                    sortDirctLoc.add(one.getId());
                }
            }
        }
 
        List<LocDetl> sortDirctDetls = new ArrayList<>();
 
        for (Long locId : sortDirctLoc) {
            List<LocDetl> detls = sortMap.get(locId);
            if(detls == null) {
                continue;
            }
 
            sortDirctDetls.addAll(detls);
        }
 
        return sortDirctDetls;
    }
 
}