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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectReader;
import com.huaheng.api.acs.domain.AcsReturn;
import com.huaheng.api.acs.domain.AcsStatus;
import com.huaheng.api.acs.domain.AcsWeightHeight;
import com.huaheng.api.acs.domain.AgvDomain;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.warecellAllocation.WarecellAllocationService;
import com.huaheng.common.constant.HttpConstant;
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.http.OkHttpUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
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.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.system.user.controller.ProfileController;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static com.huaheng.common.constant.QuantityConstant.*;

/**
 * @ClassName AcsServiceImpl
 * @Description TODO
 * @Author youjie
 * @Date 2019/12/2615:38
 */
@Service
public class AcsServiceImpl implements AcsService {
    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private AddressService addressService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private ContainerService containerService;
    @Resource
    private WarecellAllocationService warecellAllocationService;
    @Resource
    private ConfigService configService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private LocationService locationService;


    /**
     * 下发任务给ACS
     */
    @Override
    public AjaxResult createAGVTask(AgvTask agvTask) {
        AgvDomain agvDomain = new AgvDomain();
        agvDomain.setTaskNo(String.valueOf(agvTask.getId()));
        agvDomain.setTaskType(agvTask.getTaskType());
        agvDomain.setFromPort(agvTask.getFromPort());
        agvDomain.setToPort(agvTask.getToPort());
        agvDomain.setPriority(agvTask.getPriority());
        agvDomain.setCreatedBy(agvTask.getCreatedBy());
        agvDomain.setQty(agvTask.getQty());
//        agvDomain.setCreatedTime(agvTask.getCreated());
        agvDomain.setContainerCode(agvTask.getContainerCode());
        String warehouseCode = agvTask.getWarehouseCode();
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_AGV_TASK_ASSIGN, warehouseCode, null);

