EmptyContainerController.java 17.5 KB
package com.huaheng.api.wcs.controller;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.wcs.domain.ManyEmptyDomain;
import com.huaheng.api.wcs.domain.WarecellLocationDomain;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.service.ConfigService;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
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.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;

@RestController
@RequestMapping("/API/WMS/v2")
public class EmptyContainerController extends BaseController {

    @Resource
    private WorkTaskService workTaskService;
    @Resource
    private LocationAllocationService allocationService;
    @Resource
    private ConfigService configService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationService locationService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private LocationHighService locationHighService;
    @Resource
    private ContainerService containerService;
    @Resource
    private ContainerTypeService containerTypeService;
    @Autowired
    private AddressService addressService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskAssignService taskAssignService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private IDictDataService iDictDataService;
    @Resource
    private StationService stationService;

    /**
     * 生成空托盘出库任务
     * @return
     */
    @PostMapping("/emptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托盘出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "emptyOut", from = "ROBOT")
    public AjaxResult emptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("emptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult =
                    inventoryHeaderService.createEmptyOut(manyEmptyDomain.getContainerCode(), null, manyEmptyDomain.getPort(), QuantityConstant.WAREHOUSECODE);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘入库任务
     * @return
     */
    @PostMapping("/emptyIn")
    @Log(title = "任务-任务管理", operating = "生成空托盘入库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "WCS空托入库", from = "WCS")
    public AjaxResult emptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("emptyIn", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult =
                    inventoryHeaderService.createEmptyIn(manyEmptyDomain.getContainerCode(), null, manyEmptyDomain.getArea(), manyEmptyDomain.getPort());
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘组入库任务
     * @return
     */
    @PostMapping("/manyEmptyIn")
    @Log(title = "任务-任务管理", operating = "生成空托盘组入库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "manyEmptyIn", from = "ROBOT")
    public AjaxResult manyEmptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("manyEmptyIn", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerManyEmptyIn(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    /**
     * 生成空托盘组出库任务
     * @return
     */
    @PostMapping("/manyEmptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托盘组出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "manyEmptyOut", from = "ROBOT")
    public AjaxResult manyEmptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("manyEmptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerManyEmptyOut(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    private AjaxResult innerManyEmptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        if (manyEmptyDomain == null) {
            throw new ServiceException("空托盘组参数不对!");
        }
        String containerCode = manyEmptyDomain.getContainerCode();
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String roadWay = manyEmptyDomain.getRoadWay();
        List<String> roadWays = new ArrayList<>();
        String[] str = roadWay.split(",");
        roadWays = Arrays.asList(str);

        if (StringUtils.isEmpty(manyEmptyDomain.getArea())) {
            throw new ServiceException("参数area不能为空!");
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        if (zone == null) {
            return AjaxResult.error("分配库位时,没有找到库区,area" + area);
        }
        // String value = configService.getKey(QuantityConstant.RULE_ALLOCATION);
        String value = iDictDataService.getDictValueByLabel(QuantityConstant.RULE_ALLOCATION, zone.getCode());

        String height = manyEmptyDomain.getHeight();
        /*
         * if (StringUtils.isEmpty(height)) {
         * return AjaxResult.error("高度参数为空,请输入高度");
         * }
         */
        if (StringUtils.isEmpty(value)) {
            return AjaxResult.error(zone.getCode() + "库区未绑定定位规则,请到字典管理中绑定库区定位规则");
        }
        int allocationRule = Integer.parseInt(value);

        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getContainerCode, containerCode).eq(TaskHeader::getWarehouseCode, warehouseCode).lt(TaskHeader::getStatus,
            QuantityConstant.TASK_STATUS_COMPLETED);
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader != null) {
            return AjaxResult.success("已经生成空托盘组任务");
        }
        // 查询满足条件的库位类型
        LambdaQueryWrapper<LocationType> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(LocationType::getZoneCode, zone.getCode()).eq(LocationType::getWarehouseCode, warehouseCode);
        List<LocationType> locationTypeList = locationTypeService.list(lambdaQueryWrapper);
        if (locationTypeList == null) {
            return AjaxResult.error(zone.getCode() + "库区分配库位时,没有找到库位类型,请到库位类型确认库区");
        }
        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);
        int high = locationHigh.getHigh();

        WarecellLocationDomain domain =
            allocationService.createWarecellLcaotionDoamin(allocationRule, locationTypeCodeList, high, area, roadWays, warehouseCode, containerCode, null);
        String locationCode = allocationService.allocation(domain);
        if (locationCode == null) {
            return AjaxResult.error("分配库位时,没有找到库位");
        }
        return workTaskService.createManyEmptyIn(containerCode, locationCode, warehouseCode);
    }

    private AjaxResult innerManyEmptyOut(ManyEmptyDomain manyEmptyDomain) {
        if (manyEmptyDomain == null) {
            throw new ServiceException("空托盘组参数不对!");
        }
        if (StringUtils.isEmpty(manyEmptyDomain.getArea())) {
            throw new ServiceException("参数area不能为空!");
        }
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String port = manyEmptyDomain.getPort();
        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getStatus, QuantityConstant.STATUS_CONTAINER_MANY).eq(Container::getWarehouseCode, warehouseCode);
        List<Container> containerList = containerService.list(containerLambdaQueryWrapper);
        List<Container> removeContainerList = new ArrayList<>();
        if (containerList != null && containerList.size() > 0) {
            for (Container container : containerList) {
                String locationCode = container.getLocationCode();
                Location location = locationService.getLocationByCode(locationCode, warehouseCode);
                if (!location.getArea().equals(area)) {
                    removeContainerList.add(container);
                }
            }
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
            .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_MANY_EMPTYSHIPMENT).eq(TaskHeader::getPort, port).eq(TaskHeader::getZoneCode, zone.getCode());
        TaskHeader taskHeader = taskHeaderService.getOne(taskHeaderLambdaQueryWrapper);
        if (taskHeader != null) {
            WcsTask wcsTask = new WcsTask();
            wcsTask.setTaskType(taskHeader.getTaskType());
            wcsTask.setTaskNo(String.valueOf(taskHeader.getId()));
            wcsTask.setContainerCode(taskHeader.getContainerCode());
            wcsTask.setWarehouseCode(warehouseCode);
            wcsTask.setFromLocationCode(taskHeader.getFromLocation());
            wcsTask.setPlatform(QuantityConstant.PLATFORM_WMS);
            wcsTask.setPriority(1000);
            wcsTask.setTaskStatus(taskHeader.getStatus());
            wcsTask.setToPort(port);
            wcsTask = taskAssignService.switchTaskTypeToWcs(wcsTask, taskHeader);
            return AjaxResult.success(wcsTask);
        }
        containerList.removeAll(removeContainerList);
        if (!(containerList != null && containerList.size() > 0)) {
            return AjaxResult.error("没有找到合适的空托盘组!");
        }
        String containerCode = containerList.get(0).getCode();
        Container container = containerService.getContainerByCode(containerCode, warehouseCode);
        if (container == null) {
            throw new ServiceException("没有找到托盘“");
        }
        return workTaskService.createManyEmptyOut(containerCode, warehouseCode, port);
    }

    /**
     * 生成空托出库任务
     * @return
     */
    @PostMapping("/wcsCallEmptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "生成空托出库任务", from = "WCS")
    public AjaxResult wcsCallEmptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess("wcsCallEmptyOut", new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerWcsCallEmptyOut(manyEmptyDomain);
                return ajaxResult;
            }
        });
        return ajaxResult;
    }

    private AjaxResult innerWcsCallEmptyOut(ManyEmptyDomain manyEmptyDomain) {
        boolean isSuc = false;
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String port = manyEmptyDomain.getPort();
        String areaByWcs = manyEmptyDomain.getAreaByWcs();

        if (warehouseCode == null) {
            return AjaxResult.error("warehouseCode 不能为空");
        }
        if (area == null) {
            return AjaxResult.error("area 不能为空");
        }
        if (port == null) {
            return AjaxResult.error("port 不能为空");
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getWarehouseCode, warehouseCode).eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        if (zone == null) {
            return AjaxResult.error("area不正确,没有找到对应区域");
        }
        String zoneCode = zone.getCode();
        List<TaskHeader> taskHeaders =
            taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>().eq(TaskHeader::getPort, port).eq(TaskHeader::getZoneCode, zoneCode)
                .eq(TaskHeader::getWarehouseCode, QuantityConstant.WAREHOUSECODE).lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        isSuc = !taskHeaders.isEmpty();
        switch (area) {
            // 外观仓 可以出多个空托盘
            case "6":
                isSuc = taskHeaders.size() > 2;
                break;
            default:
        }
        // 有该站台任务 返回成功
        if (isSuc) {
            return AjaxResult.success();
        }
        // 对应巷道 站台出到 选择对应库内容器
        Station station = stationService.getStaionByCode(port);

        String[] strs = station.getRoadWay().split(",");
        List<String> roadways = Arrays.asList(strs);
        List<Location> list = containerService.getEmptyContainerInLocation(zoneCode, "", "", warehouseCode, null, roadways);
        if (list.isEmpty()) {
            return AjaxResult.error(zoneCode + "库区没有找到空托盘");
        }

        List<Location> areaByWcsLocations = new ArrayList<>();
        // 获取最大库位高度 空托盘
        Optional<Location> max = list.stream().max(Comparator.comparing(Location::getHigh));
        Location location1 = max.get();
        // 获取最大库位高度空托盘列表
        areaByWcsLocations = list.stream().filter(e -> e.getHigh().equals(location1.getHigh())).collect(Collectors.toList());
        areaByWcsLocations =
            areaByWcsLocations.stream().sorted(Comparator.comparing(Location::getILayer).thenComparing(Location::getId)).collect(Collectors.toList());
        if (StringUtils.isNotEmpty(areaByWcs)) {
            switch (areaByWcs) {
                case "A":
                    areaByWcsLocations.sort(Comparator.comparing(Location::getIColumn, (l1, l2) -> {
                        return l1.compareTo(l2);
                    }));
                    break;
                case "B":
                    areaByWcsLocations.sort(Comparator.comparing(Location::getIColumn, (l1, l2) -> {
                        return l2.compareTo(l1);
                    }));
                    break;
                default:
            }
        }

        Location location = areaByWcsLocations.get(0);
        String containerCode = location.getContainerCode();
        String locationCode = location.getCode();
        return inventoryHeaderService.createEmptyOut(containerCode, locationCode, port, warehouseCode);
    }
}