AcsServiceImpl.java 19.1 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
package com.huaheng.api.acs.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.support.Convert;
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.AddressService;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.service.ContainerService;
import com.huaheng.pc.config.location.domain.LocationPosition;
import com.huaheng.pc.config.points.domain.Points;
import com.huaheng.pc.config.points.service.PointsService;
import com.huaheng.pc.config.pointsRecord.domain.PointsRecord;
import com.huaheng.pc.config.pointsRecord.service.PointsRecordService;
import com.huaheng.pc.monitor.agvcarinfo.domain.StateInfoUploadModel;
import com.huaheng.pc.monitor.agvcarinfo.service.AgvStateInfoService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import com.huaheng.pc.system.user.service.UserServiceImpl;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.IAgvTaskService;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderAgvService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName AcsServiceImpl
 * @Description TODO
 * @Author Administrator
 * @Date 2019/12/2615:38
 */
@Service
public class AcsServiceImpl implements AcsService {
    private static final Logger logger = LoggerFactory.getLogger(AcsServiceImpl.class);
    @Autowired
    private AddressService addressService;
    @Autowired
    private PointsService pointsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private TaskHeaderService taskHeaderService;
    @Autowired
    private PointsRecordService pointsRecordService;
    @Autowired
    private IAgvTaskService agvTaskService;
    @Autowired
    private TaskHeaderAgvService taskHeaderAgvService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Autowired
    private ContainerService containerService;
    @Autowired
    private AgvStateInfoService agvStateInfoService;

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

    @Override
    public AjaxResult StateInfoUpload(StateInfoUploadModel model) {
        try {
            if(StringUtils.isEmpty(model.getWarehouseCode())){
                return AjaxResult.error("请填写warehouseCode字段");
            }
            String json = JSON.toJSONString(model);
            logger.info("信息上传改变货架状态1:{}",json);
            if(model.getState().equals("2")) {
                Points points = new Points();
                points.setGoodsShelfNo(model.getShelfNo());
                points.setWarehouseCode(model.getWarehouseCode());
//                points.setIsLocked(1);
                points=pointsService.selectEntity(points);
                if(points!=null) {

                    logger.info("信息上传改变货架状态2:{}",json);
                    points.setIsLocked(2);
                    boolean result = pointsService.updateById(points);
                    if (!result) {
                        throw new ServiceException("小车回传状态时修改货架状态失败");
                    }
                    agvStateInfoService.insertModel(model);
                }
            }
        }catch (Exception e){
            return AjaxResult.error("没有数据");
        }
        return AjaxResult.success("收到");
    }

    @Override
    public AjaxResult TaskConfirm(AgvTask model) {
//        model.setWarehouseCode(ShiroUtils.getWarehouseCode());
        if(StringUtils.isEmpty(model.getWarehouseCode())){
            return AjaxResult.error("没有填写warehouseCode");
        }
        boolean result=agvTaskService.save(model);
        if(!result){
            throw new ServiceException("数据插入失败");
        }
        String shelfNo = model.getShelfNo().substring(0, 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);
            boolean rt=pointsService.updateById(points);
            if(!rt){
                throw new ServiceException("删除AGV出库任务后修改货架状态失败");
            }
        }
        //回传任务为putway时,查找到改货架中物料为agv的任务,并完成任务。

