TaskHeaderRytaskService.java 15.8 KB
package com.huaheng.pc.task.taskHeader.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.api.acs.domain.AgvTask;
import com.huaheng.api.acs.service.AcsService;
import com.huaheng.api.wcs.domain.WcsTask;
import com.huaheng.api.wcs.service.taskAssignService.TaskAssignService;
import com.huaheng.common.constant.HttpConstant;
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.Wrappers;
import com.huaheng.common.utils.restful.RestUtil;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
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.domain.ContainerType;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.cycleCountPreference.domain.CycleCountPreference;
import com.huaheng.pc.config.cycleCountPreference.service.CycleCountPreferenceService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.config.points.domain.Points;
import com.huaheng.pc.config.points.service.PointsService;
import com.huaheng.pc.config.station.domain.Station;
import com.huaheng.pc.config.station.service.StationService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.inventory.cycleCountDetail.domain.CycleCountDetail;
import com.huaheng.pc.inventory.cycleCountDetail.service.CycleCountDetailService;
import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail;
import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService;
import com.huaheng.pc.inventory.inventoryHeader.domain.InventoryHeader;
import com.huaheng.pc.inventory.inventoryHeader.service.InventoryHeaderService;
import com.huaheng.pc.receipt.receiptContainerDetail.domain.ReceiptContainerDetail;
import com.huaheng.pc.receipt.receiptContainerDetail.service.ReceiptContainerDetailService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerHeader;
import com.huaheng.pc.receipt.receiptContainerHeader.service.ReceiptContainerHeaderService;
import com.huaheng.pc.receipt.receiving.service.ReceivingService;
import com.huaheng.pc.shipment.shipmentContainerHeader.domain.ShipmentContainerHeader;
import com.huaheng.pc.shipment.shipmentContainerHeader.service.ShipmentContainerHeaderService;
import com.huaheng.pc.task.agvTask.controller.AgvTaskController;
import com.huaheng.pc.task.taskDetail.domain.TaskDetail;
import com.huaheng.pc.task.taskDetail.service.TaskDetailService;
import com.huaheng.pc.task.taskHeader.domain.MobileTask;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.mapper.TaskHeaderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TaskHeaderRytaskService extends ServiceImpl<TaskHeaderMapper, TaskHeader> {


    @Resource
    private LocationService locationService;
    @Resource
    private TaskAssignService taskAssignService;
    @Resource
    private InventoryHeaderService inventoryHeaderService;
    @Resource
    private TaskHeaderService taskHeaderService;
    @Resource
    private PointsService pointsService;

    @Resource
    private ZoneService zoneService;
    @Resource
    private StationService stationService;
    @Resource
    private WorkTaskService workTaskService;


    /**展厅移库演示*/
    public AjaxResult transferTask(String roadWay){
        try {
            String s=null;
            if(roadWay.equals("1")){
                s="M";
            }else if(roadWay.equals("2")){
                s="S";
            }
            final int t = getUnCompleteTaskList(s);
            if(t>3){
                return AjaxResult.error();
            }
            final Location location = locationService.getLocationOfRule(Integer.valueOf(roadWay));
            if(StringUtils.isNull(location)){
                return  AjaxResult.error();
            }
            final AjaxResult ajax =createTransferTask(location.getCode());
            TaskHeader data = (TaskHeader) ajax.getData();
            taskAssignService.wcsTaskAssign(data);
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    private  int getUnCompleteTaskList(String contianerCode)  {
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getTaskType, 800);
        taskHeaderLambdaQueryWrapper.lt(TaskHeader::getStatus, 100);
        taskHeaderLambdaQueryWrapper.likeRight(StringUtils.isNotEmpty(contianerCode),TaskHeader::getContainerCode, contianerCode);
        int taskHeaderList = this.count(taskHeaderLambdaQueryWrapper);
        return taskHeaderList;
    }
    @Transactional
    public AjaxResult createTransferTask(String sourceLocation) {
        String warehouseCode="CS0001";
        Location temp1 = new Location();
        temp1.setCode(sourceLocation);
        temp1.setWarehouseCode(warehouseCode);
        Location location = locationService.selectFirstEntity(temp1);
        if(location==null){
            return AjaxResult.error("源库位:"+sourceLocation+"未找到");
        }
        if(!location.getStatus().equals("empty")){
            return AjaxResult.error("源库位:"+sourceLocation+"状态非空闲");
        }
        if(StringUtils.isEmpty(location.getContainerCode())){
            return AjaxResult.error("源库位:"+sourceLocation+"不存在托盘");
        }
        if(!location.getZoneCode().equals("LK")){
            return AjaxResult.error("该库位:"+sourceLocation+"不是立库库位");
        }

        if(location.getZoneCode().equals("LK")){
            TaskHeader task =new TaskHeader();
            task.setContainerCode(location.getContainerCode());
            task.setStatus(10);
            task =this.selectFirstEntity(task);
            if(task!=null){
                return AjaxResult.error(location.getContainerCode()+"该容器已有任务");
            }
        }
        //这里增加组盘校验,如果此托盘存在未完成的组盘数据,则不能移库
        //校验入库组盘
        int count1 = inventoryHeaderService.getUncompleteReceiptContainer(sourceLocation,warehouseCode);
        if(count1>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在入库组盘,不能移库");
        }
        int count2 = inventoryHeaderService.getUncompleteShipmentContainer(sourceLocation,warehouseCode);
        if(count2>0){
            return AjaxResult.error("源库位:"+sourceLocation+"存在出库组盘,不能移库");
        }
        Location loc=new Location();
        loc.setCode(sourceLocation);
        loc.setRoadway("3");

        int line;
        int layer=(int)(Math.random()*10);
        if (location.getContainerCode().startsWith("L")||location.getContainerCode().startsWith("B")) {
            if(location.getIColumn()<=8){
                line=(int)(Math.random()*9+8);
            }else {
                line=(int)(Math.random()*8);
            }
            loc.setIColumn(line);
            loc.setILayer(layer);
            loc.setStatus("empty");
            loc = locationService.selectFirstEntity(loc);
        }
        if(loc ==null){
            if(location.getContainerCode().startsWith("S")){
                loc = locationService.getLocationOfRule(2);
            }else if(location.getContainerCode().startsWith("M")){
                loc = locationService.getLocationOfRule(1);
            }else {
                loc = locationService.getLocationOfRule(3);
            }
        }
        if(loc == null){
            return AjaxResult.error("立库库位已用完,无法移库");
        }
        TaskHeader task=new TaskHeader();
        task.setTaskType(800);
        task.setZoneCode(location.getZoneCode());
        task.setContainerCode(location.getContainerCode());
        task.setFromLocation(sourceLocation);
        task.setToLocation(loc.getCode());
        task.setCreatedBy("ck");
        task=createTask(warehouseCode,task);

        //更新货位状态为预定
        location.setStatus("lock");
        loc.setStatus("lock");
        loc.setUserDef1("0");
        locationService.updateById(location);
        locationService.updateById(loc);
        return AjaxResult.success(task);
    }

    private TaskHeader selectFirstEntity( TaskHeader task){
        LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
        taskHeaderLambdaQueryWrapper.eq(TaskHeader::getStatus, 10);
        taskHeaderLambdaQueryWrapper.eq(StringUtils.isNotEmpty(task.getContainerCode()),TaskHeader::getContainerCode, task.getContainerCode());
        taskHeaderLambdaQueryWrapper.last("limit 1");
        return this.getOne(taskHeaderLambdaQueryWrapper);
    }
    /**库存监控出库查看*/
    @Transactional
    public AjaxResult checkLocationCode(String locationCode){
        String warehouseCode="CS0001";
        Location location = new Location();
        location.setCode(locationCode);
        location.setWarehouseCode("CS0001");
        location.setDeleted(false);
        Location loc = locationService.selectFirstEntity(location);
        if(!loc.getStatus().equals("empty")){
            return AjaxResult.error(locationCode+"状态非空闲,操作失败");
        }
        if(StringUtils.isEmpty(loc.getContainerCode())){
            return AjaxResult.error(locationCode+"没有托盘,操作失败");
        }
        //生成出库查看任务
        TaskHeader task=new TaskHeader();
        task.setPort("1");
        if(loc.getLocationType().equals("A")) {
            task.setZoneCode("AGV");
        }else if(loc.getLocationType().equals("L")){
            task.setZoneCode("LK");
        }
        task.setRoadway(loc.getRoadway());
        task.setContainerCode(loc.getContainerCode());
        if(loc.getContainerCode().startsWith("S")){
            task.setPort("P1004");
        }else if(loc.getContainerCode().startsWith("L")||loc.getContainerCode().startsWith("B")){
            task.setPort("P1002");
        }
        task.setTaskType(900);
        task.setFromLocation(loc.getCode());
        task.setToLocation(loc.getCode());
        task.setCreatedBy("admin");
        task=createTask(warehouseCode,task);

        //如果是AGV的需要下发agv任务
        if(loc.getLocationType().equals("A")) {
            AgvTask model = new AgvTask();
            String containerCode = loc.getContainerCode();
            Points points = new Points();
            points.setGoodsShelfNo(containerCode.substring(0, 4));
            points.setWarehouseCode("CS0001");
            points = pointsService.selectEntity(points);
            if (points != null && points.getIsLocked()!=1) {
                model.setTaskHeaderId(task.getId());
                model.setTaskDetailId(task.getId());
                model.setTaskType("pick");
                model.setShelfNo(points.getGoodsShelfNo());
                model.setStation("ws01");
                model.setFromLocationNo("");
                model.setToLocationNo("");
                if (containerCode.substring(5, 7).equals("1")) {
                    model.setOrientation("A");
                } else {
                    model.setOrientation("C");
                }
//                if (!acsService.TaskAssign(model).hasErr()) {
//                    points.setIsLocked(1);
//                    pointsService.updateById(points);
//                    model.setWarehouseCode(task.getWarehouseCode());
//                    model.setCompanyCode(task.getCompanyCode());
//                    agvTaskMapper.insertModel(model);
//                }
            }
        }
        //出库时先锁库位状态
        loc.setStatus("lock");
        locationService.updateById(loc);

        return AjaxResult.success(task.getId());

    }

    private TaskHeader createTask(String warehouseCode,TaskHeader task){
        task.setWarehouseCode(warehouseCode);
        task.setWarehouseCode("CS0001");
        task.setCompanyCode("cshuahengweld");
        //这里默认一个0
        task.setPriority(0);
        task.setPort(null);
        task.setStatus(1);
        task.setCreated(new Date());
        this.save(task);
        return task;
    }

    public void innerAutoPush(String[] areas) {
        String warehouseCode = "CS0001";
        for(String area : areas) {
            Zone zone = zoneService.getZoneByArea(area);
            LambdaQueryWrapper<InventoryHeader> inventoryHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            inventoryHeaderLambdaQueryWrapper.eq(InventoryHeader::getZoneCode, zone.getCode());
            List<InventoryHeader> inventoryHeaderList = inventoryHeaderService.list(inventoryHeaderLambdaQueryWrapper);
            List<InventoryHeader> removeInventoryHeaderList = new ArrayList<>();
            for(InventoryHeader inventoryHeader : inventoryHeaderList) {
                String locationCode = inventoryHeader.getLocationCode();
                Location location = locationService.getLocationByCode(locationCode, warehouseCode);
                if(location.getStatus().equals(QuantityConstant.STATUS_LOCATION_LOCK)) {
                    removeInventoryHeaderList.add(inventoryHeader);
                }
            }
            inventoryHeaderList.removeAll(removeInventoryHeaderList);
            int size = inventoryHeaderList.size();
            int index = 0;
            LambdaQueryWrapper<Station> stationLambdaQueryWrapper = Wrappers.lambdaQuery();
            stationLambdaQueryWrapper.eq(Station::getArea, area)
                    .eq(Station::getType, QuantityConstant.STATION_PICK);
            List<Station> stationList = stationService.list(stationLambdaQueryWrapper);
            List<Station> removeStation = new ArrayList<>();
            LambdaQueryWrapper<TaskHeader> taskHeaderLambdaQueryWrapper = Wrappers.lambdaQuery();
            taskHeaderLambdaQueryWrapper.eq(TaskHeader::getZoneCode, zone.getCode())
                    .eq(TaskHeader::getTaskType, QuantityConstant.TASK_TYPE_VIEW)
                    .lt(TaskHeader::getStatus, QuantityConstant.TASK_STATUS_COMPLETED);
            List<TaskHeader> taskHeaderList = taskHeaderService.list(taskHeaderLambdaQueryWrapper);
            for(Station station : stationList) {
                String port = station.getCode();
                for(TaskHeader taskHeader : taskHeaderList) {
                    if(port.equals(taskHeader.getPort())) {
                        removeStation.add(station);
                        break;
                    }
                }
            }
            stationList.removeAll(removeStation);
            for(int i=0; i < stationList.size(); i++) {
                Station station = stationList.get(i);
                InventoryHeader inventoryHeader = inventoryHeaderList.get(i);
                List<Integer> ids = new ArrayList<>();
                ids.add(inventoryHeader.getId());
                workTaskService.createCheckOutTaskByIds(ids, station.getCode());
            }
        }
    }

}