#
Junjie
2024-01-05 9bc4b04f1b946e31c719ec051c3c481f5faac5e8
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
package com.zy.asrs.common.wms.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.common.domain.dto.LocTypeDto;
import com.zy.asrs.common.domain.dto.StartupDto;
import com.zy.asrs.common.domain.entity.StaDesc;
import com.zy.asrs.common.sys.service.StaDescService;
import com.zy.asrs.common.wms.entity.*;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.exception.CoolException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 货架核心功能
 * Created by vincent on 2020/6/11
 */
@Slf4j
@Service
public class CommonService {
 
    @Autowired
    private WrkMastService wrkMastService;
    @Autowired
    private WrkLastnoService wrkLastnoService;
    @Autowired
    private RowLastnoService rowLastnoService;
    @Autowired
    private StaDescService staDescService;
    @Autowired
    private BasDevpService basDevpService;
    @Autowired
    private LocMastService locMastService;
    @Autowired
    private LocDetlService locDetlService;
 
    /**
     * 生成工作号
     *
     * @param wrkMk
     * @return workNo(工作号)
     */
    public int getWorkNo(Integer wrkMk) {
        WrkLastno wrkLastno = wrkLastnoService.getOne(new LambdaQueryWrapper<WrkLastno>().eq(WrkLastno::getWrkMk, wrkMk));
        if (Cools.isEmpty(wrkLastno)) {
            throw new CoolException("数据异常,请联系管理员");
        }
 
        int workNo = wrkLastno.getWrkNo();
        int sNo = wrkLastno.getStartNo();
        int eNo = wrkLastno.getTargetNo();
        workNo = workNo >= eNo ? sNo : workNo + 1;
        while (true) {
            WrkMast wrkMast = wrkMastService.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, workNo));
            if (null != wrkMast) {
                workNo = workNo >= eNo ? sNo : workNo + 1;
            } else {
                break;
            }
        }
        // 修改序号记录
        if (workNo > 0) {
            wrkLastno.setWrkNo(workNo);
            wrkLastnoService.updateById(wrkLastno);
        }
        // 检验
        if (workNo == 0) {
            throw new CoolException("生成工作号失败,请联系管理员");
        } else {
            if (wrkMastService.getOne(new LambdaQueryWrapper<WrkMast>().eq(WrkMast::getWrkNo, workNo)) != null) {
                throw new CoolException("生成工作号" + workNo + "在工作档中已存在");
            }
        }
        return workNo;
    }
 
    /**
     * 检索库位号
     */
    @Transactional
    public StartupDto getLocNo(Integer staDescId, Integer sourceStaNo, Long hostId, LocTypeDto locTypeDto, int times) {
        // 目标库位
        LocMast locMast = null;
 
        // 靠近摆放规则 --- 空托
        locMast = getLocNoStep4(staDescId, locTypeDto, sourceStaNo);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
 
//        //库位规则
//        locMast = getLocNoStepRule(matnr, locTypeDto, staDescId, sourceStaNo);
//        if (locMast != null) {
//            //找到库位,返回dto
//            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
//        }
 
//        // 靠近摆放规则 --- 相同订单号, 同天同规格物料
//        locMast = getLocNoStep3(staDescId, matnr, batch, grade, locTypeDto, sourceStaNo);
//        if (locMast != null) {
//            //找到库位,返回dto
//            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
//        }
 
        //搜索整个空库位组
        locMast = getLocNoStepSingle(locTypeDto, staDescId, sourceStaNo, hostId);
        if (locMast != null) {
            //找到库位,返回dto
            return getLocNoStep6(staDescId, sourceStaNo, locMast);//返回dto
        }
 
        //找不到库位,抛出异常
        throw new CoolException("没有空库位");
    }
 
//    /**
//     * 库位规则
//     */
//    private LocMast getLocNoStepRule(String matnr, LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo) {
//        LocMast locMast = null;
//
//        // 库区锁定
//        List<LocRule> locRules = locRuleService.find(matnr);
//        if (locRules == null || locRules.isEmpty()) {
//            return null;//没有库位规则,跳出当前任务
//        }
//
//        // 获取目标站
//        Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
//                .eq("type_no", staDescId)
//                .eq("stn_no", sourceStaNo);
//        StaDesc staDesc = staDescService.selectOne(wrapper);
//        if (staDesc == null) {
//            throw new CoolException("入库路径不存在");
//        }
//
//        for (LocRule locRule : locRules) {
//            if (locRule == null) {
//                continue;
//            }
//
//            List<LocMast> locMasts = locMastService.queryFreeLocMast2(locTypeDto.getLocType1(), locRule.getRowBeg(), locRule.getRowEnd(), locRule.getBayBeg(), locRule.getBayEnd(), locRule.getLevBeg(), locRule.getLevEnd(), staDesc.getCrnNo());
//            for (LocMast locMast0 : locMasts) {
//                //预留空库位
//                if (locMastService.checkEmptyCount(locMast0, 10)) {
//                    return locMast0;
//                }
//            }
//        }
//
//        if (locRules.get(0).getKeepGo() == 0) {
//            //找不到空库位,且禁止继续寻找其他非混载区域库位
//            //找不到库位,抛出异常
//            throw new CoolException("规则区域没有空库位");
//        }
//
//        return locMast;
//    }
 
