#
Junjie
2024-08-16 d4f47a3c09e994f896dc358df93656751a812e49
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
package com.zy.asrs.wms.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.framework.common.Cools;
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.enums.LocTypeHeightType;
import com.zy.asrs.wms.asrs.mapper.ViewLocDetlMapper;
import com.zy.asrs.wms.asrs.mapper.ViewTaskDetlMapper;
import com.zy.asrs.wms.asrs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
@Component
public class LocUtils {
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private LocService locService;
    @Autowired
    private SuggestLocRuleService suggestLocRuleService;
    @Autowired
    private LanewayRuleService lanewayRuleService;
    @Autowired
    private LocTypeService locTypeService;
    @Autowired
    private LocTypeBindService locTypeBindService;
    @Autowired
    private ViewLocDetlMapper viewLocDetlMapper;
    @Autowired
    private ViewTaskDetlMapper viewTaskDetlMapper;
 
    //从库存或任务中匹配相邻库位(满托盘)
    public Loc getNeighborLoc(Long taskType, OrderDetl orderDetl, Integer locTypeHeight) {
        //满托盘
        //从任务中进行匹配
        List<Loc> locs = new ArrayList<>();
        List<Map<String, Object>> list = viewTaskDetlMapper.getList(orderDetl.getMat$().getMatnr(), orderDetl.getBatch(), orderDetl.getUniqueField());
        for (Map<String, Object> map : list) {
            Task task = taskService.getById(map.get("taskId").toString());
            if(task == null) {
                continue;
            }
 
            String targetLoc = task.getTargetLoc();
            Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, targetLoc));
            if(one == null) {
                continue;
            }
            locs.add(one);
        }
 
        Loc loc = filterLoc(taskType, locs);
        if (loc != null) {
            return loc;
        }
 
        //从库存中进行匹配
        List<Loc> locs2 = new ArrayList<>();
        List<Map<String, Object>> list2 = viewLocDetlMapper.getList(orderDetl.getMat$().getMatnr(), orderDetl.getBatch(), orderDetl.getUniqueField());
        for (Map<String, Object> map : list2) {
            Loc one = locService.getById(map.get("locId").toString());
            if(one == null) {
                continue;
            }
            locs2.add(one);
        }
 
        Loc loc2 = filterLoc(taskType, locs2);
        if (loc2 != null) {
            return loc2;
        }
        return null;
    }
 
    //获取推荐库位(满托盘)
    public List<Loc> getSuggestLoc(Long taskType, Long matId, String batch, Integer locTypeHeight) {
        //满托盘
        List<Loc> locs = new ArrayList<>();
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);
        if (locTypeHeightType == null) {
            throw new CoolException("库位高度类型异常");
        }
        //获取库位高度
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));
        //符合库位高度的库位集合
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);
 
        LambdaQueryWrapper<SuggestLocRule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SuggestLocRule::getLocType, 1);
        wrapper.eq(SuggestLocRule::getMatId, matId);
        if (!Cools.isEmpty(batch)) {
            wrapper.eq(SuggestLocRule::getBatch, batch);
        }
        List<SuggestLocRule> suggestLocRules = suggestLocRuleService.list(wrapper);
        for (SuggestLocRule suggestLocRule : suggestLocRules) {
            LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(Loc::getRow1, suggestLocRule.getStartRow());
            queryWrapper.le(Loc::getRow1, suggestLocRule.getTargetRow());
            queryWrapper.ge(Loc::getBay1, suggestLocRule.getStartBay());
            queryWrapper.le(Loc::getBay1, suggestLocRule.getTargetBay());
            queryWrapper.ge(Loc::getLev1, suggestLocRule.getStartLev());
            queryWrapper.le(Loc::getLev1, suggestLocRule.getTargetLev());
            queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());
            queryWrapper.in(Loc::getId, locIdList);
            List<Loc> list = locService.list(queryWrapper);
            if (!list.isEmpty()) {
                locs.addAll(list);
            }
        }
        return locs;
    }
 
    //获取全局库位(完整巷道)
    public List<Loc> getGlobalLoc(Long taskType, Integer locTypeHeight) {
        List<Loc> locs = new ArrayList<>();
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);
        if (locTypeHeightType == null) {
            throw new CoolException("库位高度类型异常");
        }
        //获取库位高度
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));
        //符合库位高度的库位集合
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);
 
        LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());
        queryWrapper.in(Loc::getId, locIdList);
        List<Loc> list = locService.list(queryWrapper);
        if (!list.isEmpty()) {
            locs.addAll(list);
        }
        return locs;
    }
 
    //从库位集合中获取符合深浅的库位
    public Loc filterLoc(Long taskType, List<Loc> locs) {
        if (locs == null || locs.isEmpty()) {
            return null;
        }
 
        Loc defaultLoc = null;
        for (Loc loc : locs) {
            //获取库位所在巷道
            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$();
            }
 
            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(one.getLocStsId() != LocStsType.O.val()) {
                    continue;
                }
 
                //当前库位是空库位
                defaultLoc = one;
                break;
            }
 
            if (defaultLoc != null) {
                break;
            }
        }
 
        return defaultLoc;
    }
 
    //从库位集合中获取完整空巷道
    public Loc filterAllLoc(List<Loc> locs) {
        if (locs == null || locs.isEmpty()) {
            return null;
        }
 
        Loc defaultLoc = null;
        for (Loc loc : locs) {
            //获取库位所在巷道
            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$();
            }
 
            boolean flag = false;
            List<Loc> allLocs = new ArrayList<>();
            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(one.getLocStsId() != LocStsType.O.val()) {
                    flag = true;
                    break;
                }
 
                allLocs.add(one);
            }
 
            if (flag) {
                continue;
            }
 
            if (!allLocs.isEmpty()) {
                defaultLoc = allLocs.get(0);
                break;//找到一个完整空巷道
            }
        }
 
        return defaultLoc;
    }
 
    //从库存或任务中匹配相邻库位(空托盘)
    public Loc getNeighborEmptyLoc(Long taskType, Integer locTypeHeight) {
        //从任务中进行匹配
        List<Loc> locs = new ArrayList<>();
        List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskType, 10));
        for (Task task : list) {
            String targetLoc = task.getTargetLoc();
            Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, targetLoc));
            if(one == null) {
                continue;
            }
            locs.add(one);
        }
 
        Loc loc = filterLoc(taskType, locs);
        if (loc != null) {
            return loc;
        }
 
        //从库存中进行匹配
        List<Loc> locs2 = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocStsId, LocStsType.D.val()));
        Loc loc2 = filterLoc(taskType, locs2);
        if (loc2 != null) {
            return loc2;
        }
        return null;
    }
 
    //获取推荐库位(空托盘)
    public List<Loc> getSuggestEmptyLoc(Long taskType, Integer locTypeHeight) {
        List<Loc> locs = new ArrayList<>();
        //空托盘
        List<SuggestLocRule> suggestLocRules = suggestLocRuleService.list(new LambdaQueryWrapper<SuggestLocRule>().eq(SuggestLocRule::getLocType, 0));
        for (SuggestLocRule suggestLocRule : suggestLocRules) {
            LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(Loc::getRow1, suggestLocRule.getStartRow());
            queryWrapper.le(Loc::getRow1, suggestLocRule.getTargetRow());
            queryWrapper.ge(Loc::getBay1, suggestLocRule.getStartBay());
            queryWrapper.le(Loc::getBay1, suggestLocRule.getTargetBay());
            queryWrapper.ge(Loc::getLev1, suggestLocRule.getStartLev());
            queryWrapper.le(Loc::getLev1, suggestLocRule.getTargetLev());
            queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());
            List<Loc> list = locService.list(queryWrapper);
            if (!list.isEmpty()) {
                locs.addAll(list);
            }
        }
        return locs;
    }
 
    //获取全局库位(完整巷道)
    public List<Loc> getGlobalEmptyLoc(Long taskType, Integer locTypeHeight) {
        List<Loc> locs = new ArrayList<>();
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);
        if (locTypeHeightType == null) {
            throw new CoolException("库位高度类型异常");
        }
        //获取库位高度
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));
        //符合库位高度的库位集合
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);
 
        LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());
        queryWrapper.in(Loc::getId, locIdList);
        List<Loc> list = locService.list(queryWrapper);
        if (!list.isEmpty()) {
            locs.addAll(list);
        }
        return locs;
    }
 
}