AcsByCSWXFService.java 5.94 KB
package com.huaheng.api.acs.service;

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

import javax.annotation.Resource;

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.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.http.OkHttpUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.address.service.AddressService;
import com.huaheng.pc.task.agvTask.domain.AgvTask;
import com.huaheng.pc.task.agvTask.service.AgvTaskService;
import com.huaheng.pc.task.agvTaskCS.domain.AgvTaskCsDomain;

/**
 * 长沙agv交互接口:维修房的长沙agv接口,用的agvTask表
 */
@Service
public class AcsByCSWXFService {

    private static final Logger logger = LoggerFactory.getLogger(AcsByCSWXFService.class);

    @Resource
    private AgvTaskService agvTaskService;
    @Resource
    private AddressService addressService;
    @Resource
    private AcsTaskAndCSService acsTaskAndCSService;

    /**
     * 下发任务给ACS
     */
    public AjaxResult createAGVTask(AgvTask agvTask, String area) {
        AgvTaskCsDomain agvTaskCsDomain = acsTaskAndCSService.getAgvCsByTask(agvTask);

        String url = addressService.selectAddress("AGV", QuantityConstant.WAREHOUSECODE, area, 0) + "createAGVTask";
        String JsonParam = JSON.toJSONString(agvTaskCsDomain);
        logger.info("任务下发:{}", JsonParam);
        String result = OkHttpUtils.bodypost(url, JsonParam, "", "AGV-CS下发任务");
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("ACS接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.hasErr()) {
            agvTask.setRemark(ajaxResult.getMsg());
            agvTaskService.updateById(agvTask);

            return AjaxResult.error(ajaxResult.getMessage());
        } else {
            agvTask.setRemark(ajaxResult.getMsg());
            agvTaskService.updateById(agvTask);
        }
        // 下发成功
        agvTask.setState(QuantityConstant.AGV_TASK_STATUS_RELEASE);
        agvTaskService.updateById(agvTask);
        return AjaxResult.success(ajaxResult.getMsg()).setData(ajaxResult.getData());
    }

    /**
     * 下发任务给ACS
     */
    public AjaxResult cancelAGVTask(Integer taskNo) {
        String url = addressService.selectAddress("AGV_CS", QuantityConstant.WAREHOUSECODE, null, 0) + "cancelAGVTask";
        Map<String, Object> map = new HashMap<>();
        map.put("taskNo", taskNo);

        String JsonParam = JSON.toJSONString(map);
        logger.info("任务下发:{}", JsonParam);

        AgvTask agvTaskCs = agvTaskService.getById(taskNo);
        if (agvTaskCs == null) {
            return AjaxResult.error("任务不存在");
        }
        if (agvTaskCs.getState() >= 100) {
            return AjaxResult.success("任务已完成,不能取消");
        }
        String result = OkHttpUtils.bodypost(url, JsonParam, "", "AGV-CS取消任务");
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("ACS接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.hasErr()) {
            return ajaxResult;
        }
        agvTaskService.removeById(agvTaskCs.getTaskNo());
        return AjaxResult.success();
    }

    /**
     * 调整优先级
     */
    public AjaxResult updateAGVTask(Integer taskNo, Integer priority) {

        String url = addressService.selectAddress("AGV_CS", QuantityConstant.WAREHOUSECODE, null, 0) + "updateAGVTask";

        AgvTask agvTaskCs = agvTaskService.getById(taskNo);
        if (agvTaskCs == null) {
            return AjaxResult.error("任务不存在");
        }
        if (agvTaskCs.getState() >= 100) {
            return AjaxResult.success("任务已完成,不能取消");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("taskNo", taskNo);
        map.put("priority", priority);
        String JsonParam = JSON.toJSONString(map);
        logger.info("任务下发:{}", JsonParam);
        String result = OkHttpUtils.bodypost(url, JsonParam, "", "AGV-CS调整优先级");
        if (StringUtils.isEmpty(result)) {
            throw new ServiceException("ACS接口地址错误或服务器连接不到或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if (ajaxResult.hasErr()) {
            return ajaxResult;
        }
        agvTaskCs.setTaskLevel(priority);
        agvTaskService.updateById(agvTaskCs);
        return AjaxResult.success();
    }

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult notifyAGVTask(String taskNo, String carNo, Integer status, String updateBy) {
        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);
        }
        agvTask.setState(status);
        agvTask.setAgvNo(carNo);
        agvTask.setLastUpdatedBy(updateBy);
        boolean result = agvTaskService.updateById(agvTask);
        if (!result) {
            return AjaxResult.error("更新任务信息失败 ");
        }
        if (status == 100) {
            // 更新库存
        }
        return AjaxResult.success("更新叉车库任务信息成功");
    }

}