1
昨天 29de5e9bf0fcd2771836f3135214e9de9e7f5204
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
package com.vincent.rsf.server.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.vincent.rsf.framework.common.Cools;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.api.entity.params.PdaGeneralParam;
import com.vincent.rsf.server.api.service.AgvService;
import com.vincent.rsf.server.api.service.InBoundService;
import com.vincent.rsf.server.api.utils.LocUtils;
import com.vincent.rsf.server.manager.controller.params.GenerateTaskParams;
import com.vincent.rsf.server.manager.entity.*;
import com.vincent.rsf.server.manager.enums.*;
import com.vincent.rsf.server.manager.service.*;
import com.vincent.rsf.server.manager.utils.LocManageUtil;
import com.vincent.rsf.server.system.constant.SerialRuleCode;
import com.vincent.rsf.server.system.utils.SerialRuleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * PDA入库操作Service实现类
 */
@Slf4j
@Service
public class InBoundServiceImpl implements InBoundService {
    @Autowired
    private DeviceSiteService deviceSiteService;
    @Autowired
    private DeviceBindService deviceBindService;
    @Autowired
    private WarehouseAreasService warehouseAreasService;
    @Autowired
    private BasContainerService basContainerService;
    @Autowired
    private BasStationService basStationService;
    @Autowired
    private LocService locService;
    @Autowired
    private TaskService taskService;
 
 
    private BasStation checkStaStatus(String barcode, String sta) {
        if (Cools.isEmpty(barcode)) {
            throw new CoolException("容器码不能为空");
        }
        if (Cools.isEmpty(sta)) {
            throw new CoolException("接驳位不能为空");
        }
        BasStation isBarcodeSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getBarcode, barcode)
                , false
        );
        if (!Cools.isEmpty(isBarcodeSta)) {
            throw new CoolException("该条码已被" + isBarcodeSta.getStationName() + "站绑定");
        }
        BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                .eq(BasStation::getStationName, sta)
        );
        if (Cools.isEmpty(basStation)) {
            throw new CoolException("未找到站点信息");
        }
        if (!basStation.getUseStatus().equals("O")) {
            throw new CoolException("站点状态不为空闲");
        }
        if (!Cools.isEmpty(basStation.getContainerType())) {
            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
            List<BasContainer> containers = basContainerService.list(
                    new LambdaQueryWrapper<BasContainer>()
                            .in(BasContainer::getContainerType, longs1)
            );
            boolean matches = false;
            for (BasContainer container : containers) {
                String codeType = container.getCodeType();  // 获取正则表达式
                if (barcode.matches(codeType)) {  // 判断条码是否符合这个正则
                    matches = true;
                    break;  // 找到匹配的就退出循环
                }
            }
//            boolean matches = containers.stream()
//                    .map(BasContainer::getCodeType)
//                    .anyMatch(codeType -> barcode.matches(codeType));
            if (!matches) {
                throw new CoolException("条码与站点不匹配");
            }
        } else {
            throw new CoolException("数据异常:验证基础信息");
        }
 
        return basStation;
    }
 
 
    private BasStation checkStaStatus(String barcode, String sta,String area) {
        if (Cools.isEmpty(barcode)) {
            throw new CoolException("容器码不能为空");
        }
        if (Cools.isEmpty(sta)) {
            throw new CoolException("接驳位不能为空");
        }
        BasStation isBarcodeSta = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                        .eq(BasStation::getBarcode, barcode)
                , false
        );
        if (!Cools.isEmpty(isBarcodeSta)) {
            throw new CoolException("该条码已被" + isBarcodeSta.getStationName() + "站绑定");
        }
        BasStation basStation = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                .eq(BasStation::getStationName, sta)
        );
        if (Cools.isEmpty(basStation)) {
            throw new CoolException("未找到站点信息");
        }
        if (!basStation.getUseStatus().equals("O")) {
            throw new CoolException("站点状态不为空闲");
        }
 
        List<String> areaList = JSONObject.parseArray(basStation.getCrossZoneArea(), String.class);
        if (!areaList.contains(area)) {
            throw new CoolException("当前站点不支持目标库区");
        }
        if (!Cools.isEmpty(basStation.getContainerType())) {
            List<Long> longs1 = JSONObject.parseArray(basStation.getContainerType(), Long.class);
            List<BasContainer> containers = basContainerService.list(
                    new LambdaQueryWrapper<BasContainer>()
                            .in(BasContainer::getContainerType, longs1)
            );
            boolean matches = false;
            boolean matches2 = true;
            for (BasContainer container : containers) {
                String codeType = container.getCodeType();  // 获取正则表达式
                if (barcode.matches(codeType)) {  // 判断条码是否符合这个正则
                    List<String> areaList2 = JSONObject.parseArray(container.getAreas(), String.class);
                    if (!areaList2.contains(area)) {
                        matches2 = false;
                        continue;
                    }
                    matches = true;
                    break;  // 找到匹配的就退出循环
                }
            }
//            boolean matches = containers.stream()
//                    .map(BasContainer::getCodeType)
//                    .anyMatch(codeType -> barcode.matches(codeType));
            if (!matches2) {
                throw new CoolException("查询到的容器不支持目标库区");
            }
            if (!matches) {
                throw new CoolException("条码与站点不匹配");
            }
        } else {
            throw new CoolException("数据异常:验证基础信息");
        }
 
        return basStation;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R generateTasks(PdaGeneralParam param, Long loginUserId) {
 
        DeviceSite deviceSite = deviceSiteService.getOne(new LambdaQueryWrapper<DeviceSite>().eq(DeviceSite::getSite,param.getTransferStationNo()).orderByDesc(DeviceSite::getId),false);
        if (Objects.isNull(deviceSite)) {
            throw new CoolException("站点不存在!!");
        }
        DeviceBind deviceBind = deviceBindService.getById(LocUtils.getAreaType(deviceSite.getSite()));
        if (Cools.isEmpty(deviceBind)) {
            throw new CoolException("库位规则未知");
        }
        WarehouseAreas warehouseArea = warehouseAreasService.getById(deviceBind.getTypeId());
        if (Cools.isEmpty(warehouseArea)) {
            throw new CoolException("未找到所属库区信息");
        }
 
//        BasContainer container = basContainerService.getOne(new LambdaUpdateWrapper<BasContainer>()
//                .eq(BasContainer::getCode, param.getContainerNo()));
//        if (Objects.isNull(container)) {
//            throw new CoolException("容器未维护入库,请维护后再操作!!");
//        }
        //验证基础信息
        checkStaStatus(param.getContainerNo(), param.getTransferStationNo());
        /**获取库位*/
        String targetLoc = LocManageUtil.getTargetLoc(warehouseArea.getId(), null);
        if (Cools.isEmpty(targetLoc)) {
            throw new CoolException("该站点对应库区未找到库位");
        }
 
        String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_TASK_CODE, null);
        if (StringUtils.isBlank(ruleCode)) {
            throw new CoolException("编码错误:请确认编码「SYS_TASK_CODE」是否已生成!!");
        }
        Task task = new Task();
        task.setTaskCode(ruleCode)
                .setTaskStatus(TaskStsType.GENERATE_IN.id)
                .setTaskType(TaskType.TASK_TYPE_EMPITY_IN.type)
                .setWarehType(WarehType.WAREHOUSE_TYPE_AGV.val)//lsh待修改
                .setTargLoc(targetLoc)
                .setOrgSite(deviceSite.getSite())
                .setBarcode(param.getContainerNo())
                .setTargSite(deviceSite.getDeviceSite())
                .setCreateBy(loginUserId)
                .setUpdateBy(loginUserId);
        if (!taskService.save(task)) {
            throw new CoolException("任务保存失败!!");
        }
        BasStation station = basStationService.getOne(new LambdaQueryWrapper<BasStation>()
                .eq(BasStation::getStationName, deviceSite.getSite()));
        if (Objects.isNull(station) || !station.getUseStatus().equals(LocStsType.LOC_STS_TYPE_O.type)) {
            throw new CoolException("站点不存在或站点不处于空库状态!!");
        }
        station.setUseStatus(LocStsType.LOC_STS_TYPE_R.type);
 
        if (!basStationService.updateById(station)) {
            throw new CoolException("站点状态更新失败!!");
        }
        if (!locService.update(new LambdaUpdateWrapper<Loc>().eq(Loc::getCode, task.getTargLoc())
                .set(Loc::getUseStatus, LocStsType.LOC_STS_TYPE_S.type).set(Loc::getBarcode, param.getContainerNo()))) {
            throw new CoolException("库位预约失败!!");
        }
        return R.ok("任务生成完毕!");
    }
 
}