WarecellAllocationServiceImpl.java 21.4 KB
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
package com.huaheng.api.wcs.service.warecellAllocation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.api.wcs.domain.ToLocation;
import com.huaheng.common.utils.Wrappers;
import com.huaheng.api.wcs.domain.WCSInfo;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.FilterConfigDetail.domain.FilterConfigDetail;
import com.huaheng.pc.config.FilterConfigDetail.service.FilterConfigDetailService;
import com.huaheng.pc.config.configValue.domain.ConfigValue;
import com.huaheng.pc.config.configValue.service.ConfigValueService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.containerType.domain.ContainerType;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationHigh.domain.LocationHigh;
import com.huaheng.pc.config.locationHigh.service.LocationHighService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.config.material.domain.Material;
import com.huaheng.pc.config.material.service.MaterialService;
import com.huaheng.pc.config.materialType.domain.MaterialType;
import com.huaheng.pc.config.materialType.service.MaterialTypeService;
import com.huaheng.pc.config.receiptPreference.domain.ReceiptPreference;
import com.huaheng.pc.config.receiptPreference.service.ReceiptPreferenceService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.service.ReceiptDetailService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.TransferTaskService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.huaheng.api.wcs.domain.WcsTask;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WarecellAllocationServiceImpl implements WarecellAllocationService {

    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private LocationService locationService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private ConfigService configService;
    @Resource
    private ConfigValueService configValueService;
    @Resource
    private ReceiptPreferenceService receiptPreferenceService;
    @Resource
    private FilterConfigDetailService filterConfigDetailService;
    @Resource
    private TaskDetailService taskDetailService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ReceiptContainerDetailService receiptContainerDetailService;
    @Resource
    private ReceiptContainerHeaderService receiptContainerHeaderService;
    @Resource
    private ReceiptDetailService receiptDetailService;
    @Resource
    private MaterialService materialService;
    @Resource
    private MaterialTypeService materialTypeService;
    @Resource
    private WorkTaskService workTaskService;
    @Resource
    private TransferTaskService transferTaskService;
    @Resource
    private LocationAllocationService locationAllocationService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationHighService locationHighService;

    /**
     * 立库仓位分配
     * 1、判断非空字段
     * 2、实体转换
     * 3、查询满足条件的库位类型
     *
     * @param wcsTask
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult warecellAllocation(WcsTask wcsTask) {
        //1、判断非空字段
        if (StringUtils.isEmpty(wcsTask.getTaskNo())) {
            return AjaxResult.error("任务号为空");
        }
        if (StringUtils.isEmpty(wcsTask.getArea())) {
            return AjaxResult.error("目的区域为空");
        }
        if (StringUtils.isNull(wcsTask.getLength())) {
            return AjaxResult.error("长为空");
        }
        if (StringUtils.isNull(wcsTask.getWidth())) {
            return AjaxResult.error("宽为空");
        }
        if (StringUtils.isNull(wcsTask.getHeight())) {
            return AjaxResult.error("高为空");
        }
        if (StringUtils.isEmpty(wcsTask.getWarehouseCode())) {
            return AjaxResult.error("仓库号为空");
        }
        if (StringUtils.isEmpty(wcsTask.getRoadWay())) {
            return AjaxResult.error("巷道为空");
        }
//        long time1=System.currentTimeMillis();
        AjaxResult ajaxResult= verticalWarehouseAllocation(wcsTask);
//        long time2=System.currentTimeMillis();
//        System.out.println("=====请求库位接口========"+(time2-time1));
        return ajaxResult;
    }

    public AjaxResult verticalWarehouseAllocation(WcsTask wcsTask) {
        String warehouseCode = wcsTask.getWarehouseCode();
        String area = wcsTask.getArea();
        String locationCode = null;
        String height =  wcsTask.getHeight();
        String roadWayStr =  wcsTask.getRoadWay();
        List<String> roadWay = new ArrayList<>();
        roadWay.add(roadWayStr);
        if(height == null) {
            return AjaxResult.error("分配库位时,高度为空,任务号"+wcsTask.getTaskNo());
        }
        Zone zone=zoneService.getByArea(area,warehouseCode);
        if(zone == null) {
            return AjaxResult.error("分配库位时,没有找到库区,任务号"+wcsTask.getTaskNo()+"区域area"+area);
        }
        TaskHeader taskHeader = taskHeaderService.getById(wcsTask.getTaskNo());
        if(taskHeader == null) {
            return AjaxResult.error("分配库位时,根据任务号没有找到任务,任务号"+wcsTask.getTaskNo());
        }
        if (taskHeader.getStatus() == QuantityConstant.TASK_STATUS_COMPLETED) {
            return AjaxResult.error("任务已经完成,不能再分库位,任务号"+wcsTask.getTaskNo());
        }
        String containerCode = taskHeader.getContainerCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if(container == null) {
            return AjaxResult.error("分配库位时,任务托盘为空,托盘号"+containerCode);
        }
        container.setHeight(Integer.parseInt(wcsTask.getHeight()));
        containerService.updateById(container);

        //查询满足条件的库位类型
        LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(LocationType::getZoneCode, zone.getCode())
                .eq(LocationType::getWarehouseCode, warehouseCode);
        List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);
        if(locationTypeList == null||locationTypeList.size()==0) {
            return AjaxResult.error("分配库位时,没有找到库位类型,库区编码"+zone.getCode()+",仓库编码"+warehouseCode);
        }
        List<String> locationTypeCodeList = locationTypeList.stream().
                map(t -> t.getCode()).collect(Collectors.toList());
        int highHeight = Float.valueOf(height).intValue();
        LambdaQueryWrapper<LocationHigh> locationHighLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationHighLambdaQueryWrapper.eq(LocationHigh::getHighLevel, highHeight)
                .in(LocationHigh::getLocationTypeCode, locationTypeCodeList);
        LocationHigh locationHigh = locationHighService.getOne(locationHighLambdaQueryWrapper);
        if(locationHigh==null){
            return AjaxResult.error("未绑定定位规则,库位高度查找失败,高度等级"+highHeight);
        }
        int high = locationHigh.getHigh();
        //查询任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailLambda = Wrappers.lambdaQuery();
        taskDetailLambda.eq(TaskDetail::getTaskId, wcsTask.getTaskNo());
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambda);
        String value = configService.getKey(QuantityConstant.RULE_ALLOCATION,warehouseCode);
        if (StringUtils.isEmpty(value)) {
            return AjaxResult.error("未绑定定位规则,配置key"+QuantityConstant.RULE_ALLOCATION);
        }
        int allocationRule = Integer.parseInt(value);
        String materialAreaCode = null;
        /* 循环查询入库组盘明细*/
        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetailList) {
            ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailService.
                    getById(taskDetail.getAllocationId());
            if (receiptContainerDetail != null) {
                receiptContainerDetailList.add(receiptContainerDetail);
            }
        }
        //去重
        receiptContainerDetailList = receiptContainerDetailList.stream().distinct().collect(Collectors.toList());
        if(receiptContainerDetailList != null && receiptContainerDetailList.size() > 0) {
            String materialCode = receiptContainerDetailList.get(0).getMaterialCode();
            Material material = materialService.getMaterialByCode(materialCode, warehouseCode);
            materialAreaCode = material.getMaterialAreaCode();
        }
        locationCode = locationAllocationService.allocation(allocationRule,
             locationTypeCodeList, high, area, roadWay, warehouseCode, containerCode, materialAreaCode);
        if (StringUtils.isEmpty(locationCode)) {
            return AjaxResult.error("没有库位可分配");
        }

        locationService.updateStatus(locationCode,
                QuantityConstant.STATUS_LOCATION_LOCK, warehouseCode);
        if (StringUtils.isNotEmpty(taskHeader.getToLocation()) &&
                !locationCode.equals(taskHeader.getToLocation())) {
            locationService.updateStatus(taskHeader.getToLocation(),
                    QuantityConstant.STATUS_LOCATION_EMPTY, warehouseCode);
        }

        if (receiptContainerDetailList != null && receiptContainerDetailList.size() > 0) {
            //更新库位编码到组盘头表
            ReceiptContainerDetail receiptContainerDetail = receiptContainerDetailList.get(0);
            if (receiptContainerDetail != null) {
                ReceiptContainerHeader receiptContainerHeader =
                        receiptContainerHeaderService.getById(receiptContainerDetail.getReceiptContainerId());
                receiptContainerHeader.setToLocation(locationCode);
                if (!receiptContainerHeaderService.updateById(receiptContainerHeader)) {
                    throw new ServiceException("更新库位失败");
                }
                //把库位编码赋到该入库组盘头表下的所有明细
                LambdaQueryWrapper<ReceiptContainerDetail> lambda = Wrappers.lambdaQuery();
                lambda.eq(ReceiptContainerDetail::getReceiptContainerId, receiptContainerHeader.getId())
                        .eq(ReceiptContainerDetail::getWarehouseCode, warehouseCode);
                List<ReceiptContainerDetail> receiptContainerDetails = receiptContainerDetailService.list(lambda);
                for (ReceiptContainerDetail receiptContainerDetail2 : receiptContainerDetails) {
                    receiptContainerDetail2.setLocationCode(locationCode);
                    if (!receiptContainerDetailService.updateById(receiptContainerDetail2)) {
                        throw new ServiceException("更新库位编码到入库组盘明细");
                    }
                }
            }
        }

        for (TaskDetail taskDetail : taskDetailList) {
            taskDetail.setToLocation(locationCode);
            if (!taskDetailService.updateById(taskDetail)) {
                throw new ServiceException("更新任务明细目标库位失败");
            }
        }

        //修改任务明细目标库位
        Location location = locationService.getLocationByCode(locationCode, warehouseCode);
        int rowFlag = location.getRowFlag().intValue();
        Integer preTaskNo = 0;
        //如果是外侧库位,那么就要判断该库位对应的内侧库位是不是有托盘
        if (rowFlag == QuantityConstant.ROW_OUT) {
            Location insideLocation = locationService.getInsideNear(location);
            if (StringUtils.isNotEmpty(insideLocation.getContainerCode())) {
                Location destinationLocation = locationService.getEmptyLocation(insideLocation);
                if (destinationLocation == null) {
                    throw new ServiceException("创建移库任务失败, 目标库位为空");
                }
                AjaxResult ajaxResult = transferTaskService.createTransferTask(insideLocation.getCode(),
                        destinationLocation.getCode(), warehouseCode);
                preTaskNo = (Integer) ajaxResult.getData();
                if (ajaxResult.hasErr()) {
                    throw new ServiceException("创建移库任务失败");
                }
                return AjaxResult.success("移库任务先执行");
            }
        } else {
            Location outSideLocation = locationService.getOutSideNear(location);
            if(outSideLocation != null) {
                TaskHeader outSideTaskHeader = taskHeaderService.getTaskHeaderByLocation(outSideLocation.getCode());
                if (outSideTaskHeader != null) {
                    preTaskNo = outSideTaskHeader.getId();
                }
            }
        }
        taskHeader.setZoneCode(location.getZoneCode());
        taskHeader.setPreTaskNo(preTaskNo);
        taskHeader.setWeight(wcsTask.getWeight());
        taskHeader.setToLocation(locationCode);
        if (!taskHeaderService.updateById(taskHeader)) {
            throw new ServiceException("更新任务头表目标库位失败");
        }
        ToLocation toLocation = new ToLocation();
        toLocation.setToLocationCode(locationCode);
        WcsTask wcsTask1 = new WcsTask();
        wcsTask1.setToLocationCode(locationCode);
        return AjaxResult.success().setData(toLocation);
    }

    /**
     * 库位筛选
     *
     * @param locationList     库位列表
     * @param locationTypeList 库位类型列表
     * @param roadway          巷道
     * @return
     */
    public String filter(List<Location> locationList, List<LocationType> locationTypeList, String roadway) {
        List<String> codeList = locationTypeList.stream().map(t -> t.getCode()).collect(Collectors.toList());
        List<Location> newLocation = locationList.stream().filter(t -> codeList.contains(t.getLocationType()) && t.getRoadway().equals(roadway)).collect(Collectors.toList());
        if (newLocation.isEmpty()) {
            return null;
        } else {
            return newLocation.get(0).getCode();
        }
    }

    /**
     * 定位规则
     * @return
     */
    @Override
    public String taskPositioning() {
        //物料类别中定位规则为空时,查询入库首选项
        LambdaQueryWrapper<ConfigValue> configValueLambda = Wrappers.lambdaQuery();
        configValueLambda.eq(ConfigValue::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(ConfigValue::getModuleType, "receipt")
                .eq(ConfigValue::getRecordType, "入库首选项");
        ConfigValue configValue = configValueService.getOne(configValueLambda);
        LambdaQueryWrapper<ReceiptPreference> receiptPreferenceLambda = Wrappers.lambdaQuery();
        receiptPreferenceLambda.eq(ReceiptPreference::getCode, configValue.getValue())
                .eq(ReceiptPreference::getWarehouseCode, ShiroUtils.getWarehouseCode());
        ReceiptPreference receiptPreference = receiptPreferenceService.getOne(receiptPreferenceLambda);
        String locatingRule = receiptPreference.getLocationRule();

        //通过定位规则查找自定义sql
        if (StringUtils.isEmpty(locatingRule)) {
            throw new ServiceException("未绑定定位规则,入库首选项编码"+configValue.getValue());
        }
        LambdaQueryWrapper<FilterConfigDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(FilterConfigDetail::getCode, locatingRule)
                .eq(FilterConfigDetail::getWarehouseCode,ShiroUtils.getWarehouseCode());
        FilterConfigDetail filterConfigDetail = filterConfigDetailService.getOne(lambdaQueryWrapper);
        return filterConfigDetail.getStatement();
    }


    /**
     * 去向分配
     */
    @Override
    public AjaxResult destinationAllocation(WcsTask wcsTask) {

        //1、判断非空字段
        if (StringUtils.isEmpty(wcsTask.getTaskNo())) {
            return AjaxResult.error("任务号为空");
        }
        if (StringUtils.isNull(wcsTask.getLength())) {
            return AjaxResult.error("长为空");
        }
        if (StringUtils.isNull(wcsTask.getWidth())) {
            return AjaxResult.error("宽为空");
        }
        if (StringUtils.isNull(wcsTask.getHeight())) {
            return AjaxResult.error("高为空");
        }
//        if (StringUtils.isNull(wcsTask.getWeight())) {
//            return AjaxResult.error("重为空");
//        }

        //查询满足条件的库位类型
        LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.gt(LocationType::getLength, wcsTask.getLength())
                .gt(LocationType::getWidth, wcsTask.getWidth())
                .gt(LocationType::getHeight, wcsTask.getHeight())
                .gt(LocationType::getMaxWeight, wcsTask.getWidth());
        List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);

        if (locationTypeList.isEmpty()) {
            return AjaxResult.error("没有区域可分配");
        }
        List<String> codeList = new ArrayList<>();
        for (LocationType locationType : locationTypeList) {
            codeList.add(locationType.getCode());
        }

        //定位库位List
        List<String> positioningLocationCodeList = null;
        //查询任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailLambda = Wrappers.lambdaQuery();
        taskDetailLambda.eq(TaskDetail::getTaskId, wcsTask.getTaskNo());
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLambda);

        /* 循环查询入库组盘明细*/
        List<ReceiptContainerDetail> receiptContainerDetailList = new ArrayList<>();
        for (TaskDetail taskDetail : taskDetailList) {
            receiptContainerDetailList.add(receiptContainerDetailService.getById(taskDetail.getAllocationId()));
        }
        //去重
        receiptContainerDetailList = receiptContainerDetailList.stream().distinct().collect(Collectors.toList());

        for (ReceiptContainerDetail receiptContainerDetail : receiptContainerDetailList) {
            String locatingRule = this.taskPositioning();

            LambdaQueryWrapper<FilterConfigDetail> filterConfigDetailLambda = Wrappers.lambdaQuery();
            filterConfigDetailLambda.eq(FilterConfigDetail::getCode, locatingRule)
                    .eq(FilterConfigDetail::getWarehouseCode, ShiroUtils.getWarehouseCode());
            FilterConfigDetail filterConfigDetail = filterConfigDetailService.getOne(filterConfigDetailLambda);
            String[] locatingRules = filterConfigDetail.getStatement().split("limit");

            //根据定位规则查询库位编码
            LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
            locationLambda.last(locatingRules[0]);
            List<Location> locationList = locationService.list(locationLambda);
            List<String> locationCodeList = locationTypeList.stream().map(t -> t.getCode()).collect(Collectors.toList());
            List<Location> newLocation = locationList.stream().filter(t -> locationCodeList.contains(t.getLocationType())).collect(Collectors.toList());
            if (!newLocation.isEmpty()) {
                positioningLocationCodeList.add(newLocation.get(0).getCode());
            }

            if (StringUtils.isEmpty(positioningLocationCodeList)) {
                throw new ServiceException("没有区域可分配");
            }
        }

        LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
        locationLambdaQueryWrapper.eq(Location::getCode, positioningLocationCodeList.get(0));
        Location location = locationService.getOne(locationLambdaQueryWrapper);
        String destinationArea = location.getRoadway();
        return AjaxResult.success(destinationArea);
    }

}