//    // 靠近摆放规则 --- 相同订单号, 同天同规格物料
//    private LocMast getLocNoStep3(Integer staDescId, String matnr, String batch, String grade, LocTypeDto locTypeDto, Integer sourceStaNo) {
//        LocMast locMast = null;
//        if (staDescId == 1) {
//            // 获取目标站
//            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
//                    .eq("type_no", staDescId)
//                    .eq("stn_no", sourceStaNo);
//            StaDesc staDesc = staDescService.selectOne(wrapper);
//            if (staDesc == null) {
//                throw new CoolException("入库路径不存在");
//            }
//
//            //先判断工作档,查找主档IOType=1, wrkSts < 10的工作明细,料号相同的明细库位
//            List<WrkMast> wrkMasts = wrkMastService.selectSameMatnrs(matnr, batch, grade, staDesc.getCrnNo());
//            for (WrkMast wrkMast : wrkMasts) {
//                LocMast locMast0 = locMastService.findNearloc(wrkMast.getLocNo());
//                if (null != locMast0) {
//                    // 浅库位符合尺寸检测
//                    if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
//                        locMast = locMast0;
//                    }
//                }
//            }
//            if (Cools.isEmpty(locMast)) {
//                //再判断库存明细档,料号相同的明细库位
//                List<String> locNos = locDetlService.getSameDetl(matnr, batch, grade, staDesc.getCrnNo());
//                for (String locNo : locNos) {
//                    LocMast locMast0 = locMastService.findNearloc(locNo);
//                    if (null != locMast0) {
//                        // 浅库位符合尺寸检测
//                        if (VersionUtils.checkLocType(locMast0, locTypeDto)) {
//                            locMast = locMast0;
//                            break;
//                        }
//                    }
//                }
//            }
//        }
//
//        return locMast;
//    }
 
    // 靠近摆放规则 --- 空托
    private LocMast getLocNoStep4(Integer staDescId, LocTypeDto locTypeDto, Integer sourceStaNo) {
        LocMast locMast = null;
        // 靠近摆放规则 --- 空托
        if (staDescId == 10) {
            // 获取目标站
            LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
                    .eq(StaDesc::getTypeNo, staDescId)
                    .eq(StaDesc::getStnNo, sourceStaNo);
            StaDesc staDesc = staDescService.getOne(wrapper);
            if (staDesc == null) {
                throw new CoolException("入库路径不存在");
            }
 
            List<LocMast> locMasts = locMastService.list(new LambdaQueryWrapper<LocMast>()
                    .eq(LocMast::getLocSts, "O")
                    .eq(LocMast::getLocType1, 1)
                    .eq(LocMast::getCrnNo, staDesc.getDeviceNo())
                    .orderByAsc(LocMast::getLev1, LocMast::getBay1, LocMast::getRow1));
            if (!locMasts.isEmpty()) {
                for (LocMast loc : locMasts) {
//                    LocMast locMast0 = locMastService.findNearloc(loc.getLocNo());
//                    if (null != locMast0) {
//                        locMast = locMast0;
//                        break;
//                    }
                    locMast = loc;
                    break;
                }
            }
 
            if (locMast == null) {
                throw new CoolException("缺少空库位");
            }
        }
        return locMast;
    }
 
    // 搜索单品(整个库位组)
    private LocMast getLocNoStepSingle(LocTypeDto locTypeDto, Integer staDescId, Integer sourceStaNo, Long hostId) {
        LocMast locMast = null;
        // 获取目标站
        LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
                .eq(StaDesc::getTypeNo, staDescId)
                .eq(StaDesc::getStnNo, sourceStaNo)
                .eq(StaDesc::getHostId, hostId);
        StaDesc staDesc = staDescService.getOne(wrapper);
        if (staDesc == null) {
            throw new CoolException("入库路径不存在");
        }
 
        List<LocMast> locMasts = locMastService.list(new LambdaQueryWrapper<LocMast>().eq(LocMast::getLocSts, "O").eq(LocMast::getHostId, hostId));//搜索货物
 
        if (locMasts == null) {
            return null;
        }
 
        for (LocMast mast : locMasts) {
            locMast = mast;
            break;
        }
 
        return locMast;
    }
 
    //返回dto
    private StartupDto getLocNoStep6(Integer staDescId, Integer sourceStaNo, LocMast locMast) {
        StartupDto startupDto = new StartupDto();
        // 获取目标站
        LambdaQueryWrapper<StaDesc> wrapper = new LambdaQueryWrapper<StaDesc>()
                .eq(StaDesc::getTypeNo, staDescId)
                .eq(StaDesc::getStnNo, sourceStaNo);
        StaDesc staDesc = staDescService.getOne(wrapper);
        if (Cools.isEmpty(staDesc)) {
            log.error("入库路径不存在, staDescId={}, sourceStaNo={}", staDescId, sourceStaNo);
            throw new CoolException("入库路径不存在");
        }
 
        // 检测目标站
        BasDevp staNo = basDevpService.getById(staDesc.getDeviceStn());
        if (!staNo.getAutoing().equals("Y")) {
            throw new CoolException("目标站" + staDesc.getDeviceStn() + "不可用");
        }
 
        // 生成工作号
        int workNo = getWorkNo(0);
        // 返回dto
        startupDto.setWorkNo(workNo);
        startupDto.setSourceStaNo(sourceStaNo);
        startupDto.setStaNo(staNo.getDevNo());
        startupDto.setLocNo(locMast.getLocNo());
        return startupDto;
    }
}