        String jsonParam = JSON.toJSONString(agvDomain);
        String result = HttpUtils.bodypost(url, jsonParam, agvTask.getWarehouseCode());
        //String result = OkHttpUtils.bodypost(url, jsonParam, agvTask.getWarehouseCode());
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        AcsReturn ajaxResult = JSON.parseObject(result, AcsReturn.class);
        if (!ajaxResult.getCode().equals("200")) {
            return AjaxResult.error(ajaxResult.getMessage());
        }
        return AjaxResult.success("创建AGV任务成功");
    }

    /**
     * 取消AGV任务
     */
    @Override
    public AjaxResult cancelAGVTask(AgvTask agvTask) {
        List<String> ids = new ArrayList<String>();
        ids.add(String.valueOf(agvTask.getId()));
        AgvDomain agvDomain = new AgvDomain();
        agvDomain.setTaskNo(String.valueOf(agvTask.getId()));
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_AGV_TASK_CANCEL);
        String jsonParam = JSON.toJSONString(agvDomain);
        String result = HttpUtils.bodypost(url, jsonParam, agvTask.getWarehouseCode());
        //String result = OkHttpUtils.bodypost(url, jsonParam,agvTask.getWarehouseCode());
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.getCode() != HttpConstant.OK) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
        return AjaxResult.success("取消AGV任务成功");
    }

    /**
     * 更新AGV任务
     */
    @Override
    public AjaxResult updateAGVTask(AgvTask agvTask) {
        AgvDomain agvDomain = new AgvDomain();
        agvDomain.setTaskNo(String.valueOf(agvTask.getId()));
        agvDomain.setPriority(agvTask.getPriority());
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_AGV_TASK_UPDATE);
        String jsonParam = JSON.toJSONString(agvDomain);
        String result = HttpUtils.bodypost(url, jsonParam, agvTask.getWarehouseCode());
        //String result = OkHttpUtils.bodypost(url, jsonParam, agvTask.getWarehouseCode());
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("接口地址错误或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.getCode() != HttpConstant.OK) {
            return AjaxResult.error(ajaxResult.getMsg());
        }
        return AjaxResult.success("更新AGV任务成功");
    }

    /**
     * 更新AGV状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult notifyAGVTask(String taskNo, String carNo, int status, String updateBy) {
        AgvTask agvTask = agvTaskService.getById(taskNo);
        if (agvTask == null) {
            return AjaxResult.error("没有找到对应AGV任务,任务号为" + taskNo);
        }
        if (agvTask.getStatus() < QuantityConstant.AGV_TASK_STATUS_RELEASE) {
            return AjaxResult.success("该任务状态还未下发,任务号为" + taskNo);
        }
        if (agvTask.getStatus() == status) {
            return AjaxResult.success("该任务状态已更新,任务号为" + taskNo);
        }
        agvTask.setStatus(status);
        agvTask.setCarNo(carNo);
        agvTask.setLastUpdatedBy(updateBy);
        boolean result = agvTaskService.updateById(agvTask);

        if (!result) {
            return AjaxResult.error("更新任务信息失败 ");
        }
        //当status为100时,则完成任务
        //补充入库:先出库 agvtaskid2 状态400,后入库 agvtaskid 状态300
        //分拣出库:先出库 agvtaskid 状态400,后入库 agvtaskid2 状态300
        if (status == 100) {
            TaskHeader taskHeader = taskHeaderService.getOne(new LambdaQueryWrapper<TaskHeader>().eq(TaskHeader::getId, agvTask.getTaskNo()));
            String[] weight = new String[]{"0"};
            if (taskHeader != null) {
                switch (taskHeader.getTaskType()) {
                    //补充入库
                    case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT:
                        //补充入库,agv类型为出库,不完成接口
                        if (agvTask.getTaskType().equals(QuantityConstant.AGV_TYPE_RELEASE)) {

                        } else {
                            //补充入库判断托盘出库否,是否完成agv出库任务
                            AgvTask agvTask2 = agvTaskService.getById(taskHeader.getAgvTaskId2());
                            if (agvTask2 != null && agvTask2.getStatus() == 100) {
                                taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                            } else {
                                throw new ServiceException("找不到补充入库托盘出库agv任务" + taskHeader.getAgvTaskId2() + "或者该任务还未完成");
                            }
                        }
                        break;
                        //分拣出库
                    case TASK_TYPE_SORTINGSHIPMENT:
                        if (agvTask.getTaskType().equals(QuantityConstant.AGV_TYPE_TAKE)) {
                            taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                        } else {
                            updateInventoryWeight(agvTask, taskHeader); //更新库存重量和库位重量
                        }
                        break;
                        //出库查看
                    case QuantityConstant.TASK_TYPE_VIEW:
                        if (agvTask.getTaskType().equals(QuantityConstant.AGV_TYPE_TAKE)) {
                            taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                            updateInventoryWeight(agvTask, taskHeader); //更新库存重量和库位重量
                        }
                        break;
                    default:
                        taskHeaderService.completeTaskByWMS(Convert.toIntArray(String.valueOf(taskHeader.getId())), weight);
                        break;
                }
            }
        }
        return AjaxResult.success("更新任务信息成功");
    }

    /**
     * 更新库存重量和库位重量
     */
    public void updateInventoryWeight(AgvTask agvTask, TaskHeader taskHeader) {

        InventoryHeader inventoryHeader = inventoryHeaderService.getOne(new LambdaQueryWrapper<InventoryHeader>()
                .eq(InventoryHeader::getContainerCode, agvTask.getContainerCode()).last("limit 1"));

        Container container = containerService.getContainerByCode(taskHeader.getContainerCode());
        inventoryHeader.setTotalWeight(container.getWeight().toString());
        //inventoryHeader.setCheckCount(0);
        inventoryHeaderService.updateById(inventoryHeader);
        Location location = locationService.getLocationByCode(taskHeader.getToLocation());
        location.setWeight(container.getWeight());
        locationService.updateById(location);
    }

    /**
     * 更新AGV点位信息
     */
    @Override
    public AjaxResult notifyAGVPort(AcsStatus acsStatus) {
        if (acsStatus == null) {
            return AjaxResult.error("更新AGV点位信息,传参信息为空");
        }
        if (StringUtils.isEmpty(acsStatus.getTaskNo())) {
            return AjaxResult.error("更新AGV点位信息,任务号为空");
        }
        if (StringUtils.isEmpty(acsStatus.getCarNo())) {
            return AjaxResult.error("更新AGV点位信息,车辆编号为空");
        }
        if (StringUtils.isEmpty(acsStatus.getOldPort())) {
            return AjaxResult.error("更新AGV点位信息,原来得出入口为空");
        }
        if (StringUtils.isEmpty(acsStatus.getNewPort())) {
            return AjaxResult.error("更新AGV点位信息,新得出入口为空");
        }
        AgvTask agvTask = agvTaskService.getById(acsStatus.getTaskNo());
        if (agvTask == null) {
            return AjaxResult.error("更新AGV点位信息,任务号为" + acsStatus.getTaskNo());
        }
        agvTask.setToPort(acsStatus.getNewPort());
        agvTask.setLastUpdatedBy(acsStatus.getCreatedBy());
        return AjaxResult.success("更新任务信息成功");
    }

    /**
     * 1、保存重量高度,查找任务,根据重量高度分配库位,任务库位
     * 2、入库更新任务主表站点
     * 3、更新agv任务表起点和终点
     *
     * @param acsWeightHeight
     * @return
     */
    @Override
    @Transactional(timeout = 60, rollbackFor = Exception.class)
    public AjaxResult weightAndHeightByContainer(AcsWeightHeight acsWeightHeight) {
        //检验参数
        Container container = checkParams(acsWeightHeight);
        //根据托盘号查找未完成任务
        TaskHeader taskHeader  = getTaskHeader(acsWeightHeight);
        //更新agv起点终点
        AgvTask agvTask;
        //补充入库:先出库 agvtaskid2 状态400,后入库 agvtaskid 状态300
        //分拣出库:先出库 agvtaskid 状态400,后入库 agvtaskid2 状态300
        if (taskHeader.getTaskType() == TASK_TYPE_SORTINGSHIPMENT || taskHeader.getTaskType() == QuantityConstant.TASK_TYPE_VIEW) {
            agvTask = agvTaskService.getById(taskHeader.getAgvTaskId2());
        } else {
            agvTask = agvTaskService.getById(taskHeader.getAgvTaskId());
            if (agvTask.getStatus() >= QuantityConstant.AGV_TASK_STATUS_RELEASE) {
                return AjaxResult.success(taskHeader.getAgvTaskId() + "agv任务已下发,任务已有起点中终点");
            }
        }
        container.setWeight(acsWeightHeight.getWeight());
        container.setHeight(acsWeightHeight.getHeight());
        containerService.updateById(container);
        //分配库位,更新任务表库位,巷道信息
        taskHeader = updateTaskHeaderLocation(taskHeader, acsWeightHeight);
        //更新agv起点终点
        if (agvTask != null) {
            agvTask.setToPort(taskHeader.getToLocation());
            agvTask.setFromPort(acsWeightHeight.getFormPort());
            agvTaskService.updateById(agvTask);
        }
        return AjaxResult.success().setData(taskHeader.getToLocation());
    }

    /**
     * 根据托盘号查找未完成任务
     */
    public TaskHeader getTaskHeader(AcsWeightHeight acsWeightHeight) {
        List<TaskHeader> taskHeaders = taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>()
                .eq(TaskHeader::getContainerCode, acsWeightHeight.getContainerCode())
                //生成任务
                .eq(TaskHeader::getStatus, TASK_STATUS_BUILD)
                //整盘入库、补充入库、出库查询
                .in(TaskHeader::getTaskType, TASK_TYPE_WHOLERECEIPT, TASK_TYPE_SUPPLEMENTRECEIPT, TASK_TYPE_VIEW, TASK_TYPE_SORTINGSHIPMENT)
                .orderByDesc(TaskHeader::getId));

        //状态为生成任务的没有,就找完成任务的
        if (taskHeaders == null || taskHeaders.size() == 0) {
            taskHeaders = taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>()
                    .eq(TaskHeader::getContainerCode, acsWeightHeight.getContainerCode())
                    //任务完成
                    .eq(TaskHeader::getStatus, TASK_STATUS_COMPLETED)
                    //分拣出库、出库查看
                    .in(TaskHeader::getTaskType, TASK_TYPE_SORTINGSHIPMENT, TASK_TYPE_VIEW)
                    .orderByDesc(TaskHeader::getId));
            if (taskHeaders == null || taskHeaders.size() == 0) {
                //List<TaskHeader> list = taskHeaderService.list(new LambdaQueryWrapper<TaskHeader>()
                //        .eq(TaskHeader::getContainerCode, acsWeightHeight.getContainerCode())
                //        .in(TaskHeader::getStatus, TASK_STATUS_BUILD,TASK_STATUS_RELEASE,TASK_STATUS_RUNNING,TASK_STATUS_ARRIVED_STATION)
                //        .in(TaskHeader::getTaskType, TASK_TYPE_SORTINGSHIPMENT,TASK_TYPE_VIEW));
                //if (list.size()==1) {
                //    throw new ServiceException("AGV没传出库完成信号,这个任务未完成,托盘:"+acsWeightHeight.getContainerCode());
                //}
                throw new ServiceException("传递AGV托盘重量高度接口," + acsWeightHeight.getContainerCode() + "该托盘在任务中找不到,请先生成任务");
            }

        }
        TaskHeader taskHeader = taskHeaders.get(0);
        //更新任务主表站点
        taskHeader.setPort(acsWeightHeight.getFormPort());
        taskHeader.setWeight(acsWeightHeight.getWeight().toString());
        taskHeaderService.updateById(taskHeader);
        return taskHeader;
    }

    /**
     * 分配库位,更新任务表库位,巷道信息
     */
    public TaskHeader updateTaskHeaderLocation(TaskHeader taskHeader, AcsWeightHeight acsWeightHeight) {
        if (taskHeader.getTaskType() == QuantityConstant.TASK_TYPE_WHOLERECEIPT) {
            WcsTask wcsTask = createWcsTask(taskHeader.getId().toString(), acsWeightHeight.getHeight(), acsWeightHeight.getWeight());
            AjaxResult ajaxResult = warecellAllocationService.warecellAllocation(wcsTask);
            if (ajaxResult.hasErr()) {
                throw new ServiceException("传递AGV托盘重量高度,请求库位时:" + ajaxResult.getMsg());
            }
            WcsTask wcsTask1 = (WcsTask) ajaxResult.getData();
            taskHeader.setRoadway(wcsTask1.getRoadWay());
            taskHeader.setToLocation(wcsTask1.getToLocationCode());
        } else {
            Location location = locationService.getLocationByCode(taskHeader.getToLocation());
            taskHeader.setRoadway(location.getRoadway());
        }
        return taskHeader;
    }

    /**
     * 检验参数
     */
    public Container checkParams(AcsWeightHeight acsWeightHeight) {
        if (acsWeightHeight == null) {
            throw new ServiceException("传递AGV托盘重量高度,对象为空");
        }
        BigDecimal weight = acsWeightHeight.getWeight();
        if (weight == null) {
            throw new ServiceException("传递AGV托盘重量高度,重量为空");
        }
        String weight_config = configService.getKey(QuantityConstant.CONFIG_WEGHT);//容器重量
        BigDecimal weight_bigdecimal = new BigDecimal(weight_config);
        if (weight.compareTo(weight_bigdecimal) > 0) {
            throw new ServiceException("传递AGV托盘重量高度," + weight + "重量超过单个托盘限定重量");
        }
        BigDecimal height = acsWeightHeight.getHeight();
        if (height == null) {
            throw new ServiceException("传递AGV托盘重量高度,高度为空");
        }
        /*String height_config = configService.getKey(QuantityConstant.CONFIG_HEIGHT);//容器高度
        BigDecimal height_bigdecimal=new BigDecimal(height_config);
        if(height.compareTo(height_bigdecimal)>0){
            throw  new ServiceException("传递AGV托盘重量高度,"+height+"高度超过单个托盘限定高度");
        }*/
        String containerCode = acsWeightHeight.getContainerCode();
        if (StringUtils.isEmpty(containerCode)) {
            throw new ServiceException("传递AGV托盘重量高度,托盘号为空");
        }
        String fromPort = acsWeightHeight.getFormPort();
        if (StringUtils.isEmpty(fromPort)) {
            throw new ServiceException("传递AGV托盘重量高度,起点为空");
        }
        //log.info(containerCode);
        Container container = containerService.getContainerByCode(containerCode, QuantityConstant.ryTask_warehouse_code);
        //log.info(container.toString());
        if (container == null) {
            throw new ServiceException("传递AGV托盘重量高度," + containerCode + "容器在容器管理中找不到");
        }
        return container;
    }

    public WcsTask createWcsTask(String taskNo, BigDecimal height, BigDecimal weight) {
        WcsTask wcsTask = new WcsTask();
        wcsTask.setTaskNo(taskNo);
        wcsTask.setWarehouseCode("CS0001");
        wcsTask.setArea("2");
        wcsTask.setLength("100");
        //String height_config = configService.getKey(QuantityConstant.CONFIG_AGV_HEIGHT);//容器重量
        //判断高低库位,1.4以下低库位,1.4-1。5高库位
        /*if(height.compareTo(new BigDecimal(height_config))>=0){
            wcsTask.setHeight("2");
        }else{
            wcsTask.setHeight("1");
        }*/
        wcsTask.setHeight(String.valueOf(height.intValue()));
        wcsTask.setWidth("1000");
        wcsTask.setWeight(weight.toString());
        return wcsTask;
    }


}