EmptyContainerController.java 12.2 KB
package com.huaheng.api.wcs.controller;


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.huaheng.api.wcs.domain.ManyEmptyDomain;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.domain.WmsTask;
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
import com.huaheng.api.wcs.service.warecellAllocation.LocationAllocationService;
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.security.ShiroUtils;
import com.huaheng.framework.aspectj.lang.annotation.ApiLogger;
import com.huaheng.framework.aspectj.lang.annotation.Log;
import com.huaheng.framework.aspectj.lang.constant.BusinessType;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.framework.web.domain.RetCode;
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.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.locationHigh.domain.LocationHigh;
import com.huaheng.pc.config.locationHigh.service.LocationHighService;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.system.config.service.IConfigService;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.ReceiptTaskService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import com.huaheng.pc.task.taskHeader.service.WorkTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/API/WMS/v2")
public class EmptyContainerController extends BaseController {
    @Resource
    private ZoneService zoneService;
    @Resource
    private ContainerService containerService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private ReceiptTaskService receiptTaskService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private IConfigService iConfigService;
    @Resource
    private AddressService addressService;



    /**
     * 生成空托入库任务
     * @return
     */
    @PostMapping("/wcsCallEmptyIn")
    @Log(title = "任务-任务管理", operating = "生成空托入库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "生成空托入库任务", from="WCS")
    public AjaxResult wcsCallEmptyIn(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        if(StringUtils.isEmpty(manyEmptyDomain.getContainerCode())){
            throw new ServiceException("容器不能有空值!");
        }
        String [] data=getPltCheck(manyEmptyDomain.getContainerCode(),manyEmptyDomain.getFromLocationCode());
        if (data.length<0){
            return AjaxResult.error("调用三一wms空托盘校验失败");
        } else{
            Integer id =receiptTaskService.emptyAudoIn(manyEmptyDomain.getContainerCode(),200,data[0]);
            //更新托盘是否为空
            containerService.updateemptyPallet(manyEmptyDomain.getContainerCode(),data[1], ShiroUtils.getWarehouseCode());
            if(id!=null){//创建任务成功,直接下发wcs
                try{
                    taskHeaderService.sendTaskToWcs(Convert.toIntArray(id.toString()));
                    return AjaxResult.success("任务:"+id+":执行下发成功");
                }catch(Exception e){
                    return AjaxResult.error("任务:"+id+":执行下发失败");
                }
            }else{
                return AjaxResult.error("任务生产失败");
            }
        }

    }



    /**
     * 生成空托出库任务
     * @return
     */
    @PostMapping("/wcsCallEmptyOut")
    @Log(title = "任务-任务管理", operating = "生成空托出库任务", action = BusinessType.INSERT)
    @ResponseBody
    @ApiLogger(apiName = "生成空托出库任务", from="WCS")
    public AjaxResult wcsCallEmptyOut(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        AjaxResult ajaxResult = handleMultiProcess(new MultiProcessListener() {
            @Override
            public AjaxResult doProcess() {
                AjaxResult ajaxResult = innerWcsCallEmptyOut(manyEmptyDomain);
                return ajaxResult;
            }
        });
        if(ajaxResult.getCode()==200){//创建任务成功,直接下发wcs
             Integer[] taskIds= {Integer.parseInt(ajaxResult.getData().toString())};
             ajaxResult= taskHeaderService.sendTaskToWcs(taskIds);
        }else{
            return ajaxResult;
        }
        return ajaxResult;

    }

    private AjaxResult innerWcsCallEmptyOut(ManyEmptyDomain manyEmptyDomain) {
        String warehouseCode = manyEmptyDomain.getWarehouseCode();
        String area = manyEmptyDomain.getArea();
        String port = manyEmptyDomain.getPort();
        if(warehouseCode == null) {
            return AjaxResult.error("warehouseCode 不能为空");
        }
        if(area == null) {
            return AjaxResult.error("area 不能为空");
        }
        if(port == null) {
            return AjaxResult.error("port 不能为空");
        }
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getWarehouseCode, warehouseCode)
                .eq(Zone::getArea, area);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        if(zone == null) {
            return AjaxResult.error("area不正确,没有找到对应区域");
        }
        String zoneCode = zone.getCode();
//        List<Location> list = containerService.getEmptyContainerInLocation(zoneCode,
//                "","", warehouseCode);
        List<Container> list = containerService.getEmptyContainerOut("LK",
                "","", "ZH0001");
        if(list == null || list.size() <= 0) {
            return AjaxResult.error("没有找到空托盘");
        }
        Container container = list.get(0);
        String containerCode = container.getCode();
        String locationCode = container.getLocationCode();
        return inventoryHeaderService.createEmptyOut(containerCode, locationCode, port, warehouseCode);
    }


