AcsService.java 28.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 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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
package com.huaheng.api.ACS.service;

import com.alibaba.fastjson.JSON;
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.pc.config.address.domain.Address;
import com.huaheng.pc.config.address.service.IAddressService;
import com.huaheng.pc.config.points.domain.Points;
import com.huaheng.pc.config.points.service.PointsService;
import com.huaheng.pc.general.container.domain.Container;
import com.huaheng.pc.general.container.service.IContainerService;
import com.huaheng.pc.general.location.domain.Location;
import com.huaheng.pc.general.location.domain.LocationPosition;
import com.huaheng.pc.general.location.service.ILocationService;
import com.huaheng.pc.inventory.inventory.domain.Inventory;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import com.huaheng.pc.monitor.agvcarinfo.domain.StateInfoUploadModel;
import com.huaheng.pc.monitor.agvcarinfo.mapper.AgvStateInfoMapper;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.IReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiptDetail.domain.ReceiptDetail;
import com.huaheng.pc.receipt.receiptDetail.service.IReceiptDetailService;
import com.huaheng.pc.receipt.receiptHeader.domain.ReceiptHeader;
import com.huaheng.pc.receipt.receiptHeader.service.IReceiptHeaderService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.IShipmentContainerHeaderService;
import com.huaheng.pc.shipment.shipmentDetail.domain.ShipmentDetail;
import com.huaheng.pc.shipment.shipmentDetail.service.IShipmentDetailService;
import com.huaheng.pc.shipment.shipmentHeader.domain.ShipmentHeader;
import com.huaheng.pc.shipment.shipmentHeader.service.IShipmentHeaderService;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.mapper.AgvTaskMapper;
import com.huaheng.pc.task.task.domain.Task;
import com.huaheng.pc.task.task.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AcsService {

    @Autowired
    private ITaskService taskService;
    @Resource
    private AgvTaskMapper agvTaskMapper;
    @Resource
    private AgvStateInfoMapper agvStateInfoMapper;
    @Resource
    private PointsService pointsService;
    @Autowired
    private IReceiptHeaderService receiptHeaderService;
    @Autowired
    private IShipmentHeaderService shipmentHeaderService;
    @Autowired
    private IReceiptDetailService receiptDetailService;
    @Autowired
    private IShipmentDetailService shipmentDetailService;
    @Autowired
    private IInventoryService iInventoryService;
    @Autowired
    private IReceiptContainerHeaderService receiptContainerHeaderService;
    @Autowired
    private IShipmentContainerHeaderService shipmentContainerHeaderService;
    @Autowired
    private ILocationService locationService;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private IContainerService containerService;


    final String interactionId = "6EAB7794-CFD7-449A-A754-FFDB98CE44C7";




    private String getUrl(){
        Address address =new Address();
        address.setParam("AGV");
        address.setWarehouseCode(ShiroUtils.getWarehouseCode());
        String url=addressService.selectEntity(address).getUrl();
        return url;
    }
    //private  static String address="http://172.16.30.48:8081/api/";


    //pick任务下发
    @Transactional
     public AjaxResult Task(String code,String station){
         //查找下发任务
        AgvTask agvTask=new AgvTask();
        agvTask.setTaskType("pick");
        agvTask.setStation(station);
        int num=0;
        ReceiptHeader receiptHeader = new ReceiptHeader();
        ReceiptDetail receiptDetail = new ReceiptDetail();
        ShipmentHeader shipmentHeader = new ShipmentHeader();
        ShipmentDetail shipmentDetail = new ShipmentDetail();
        Inventory inventory = new Inventory();
        receiptHeader.setCode(code);
        receiptHeader = receiptHeaderService.selectFirstEntity(receiptHeader);
        //判断code是否属于入库单,不属于则查找出库单
        if (receiptHeader == null) {
            shipmentHeader.setCode(code);
            shipmentHeader = shipmentHeaderService.selectFirstEntity(shipmentHeader);
            shipmentDetail.setShipmentCode(code);
            shipmentDetail.setWarehouseId(ShiroUtils.getWarehouseId());
            //查找AGV的子单
            List<ShipmentDetail> shipmentDetails = shipmentDetailService.selectListAgvDetail(shipmentDetail);
            for (ShipmentDetail item : shipmentDetails) {
                if (item.getQty().compareTo(item.getQtyCompleted()) != 0) {
                    inventory.setMaterialCode(item.getMaterialCode());
                    inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
                    inventory.setZoneCode("AGV");
                    //查找物料对应的满盘的库存,没有则查找不满盘的库存
                    inventory.setContainerStatus("half");
                    Inventory inventorie = iInventoryService.selectFirstEntity(inventory);
                    if (inventorie == null) {
                        inventory.setContainerStatus("some");
                        inventorie = iInventoryService.selectFirstEntity(inventory);
                    }
                    agvTask.setTaskHeaderId(shipmentHeader.getId());
                    agvTask.setTaskDetailId(item.getId());
                    agvTask.setWarehouseId(item.getWarehouseId());
                    agvTask.setWarehouseCode(item.getWarehouseCode());
                    agvTask.setCompanyId(item.getCompanyId());
                    agvTask.setCompanyCode(item.getCompanyCode());
                    if (inventorie == null) {
                        num=num+1;
                    }else {
                        this.TaskModel(inventorie.getContainerCode(), agvTask);
                    }
                }
            }
            if(num==shipmentDetails.size()){
                throw new ServiceException("所有单据都没有库存,无法出库");
            }
        } else {
            receiptDetail.setReceiptCode(code);
            receiptDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            List<ReceiptDetail> receiptDetails = receiptDetailService.selectListAgvEntity(receiptDetail);
            for (ReceiptDetail item : receiptDetails) {
                if (item.getQty().compareTo(item.getQtyCompleted()) != 0) {
                    inventory.setMaterialCode(item.getMaterialCode());
                    inventory.setWarehouseCode(ShiroUtils.getWarehouseCode());
                    inventory.setZoneCode("AGV");
                    //查找物料对应的不满盘的库存
                    inventory.setContainerStatus("some");
                    Inventory inventorie = iInventoryService.selectFirstEntity(inventory);
                    agvTask.setTaskHeaderId(receiptHeader.getId());
                    agvTask.setTaskDetailId(item.getId());
                    agvTask.setWarehouseId(item.getWarehouseId());
                    agvTask.setWarehouseCode(item.getWarehouseCode());
                    agvTask.setCompanyId(item.getCompanyId());
                    agvTask.setCompanyCode(item.getCompanyCode());
                    if (inventorie == null) {
                        num=num+1;
                    }else {
                        this.TaskModel(inventorie.getContainerCode(), agvTask);
                    }
                }
            }
            if(num==receiptDetails.size()){
                throw new ServiceException("请手动叫车");
            }
        }
        return AjaxResult.success("成功");
     }



    /**
     * 设置agv下发任务的实体
     *
     */
    @Transactional
    public AjaxResult TaskModel(String containerCode,AgvTask agvTask){
        AjaxResult ajaxResult=new AjaxResult();
        AgvTask model=new AgvTask();
        String code=containerCode.substring(0,4);
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setGoodsShelfNo(code);
        points=pointsService.selectEntity(points);

        //任务下发实体
        model.setTaskHeaderId(agvTask.getTaskHeaderId());
        model.setTaskDetailId(agvTask.getTaskDetailId());
        model.setTaskType(agvTask.getTaskType());
        model.setStation(agvTask.getStation());
        model.setStatus(10);
        if(containerCode.substring(5,7).equals("1")) {
            model.setOrientation("A");
        }else {
            model.setOrientation("C");
        }
        //判断货架是否处于1阶段,1阶段为pick阶段。
        if (points == null) {
            throw new ServiceException("货架不存在!");
        }
        if(points.getIsLocked()!=1 && points.getIsLocked()!=2){
            model.setShelfNo(points.getGoodsShelfNo());
            model.setFromLocationNo("");
            model.setToLocationNo("");
            ajaxResult=this.TaskAssign(model);
            if(!ajaxResult.hasErr()){
                //修改points表,设为出库状态,修改出库前位置
                points.setIsLocked(1);
                points.setoIntX(points.getIntX());
                points.setoIntY(points.getIntY());
                pointsService.updatePoints(points);
                model.setCompanyId(agvTask.getCompanyId());
                model.setCompanyCode(agvTask.getCompanyCode());
                model.setWarehouseId(agvTask.getWarehouseId());
                model.setWarehouseCode(agvTask.getWarehouseCode());
                if(agvTaskMapper.insertModel(model)<1){
                    throw new ServiceException("agv任务下发失败");
                }
            }
        }else{
            return AjaxResult.success("该货架已出库");
        }
       return  ajaxResult;
    }

    //手动叫车
    @Transactional
    public AjaxResult ManualCalling(String station,String shelfNo,String code){
        AgvTask model = new AgvTask();
        ReceiptHeader receiptHeader=new ReceiptHeader();
        ReceiptDetail receiptDetail=new ReceiptDetail();
        ShipmentHeader shipmentHeader=new ShipmentHeader();
        ShipmentDetail shipmentDetail=new ShipmentDetail();
        receiptHeader.setCode(code);
        receiptHeader=receiptHeaderService.selectFirstEntity(receiptHeader);
        //判断code是否属于入库单,不属于则查找出库单
        if(receiptHeader==null){
            shipmentHeader.setCode(code);
            shipmentHeader=shipmentHeaderService.selectFirstEntity(shipmentHeader);
            shipmentDetail.setShipmentCode(code);
            shipmentDetail.setZoneCode("AGV");
            shipmentDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            shipmentDetail=shipmentDetailService.selectFirstEntity(shipmentDetail);
            model.setTaskHeaderId(shipmentHeader.getId());
            model.setTaskDetailId(shipmentDetail.getId());
            model.setWarehouseId(shipmentDetail.getWarehouseId());
            model.setWarehouseCode(shipmentDetail.getWarehouseCode());
            model.setCompanyId(shipmentDetail.getCompanyId());
            model.setCompanyCode(shipmentDetail.getCompanyCode());
        }else {
            receiptDetail.setReceiptCode(code);
            receiptDetail.setZoneCode("AGV");
            receiptDetail.setWarehouseCode(ShiroUtils.getWarehouseCode());
            receiptDetail=receiptDetailService.selectFirstEntity(receiptDetail);
            model.setTaskHeaderId(receiptHeader.getId());
            model.setTaskDetailId(receiptDetail.getId());
            model.setWarehouseId(receiptDetail.getWarehouseId());
            model.setWarehouseCode(receiptDetail.getWarehouseCode());
            model.setCompanyId(receiptDetail.getCompanyId());
            model.setCompanyCode(receiptDetail.getCompanyCode());
        }
        model.setTaskType("pick");
        model.setShelfNo(shelfNo);
        model.setStation(station);
        model.setStatus(20);
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setGoodsShelfNo(shelfNo);
        AjaxResult ajaxResult=new AjaxResult();
        points=pointsService.selectFirstEntity(points);
        if(points==null){
            throw new ServiceException("没有此货架");
        }
        if(points.getIsLocked()!=1 && points.getIsLocked()!=2) {
            model.setFromLocationNo("");
            model.setToLocationNo("");
            model.setOrientation("A");
            ajaxResult = this.TaskAssign(model);
            if (!ajaxResult.hasErr()) {
                points.setIsLocked(1);
                points.setoIntX(points.getIntX());
                points.setoIntY(points.getIntY());
                pointsService.updatePoints(points);
            }
        }else {
            throw new ServiceException("该货架已出库");
        }
        return ajaxResult;
    }

    /**
     * AGV 任务下发
     */

    public AjaxResult TaskAssign(AgvTask model)
    {
            model.setInteractionId(interactionId);
            model.setPlatform("wms");
            String url = this.getUrl()+"TaskAssign";
            String JsonParam = JSON.toJSONString(model);
            String result = HttpUtils.sendPost(url, JsonParam);
            AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
            return ajaxResult;
    }


    /**
     * agv  putway任务下发
     *
     */
    @Transactional
    public AjaxResult TaskAssigns(String code,String containerCode,String station,String row,String line)
    {
        //查找下发任务
        int result=0;
        String warehouseCode=ShiroUtils.getWarehouseCode();
        ReceiptHeader receiptHeader=new ReceiptHeader();
        ReceiptDetail receiptDetail=new ReceiptDetail();
        ShipmentHeader shipmentHeader=new ShipmentHeader();
        ShipmentDetail shipmentDetail=new ShipmentDetail();
        AgvTask agvTask=new AgvTask();
        receiptHeader.setCode(code);
        receiptHeader=receiptHeaderService.selectFirstEntity(receiptHeader);

        //判断code是否属于入库单,不属于则查找出库单
        if(receiptHeader==null){
            shipmentHeader.setCode(code);
            shipmentHeader=shipmentHeaderService.selectFirstEntity(shipmentHeader);
            shipmentDetail.setShipmentCode(code);
            shipmentDetail.setZoneCode("AGV");
            shipmentDetail.setWarehouseCode(warehouseCode);
            shipmentDetail=shipmentDetailService.selectFirstEntity(shipmentDetail);
            agvTask.setTaskHeaderId(shipmentHeader.getId());
            agvTask.setTaskDetailId(shipmentDetail.getId());
            agvTask.setWarehouseId(shipmentDetail.getWarehouseId());
            agvTask.setWarehouseCode(shipmentDetail.getWarehouseCode());
            agvTask.setCompanyId(shipmentDetail.getCompanyId());
            agvTask.setCompanyCode(shipmentDetail.getCompanyCode());
        }else {
            receiptDetail.setReceiptCode(code);
            receiptDetail.setZoneCode("AGV");
            receiptDetail.setWarehouseCode(warehouseCode);
            receiptDetail=receiptDetailService.selectFirstEntity(receiptDetail);
            agvTask.setTaskHeaderId(receiptHeader.getId());
            agvTask.setTaskDetailId(receiptDetail.getId());
            agvTask.setWarehouseId(receiptDetail.getWarehouseId());
            agvTask.setWarehouseCode(receiptDetail.getWarehouseCode());
            agvTask.setCompanyId(receiptDetail.getCompanyId());
            agvTask.setCompanyCode(receiptDetail.getCompanyCode());
        }
        agvTask.setTaskType("putway");
        AjaxResult ajaxResult = new AjaxResult();
        //查找需要回去的对应的货架
        String containCode=containerCode.substring(0,4);
        Points points=new Points();
        points.setWarehouseCode(warehouseCode);
        points.setGoodsShelfNo(containCode);
        points.setIsLocked(2);
        points=pointsService.selectEntity(points);
        if(points!=null) {
            AgvTask model = new AgvTask();
            model.setTaskHeaderId(agvTask.getTaskHeaderId());
            model.setTaskDetailId(agvTask.getTaskDetailId());
            model.setTaskType("putway");
            model.setShelfNo(points.getGoodsShelfNo());
            model.setStation(station);
            model.setFromLocationNo("");

            //给出目的位置
            //找出已出库的货架
            Points point=new Points();
            point.setWarehouseCode(warehouseCode);
            point.setGoodsShelfNo(points.getGoodsShelfNo());
            point=pointsService.selectFirstEntity(point);
//            point=pointsService.getFirstEntity(point);
            //如果查找到的货架和回库任务的货架相同,就不做操作
            if(point.getoIntX().equals(points.getIntX()) && point.getoIntY().equals(points.getIntY())){
                model.setToLocationNo("");
            }else {
                //如果不同,则目的位置为该货架的oIntX,oIntY
                model.setToLocationNo(point.getoIntX() + "," + point.getoIntY());
            }
            //有手动输入位置时,已手动位置为主
            if(StringUtils.isNotEmpty(row) && StringUtils.isNotEmpty(line) ){
                //查找手动位置的库位
                Location location = new Location();
                location.setWarehouseCode(warehouseCode);
                location.setType("A");
                location.setRow(Integer.valueOf(row));
                location.setLine(Integer.valueOf(line));
                location=locationService.selectFirstEntity(location);

                //再查找库位的地址
                LocationPosition lp=new LocationPosition();
                lp.setWarehouseCode(warehouseCode);
                lp.setLocationCode(location.getCode());
                lp=locationService.selectLocationPosition(lp);

                //通过地址匹配货架
                Points p=new Points();
                p.setWarehouseCode(warehouseCode);
                p.setoIntX(lp.getIntX());
                p.setoIntY(lp.getIntY());
                p.setIsLocked(2);
                p=pointsService.selectFirstEntity(p);
                if (p==null){
                    throw new ServiceException("此处位置已有货架,请另选位置");
                }
                point=p;
                model.setToLocationNo(point.getoIntX()+","+point.getoIntY());
            }

            model.setOrientation("A");
            ajaxResult = this.TaskAssign(model);
            String shelfNo=points.getGoodsShelfNo().substring(1, 5);
            LocationPosition lps=new LocationPosition();
            lps.setWarehouseCode(warehouseCode);
            lps.setIntX(point.getoIntX());
            lps.setIntY(point.getoIntY());


            if (!ajaxResult.hasErr()) {
                //回去位置与货架位置不一样时
                if(!lps.getIntX().equals(points.getIntX()) || !lps.getIntY().equals(points.getIntY()) ) {
                    //先通过货架points找到容器code
                    Container container=new Container();
                    container.setWarehouseCode(warehouseCode);
                    container.setType("A");
                    container.setCode(containCode);
                    //修改container表
                    result=containerService.updateLocationCode(container,lps);
                    if(result<1){
                        throw new ServiceException("修改容器对应的库位失败");
                    }
                    //修改location表
                    result=locationService.updateContainer(container,lps);
                    if(result<1){
                        throw new ServiceException("修改库位对应的容器失败");
                    }

                    int results=iInventoryService.updateLocationCode(shelfNo,warehouseCode);
                    if(results<1){
                        throw new ServiceException("AGV移库后库存修改库位失败");
                    }
                    //位置不同时,修改回库货架的x,y值
                    points.setIntX(lps.getIntX());
                    points.setIntY(lps.getIntY());

                    //位置不同,货架编号不同时,修改回去位置的货架的o属性x,y值
                    if(!point.getGoodsShelfNo().equals(points.getGoodsShelfNo())) {
                        point.setoIntX(points.getoIntX());
                        point.setoIntY(points.getoIntY());
                        pointsService.updatePoints(point);
                    }
                }
                points.setIsLocked(3);
                points.setNum(points.getNum()+1);
                pointsService.updatePoints(points);

                List<ReceiptContainerHeader> list = receiptContainerHeaderService.selectAgvListByLike(shelfNo, agvTask.getWarehouseCode());
                if (list.size()!=0) {
                    //修改receiptContainerHeader表
                    result=receiptContainerHeaderService.updateList(shelfNo,warehouseCode);
                    if(result<1){
                        throw new ServiceException("修改入库组盘头失败");
                    }
                    Integer[] ids = list.stream().map(x -> x.getId()).toArray(Integer[]::new);
                    try {
                        //生成任务
                        taskService.createReceiptTask(ids);
                    } catch (Exception e) {
                        return AjaxResult.error("生成任务失败");
                    }
                }
                List<ShipmentContainerHeader> listt = shipmentContainerHeaderService.selectAgvListByLike(shelfNo, agvTask.getWarehouseCode());
                if (listt.size()!=0) {
                    //修改shipmentContainerHeader表
                    result=shipmentContainerHeaderService.updateList(shelfNo,warehouseCode);
                    if(result<1){
                        throw new ServiceException("修改出库组盘头失败");
                    }
                    List<Integer> ids = listt.stream().map(x -> x.getId()).collect(Collectors.toList());
                    try {
                        //生成任务
                        shipmentContainerHeaderService.createTask(ids);
                    } catch (Exception e) {
                        return AjaxResult.error("生成任务失败");
                    }
                }

                model.setCompanyId(agvTask.getCompanyId());
                model.setCompanyCode(agvTask.getCompanyCode());
                model.setWarehouseId(agvTask.getWarehouseId());
                model.setWarehouseCode(agvTask.getWarehouseCode());
                if (agvTaskMapper.insertModel(model) < 1) {
                    throw new ServiceException("agv任务下发失败");
                }
            }
        }else {
            throw new ServiceException("该货架不在站台,无法下发回库任务,或者AGV小车没有反馈信息");
        }
      return ajaxResult;
    }


    /**
     * AGV 任务取消
     */
    public AjaxResult TaskCancel(String message)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("interactionId", interactionId);
        map.put("reason", message);
        String JsonParam = JSON.toJSONString(map);
        String url= this.getUrl()+"TaskCancel";
        String result = HttpUtils.sendPost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * AGV 信息查询
     */

    public AjaxResult StateInfoGET(String carNo)
    {
        Map<String, String> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("interactionId", interactionId);
        String JsonParam = JSON.toJSONString(map);
        String url= this.getUrl()+"StateInfoGET";
        String result = HttpUtils.sendPost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * AGV 旋转方向
     */
    public AjaxResult TurnOriention(String carNo,String orientation)
    {
        Map<String, String> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("orientation", orientation);
        String JsonParam = JSON.toJSONString(map);
        String url= this.getUrl()+"TurnOriention";
        String result = HttpUtils.sendPost(url, JsonParam);
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * AGV 任务回传
     */
    @Transactional
    public AjaxResult TaskConfirm(AgvTask model)
    {
        model.setWarehouseId(ShiroUtils.getWarehouseId());
        model.setWarehouseCode(ShiroUtils.getWarehouseCode());
        int result=agvTaskMapper.insertModel(model);
        if(result<1){
            throw new ServiceException("数据插入失败");
        }
        String shelfNo = model.getShelfNo().substring(1, 5);
        String warehouseCode = ShiroUtils.getWarehouseCode();
        Points points=new Points();
        points.setWarehouseCode(warehouseCode);
        points.setGoodsShelfNo(shelfNo);
        points=pointsService.selectEntity(points);
        if(points==null){
            throw new ServiceException("没有对应的货架");
        }
        if(model.getTaskType().equals("pick") && model.getState().equals("1")){
            points.setIsLocked(0);
            int rt=pointsService.updatePoints(points);
            if(rt<1){
                throw new ServiceException("删除AGV出库任务后修改货架状态失败");
            }
        }
        //回传任务为putway时,查找到改货架中物料为agv的任务,并完成任务。
        if (model.getTaskType().equals("putway")) {
            List<Task> list = taskService.selectAgvListByLike(shelfNo, warehouseCode);
            if(list.size()!=0) {
                Integer[] ids = list.stream().map(x -> x.getId()).toArray(Integer[]::new);
                try {
                    taskService.completeTaskByWMS(ids);
                } catch (Exception e) {
                    return AjaxResult.error("完成任务失败");
                }
            }
        }
        return AjaxResult.success(model);
    }

    /**
     * AGV 信息上传
     */
    public AjaxResult StateInfoUpload(@RequestBody StateInfoUploadModel model)
    {
        try {
            model.setWarehouseId(ShiroUtils.getWarehouseId());
            model.setWarehouseCode(ShiroUtils.getWarehouseCode());
            if(model.getState().equals("2")) {
                Points points = new Points();
                points.setGoodsShelfNo(model.getShelfNo());
                points.setWarehouseCode(ShiroUtils.getWarehouseCode());
                points.setIsLocked(1);
                points=pointsService.selectFirstEntity(points);
                if(points!=null) {
                    points.setIsLocked(2);
                    int result = pointsService.updatePoints(points);
                    if (result < 1) {
                        throw new ServiceException("小车回传状态时修改货架状态失败");
                    }
                }
                agvStateInfoMapper.insertModel(model);
            }else {
                agvStateInfoMapper.insertModel(model);
            }
        }catch (Exception e){
            return AjaxResult.error("没有数据");
        }
        return AjaxResult.success("收到");
    }


    /**
     *
     * 自动组盘时下发任务
     *
     */
    @Transactional
    public AjaxResult TaskAss(Integer id)
    {
        Task task=new Task();
        task=taskService.selectEntityById(id);
        AgvTask agvTask=new AgvTask();
        agvTask.setTaskHeaderId(task.getId());
        agvTask.setTaskDetailId(task.getId());
        agvTask.setWarehouseId(task.getWarehouseId());
        agvTask.setWarehouseCode(task.getWarehouseCode());
        agvTask.setCompanyId(task.getCompanyId());
        agvTask.setCompanyCode(task.getCompanyCode());
        this.TaskModel(task.getContainerCode(), agvTask);
        return AjaxResult.success("成功");
    }
}