LocationAllocationServiceImpl.java 8.01 KB
package com.huaheng.api.wcs.service.warecellAllocation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.common.constant.Constants;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
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.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;

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

import static java.util.stream.Collectors.toList;

@Service
public class LocationAllocationServiceImpl implements LocationAllocationService {

    @Resource
    private LocationService locationService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private TaskHeaderService taskHeaderService;

    @Override
    public String allocation(int locationRule, List<String> locationTypeCodeList, int high, String area, String warehouseCode, String containerCode, Integer materialAreas) {
        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getCode, containerCode)
                .eq(Container::getWarehouseCode, warehouseCode);
        Container container = containerService.getOne(containerLambdaQueryWrapper);
        LambdaQueryWrapper<ContainerType> containerTypeLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerTypeLambdaQueryWrapper.eq(ContainerType::getCode, container.getContainerType())
                .eq(ContainerType::getWarehouseCode, warehouseCode);
        ContainerType containerType = containerTypeService.getOne(containerTypeLambdaQueryWrapper);
        String locationType = containerType.getLocationType();
        List<LocationType> locationTypeList = new ArrayList<>();
        String[] list = locationType.split(",");
        for(String str : list) {
            LambdaQueryWrapper<LocationType> locationTypeLambdaQueryWrapper = Wrappers.lambdaQuery();
            locationTypeLambdaQueryWrapper.eq(LocationType::getWarehouseCode, warehouseCode)
                    .eq(LocationType::getCode, str);
            LocationType locationType1 = locationTypeService.getOne(locationTypeLambdaQueryWrapper);
            locationTypeList.add(locationType1);
        }
        List<String> locationTypeCodes = locationTypeList.stream().
                map(t -> t.getCode()).collect(toList());
        List<String> mergelocationTypeCodeList = locationTypeCodeList.stream().filter(item -> locationTypeCodes.contains(item)).collect(toList());
//        if(area == null) {
//           LambdaQueryWrapper<Location> locationLambdaQueryWrapper = Wrappers.lambdaQuery();
//           locationLambdaQueryWrapper.in(Location::getLocationType, mergelocationTypeCodeList)
//                                    .eq(Location::getDeleted, 0);
//           List<Location> locations = locationService.list(locationLambdaQueryWrapper);
//           Location location = locations.get(0);
//           area = location.getArea();
//        }
        switch (locationRule) {
            case QuantityConstant.DOUBLE_FORK:
                return doubleRk(area, high, warehouseCode, mergelocationTypeCodeList, materialAreas);
            case QuantityConstant.SINGLE_FORK:
                return singleRk(area, high, warehouseCode, mergelocationTypeCodeList, materialAreas);
        }
        return null;
    }

    /*
     * 双伸位库位入库分配库位
     */
    private String doubleRk(String area, int high, String warehouseCode, List<String> locationTypeCodeList, Integer materialAreas) {
        LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
        locationLambda.eq(Location::getArea, area).
                eq(Location::getWarehouseCode, warehouseCode)
                .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY)
                .eq(Location::getHigh, high)
                .eq(Location::getRowFlag, QuantityConstant.ROW_OUT)
                .eq(Location::getContainerCode, "");
        List<Location> locationList = locationService.list(locationLambda);
        List<Location> removeLocaationList = new ArrayList<>();
        for (Location location1 : locationList) {
            if (taskHeaderService.getUncompleteTaskInNear(location1) > 0) {
                removeLocaationList.add(location1);
            }
            Integer areas = location1.getMaterialAreas();
            if(areas.intValue() != materialAreas.intValue()) {
                removeLocaationList.add(location1);
            }
        }
        locationList.removeAll(removeLocaationList);
        if (locationList == null || locationList.size() == 0) {
            locationLambda = Wrappers.lambdaQuery();
            locationLambda.eq(Location::getArea, area).
                    eq(Location::getWarehouseCode, warehouseCode)
                    .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY)
                    .eq(Location::getHigh, high)
                    .eq(Location::getRowFlag, QuantityConstant.ROW_IN)
                    .eq(Location::getContainerCode, "");
            locationList = locationService.list(locationLambda);
            removeLocaationList = new ArrayList<>();
            for (Location location1 : locationList) {
                if (taskHeaderService.getUncompleteTaskInNear(location1) > 0) {
                    removeLocaationList.add(location1);
                }
            }
            locationList.removeAll(removeLocaationList);
        }
        String locationCode =  filter(locationList, locationTypeCodeList, area);
        return locationCode;
    }

    /*
     * 单伸位库位入库分配库位
     */
    private String singleRk(String area, int high, String warehouseCode, List<String> locationTypeCodeList, Integer materialAreas) {
        LambdaQueryWrapper<Location> locationLambda = Wrappers.lambdaQuery();
        locationLambda.eq(Location::getArea, area).
                eq(Location::getWarehouseCode, warehouseCode)
                .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY)
                .eq(Location::getHigh, high)
                .in(Location::getLocationType, locationTypeCodeList)
                .eq(Location::getContainerCode, "");
        List<Location> locationList = locationService.list(locationLambda);
        List<Location> removeLocaationList = new ArrayList<>();
        if(!(materialAreas.intValue() == QuantityConstant.NOT_MATERIAL_AREAS)) {
            for (Location location1 : locationList) {
                Integer areas = location1.getMaterialAreas();
                if (areas.intValue() != materialAreas.intValue()) {
                    removeLocaationList.add(location1);
                }
            }
        }
        locationList.removeAll(removeLocaationList);
        Location location = locationList.stream().findFirst().orElse(null);
        String locationCode = location.getCode();
        return locationCode;
    }

    private String filter(List<Location> locationList, List<String> locationTypeList, String area) {
        List<Location> newLocation = locationList.stream().filter(t -> locationTypeList.contains(t.getLocationType()) && t.getArea().equals(area)).collect(toList());
        if (newLocation.isEmpty()) {
            return null;
        } else {
            return newLocation.get(0).getCode();
        }
    }
}