    @PostMapping("/emptyContainerJudge")
    @Log(title = "任务-任务管理", operating = "判断是否为空托盘", action = BusinessType.INSERT)
    @ResponseBody
    @Transactional
    public AjaxResult emptyContainerJudge(@RequestBody ManyEmptyDomain manyEmptyDomain) {
        JSONObject data = new JSONObject();
        if(manyEmptyDomain.getContainerCode() == null){
            data.put("code", RetCode.FAIL);
            data.put("msg", "容器编号为空");
            throw new ServiceException("容器编号为空!");
        }
        else if(containerExit(manyEmptyDomain.getContainerCode())==false){
            data.put("code", RetCode.FAIL);
            data.put("msg", "库位没有该容器");
            throw new ServiceException("库位没有该容器!");
        }else{
            //1:空托盘,0:非空托盘
            String emptyPallet=getPltCheckIn(manyEmptyDomain.getContainerCode());
            if(emptyPallet.equals("1")){
                data.put("code", RetCode.SUCCESS);
                data.put("msg", manyEmptyDomain.getContainerCode()+":校验通过");
                data.put("emptyPallet", emptyPallet);
                return AjaxResult.success(data);
            }else{
//                data.put("msg", manyEmptyDomain.getContainerCode()+":校验未通过");
//                data.put("code", RetCode.FAIL);
//                data.put("emptyPallet", emptyPallet);
                return AjaxResult.error(manyEmptyDomain.getContainerCode()+":校验未通过");
            }
        }

    }

    /**
     * 方法描述    判断容器是否存在
     * @param: [containercode]
     * @return: java.lang.Integer
     * @author: chenao
     * @date: 2021/11/12
     */
    public boolean containerExit(String containercode){
        LambdaQueryWrapper<Container> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Container::getCode, containercode);
        List<Container> list = containerService.list(lambdaQueryWrapper);
        return  list.size()>0?true:false;
    }


    public String [] getPltCheck(String containercode,String fromLocationCode){
        WmsTask wmsTask=new WmsTask();
        String emptyPallet="";
        String taskNo="";
        JSONObject json=new JSONObject();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        wmsTask.setContainerCode(containercode);
        wmsTask.setReqID(iConfigService.getRegId());
        wmsTask.setRequestTime(df.format(new Date()));
        wmsTask.setContainerCode(containercode);
        wmsTask.setFromLocationCode(fromLocationCode);
        String JsonParam = JSON.toJSONString(wmsTask);
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_WMS_EMPTY_JUDGE, "ZH0001", "1");
        String result = HttpUtils.bodypost(url, JsonParam);
        json=JSONObject.parseObject(result);
        String [] data;
        System.out.println("aaaaaaaaa:"+json.getString("data"));
        if (json.getString("code").equals("1")){
            JSONObject jsondata=JSONObject.parseObject(json.getString("data"));
            taskNo=jsondata.getString("taskNo");
            emptyPallet=jsondata.getString("emptyPallet");
            data=new String[2];
            data[0]=taskNo;
            data[1]=emptyPallet;
        }else{
            data=null;
        }
        return data;
    }


    public String  getPltCheckIn(String containercode){
        //根据托盘号查询刚下发的任务 站台
        LambdaQueryWrapper<TaskHeader>queryWrapper=Wrappers.lambdaQuery();
        queryWrapper.eq(TaskHeader::getContainerCode,containercode)
        .eq(TaskHeader::getStatus,1)
        .last("limit 1");
        TaskHeader task=taskHeaderService.getOne(queryWrapper);
        String port="";
        if (task!=null){
            if(StringUtils.isNotEmpty(task.getPort())){
                port=task.getPort();
            }
        }
        WmsTask wmsTask=new WmsTask();
        String emptyPallet="";
        JSONObject json=new JSONObject();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        wmsTask.setContainerCode(containercode);
        wmsTask.setReqID(iConfigService.getRegId());
        wmsTask.setRequestTime(df.format(new Date()));
        wmsTask.setContainerCode(containercode);
        wmsTask.setFromLocationCode(port);
        String JsonParam = JSON.toJSONString(wmsTask);
        String url = addressService.selectAddress(QuantityConstant.ADDRESS_WMS_EMPTY_JUDGE, "ZH0001", "1");
        String result = HttpUtils.bodypost(url, JsonParam);
        json=JSONObject.parseObject(result);
        String code=json.getString("code");
        if (code.equals("1")){
            JSONObject jsondata=JSONObject.parseObject(json.getString("data"));
            //taskNo=jsondata.getString("taskNo");
            emptyPallet=jsondata.getString("emptyPallet");
            //更新托盘是否为空
            containerService.updateemptyPallet(containercode,emptyPallet,ShiroUtils.getWarehouseCode());
        }
        return code;
    }

}