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

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.huaheng.pc.config.container.service.ContainerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.acs.domain.AcsStatus;
import com.huaheng.api.acs.domain.AgvDomain;
import com.huaheng.api.acs.domain.StateInfoUploadModel;
import com.huaheng.api.wcs.domain.TaskFinishDomain;
import com.huaheng.api.wcs.service.taskFinish.TaskFinishService;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.ZoneAreaUtils;
import com.huaheng.common.utils.bean.BeanUtils;
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.domain.AjaxResultACS;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.taskDetail.service.TaskDetailAgvService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;

/**
 * @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);

    @Resource
    private LocationService locationService;
    @Resource
    private ContainerService containerService;
    @Resource
    private AddressService addressService;
    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private TaskFinishService taskFinishService;
    @Resource
    private TaskDetailAgvService taskDetailAgvService;

    @Override
    public AjaxResult StateInfoUpload(StateInfoUploadModel model) {
        return null;
    }

    private String getUrl(String zoneCode) {
        Integer area = ZoneAreaUtils.getAreaByZoneCode(zoneCode);
        String url = addressService.selectAddress("AGV", QuantityConstant.WAREHOUSECODE, area.toString());
        return url;
    }

    /**
     * 下发任务
     * wms-acs
     * @param  model
     * @return
     */
    @Override
    public AjaxResult TaskAssign(AgvTask model) {
        String PalletNo = model.getPalletNo();
        if (StringUtils.isEmpty(PalletNo)) {
//            return AjaxResult.error("没有填写任务托盘号");
        }
        if (StringUtils.isEmpty(ShiroUtils.getLoginName())) {
            model.setPlatform("wms");
        } else {
            model.setPlatform(ShiroUtils.getLoginName());
        }
        AgvDomain domain = new AgvDomain();
        BeanUtils.copyBeanProp(domain, model);

        String url = this.getUrl(model.getZoneCode()) + "TaskAssign";
        String JsonParam = JSON.toJSONString(domain);
        logger.info("任务下发:{}", JsonParam);
        String result = HttpUtils.bodypost(url, JsonParam, QuantityConstant.WAREHOUSECODE, "AGV下发任务");
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("ACS接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResultACS ajaxResultACS = JSON.parseObject(result, AjaxResultACS.class);
//        AjaxResultACS ajaxResultACS=new AjaxResultACS();
//        ajaxResultACS.setState(true);
        if (!ajaxResultACS.state) {
            model.setRemark(ajaxResultACS.getMessage());
            agvTaskService.updateById(model);
            return AjaxResult.error(ajaxResultACS.getMessage());
        } else {
            model.setRemark(ajaxResultACS.getMessage());
            agvTaskService.updateById(model);
        }
        // 下发成功
        model.setState(10);
        agvTaskService.updateById(model);
        return AjaxResult.success(ajaxResultACS.getMessage()).setData(ajaxResultACS.getData());
    }

    /**
     * 回传任务
     * acs-wms
     * @param  model
     * @return
     */
    @Override
    public AjaxResult TaskConfirm(AgvTask model) {
        Integer taskNo = model.getTaskNo();
        if (StringUtils.isNull(taskNo)) {
            return AjaxResult.error("没有填写任务托盘号");
        }
        if (StringUtils.isEmpty(model.getAgvNo())) {
            return AjaxResult.error("没有填写小车号");
        }
        if (StringUtils.isNull(model.getState())) {
            return AjaxResult.error("没有填写任务状态");
        }
        AgvTask agvTask = agvTaskService.getAgvTaskByTaskNo(taskNo);
        if (agvTask == null) {
            return AjaxResult.error("找不到该任务" + taskNo);
        }
        agvTask.setState(model.getState());
        agvTask.setLastUpdatedBy("acs");
        agvTaskService.updateById(agvTask);

        return AjaxResult.success(agvTask);
    }

    /**
     * 取消任务
     * wms-acs
     * @param  model
     * @return
     */
    @Override
    public AjaxResult TaskCancel(AgvTask model) {
        Integer taskNo = model.getTaskNo();
        if (StringUtils.isNull(taskNo)) {
            return AjaxResult.error("没有填写任务号");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("TaskNo", taskNo);
        map.put("Reason", model.getRemark());
        String JsonParam = JSON.toJSONString(map);
        String zoneCode = model.getZoneCode();
        if (StringUtils.isEmpty(model.getZoneCode())) {
            zoneCode = "C";
        }
        AgvTask agvTask = agvTaskService.getById(taskNo);
        if (agvTask == null) {
            return AjaxResult.error("任务不存在");
        }
        if (agvTask.getState() >= 90) {
            return AjaxResult.success("任务已完成,不能取消");
        }
        if (agvTask.getState() >= 10) {
            String url = this.getUrl(zoneCode) + "TaskCancel";
            String result = HttpUtils.bodypost(url, JsonParam, QuantityConstant.WAREHOUSECODE, "AGV任务取消");
            AjaxResultACS ajaxResultACS = JSON.parseObject(result, AjaxResultACS.class);
            if (ajaxResultACS == null || !ajaxResultACS.state) {
//                return AjaxResult.error("取消agv任务:" + ajaxResultACS.getMessage());
            }
        }



// 取消库位数据恢复
//        Location location = null;
//        switch (agvTask.getTaskType()) {
//            case "putway":// 库位到站台,库位有货
//                location = locationService.getLocationByCode(agvTask.getFromPoint(), QuantityConstant.WAREHOUSECODE);
//                location.setStatus(QuantityConstant.STATUS_LOCATION_SOME);
//                locationService.updateById(location);
//                break;
//            case "pick":// 站台到库位,库位无货
//                location = locationService.getLocationByCode(agvTask.getToPoint(), QuantityConstant.WAREHOUSECODE);
//                location.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
//                locationService.updateById(location);
//                break;
//            case "pickPutway":// 库位到库位,起点有货终点无货
//            case "配送":// 库位到库位,起点有货终点无货
//                location = locationService.getLocationByCode(agvTask.getFromPoint(), QuantityConstant.WAREHOUSECODE);
//                location.setStatus(QuantityConstant.STATUS_LOCATION_SOME);
//                locationService.updateById(location);
//                Location location1 = locationService.getLocationByCode(agvTask.getToPoint(), QuantityConstant.WAREHOUSECODE);
//                location1.setStatus(QuantityConstant.STATUS_LOCATION_EMPTY);
//                locationService.updateById(location1);
//                break;
//        }
//        locationService.updateById(location);

        agvTaskService.removeById(agvTask.getTaskNo());
        //
        containerService.updateStatus(agvTask.getContainerCode(),QuantityConstant.STATUS_CONTAINER_EMPTY,QuantityConstant.WAREHOUSECODE);
        locationService.updateStatus(agvTask.getLocationCode(),QuantityConstant.STATUS_CONTAINER_EMPTY,QuantityConstant.WAREHOUSECODE);
        return AjaxResult.success("取消成功");
    }

    /**
     * 人工完成接口
     * wms-acs
     * @param  model
     * @return
     */
    @Override
    public AjaxResult TaskFinish(AgvTask model) {
        if (StringUtils.isEmpty(model.getPalletNo())) {
            return AjaxResult.error("没有填写任务托盘号");
        }
        Map<String, Object> map = new HashMap<>();
        // 站台
        map.put("StName", model.getToPoint());
        map.put("PalletNo", model.getPalletNo());
        map.put("StState", 99);
        String JsonParam = JSON.toJSONString(map);
        String url = this.getUrl(model.getZoneCode()) + "TaskFinish";
        String result = HttpUtils.bodypost(url, JsonParam, QuantityConstant.WAREHOUSECODE, "AGV任务完成");
        AjaxResultACS ajaxResultACS = JSON.parseObject(result, AjaxResultACS.class);
        if (!ajaxResultACS.state) {
            return AjaxResult.error(ajaxResultACS.getMessage());
        }
        return AjaxResult.success(ajaxResultACS.getMessage()).setData(ajaxResultACS.getData());
    }

    /**
     * AGV 的状态信息
     * wms-acs
     * @param  model
     * @return
     */
    @Override
    public AjaxResult AgvInfo(AgvTask model) {
        if (StringUtils.isEmpty(model.getAgvNo())) {
            return AjaxResult.error("没有填写小车号");
        }
        Map<String, Object> map = new HashMap<>();
        // 站台
        map.put("AgvNos", model.getAgvNo());
        String JsonParam = JSON.toJSONString(map);
        String url = this.getUrl(model.getZoneCode()) + "AgvInfo";
        String result = HttpUtils.bodypost(url, JsonParam, QuantityConstant.WAREHOUSECODE, "获取AGV信息");
        AjaxResultACS ajaxResultACS = JSON.parseObject(result, AjaxResultACS.class);
        if (!ajaxResultACS.state) {
            return AjaxResult.error(ajaxResultACS.getMessage());
        }
        return AjaxResult.success(ajaxResultACS.getMessage()).setData(ajaxResultACS.getData());
    }

    /**
     * 继续任务
     * wms-acs
     * @param  model
     * @return
     */
    @Override
    public AjaxResult ContinueTask(AgvTask model) {
        if (StringUtils.isNull(model.getTaskNo())) {
            return AjaxResult.error("没有填写任务号");
        }
        Map<String, Object> map = new HashMap<>();
        // 站台
        map.put("TaskNo", model.getTaskNo());
        map.put("TaskStatus", model.getState());
        String JsonParam = JSON.toJSONString(map);
        String url = this.getUrl(model.getZoneCode()) + "ContinueTask";
        String result = HttpUtils.bodypost(url, JsonParam, QuantityConstant.WAREHOUSECODE, "AGV任务继续");
        AjaxResultACS ajaxResultACS = JSON.parseObject(result, AjaxResultACS.class);
        if (!ajaxResultACS.state) {
            return AjaxResult.error(ajaxResultACS.getMessage());
        }
        return AjaxResult.success(ajaxResultACS.getMessage()).setData(ajaxResultACS.getData());
    }

    @Override
    @Transactional
    public AjaxResult notifyAGVTask(AcsStatus acsStatus) {
        if (acsStatus == null) {
            return AjaxResult.error("更新AGV状态信息为空");
        }
        String taskNo = acsStatus.getTaskNo();
        String carNo = acsStatus.getAgvNo();
        int status = acsStatus.getState();
        if (StringUtils.isNull(acsStatus.getTaskNo())) {
            return AjaxResult.error("更新AGV状态,任务号为空");
        }
        if (status == 0) {
            return AjaxResult.error("更新AGV状态,状态信息为空");
        }
        String updateBy = "acs";
        AgvTask agvTask = agvTaskService.getById(taskNo);
        if (agvTask == null) {
            return AjaxResult.error("没有找到对应AGV任务,任务号为" + taskNo);
        }
        if (agvTask.getState() < QuantityConstant.AGV_TASK_STATUS_RELEASE) {
            return AjaxResult.success("该任务状态还未下发,任务号为" + taskNo);
        }
        if (agvTask.getState() == status) {
            return AjaxResult.success("该任务状态已更新,任务号为" + taskNo);
        }
        if (status < 90) {
            return AjaxResult.success("该任务状态已更新,任务号为" + taskNo);
        }
        TaskHeader task = findTaskByAgvNo(agvTask.getTaskNo());

//        TaskHeader task = taskHeaderService.getById(agvTask.getTaskId());
        /*
         * if(task!=null&&StringUtils.isNotEmpty(acsStatus.getLocationCode())){
         * //当acs返回库位的时候任务库位改成acs返回的
         * task.setToLocation(acsStatus.getLocationCode());
         * taskHeaderService.updateById(task);
         * }
         */
        if (status == 90 && task != null && task.getSendTo().equals(QuantityConstant.TASK_SEND_TO_AGV)) {
            // 当任务时AGV任务时,调用通用完成任务接口
            TaskFinishDomain taskFinishDomain = new TaskFinishDomain();
            taskFinishDomain.setTaskNo(task.getId().toString());
            taskFinishDomain.setIsEmptyOut(0);
            AjaxResult ajaxResult = taskFinishService.completeTaskByWCSNoTransaction(taskFinishDomain);
            if (ajaxResult == null || ajaxResult.hasErr()) {
                throw new ServiceException("AGV,调用任务完成接口失败:" + ajaxResult.getMsg());
            }
        } else if (task != null && task.getSendTo().equals(QuantityConstant.TASK_SEND_TO_WCS)) {
            // 立库出库任务:agv任务完成线边库更新库存,线边库出入库才更新
        }
        agvTask.setState(status);
        agvTask.setAgvNo(carNo);
        agvTask.setLastUpdatedBy(updateBy);
        boolean result = agvTaskService.updateById(agvTask);

        if (!result) {
            return AjaxResult.error("更新AGV任务状态信息失败 ");
        }
        return AjaxResult.success("更新任务信息成功");
    }

    @Override
    @Transactional
    public AjaxResult acsCancelAGVTask(AcsStatus acsStatus) {
        if (StringUtils.isEmpty(acsStatus.getTaskNo())) {
            return AjaxResult.error("请填写任务号");
        }
        AgvTask agvTask = agvTaskService.getById(acsStatus.getTaskNo());
        if (agvTask == null) {
            return AjaxResult.error("任务不存在");
        }
        agvTaskService.removeById(agvTask.getTaskNo());
        return AjaxResult.success();
    }

    @Override
    public AjaxResultACS busTaskFinish(AgvTask agvTask) {
        Integer taskNo = agvTask.getTaskNo();
        AgvTask agvTask1 = agvTaskService.getAgvTaskByTaskNo(taskNo);
        if (agvTask1 == null) {
            return AjaxResultACS.error("任务号" + taskNo + "未找到任务");
        }
        agvTask1.setState(90);
        agvTaskService.updateById(agvTask1);
        return AjaxResultACS.success();
    }

    public AjaxResult bindContainer(AcsStatus acsStatus) {
        String taskNo = acsStatus.getTaskNo();
        if (StringUtils.isEmpty(taskNo)) {
            return AjaxResult.error("agv任务绑定托盘:未找到任务号");
        }
        if (StringUtils.isEmpty(acsStatus.getContainerCode())) {
            return AjaxResult.error("agv任务绑定托盘:未找到任务号");
        }
        AgvTask agvTask = agvTaskService.getAgvTaskByTaskNo(Integer.parseInt(taskNo));
        if (agvTask == null) {
            return AjaxResult.error("agv任务绑定托盘:agv任务号" + taskNo + "未找到任务");
        }
        TaskHeader task = findTaskByAgvNo(Integer.parseInt(taskNo));
        if (task == null) {
            return AjaxResult.error("agv任务绑定托盘:agv任务号" + taskNo + "未找到立库任务");
        }
        if (task.getStatus().intValue() == QuantityConstant.TASK_STATUS_COMPLETED) {
            return AjaxResult.error("agv任务绑定托盘:agv任务号" + taskNo + "立库任务已完成");
        }
        task.setContainerCode(acsStatus.getContainerCode());
        taskHeaderService.updateById(task);
        return AjaxResult.success();
    }

    public TaskHeader findTaskByAgvNo(Integer agvNo) {
        LambdaQueryWrapper<TaskHeader> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TaskHeader::getAgvNo, agvNo);
        lambdaQuery.last("limit 1");
        TaskHeader task = taskHeaderService.getOne(lambdaQuery);
        return task;
    }
}