        return AjaxResult.success(model);
    }

    private String getUrl(){
        Address address =new Address();
        address.setParam("AGV");
        address.setWarehouseCode(ShiroUtils.getWarehouseCode());
        String url=addressService.selectEntity(address).getUrl();
        return url;
    }
    /**
     * AGV 任务取消
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    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.bodypost(url, JsonParam,"KS0001");
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }
    /**
     * AGV 旋转方向
     */
    @Override
    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.bodypost(url, JsonParam,"KS0001");
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }
    //手动叫车
    @Transactional
    @Override
    public AjaxResult ManualCalling(String station,String shelfNo){
        AgvTask model = new AgvTask();
        model.setTaskHeaderId(666);
        model.setTaskDetailId(666);
        model.setWarehouseCode(ShiroUtils.getWarehouseCode());
        model.setCompanyId(ShiroUtils.getCompanyIdList().get(0));
        model.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
        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.selectEntity(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.setStation(station);
                points.setOIntX(points.getIntX());
                points.setOIntY(points.getIntY());
                pointsService.updateById(points);
            }
        }else {
            throw new ServiceException("该货架已出库");
        }
        return ajaxResult;
    }
    /**
     * AGV 任务下发
     */

    public AjaxResult TaskAssign(AgvTask model)
    {
        model.setInteractionId(interactionId);
        User user =  userService.selectUserByLoginName(ShiroUtils.getLoginName());
        if(user == null){
            model.setPlatform("wms");
        }else {
            model.setPlatform(user.getUserName());
        }
        String url = this.getUrl()+"TaskAssign";
        String JsonParam = JSON.toJSONString(model);
        logger.info("任务下发:{}",JsonParam);
        String result = HttpUtils.bodypost(url, JsonParam,model.getWarehouseCode());
        if(StringUtils.isEmpty(result)){
            throw new ServiceException("ACS接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        return ajaxResult;
    }
    /**
     * 设置agv下发任务的实体
     *
     */
    @Transactional
    @Override
    public AjaxResult TaskModel(String station,Integer id){
        AjaxResult ajaxResult=new AjaxResult();
        AgvTask model=new AgvTask();

        //通过id查找任务
        TaskHeader task= taskHeaderService.getById(id);
        if(task==null){
            return AjaxResult.error("任务不存在");
        }

        String containerCode=task.getContainerCode();
//        String code=containerCode.substring(0,4);
        Container container=containerService.getContainerByCode(containerCode,task.getWarehouseCode());
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setGoodsShelfNo(container.getGoodsShelfNo());
        points=pointsService.selectEntity(points);

        //任务下发实体
        model.setTaskHeaderId(task.getId());
        model.setTaskDetailId(task.getId());
        model.setTaskType("pick");
        model.setStation(station);
        model.setWarehouseId(task.getWarehouseId());
        model.setWarehouseCode(task.getWarehouseCode());
        model.setCompanyId(task.getCompanyId());
        model.setCompanyCode(task.getCompanyCode());
        model.setStatus(10);
        if(containerCode.substring(5,6).equals("1")) {
            model.setOrientation("A");
        }else {
            model.setOrientation("C");
        }
        //判断货架是否处于1阶段,1阶段为pick阶段。
        if (points == null) {
            throw new ServiceException("货架不存在!");
        }
        //如果其他人有这个货架的任务已下发,则提示该货架有任务
        List<TaskHeader> agvUncompletedTasks = taskHeaderService.list(
                new LambdaQueryWrapper<TaskHeader>()
                        .eq(TaskHeader::getGoodsShelfNo, points.getGoodsShelfNo())
                        .ne(TaskHeader::getCreatedBy, task.getCreatedBy())
                        .eq(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_RELEASE)
        );
        if (agvUncompletedTasks!=null&&agvUncompletedTasks.size()>0) {
            throw new ServiceException("货架存在其他任务,请稍后下发!");
        }


        Integer[] ids=null;
        if(points.getGoodsShelfNo()!=null){
            List<TaskHeader> agvUncompletedTask = taskHeaderService.list(
                    new LambdaQueryWrapper<TaskHeader>()
                            .eq(TaskHeader::getGoodsShelfNo, points.getGoodsShelfNo())
                            .eq(TaskHeader::getCreatedBy, task.getCreatedBy())
                            .ge(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_BUILD)
                            .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED)
            );
            /*if(!agvUncompletedTask.isEmpty()){
                return AjaxResult.error(""+points.getGoodsShelfNo()+"有在执行任务");
            }*/
            List<Integer> codeList = agvUncompletedTask.stream().map(t -> t.getId()).collect(Collectors.toList());
            ids=codeList.toArray(new Integer[codeList.size()]);

        }

//        String ids=id.toString();
        //islocked  1出库中2在站台3在库内
        if((points.getIsLocked()==0 || points.getIsLocked()==3) || !station.equals(points.getStation())){
            model.setShelfNo(points.getGoodsShelfNo());
            model.setFromLocationNo("");
            model.setToLocationNo("");
            model.setDate(new Date());
            ajaxResult=this.TaskAssign(model);
//            ajaxResult=AjaxResult.success();
            if(!ajaxResult.hasErr()){
                //修改points表,设为出库状态,修改出库前位置
                points.setIsLocked(1);
                points.setOIntX(points.getIntX());
                points.setOIntY(points.getIntY());
                points.setStation(station);
                pointsService.updateById(points);
                if(!agvTaskService.save(model)){
                    throw new ServiceException("agv任务下发失败");
                }
                if(taskHeaderAgvService.sendTaskToAGV(ids,station).hasErr()){
                    throw new ServiceException("执行任务失败");
                }

                PointsRecord pointsRecord = new PointsRecord();
                pointsRecord.setCode(points.getGoodsShelfNo());
                pointsRecord.setStation(station);
                pointsRecord.setCreated(new Date());
                pointsRecord.setCreatedby(ShiroUtils.getLoginName());
                pointsRecordService.save(pointsRecord);
            }else{
                return ajaxResult;
            }
        }else{
            if(taskHeaderAgvService.sendTaskToAGV(ids,station).hasErr()){
                throw new ServiceException("执行任务失败");
            }
            return AjaxResult.success("任务执行成功,该货架已出库");
        }
        return  ajaxResult;
    }

    /**
     * agv  putway任务下发
     *
     */
    @Override
    @Transactional
    public AjaxResult TaskAssigns(Integer taskId,String shlfNo,String station,String row,String line)
    {
        //查找下发任务
        int result=0;
        String warehouseCode=ShiroUtils.getWarehouseCode();
        AgvTask agvTask=new AgvTask();
        if(taskId==null){
            agvTask.setTaskHeaderId(666);
            agvTask.setTaskDetailId(666);
        }else {
            TaskDetail task = taskDetailService.getById(taskId);
            agvTask.setTaskHeaderId(task.getId());
            agvTask.setTaskDetailId(task.getId());
            agvTask.setCompanyId(task.getCompanyId());
            agvTask.setCompanyCode(task.getCompanyCode());
        }
        agvTask.setWarehouseCode(ShiroUtils.getWarehouseCode());
        agvTask.setTaskType("putway");
        AjaxResult ajaxResult = new AjaxResult();
        //查找需要回去的对应的货架
        String containCode=shlfNo.substring(0, 5);
        Points points=new Points();
        points.setWarehouseCode(warehouseCode);
        points.setGoodsShelfNo(containCode);
        points.setIsLocked(2);
        points=pointsService.selectEntityV(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());
//            Integer number=pointsService.selectRanking(point);
//            if(number == null){
//                return AjaxResult.error("该货架不处于在站台状态,无法回库");
//            }
//
//            List<Points> pointsList = pointsService.selectILPointsList();
//            point = pointsList.get(number-1);

            point=pointsService.selectEntity(point);


            //如果查找到的货架和回库任务的货架相同,就不做操作
            if(point.getOIntX().equals(points.getIntX()) && point.getOIntY().equals(points.getIntY())){
                model.setToLocationNo("");
            }else {
                //如果不同,则目的位置为该货架的oIntX,oIntY
                model.setToLocationNo(point.getOIntX() + "," + point.getOIntY());
            }

            model.setOrientation("A");
            //#####################
            ajaxResult = this.TaskAssign(model);
            String jsons = JSON.toJSONString(ajaxResult);
            logger.info("回库回传信息:{}",jsons);
            String shelfNo=points.getGoodsShelfNo().substring(1, 5);
            LocationPosition lps=new LocationPosition();
            lps.setWarehouseCode(warehouseCode);
            lps.setIntX(point.getOIntX());
            lps.setIntY(point.getOIntY());

            LocationPosition lpss=new LocationPosition();
            lpss.setWarehouseCode(warehouseCode);
            lpss.setIntX(points.getIntX());
            lpss.setIntY(points.getIntY());


            if (!ajaxResult.hasErr()) {

                //回去位置与货架位置不一样时
                if(!lps.getIntX().equals(points.getIntX()) || !lps.getIntY().equals(points.getIntY()) ) {

                    //位置不同时,修改回库货架的x,y值
                    points.setIntX(lps.getIntX());
                    points.setIntY(lps.getIntY());
                    pointsService.updateById(points);

                    //位置不同,货架编号不同时,修改回去位置的货架的o属性x,y值
                    if(!point.getGoodsShelfNo().equals(points.getGoodsShelfNo())) {
                        point.setOIntX(points.getOIntX());
                        point.setOIntY(points.getOIntY());
                        pointsService.updateById(point);
                    }
                }

                points.setIsLocked(3);
                points.setNum(points.getNum()+1);
                points.setStation("");

                pointsService.updateById(points);
                String json = JSON.toJSONString(points);
                logger.info("回库后货架信息:{}",json);
//                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("该货架已建立回库任务");
        }
        return ajaxResult;
    }



}