package org.wcs.business.stacker;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.wcs.constant.enums.plc.StackerTaskTypeEnum;
import org.wcs.mapper.intf.AppStackerLocationService;
import org.wcs.mapper.intf.AppStackerStandService;
import org.wcs.model.po.app.AppStackerLocation;
import org.wcs.model.po.app.AppStackerStand;
import org.wcs.plugin.plc.model.StackerTask;
import org.wcs.utils.AppStringUtils;
import org.wcs.utils.AppObjectUtils;

import java.util.List;

/**
 * 堆垛机任务创建者
 */
@Component
@RequiredArgsConstructor
public class StackerTaskCreator {

    private final AppStackerLocationService stackerLocationDao;
    private final AppStackerStandService stackerStandDao;


    /**
     * 创建堆垛机入库任务
     * @param plcTaskId PLC任务ID
     * @param origin 起点
     * @param destination 终点
     * @param vehicleNo 载具号
     * @param vehicleSize 载具大小
     * @param weight 载具重量
     * @return 堆垛机入库任务
     */
    public StackerTask createStackerInTask(Integer plcTaskId, String origin, String destination, String vehicleNo, Short vehicleSize, Short weight)
    {
        try {
            if(plcTaskId == null || plcTaskId <= 0 || AppStringUtils.isEmpty(origin) || AppStringUtils.isEmpty(destination)) {
                return null;
            }
            List<AppStackerStand> stackerStands = stackerStandDao.queryStandInfo(origin);
            if(stackerStands == null || stackerStands.isEmpty()) {
                return null;
            }
            List<AppStackerLocation> destinationLocations = stackerLocationDao.queryStackerLocationById(destination);
            if(destinationLocations == null || destinationLocations.isEmpty()) {
                return null;
            }
            AppStackerStand originLocation = stackerStands.getFirst(); // 起点信息
            String[] originLocationData = originLocation.getStandLocation().replace("(","").replace(")","").split(",");
            AppStackerLocation destinationLocation = destinationLocations.getFirst(); // 终点信息
            StackerTask task = new StackerTask();
            task.setPlcTaskId(plcTaskId);
            task.setTaskType(StackerTaskTypeEnum.IN.getCode().shortValue());
            task.setGetStand(AppStringUtils.forceToShort(origin));
            task.setGetLaneId(originLocation.getLaneId().shortValue());
            task.setSetDepth(destinationLocation.getLaneId().shortValue());
            task.setSetStand(AppStringUtils.forceToShort(destination));
            task.setGetRow(AppObjectUtils.stringToInteger(originLocationData.length > 0, originLocationData[0]).shortValue());
            task.setGetLine(AppObjectUtils.stringToInteger(originLocationData.length > 1, originLocationData[1]).shortValue());
            task.setGetLayer(AppObjectUtils.stringToInteger(originLocationData.length > 2, originLocationData[2]).shortValue());
            task.setGetDepth(AppObjectUtils.stringToInteger(originLocationData.length > 3, originLocationData[3]).shortValue());
            task.setSetRow(destinationLocation.getLRow().shortValue());
            task.setSetLine(destinationLocation.getLLine().shortValue());
            task.setSetLayer(destinationLocation.getLLayer().shortValue());
            task.setSetDepth(destinationLocation.getLDepth().shortValue());
            task.setVehicleSize(vehicleSize);
            task.setWeight(weight);
            task.setVehicleNo(AppStringUtils.forceToInt(vehicleNo));
            return task;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 创建堆垛机入库任务
     * @param plcTaskId PLC任务ID
     * @param origin 起点
     * @param destination 终点
     * @param vehicleNo 载具号
     * @return 堆垛机入库任务
     */
    public StackerTask createStackerInTask(Integer plcTaskId, String origin, String destination, String vehicleNo) {
        return createStackerInTask(plcTaskId, origin, destination, vehicleNo, (short) 0, (short) 0);
    }

    /**
     * 创建堆垛机入口异常搬出任务
     * @param plcTaskId PLC任务ID
     * @param origin 起点
     * @param destination 终点
     * @param vehicleNo 载具号
     * @return 堆垛机错误任务
     */
    public StackerTask createStackerErrTask(Integer plcTaskId, String origin, String destination, String vehicleNo) {
        try {
            if(plcTaskId == null || plcTaskId <= 0 || AppStringUtils.isEmpty(origin) || AppStringUtils.isEmpty(destination)) {
                return null;
            }
            List<AppStackerStand> stackerOriginStands = stackerStandDao.queryStandInfo(origin);
            if(stackerOriginStands == null || stackerOriginStands.isEmpty()) {
                return null;
            }
            List<AppStackerStand> stackerDestinationStands = stackerStandDao.queryStandInfo(destination);
            if(stackerDestinationStands == null || stackerDestinationStands.isEmpty()) {
                return null;
            }
            AppStackerStand originLocation = stackerOriginStands.getFirst(); // 起点信息
            String[] originLocationData = originLocation.getStandLocation().replace("(","").replace(")","").split(",");
            AppStackerStand destinationLocation = stackerOriginStands.getFirst(); // 终点信息
            String[] destinationLocationData = originLocation.getStandLocation().replace("(","").replace(")","").split(",");
            StackerTask task = new StackerTask();
            task.setPlcTaskId(plcTaskId);
            task.setTaskType(StackerTaskTypeEnum.ERROR.getCode().shortValue());
            task.setGetStand(AppStringUtils.forceToShort(origin));
            task.setGetLaneId(originLocation.getLaneId().shortValue());
            task.setSetDepth(destinationLocation.getLaneId().shortValue());
            task.setSetStand(AppStringUtils.forceToShort(destination));
            task.setGetRow(AppObjectUtils.stringToInteger(originLocationData.length > 0, originLocationData[0]).shortValue());
            task.setGetLine(AppObjectUtils.stringToInteger(originLocationData.length > 1, originLocationData[1]).shortValue());
            task.setGetLayer(AppObjectUtils.stringToInteger(originLocationData.length > 2, originLocationData[2]).shortValue());
            task.setGetDepth(AppObjectUtils.stringToInteger(originLocationData.length > 3, originLocationData[3]).shortValue());
            task.setSetRow(AppObjectUtils.stringToInteger(destinationLocationData.length > 0, destinationLocationData[0]).shortValue());
            task.setSetLine(AppObjectUtils.stringToInteger(destinationLocationData.length > 1, destinationLocationData[1]).shortValue());
            task.setSetLayer(AppObjectUtils.stringToInteger(destinationLocationData.length > 2, destinationLocationData[2]).shortValue());
            task.setSetDepth(AppObjectUtils.stringToInteger(destinationLocationData.length > 3, destinationLocationData[3]).shortValue());
            task.setVehicleSize((short)0);
            task.setWeight((short)0);
            task.setVehicleNo(AppStringUtils.forceToInt(vehicleNo));
            return task;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 创建堆垛机出库任务
     * @param plcTaskId PLC任务ID
     * @param origin 起点
     * @param destination 终点
     * @param vehicleNo 载具号
     * @param vehicleSize 载具尺寸
     * @param weight 载具重量
     * @return 堆垛机出库任务
     */
    public StackerTask createStackerOutTask(Integer plcTaskId, String origin, String destination, String vehicleNo, Short vehicleSize, Short weight)
    {
        try {
            if(plcTaskId == null || plcTaskId <= 0 || AppStringUtils.isEmpty(origin) || AppStringUtils.isEmpty(destination)) {
                return null;
            }
            // 起点
            List<AppStackerLocation> originLocations = stackerLocationDao.queryStackerLocationById(origin);
            if(originLocations == null || originLocations.isEmpty()) {
                return null;
            }
            // 终点
            List<AppStackerStand> stackerStands = stackerStandDao.queryStandInfo(destination);
            if(stackerStands == null || stackerStands.isEmpty()) {
                return null;
            }
            AppStackerStand destinationLocation = stackerStands.getFirst(); // 终点信息
            String[] destinationLocationData = destinationLocation.getStandLocation().replace("(","").replace(")","").split(",");
            AppStackerLocation originLocation = originLocations.getFirst(); // 起点信息
            StackerTask task = new StackerTask();
            task.setPlcTaskId(plcTaskId);
            task.setTaskType(StackerTaskTypeEnum.OUT.getCode().shortValue());
            task.setGetStand(AppStringUtils.forceToShort(origin));
            task.setGetLaneId(originLocation.getLaneId().shortValue());
            task.setSetDepth(destinationLocation.getLaneId().shortValue());
            task.setSetStand(AppStringUtils.forceToShort(destination));
            task.setGetRow(originLocation.getLRow().shortValue());
            task.setGetLine(originLocation.getLLine().shortValue());
            task.setGetLayer(originLocation.getLLayer().shortValue());
            task.setGetDepth(originLocation.getLDepth().shortValue());
            task.setSetRow(AppObjectUtils.stringToInteger(destinationLocationData.length > 0, destinationLocationData[0]).shortValue());
            task.setSetLine(AppObjectUtils.stringToInteger(destinationLocationData.length > 1, destinationLocationData[1]).shortValue());
            task.setSetLayer(AppObjectUtils.stringToInteger(destinationLocationData.length > 2, destinationLocationData[2]).shortValue());
            task.setSetDepth(AppObjectUtils.stringToInteger(destinationLocationData.length > 3, destinationLocationData[3]).shortValue());
            task.setVehicleSize(vehicleSize);
            task.setWeight(weight);
            task.setVehicleNo(AppStringUtils.forceToInt(vehicleNo));
            return task;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 创建堆垛机移库任务
     * @param plcTaskId PLC任务ID
     * @param origin 起点
     * @param destination 终点
     * @param vehicleNo 载具号
     * @param vehicleSize 载具尺寸
     * @param weight 载具重量
     * @return 堆垛机移库任务
     */
    public StackerTask createStackerMoveTask(Integer plcTaskId, String origin, String destination, String vehicleNo, Short vehicleSize, Short weight) {
        try {
            if(plcTaskId == null || plcTaskId <= 0 || AppStringUtils.isEmpty(origin) || AppStringUtils.isEmpty(destination)) {
                return null;
            }
            // 起点
            List<AppStackerLocation> originLocations = stackerLocationDao.queryStackerLocationById(origin);
            if(originLocations == null || originLocations.isEmpty()) {
                return null;
            }
            // 终点
            List<AppStackerLocation> destinationLocations = stackerLocationDao.queryStackerLocationById(destination);
            if(destinationLocations == null || destinationLocations.isEmpty()) {
                return null;
            }
            AppStackerLocation destinationLocation = destinationLocations.getFirst(); // 终点信息
            AppStackerLocation originLocation = originLocations.getFirst(); // 起点信息
            StackerTask task = new StackerTask();
            task.setPlcTaskId(plcTaskId);
            task.setTaskType(StackerTaskTypeEnum.MOVE.getCode().shortValue());
            task.setGetStand(AppStringUtils.forceToShort(origin));
            task.setGetLaneId(originLocation.getLaneId().shortValue());
            task.setSetDepth(destinationLocation.getLaneId().shortValue());
            task.setSetStand(AppStringUtils.forceToShort(destination));
            task.setGetRow(originLocation.getLRow().shortValue());
            task.setGetLine(originLocation.getLLine().shortValue());
            task.setGetLayer(originLocation.getLLayer().shortValue());
            task.setGetDepth(originLocation.getLDepth().shortValue());
            task.setSetRow(destinationLocation.getLRow().shortValue());
            task.setSetLine(destinationLocation.getLLine().shortValue());
            task.setSetLayer(destinationLocation.getLLayer().shortValue());
            task.setSetDepth(destinationLocation.getLDepth().shortValue());
            task.setVehicleSize(vehicleSize);
            task.setWeight(weight);
            task.setVehicleNo(AppStringUtils.forceToInt(vehicleNo));
            return task;
        } catch (Exception e) {
            return null;
        }
    }
}
