自动化立体仓库 - WMS系统
zwl
3 天以前 2acfc2d2a0e956910c51bd996f443b3cb9bd3dc9
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
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.zy.common.service;
 
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.RowLastno;
import com.zy.asrs.entity.RowLastnoType;
import com.zy.asrs.service.BasCrnDepthRuleService;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.RowLastnoService;
import com.zy.asrs.service.StaDescService;
import com.zy.common.model.CrnDepthRuleProfile;
import com.zy.common.model.LocTypeDto;
import com.zy.common.model.StartupDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
 
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class CommonServiceRun2AllocationTest {
 
    @Mock
    private BasCrnpService basCrnpService;
    @Mock
    private StaDescService staDescService;
    @Mock
    private BasDevpService basDevpService;
    @Mock
    private LocMastService locMastService;
    @Mock
    private BasCrnDepthRuleService basCrnDepthRuleService;
    @Mock
    private RowLastnoService rowLastnoService;
 
    private CommonService commonService;
 
    @BeforeEach
    void setUp() {
        commonService = new CommonService();
        ReflectionTestUtils.setField(commonService, "basCrnpService", basCrnpService);
        ReflectionTestUtils.setField(commonService, "staDescService", staDescService);
        ReflectionTestUtils.setField(commonService, "basDevpService", basDevpService);
        ReflectionTestUtils.setField(commonService, "locMastService", locMastService);
        ReflectionTestUtils.setField(commonService, "basCrnDepthRuleService", basCrnDepthRuleService);
        ReflectionTestUtils.setField(commonService, "rowLastnoService", rowLastnoService);
    }
 
    @Test
    void findRun2EmptyLocByCrnNos_shouldStopAtFirstMatchingCraneInOrder() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 2, 1, 4);
        RowLastnoType rowLastnoType = standardRowLastnoType();
        StartupDto startupDto = new StartupDto();
        LocTypeDto locTypeDto = fullLocType((short) 1);
        LocMast firstCraneLoc = openLoc("0200101", 2, 2, 1, 1);
 
        when(basCrnpService.selectById(2)).thenReturn(activeCrn(2));
        when(basCrnDepthRuleService.resolveProfile(eq(rowLastno), eq(2), any()))
                .thenReturn(singleExtensionProfile(1, 2));
        when(locMastService.selectList(any())).thenReturn(Collections.singletonList(firstCraneLoc));
 
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findRun2EmptyLocByCrnNos",
                rowLastno, rowLastnoType, Arrays.asList(2, 1), locTypeDto,
                1, 100, startupDto, 1, "run2-order", false);
 
        assertEquals("0200101", result.getLocNo());
        assertEquals(Integer.valueOf(2), result.getCrnNo());
        verify(locMastService, times(1)).selectList(any());
    }
 
    @Test
    void findRun2EmptyLocByCrnNos_shouldSkipInactiveCrane() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 2, 1, 4);
        RowLastnoType rowLastnoType = standardRowLastnoType();
        StartupDto startupDto = new StartupDto();
        LocTypeDto locTypeDto = fullLocType((short) 1);
        LocMast secondCraneLoc = openLoc("0400101", 1, 4, 1, 1);
 
        when(basCrnpService.selectById(2)).thenReturn(inactiveCrn(2));
        when(basCrnpService.selectById(1)).thenReturn(activeCrn(1));
        when(basCrnDepthRuleService.resolveProfile(eq(rowLastno), eq(1), any()))
                .thenReturn(singleExtensionProfile(1, 4));
        when(locMastService.selectList(any())).thenReturn(Collections.singletonList(secondCraneLoc));
 
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findRun2EmptyLocByCrnNos",
                rowLastno, rowLastnoType, Arrays.asList(2, 1), locTypeDto,
                1, 100, startupDto, 1, "run2-skip-offline", false);
 
        assertEquals("0400101", result.getLocNo());
        assertEquals(Integer.valueOf(1), result.getCrnNo());
        verify(locMastService, times(1)).selectList(any());
    }
 
    @Test
    void findOpenLocsByRow_forSingleExtension_shouldOrderByLevThenBay() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 1, 1, 2);
        RowLastnoType rowLastnoType = standardRowLastnoType();
 
        when(locMastService.selectList(any())).thenReturn(Collections.emptyList());
 
        ReflectionTestUtils.invokeMethod(commonService, "findOpenLocsByRow",
                rowLastno, rowLastnoType, 1, 1, fullLocType((short) 1), true);
 
        @SuppressWarnings("rawtypes")
        ArgumentCaptor<Wrapper> wrapperCaptor = ArgumentCaptor.forClass(Wrapper.class);
        verify(locMastService).selectList(wrapperCaptor.capture());
        String sqlSegment = wrapperCaptor.getValue().getSqlSegment().toLowerCase();
 
        assertTrue(sqlSegment.contains("order by"));
        assertTrue(sqlSegment.indexOf("lev1") < sqlSegment.indexOf("bay1"));
    }
 
    @Test
    void findConfiguredEmptyLocForCrn_forDoubleExtension_shouldPreferDeepOpenLoc() {
        RowLastno rowLastno = doubleExtensionRowLastno();
        RowLastnoType rowLastnoType = standardRowLastnoType();
        LocMast shallowLoc = openLoc("0100101", 1, 1, 1, 1);
        LocMast deepOpenLoc = openLoc("0200101", 1, 2, 1, 1);
 
        when(basCrnDepthRuleService.resolveProfile(eq(rowLastno), eq(1), any()))
                .thenReturn(doubleExtensionProfile());
        when(locMastService.selectList(any())).thenReturn(Collections.singletonList(shallowLoc));
        when(locMastService.selectOne(any())).thenReturn(deepOpenLoc);
 
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, rowLastnoType, 1, 1, fullLocType((short) 1));
 
        assertEquals("0200101", result.getLocNo());
    }
 
    @Test
    void findConfiguredEmptyLocForCrn_forDoubleExtension_shouldFallbackToShallowWhenDeepBlocked() {
        RowLastno rowLastno = doubleExtensionRowLastno();
        RowLastnoType rowLastnoType = standardRowLastnoType();
        LocMast shallowLoc = openLoc("0100101", 1, 1, 1, 1);
        LocMast deepBlockedLoc = blockedLoc("0200101", 1, 2, 1, 1, "F");
 
        when(basCrnDepthRuleService.resolveProfile(eq(rowLastno), eq(1), any()))
                .thenReturn(doubleExtensionProfile());
        when(locMastService.selectList(any())).thenReturn(Collections.singletonList(shallowLoc));
        when(locMastService.selectOne(any())).thenReturn(null, deepBlockedLoc);
 
        LocMast result = ReflectionTestUtils.invokeMethod(commonService, "findConfiguredEmptyLocForCrn",
                rowLastno, rowLastnoType, 1, 1, fullLocType((short) 1));
 
        assertEquals("0100101", result.getLocNo());
    }
 
    @Test
    void getNextRun2CurrentRow_shouldAdvanceWithinRunnableCrnsOnly() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 4, 1, 8);
 
        Integer nextRow = ReflectionTestUtils.invokeMethod(commonService, "getNextRun2CurrentRow",
                rowLastno, Arrays.asList(2, 4), 2, 3);
 
        assertEquals(Integer.valueOf(7), nextRow);
    }
 
    @Test
    void resolveRun2CrnNo_shouldPreferCurrentCrnNoOverCurrentRow() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 4, 1, 8);
        rowLastno.setCurrentRow(7);
        rowLastno.setCurrentCrnNo(2);
 
        Integer currentCrnNo = ReflectionTestUtils.invokeMethod(commonService, "resolveRun2CrnNo", rowLastno);
 
        assertEquals(Integer.valueOf(2), currentCrnNo);
    }
 
    @Test
    void resolveRun2CrnNo_shouldFallbackToStartCrnNoWhenCurrentCrnNoIsInvalid() {
        RowLastno rowLastno = singleExtensionRowLastno(2, 4, 3, 8);
 
        rowLastno.setCurrentCrnNo(null);
        assertEquals(Integer.valueOf(2),
                ReflectionTestUtils.invokeMethod(commonService, "resolveRun2CrnNo", rowLastno));
 
        rowLastno.setCurrentCrnNo(0);
        assertEquals(Integer.valueOf(2),
                ReflectionTestUtils.invokeMethod(commonService, "resolveRun2CrnNo", rowLastno));
 
        rowLastno.setCurrentCrnNo(5);
        assertEquals(Integer.valueOf(2),
                ReflectionTestUtils.invokeMethod(commonService, "resolveRun2CrnNo", rowLastno));
    }
 
    @Test
    void advanceNormalRun2Cursor_shouldUpdateCurrentRowAndCurrentCrnNoTogether() {
        RowLastno rowLastno = singleExtensionRowLastno(1, 4, 1, 8);
        rowLastno.setCurrentRow(1);
        rowLastno.setCurrentCrnNo(1);
 
        ReflectionTestUtils.invokeMethod(commonService, "advanceNormalRun2Cursor",
                rowLastno, 1, Arrays.asList(1, 3), 1);
 
        assertEquals(Integer.valueOf(5), rowLastno.getCurrentRow());
        assertEquals(Integer.valueOf(3), rowLastno.getCurrentCrnNo());
        verify(rowLastnoService).updateById(rowLastno);
    }
 
    @Test
    void advanceEmptyPalletRun2Cursor_shouldUpdateCurrentRowAndCurrentCrnNoTogether() throws Exception {
        RowLastno rowLastno = singleExtensionRowLastno(1, 4, 1, 8);
        rowLastno.setCurrentRow(5);
        rowLastno.setCurrentCrnNo(3);
        LocMast locMast = openLoc("0500101", 3, 5, 1, 1);
        Object searchResult = run2AreaSearchResult(locMast, rowLastno, Arrays.asList(1, 3));
 
        ReflectionTestUtils.invokeMethod(commonService, "advanceEmptyPalletRun2Cursor", searchResult, locMast);
 
        assertEquals(Integer.valueOf(1), rowLastno.getCurrentRow());
        assertEquals(Integer.valueOf(1), rowLastno.getCurrentCrnNo());
        verify(rowLastnoService).updateById(rowLastno);
    }
 
    private RowLastno singleExtensionRowLastno(int startCrnNo, int endCrnNo, int startRow, int endRow) {
        RowLastno rowLastno = new RowLastno();
        rowLastno.setWhsType(1);
        rowLastno.setsCrnNo(startCrnNo);
        rowLastno.seteCrnNo(endCrnNo);
        rowLastno.setsRow(startRow);
        rowLastno.seteRow(endRow);
        rowLastno.setTypeId(2);
        return rowLastno;
    }
 
    private RowLastno doubleExtensionRowLastno() {
        RowLastno rowLastno = new RowLastno();
        rowLastno.setWhsType(1);
        rowLastno.setsCrnNo(1);
        rowLastno.seteCrnNo(1);
        rowLastno.setsRow(1);
        rowLastno.seteRow(2);
        rowLastno.setTypeId(1);
        return rowLastno;
    }
 
    private RowLastnoType standardRowLastnoType() {
        RowLastnoType rowLastnoType = new RowLastnoType();
        rowLastnoType.setType(1);
        return rowLastnoType;
    }
 
    private Object run2AreaSearchResult(LocMast locMast, RowLastno rowLastno, List<Integer> runnableCrnNos) throws Exception {
        Class<?> clazz = Class.forName("com.zy.common.service.CommonService$Run2AreaSearchResult");
        Constructor<?> constructor = clazz.getDeclaredConstructor(LocMast.class, RowLastno.class, List.class);
        constructor.setAccessible(true);
        return constructor.newInstance(locMast, rowLastno, runnableCrnNos);
    }
 
    private CrnDepthRuleProfile singleExtensionProfile(int shallowRow, int searchRow) {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setLayoutType(1);
        profile.setSearchRows(Collections.singletonList(searchRow));
        profile.setShallowRows(Collections.singletonList(shallowRow));
        return profile;
    }
 
    private CrnDepthRuleProfile doubleExtensionProfile() {
        CrnDepthRuleProfile profile = new CrnDepthRuleProfile();
        profile.setLayoutType(2);
        profile.setSearchRows(Arrays.asList(1, 2));
        profile.setShallowRows(Collections.singletonList(1));
        profile.setDeepRows(Collections.singletonList(2));
        profile.getShallowToDeepRow().put(1, 2);
        profile.getDeepToShallowRow().put(2, 1);
        return profile;
    }
 
    private BasCrnp activeCrn(int crnNo) {
        BasCrnp basCrnp = new BasCrnp();
        basCrnp.setCrnNo(crnNo);
        basCrnp.setInEnable("Y");
        basCrnp.setOutEnable("Y");
        basCrnp.setCrnSts(3);
        basCrnp.setCrnErr(0L);
        return basCrnp;
    }
 
    private BasCrnp inactiveCrn(int crnNo) {
        BasCrnp basCrnp = activeCrn(crnNo);
        basCrnp.setInEnable("N");
        return basCrnp;
    }
 
    private LocTypeDto fullLocType(short locType1) {
        LocTypeDto locTypeDto = new LocTypeDto();
        locTypeDto.setLocType1(locType1);
        return locTypeDto;
    }
 
    private LocMast openLoc(String locNo, int crnNo, int row, int bay, int lev) {
        LocMast locMast = new LocMast();
        locMast.setLocNo(locNo);
        locMast.setCrnNo(crnNo);
        locMast.setWhsType(1L);
        locMast.setRow1(row);
        locMast.setBay1(bay);
        locMast.setLev1(lev);
        locMast.setLocSts("O");
        locMast.setLocType1((short) 1);
        locMast.setLocType2((short) 2);
        return locMast;
    }
 
    private LocMast blockedLoc(String locNo, int crnNo, int row, int bay, int lev, String locSts) {
        LocMast locMast = openLoc(locNo, crnNo, row, bay, lev);
        locMast.setLocSts(locSts);
        return locMast;
    }
}