TaskAssignServiceImpl.java 6.29 KB
package com.huaheng.api.wcs.service.taskAssignService;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huaheng.api.wcs.domain.TaskDetails;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.common.exception.service.ServiceException;
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.service.AddressService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 任务下发接口ServiceImpl层
 * @author ricard
 * @date    2019/10/11
 *
 */

@Service
public class TaskAssignServiceImpl implements TaskAssignService {

    @Autowired
    private AddressService addressService;
    @Autowired
    private TaskDetailService taskDetailService;
    @Resource
    private LocationService locationService;


    public static String platform = "wms";

    //wms下发任务给wcs
    /**
     * 1、判断taskHeader是否为空
     * 2、判断必填字段是否满足
     * 3、转换实体
     * 4、发送数据
     *
     * @param taskHeader
     * @return
     */
    @Override
    @Transactional
    public AjaxResult wcsTaskAssign(TaskHeader taskHeader) {
        //1、判断taskHeader是否为空
        if(taskHeader == null){
            throw new ServiceException("wms任务为空");
        }

        //2、判断必填字段是否满足
        if(taskHeader.getId() == null){
            throw new ServiceException("wms任务号Id为空");
        }
        if(taskHeader.getTaskType() == null){
            throw new ServiceException("wms任务类型为空");
        }
        if(StringUtils.isEmpty(taskHeader.getContainerCode())){
            throw new ServiceException("wms任务中容器为空");
        }


        //入库性质的任务源库位不能为空
        if(taskHeader.getTaskType()==100 || taskHeader.getTaskType()==200
                || taskHeader.getTaskType()==500 || taskHeader.getTaskType()==800) {
            if (taskHeader.getFromLocation() == null) {
                throw new ServiceException("源库位为空");
            }
        }

       // 出库性质的任务目的库位不能为空
        if(taskHeader.getTaskType()==300 || taskHeader.getTaskType()==400
                || taskHeader.getTaskType()==600 || taskHeader.getTaskType()==700
                || taskHeader.getTaskType()==800 || taskHeader.getTaskType()==900)
        {
            if (taskHeader.getToLocation() == null) {
                throw new ServiceException("目的库位为空");
            }
        }

        //3、转换实体,初始化wcs任务实体
        WcsTask wcsTask = new WcsTask();
//        wcsTask.setWarehouseCode(ShiroUtils.getWarehouseCode());
        wcsTask.setTaskNo(taskHeader.getId().toString());
        wcsTask.setWarehouseCode(ShiroUtils.getWarehouseCode());
        wcsTask.setPreTaskNo("0");
        wcsTask.setTaskType(taskHeader.getTaskType().toString());
        wcsTask.setFromPort("1000");

        wcsTask.setContainerCode(taskHeader.getContainerCode());
        Location location;
        if ("300".equals(wcsTask.getTaskType()) || "400".equals(wcsTask.getTaskType()) || "600".equals(wcsTask.getTaskType())){
            location = locationService.findLocationByCode(taskHeader.getFromLocation());
        } else {
            location = locationService.findLocationByCode(taskHeader.getToLocation());
        }

        wcsTask.setStation(location.getRoadway());
        wcsTask.setDestination(location.getRoadway());
        wcsTask.setToPort(location.getRoadway());
        if(StringUtils.isEmpty(taskHeader.getFromLocation())){
            wcsTask.setFromLocationCode("0");
        }else {
            wcsTask.setFromLocationCode(taskHeader.getFromLocation());
        }
        if(StringUtils.isEmpty(taskHeader.getToLocation())){
            wcsTask.setToLocationCode("0");
        }else {
            wcsTask.setToLocationCode(taskHeader.getToLocation());
        }
        wcsTask.setPriority(100);
        wcsTask.setRemark("0");
        wcsTask.setPlatform(platform);

        //找到任务明细
        LambdaQueryWrapper<TaskDetail> taskDetailLam = Wrappers.lambdaQuery();
        taskDetailLam.eq(TaskDetail::getTaskId,taskHeader.getId());
        List<TaskDetail> taskDetailList = taskDetailService.list(taskDetailLam);
        if(taskDetailList == null){
            throw new ServiceException("没有子任务");
        }
        List<TaskDetails> taskDetails =new ArrayList<>();
        for(TaskDetail item : taskDetailList){
            TaskDetails details = new TaskDetails();
            details.setMaterialCode(item.getMaterialCode());
            details.setMaterialName(item.getMaterialName());
            if(StringUtils.isEmpty(item.getMaterialUnit())){
                details.setUnit("PCS");
            }else {
                details.setUnit(item.getMaterialUnit());
            }
            details.setQty(item.getQty());
            details.setReferLineNo(item.getId().toString());
            taskDetails.add(details);
        }

        wcsTask.setTaskDetails(taskDetails);

        //4、发送数据
        String param="wcs";
        String url=addressService.selectAddress(param)+"TaskAssign";
        String JsonParam = JSON.toJSONString(wcsTask);
        System.out.println(JsonParam);
        String result = HttpUtils.bodypost(url, JsonParam);
        if(StringUtils.isEmpty(result)){
            throw new ServiceException("接口地址错误或返回为空");
        }
        AjaxResult ajaxResult = JSON.parseObject(result, AjaxResult.class);
        if(ajaxResult.getCode()!=200){
            throw new ServiceException(ajaxResult.getMsg());
        }
        return ajaxResult;
    }
}