AcsServiceImpl.java 26.2 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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
package com.huaheng.api.acs.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.acs.domain.AgvTask;
import com.huaheng.api.acs.domain.StateInfoUploadModel;
import com.huaheng.api.acs.mapper.AcsMapper;
import com.huaheng.common.constant.HttpConstant;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.HttpUtils;
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.address.service.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.pointPosition.domain.PointPosition;
import com.huaheng.pc.config.pointPosition.service.PointPositionService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName AcsServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2019/12/2615:38
 */
@Service
public class AcsServiceImpl extends ServiceImpl<AcsMapper,AgvTask> implements AcsService {
//    public static final  int STATUS_TASK_CARRY=100; 搬运任务
//    public static final  int STATUS_TASK_WALK=200;  行走任务
//    public static final  int STATUS_TASK_PICKUP=300;取货任务
//    public static final  int STATUS_TASK_Release=400;放货任务
//    public static final  String STATUS_POSITION_EMPTY="empty";
//    public static final  String STATUS_POSITION_SOME="some";
//    public static final  String STATUS_POSITION_FULL="full";
//    public static final  String STATUS_POSITION_LOCK="lock";
    @Resource
    private ConfigService configService;
    @Resource
    private AddressService addressService;
    @Resource
    private PointPositionService pointPositionService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;

    //AGV信息上传
    @Override
    public AjaxResult StateInfoUpload(StateInfoUploadModel model) {
        return null;
    }

    //AGV信息回传
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult TaskConfirm(AgvTask agvTask) { return null;}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult createAGVTask(AgvTask agvTask) {

        //校验请求参数
        checkAgvTaskPosition(agvTask);
        //校验托盘是否存在任务及是否锁定
        checkContainerAndPositionStatus(agvTask);
        //校验自己生成的任务中源点位的托盘和实际托盘一不一致
        checkContainerAndPositionLove(agvTask);
        agvTask.setStatus(QuantityConstant.TASK_STATUS_BUILD);
        agvTask.setCreatedBy(ShiroUtils.getLoginName());
        agvTask.setTaskType(QuantityConstant.STATUS_TASK_CARRY);
        agvTask.setCreatedTime(new Date());
        agvTask.setUpdated(new Date());
        agvTask.setUpdatedBy(ShiroUtils.getLoginName());
        if (save(agvTask)) {
            pointPositionService.updateStatus(agvTask.getFromPort(), QuantityConstant.STATUS_POSITION_LOCK, agvTask.getWarehouseCode());
            pointPositionService.updateStatus(agvTask.getToPort(), QuantityConstant.STATUS_POSITION_LOCK, agvTask.getWarehouseCode());
            containerService.updateStatus(agvTask.getContainerCode(),QuantityConstant.STATUS_CONTAINER_LOCK,agvTask.getWarehouseCode());
        } else {
            throw new ServiceException("AGV创建任务失败");
        }
        return AjaxResult.success("执行生成任务成功", agvTask);
    }

