AcsServiceImpl.java 9.2 KB
package com.huaheng.api.acs.service;

import com.alibaba.fastjson.JSON;
import com.huaheng.api.acs.domain.StateInfoUploadModel;
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.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.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.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderAgvService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
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.Map;

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

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

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

    @Override
    public AjaxResult TaskConfirm(AgvTask agvTask) {
        return null;
    }

    private String getUrl(){
        Address address =new Address();
        address.setParam("AGV");
        address.setWarehouseCode(ShiroUtils.getWarehouseCode());
        String url=addressService.selectEntity(address).getUrl();
        return url;
    }
    /**
     * AGV 任务取消
     */
    @Override
    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 旋转方向
     */
    @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.sendPost(url, JsonParam);
        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.sendPost(url, JsonParam);
        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);
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setGoodsShelfNo(code);
        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("货架不存在!");
        }
        String ids=id.toString();
        if((points.getIsLocked()!=1 && points.getIsLocked()!=2) || !station.equals(points.getStation())){
            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());
                points.setStation(station);
                pointsService.updateById(points);
                if(!agvTaskService.save(model)){
                    throw new ServiceException("agv任务下发失败");
                }
                if(taskHeaderAgvService.sendTaskToAGV(Convert.toIntArray(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(Convert.toIntArray(ids),station).hasErr()){
                throw new ServiceException("执行任务失败");
            }
            return AjaxResult.success("任务执行成功,该货架已出库");
        }
        return  ajaxResult;
    }
}