    @Override
    @Transactional
    public AjaxResult createAGVTask2(TaskHeader taskHeader) {
        try {
            if(taskHeader.getTaskType().equals(QuantityConstant.TASK_TYPE_WHOLESHIPMENT)){
                AgvTask agvTask=new AgvTask();
                agvTask.setTaskType(QuantityConstant.STATUS_TASK_CARRY);
                agvTask.setContainerCode(taskHeader.getContainerCode());
                agvTask.setFromPort(taskHeader.getPort());
                agvTask.setToPort("");
                agvTask.setWarehouseCode(taskHeader.getWarehouseCode());
                agvTask.setStatus(QuantityConstant.TASK_STATUS_BUILD);
                agvTask.setCreatedBy(taskHeader.getCreatedBy());
                agvTask.setCreatedTime(new Date());
                if (save(agvTask)) {
                    pointPositionService.updateStatus(agvTask.getFromPort(), QuantityConstant.STATUS_POSITION_LOCK, agvTask.getWarehouseCode());
                    containerService.updateStatus(agvTask.getContainerCode(),QuantityConstant.STATUS_CONTAINER_LOCK,agvTask.getWarehouseCode());
                }
            }
        }catch (Exception e) {
            taskHeader.setUserDef3("新增AGV任务失败了");
            taskHeaderService.updateById(taskHeader);
        }
        return null;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult sendTaskToAGV(Integer[] AgvTaskIds) {
        AgvTask task = null;
        for (Integer taskId : AgvTaskIds) {
            task = getById(taskId);

            if (task.getStatus() >= QuantityConstant.TASK_STATUS_RELEASE) {
                return AjaxResult.error("任务" + taskId + "已经下发,请不要重复下发,操作中止");
            }
            if(StringUtils.isEmpty(task.getToPort())){
                return AjaxResult.error("任务没有目标点位,不允许下发");
            }
            LambdaQueryWrapper<AgvTask> agvTaskLambdaQueryWrapper=Wrappers.lambdaQuery();
            agvTaskLambdaQueryWrapper.eq(AgvTask::getToPort,task.getToPort())
                    .eq(AgvTask::getWarehouseCode,task.getWarehouseCode())
                    .ge(AgvTask::getStatus,QuantityConstant.TASK_STATUS_RELEASE)
                    .lt(AgvTask::getStatus,QuantityConstant.TASK_STATUS_COMPLETED);
            List<AgvTask> list = list(agvTaskLambdaQueryWrapper);
            if(list!=null&&list.size()>0){
               return AjaxResult.error("目标点位存在任务,请更换目标点位");
            }
            // 给AGV传递任务
            AjaxResult ajaxResult = agvTaskAssign(task);
            if (ajaxResult != null && ajaxResult.hasErr()) {
                return AjaxResult.error(ajaxResult.getMsg());
            }
            //修改任务头表
            task.setId(taskId);
            task.setStatus(QuantityConstant.TASK_STATUS_RELEASE);
            task.setUpdated(new Date());
            task.setUpdatedBy(ShiroUtils.getLoginName());
            saveOrUpdate(task);
        }
        return AjaxResult.success("执行下发任务成功", task);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult agvTaskAssign(AgvTask agvTask) {
        //1、判断agvHeader是否为空
        if (agvTask == null) {
            return AjaxResult.error("Agv任务为空");
        }
        //2、判断必填字段是否满足
        if (agvTask.getId() == null) {
            return AjaxResult.error("Agv任务号Id为空");
        }
        if (agvTask.getTaskType() == null) {
            return AjaxResult.error("Agv任务类型为空");
        }
        String containerCode = agvTask.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            return AjaxResult.error("Agv任务中容器为空");
        }
        String warehouseCode = agvTask.getWarehouseCode();
        //3、转换实体,初始化Agv任务实体
        agvTask.setTaskNo(String.valueOf(agvTask.getId()));
        //4、发送数据
        return  sendDataToAgv(agvTask.getId(),warehouseCode,"1",agvTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelAGVTask(Integer[] AgvTaskIds) {
        for (int taskId : AgvTaskIds) {
            AgvTask agvTask = getById(taskId);
            if (agvTask == null) {
                return AjaxResult.error("任务" + taskId + "未找到,操作中止");
            }
            if (agvTask.getStatus() > QuantityConstant.TASK_STATUS_RELEASE) {
                return AjaxResult.error("存在任务" + agvTask.getId() + "已下发或执行,操作中止");
            }
            int preTaskNo = taskId;
            LambdaQueryWrapper<AgvTask> AgvLambdaQueryWrapper = Wrappers.lambdaQuery();
            AgvLambdaQueryWrapper.eq(AgvTask::getPreTaskNo, preTaskNo);
            AgvTask AgvTaskHeader = getOne(AgvLambdaQueryWrapper);
            if (AgvTaskHeader != null) {
                return AjaxResult.error("取消任务失败, 有前置任务" + agvTask.getId());
            }
            //查出任务明细
            //删除子任务
            //删除主任务
            removeById(taskId);
            //盘点取消任务,恢复明细状态为1
            int taskType = agvTask.getTaskType().intValue();
            switch (taskType) {
                case QuantityConstant.STATUS_TASK_CARRY:
                case QuantityConstant.STATUS_TASK_WALK:
                    break;
                case QuantityConstant.STATUS_TASK_PICKUP:
                case QuantityConstant.STATUS_TASK_RELEASE:
                    break;
                default:
                    break;
            }
            cancelPositionStatus(agvTask);
            cancelContainerStatus(agvTask);
        }
        return AjaxResult.success("取消任务成功!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelTask(Integer id, String warehouseCode, String area) {
        //1、判断参数是否为空
        if(id == null) throw new ServiceException("任务号为空");
        List<Integer> list=new ArrayList<>();
        list.add(id);
        //2、发送数据
        return sendDataToAgv(id,warehouseCode,area,list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateAGVTask(String agvTaskId, Integer priority) {
        if(StringUtils.isEmpty(agvTaskId)) throw new ServiceException("任务号为空");
        AgvTask agvTask = getById(Integer.parseInt(agvTaskId));
        agvTask.setPriority(priority);
        agvTask.setUpdated(new Date());
        agvTask.setUpdatedBy(ShiroUtils.getLoginName());
        if(!updateById(agvTask))throw new ServiceException("更新优先级失败");
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateTask(Integer id, String warehouseCode, Integer priority) {
        if(id == null) throw new ServiceException("任务号为空");

        AgvTask agvTask=new AgvTask();
        agvTask.setTaskNo(String.valueOf(id));
        agvTask.setPriority(priority);
        return sendDataToAgv(id,warehouseCode,"1",agvTask);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult notifyAGVPort(Map map) {
        AgvTask agvTask=new AgvTask();
        if(map.isEmpty()) return AjaxResult.error("请求数据为空");
        Object o1=map.get("taskNo");
        Object o2=map.get("carNo");
        Object o3=map.get("oldPort");
        Object o4=map.get("newPort");
        if(StringUtils.isNull(o1)) return AjaxResult.error("任务号为空");
        if(StringUtils.isNull(o2)) return AjaxResult.error("小车编号为空");
        if(StringUtils.isNull(o3)) return AjaxResult.error("起点为空");
        if(StringUtils.isNull(o4)) return AjaxResult.error("终点为空");
        Integer taskNo=Integer.valueOf(o1.toString());
        String carNo=o2.toString();
        String fromPort=o3.toString();
        String toPort=o4.toString();
        agvTask.setId(taskNo);
        agvTask.setCarNo(carNo);
        agvTask.setFromPort(fromPort);
        agvTask.setToPort(toPort);
        AjaxResult ajaxResult = checkPointPositionExist(agvTask);
        if(ajaxResult.hasErr()) return ajaxResult;
        if(!updateById(agvTask)){ throw new ServiceException("更新失败"); };
        return ajaxResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult notifyAGVTask(Map map) {
        AgvTask agvTask=new AgvTask();
        if(map.isEmpty()) return AjaxResult.error("请求数据为空");
        Object o1=map.get("taskNo");
        Object o2=map.get("carNo");
        Object o3=map.get("status");
        if(StringUtils.isNull(o1)) return AjaxResult.error("任务号为空");
        if(StringUtils.isNull(o2)) return AjaxResult.error("小车编号为空");
        if(StringUtils.isNull(o3)) return AjaxResult.error("任务状态为空");
        Integer taskNo=Integer.valueOf(o1.toString());
        String carNo=o2.toString();
        String status=o3.toString();
        agvTask.setId(taskNo);
        agvTask.setCarNo(carNo);
        agvTask.setStatus(Integer.parseInt(status));
        AgvTask task = getById(taskNo);
        if(task==null)return AjaxResult.error("任务号"+taskNo+"不存在");
        if(task.getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED))return AjaxResult.error("任务"+taskNo+"已经完成");
        if(agvTask.getStatus().equals(QuantityConstant.TASK_STATUS_RUNNING)){
            pointPositionService.updateStatus(task.getFromPort(),QuantityConstant.STATUS_POSITION_EMPTY,task.getWarehouseCode());
            pointPositionService.setContainerCode(task.getFromPort(),null,task.getWarehouseCode());
            updateById(agvTask);
        }
        if(agvTask.getStatus().equals(QuantityConstant.TASK_STATUS_COMPLETED)){
            pointPositionService.updateStatus(task.getFromPort(),QuantityConstant.STATUS_POSITION_EMPTY,task.getWarehouseCode());
            pointPositionService.updateStatus(task.getToPort(),QuantityConstant.STATUS_POSITION_SOME,task.getWarehouseCode());
            pointPositionService.setContainerCode(task.getFromPort(),null,task.getWarehouseCode());
            pointPositionService.setContainerCode(task.getToPort(),task.getContainerCode(),task.getWarehouseCode());
            containerService.updateLocationCodeAndStatus(task.getContainerCode(),task.getToPort(),QuantityConstant.STATUS_CONTAINER_EMPTY,task.getWarehouseCode());
            updateById(agvTask);
            cancelContainerStatus(task);
        }
        if(!updateById(agvTask)){ throw new ServiceException("更新失败"); };
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult completeTaskByWMS(String taskId) {
        if(StringUtils.isEmpty(taskId)){
            return AjaxResult.error("任务id为空");
        }
        AgvTask agvTask = getById(taskId);
        if (agvTask.getStatus() == QuantityConstant.TASK_STATUS_BUILD) {
            return AjaxResult.error("任务" + taskId + "未下发,请不要直接完成");
        }
        //更新点位和托盘信息
        if(StringUtils.isNotEmpty(agvTask.getFromPort())){
            PointPosition sourcePosition = pointPositionService.getPositionByCode(agvTask.getFromPort(), agvTask.getWarehouseCode());
            if(StringUtils.isNull(sourcePosition)){
                return AjaxResult.error("源点位未找到");
            }
            pointPositionService.updateStatus(agvTask.getFromPort(), QuantityConstant.STATUS_POSITION_EMPTY,agvTask.getWarehouseCode());
        }
        if(StringUtils.isNotEmpty(agvTask.getToPort())){
            PointPosition desPosition = pointPositionService.getPositionByCode(agvTask.getToPort(), agvTask.getWarehouseCode());
            if(StringUtils.isNull(desPosition)){
                return AjaxResult.error("目标点位未找到");
            }
            pointPositionService.updateStatus(agvTask.getToPort(), QuantityConstant.STATUS_POSITION_SOME,agvTask.getWarehouseCode());
            pointPositionService.setContainerCode(agvTask.getToPort(),agvTask.getContainerCode(),agvTask.getWarehouseCode());
            containerService.updateLocationCodeAndStatus(agvTask.getContainerCode(),agvTask.getToPort(),QuantityConstant.STATUS_CONTAINER_EMPTY,agvTask.getWarehouseCode());
        }
        //更新agv任务状态
        agvTask.setStatus(QuantityConstant.TASK_STATUS_COMPLETED);
        agvTask.setUpdated(new Date());
        agvTask.setUpdatedBy(ShiroUtils.getLoginName());
        if(!updateById(agvTask))throw new ServiceException("完成任务失败");
        return AjaxResult.success();
    }

    //将参数传输给AGV
    private AjaxResult sendDataToAgv(Integer id,String warehouseCode,String area,Object object){
        String value = configService.getKey(QuantityConstant.RULE_CONNECT_AGV);
        int connectAGV = Integer.parseInt(value);
        AjaxResult ajaxResult =new AjaxResult();
        if (connectAGV == QuantityConstant.RULE_AGV_CONNECT) {
            String url = addressService.selectAddress(QuantityConstant.ADDRESS_AGV_TASK_ASSIGN, warehouseCode, area);
            String JsonParam = JSON.toJSONString(object);
            System.out.println(JsonParam);
            String result = HttpUtils.bodypost(url, JsonParam, warehouseCode);
            if (StringUtils.isEmpty(result)) {
                throw new ServiceException("接口地址错误或返回为空");
            }
            ajaxResult = JSON.parseObject(result, AjaxResult.class);
            if (ajaxResult.getCode() != HttpConstant.OK) {
                return AjaxResult.error(ajaxResult.getMsg());
            }
            return ajaxResult;
        }
        return AjaxResult.success("下发成功");
    }

    //检查托盘和点位是否存在未完成的任务
    private void checkContainerAndPositionStatus(AgvTask agvTask){

        TaskHeader containerLKTaskStatus = taskHeaderService.getOne(new LambdaQueryWrapper<TaskHeader>()
                .eq(TaskHeader::getContainerCode, agvTask.getContainerCode())
                .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        AgvTask containerAgvTaskStatus = getOne(new LambdaQueryWrapper<AgvTask>()
                .eq(AgvTask::getContainerCode, agvTask.getContainerCode())
                .lt(AgvTask::getStatus, QuantityConstant.TASK_STATUS_COMPLETED));
        if (StringUtils.isNotNull(containerLKTaskStatus)||StringUtils.isNotNull(containerAgvTaskStatus)) {
            throw  new ServiceException("托盘"+agvTask.getContainerCode()+"存在未完成的任务");
        }

        Container containerStatus = containerService.getContainerByCode(agvTask.getContainerCode());
        if(StringUtils.isNull(containerStatus)){
            throw  new ServiceException("托盘"+agvTask.getContainerCode()+"不存在");
        }
        if(QuantityConstant.STATUS_CONTAINER_LOCK.equals(containerStatus.getStatus())){
            throw  new ServiceException("托盘"+agvTask.getContainerCode()+"已锁定");
        }

        PointPosition pointPosition = pointPositionService.getOne(new LambdaQueryWrapper<PointPosition>()
                .eq(PointPosition::getContainerCode, agvTask.getContainerCode())
                .eq(PointPosition::getStatus, QuantityConstant.STATUS_CONTAINER_LOCK));
        if (pointPosition != null) {
            throw  new ServiceException("点位"+pointPosition.getPositionName()+"已锁定,托盘在此位置不能生成任务");
        }
    }

    //根据任务类型检验参数
    private void checkAgvTaskPosition(AgvTask agvTask){
        if (StringUtils.isNull(agvTask)) {
            throw new ServiceException("任务数据为空,请重新下发");
        }
        if (StringUtils.isEmpty(agvTask.getContainerCode())) {
            throw new ServiceException("托盘为空");
        }
//        if (StringUtils.isNull(agvTask.getTaskType())) {
//            throw new ServiceException("任务类型无");
//        }
//        int taskType = agvTask.getTaskType().intValue();
        int taskType=100;
        PointPosition sourcePosition = pointPositionService.getPositionByCode(agvTask.getFromPort(), agvTask.getWarehouseCode());
        PointPosition desPosition = pointPositionService.getPositionByCode(agvTask.getToPort(), agvTask.getWarehouseCode());
        if (taskType == QuantityConstant.STATUS_TASK_CARRY || taskType == QuantityConstant.STATUS_TASK_WALK
                || taskType == QuantityConstant.STATUS_TASK_PICKUP) {
            if (StringUtils.isEmpty(agvTask.getFromPort())) {
                throw  new ServiceException("源点位为空");
            }
            if (StringUtils.isNull(sourcePosition)) {
                throw new ServiceException("源点位:" + sourcePosition + "未找到");
            }
            if(sourcePosition.getStatus().equals(QuantityConstant.STATUS_POSITION_LOCK)){
                throw new ServiceException("源点位:" + sourcePosition + "禁用");
            }
        }
        if (taskType == QuantityConstant.STATUS_TASK_CARRY || taskType == QuantityConstant.STATUS_TASK_WALK
                || taskType == QuantityConstant.STATUS_TASK_RELEASE) {
            if (StringUtils.isEmpty(agvTask.getToPort())) {
                throw  new ServiceException("目标点位为空");
            }
            if (StringUtils.isNull(desPosition)) {
                throw new ServiceException("目标点位:" + desPosition + "未找到");
            }
            if(desPosition.getStatus().equals(QuantityConstant.STATUS_POSITION_LOCK)){
                throw new ServiceException("目标点位:" + desPosition + "禁用");
            }
            if(StringUtils.isNotEmpty(desPosition.getContainerCode())){
                throw new ServiceException("目标点位:" + desPosition + "存在托盘:"+desPosition.getContainerCode());
            }
            if (QuantityConstant.STATUS_CONTAINER_FULL.equals(desPosition.getStatus())) {
                throw new ServiceException("目标点位:" + desPosition + "已满");
            }
        }
    }

    //更新点位状态
    private void cancelPositionStatus(AgvTask agvTask) {
        if (agvTask.getFromPort() != null) {
            //更新点位状态
            PointPosition pointPosition = pointPositionService.getOne(new LambdaQueryWrapper<PointPosition>().eq(PointPosition::getPositionName, agvTask.getFromPort()));
            if(pointPosition!=null&&StringUtils.isNotEmpty(pointPosition.getContainerCode())){
                pointPositionService.updateStatus(agvTask.getFromPort(), QuantityConstant.STATUS_POSITION_SOME,agvTask.getWarehouseCode());
            } else {
                pointPositionService.updateStatus(agvTask.getFromPort(), QuantityConstant.STATUS_POSITION_EMPTY,agvTask.getWarehouseCode());
            }
        }
        if (agvTask.getToPort() != null) {
            //更新点位状态
            PointPosition pointPosition = pointPositionService.getOne(new LambdaQueryWrapper<PointPosition>().eq(PointPosition::getPositionName, agvTask.getToPort()));
            if(pointPosition!=null&&StringUtils.isNotEmpty(pointPosition.getContainerCode())){
                pointPositionService.updateStatus(agvTask.getToPort(), QuantityConstant.STATUS_POSITION_SOME,agvTask.getWarehouseCode());
            } else {
                pointPositionService.updateStatus(agvTask.getToPort(), QuantityConstant.STATUS_POSITION_EMPTY,agvTask.getWarehouseCode());

            }
        }

    }
    //更新容器状态
    private void cancelContainerStatus(AgvTask agvTask){
        LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getContainerCode, agvTask.getContainerCode());
        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(inventoryHeaderLambdaQueryWrapper);
        Container container = new Container();
        container.setStatus(QuantityConstant.STATUS_CONTAINER_EMPTY);
        if (inventoryHeader != null) {
            if (QuantityConstant.STATUS_CONTAINER_SOME.equals(inventoryHeader.getContainerStatus())) {
                container.setStatus(QuantityConstant.STATUS_CONTAINER_SOME);
            }
        }
        LambdaUpdateWrapper<Container> containerUpdateWrapper = Wrappers.lambdaUpdate();
        containerUpdateWrapper.eq(Container::getCode, agvTask.getContainerCode());
        containerService.update(container, containerUpdateWrapper);
    }


    //检查点位是否存在
    private AjaxResult checkPointPositionExist(AgvTask agvTask){
        PointPosition sourcePosition = pointPositionService.getPositionByCode(agvTask.getFromPort(), "CS0001");
        PointPosition desPosition = pointPositionService.getPositionByCode(agvTask.getToPort(), "CS0001");
        if (StringUtils.isNull(sourcePosition)) {
            return AjaxResult.error("源点位:" + sourcePosition + "未找到");
        }
        if (StringUtils.isNull(desPosition)) {
            return AjaxResult.error("目标点位:" + desPosition + "未找到");
        }
        /*if(desPosition.getStatus().equals(QuantityConstant.STATUS_POSITION_LOCK)){
            return AjaxResult.error("目标点位:" + sourcePosition + "禁用");
        }
        if (QuantityConstant.STATUS_CONTAINER_FULL.equals(desPosition.getStatus())) {
            return AjaxResult.error("目标点位:" + desPosition + "已满");
        }*/
        return AjaxResult.success();
    }
    //校验自己生成的任务中源点位的托盘和实际托盘一不一致
    private void  checkContainerAndPositionLove(AgvTask agvTask){
        PointPosition pointPosition = pointPositionService.getOne(new LambdaQueryWrapper<PointPosition>().eq(PointPosition::getPositionName, agvTask.getFromPort()));
        if(pointPosition!=null&&StringUtils.isNotEmpty(pointPosition.getContainerCode())){
            if(!agvTask.getContainerCode().equals(pointPosition.getContainerCode())){
                throw  new ServiceException("源点位上托盘数据和分配的托盘不一致,请核对");
            }
        }

    }


}