package com.zy.asrs.wms.utils;  
 | 
  
 | 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;  
 | 
import com.zy.asrs.framework.common.Cools;  
 | 
import com.zy.asrs.framework.exception.CoolException;  
 | 
import com.zy.asrs.wms.asrs.entity.*;  
 | 
import com.zy.asrs.wms.asrs.entity.enums.LocStsType;  
 | 
import com.zy.asrs.wms.asrs.entity.enums.LocTypeHeightType;  
 | 
import com.zy.asrs.wms.asrs.entity.param.FieldParam;  
 | 
import com.zy.asrs.wms.asrs.mapper.ViewLocDetlMapper;  
 | 
import com.zy.asrs.wms.asrs.mapper.ViewTaskDetlMapper;  
 | 
import com.zy.asrs.wms.asrs.service.*;  
 | 
import org.springframework.beans.factory.annotation.Autowired;  
 | 
import org.springframework.stereotype.Component;  
 | 
  
 | 
import java.util.ArrayList;  
 | 
import java.util.List;  
 | 
import java.util.Map;  
 | 
  
 | 
@Component  
 | 
public class LocUtils {  
 | 
  
 | 
    @Autowired  
 | 
    private TaskService taskService;  
 | 
    @Autowired  
 | 
    private LocService locService;  
 | 
    @Autowired  
 | 
    private SuggestLocRuleService suggestLocRuleService;  
 | 
    @Autowired  
 | 
    private LanewayRuleService lanewayRuleService;  
 | 
    @Autowired  
 | 
    private LocTypeService locTypeService;  
 | 
    @Autowired  
 | 
    private LocTypeBindService locTypeBindService;  
 | 
    @Autowired  
 | 
    private ViewLocDetlMapper viewLocDetlMapper;  
 | 
    @Autowired  
 | 
    private ViewTaskDetlMapper viewTaskDetlMapper;  
 | 
    @Autowired  
 | 
    private MatService matService;  
 | 
  
 | 
    //从库存或任务中匹配相邻库位(满托盘)  
 | 
    public Loc getNeighborLoc(Long taskType, Long matId, String batch, List<FieldParam> uniqueFields, Integer locTypeHeight) {  
 | 
        //满托盘  
 | 
        Mat mat = matService.getById(matId);  
 | 
        if (mat == null) {  
 | 
            return null;  
 | 
        }  
 | 
  
 | 
        //从任务中进行匹配  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        List<Map<String, Object>> list = viewTaskDetlMapper.getList(mat.getMatnr(), batch, uniqueFields);  
 | 
        for (Map<String, Object> map : list) {  
 | 
            Task task = taskService.getById(map.get("taskId").toString());  
 | 
            if (task == null) {  
 | 
                continue;  
 | 
            }  
 | 
  
 | 
            String targetLoc = task.getTargetLoc();  
 | 
            Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, targetLoc));  
 | 
            if (one == null) {  
 | 
                continue;  
 | 
            }  
 | 
            locs.add(one);  
 | 
        }  
 | 
  
 | 
        Loc loc = filterLoc(taskType, locs);  
 | 
        if (loc != null) {  
 | 
            return loc;  
 | 
        }  
 | 
  
 | 
        //从库存中进行匹配  
 | 
        List<Loc> locs2 = new ArrayList<>();  
 | 
        List<Map<String, Object>> list2 = viewLocDetlMapper.getList(mat.getMatnr(), batch, uniqueFields, null);  
 | 
        for (Map<String, Object> map : list2) {  
 | 
            Loc one = locService.getById(map.get("locId").toString());  
 | 
            if (one == null) {  
 | 
                continue;  
 | 
            }  
 | 
            locs2.add(one);  
 | 
        }  
 | 
  
 | 
        Loc loc2 = filterLoc(taskType, locs2);  
 | 
        if (loc2 != null) {  
 | 
            return loc2;  
 | 
        }  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    //获取推荐库位(满托盘)  
 | 
    public List<Loc> getSuggestLoc(Long taskType, Long matId, String batch, Integer locTypeHeight, List<Integer> laneRowList, Integer currentLev) {  
 | 
        //满托盘  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);  
 | 
        if (locTypeHeightType == null) {  
 | 
            throw new CoolException("库位高度类型异常");  
 | 
        }  
 | 
        //获取库位高度  
 | 
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));  
 | 
        //符合库位高度的库位集合  
 | 
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);  
 | 
  
 | 
        LambdaQueryWrapper<SuggestLocRule> wrapper = new LambdaQueryWrapper<>();  
 | 
        wrapper.eq(SuggestLocRule::getLocType, 1);  
 | 
        wrapper.eq(SuggestLocRule::getMatId, matId);  
 | 
        if (!Cools.isEmpty(batch)) {  
 | 
            wrapper.eq(SuggestLocRule::getBatch, batch);  
 | 
        }  
 | 
        List<SuggestLocRule> suggestLocRules = suggestLocRuleService.list(wrapper);  
 | 
        for (SuggestLocRule suggestLocRule : suggestLocRules) {  
 | 
            LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();  
 | 
            queryWrapper.ge(Loc::getRow1, suggestLocRule.getStartRow());  
 | 
            queryWrapper.le(Loc::getRow1, suggestLocRule.getTargetRow());  
 | 
            queryWrapper.ge(Loc::getBay1, suggestLocRule.getStartBay());  
 | 
            queryWrapper.le(Loc::getBay1, suggestLocRule.getTargetBay());  
 | 
            queryWrapper.ge(Loc::getLev1, suggestLocRule.getStartLev());  
 | 
            queryWrapper.le(Loc::getLev1, suggestLocRule.getTargetLev());  
 | 
            queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());  
 | 
            queryWrapper.in(Loc::getId, locIdList);  
 | 
  
 | 
            if (laneRowList != null && !laneRowList.isEmpty()) {  
 | 
                queryWrapper.in(Loc::getRow1, laneRowList);  
 | 
            }  
 | 
  
 | 
            if (currentLev != null) {  
 | 
                queryWrapper.eq(Loc::getLev1, currentLev);  
 | 
            }  
 | 
  
 | 
            List<Loc> list = locService.list(queryWrapper);  
 | 
            if (!list.isEmpty()) {  
 | 
                locs.addAll(list);  
 | 
            }  
 | 
        }  
 | 
        return locs;  
 | 
    }  
 | 
  
 | 
    //获取全局库位(完整巷道)  
 | 
    public List<Loc> getGlobalLoc(Long taskType, Integer locTypeHeight, List<Integer> laneRowList, Integer currentLev) {  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);  
 | 
        if (locTypeHeightType == null) {  
 | 
            throw new CoolException("库位高度类型异常");  
 | 
        }  
 | 
        //获取库位高度  
 | 
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));  
 | 
        //符合库位高度的库位集合  
 | 
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);  
 | 
  
 | 
        LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();  
 | 
        queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());  
 | 
        queryWrapper.in(Loc::getId, locIdList);  
 | 
  
 | 
        if (laneRowList != null && !laneRowList.isEmpty()) {  
 | 
            queryWrapper.in(Loc::getRow1, laneRowList);  
 | 
        }  
 | 
  
 | 
        if (currentLev != null) {  
 | 
            queryWrapper.eq(Loc::getLev1, currentLev);  
 | 
        }  
 | 
  
 | 
        List<Loc> list = locService.list(queryWrapper);  
 | 
        if (!list.isEmpty()) {  
 | 
            locs.addAll(list);  
 | 
        }  
 | 
        return locs;  
 | 
    }  
 | 
  
 | 
    //从库位集合中获取符合深浅的库位  
 | 
    public Loc filterLoc(Long taskType, List<Loc> locs) {  
 | 
        if (locs == null || locs.isEmpty()) {  
 | 
            return null;  
 | 
        }  
 | 
  
 | 
        Loc defaultLoc = null;  
 | 
        for (Loc loc : locs) {  
 | 
            //获取库位所在巷道  
 | 
            LanewayRule lanewayRule = lanewayRuleService.getLaneByLoc(loc);  
 | 
            if(lanewayRule == null) {  
 | 
                throw new CoolException("库位未配置巷道");  
 | 
            }  
 | 
  
 | 
            //获取库位方向  
 | 
            List<Integer> direction = null;  
 | 
            if (lanewayRule.getLaneX$().contains(loc.getRow1())) {  
 | 
                direction = lanewayRule.getLaneX$();  
 | 
            }else {  
 | 
                direction = lanewayRule.getLaneY$();  
 | 
            }  
 | 
  
 | 
            for (Integer row : direction) {  
 | 
                Loc one = locService.getOne(new LambdaQueryWrapper<Loc>()  
 | 
                        .eq(Loc::getRow1, row)  
 | 
                        .eq(Loc::getBay1, loc.getBay1())  
 | 
                        .eq(Loc::getLev1, loc.getLev1()));  
 | 
                if (one == null) {  
 | 
                    continue;  
 | 
                }  
 | 
  
 | 
                if(one.getLocStsId() != LocStsType.O.val()) {  
 | 
                    continue;  
 | 
                }  
 | 
  
 | 
                //当前库位是空库位  
 | 
                defaultLoc = one;  
 | 
                break;  
 | 
            }  
 | 
  
 | 
            if (defaultLoc != null) {  
 | 
                break;  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return defaultLoc;  
 | 
    }  
 | 
  
 | 
    //从库位集合中获取完整空巷道  
 | 
    public Loc filterAllLoc(List<Loc> locs) {  
 | 
        if (locs == null || locs.isEmpty()) {  
 | 
            return null;  
 | 
        }  
 | 
  
 | 
        Loc defaultLoc = null;  
 | 
        for (Loc loc : locs) {  
 | 
            //获取库位所在巷道  
 | 
            LanewayRule lanewayRule = lanewayRuleService.getLaneByLoc(loc);  
 | 
            if(lanewayRule == null) {  
 | 
                throw new CoolException("库位未配置巷道");  
 | 
            }  
 | 
  
 | 
            //获取库位方向  
 | 
            List<Integer> direction = null;  
 | 
            if (lanewayRule.getLaneX$().contains(loc.getRow1())) {  
 | 
                direction = lanewayRule.getLaneX$();  
 | 
            }else {  
 | 
                direction = lanewayRule.getLaneY$();  
 | 
            }  
 | 
  
 | 
            boolean flag = false;  
 | 
            List<Loc> allLocs = new ArrayList<>();  
 | 
            for (Integer row : direction) {  
 | 
                Loc one = locService.getOne(new LambdaQueryWrapper<Loc>()  
 | 
                        .eq(Loc::getRow1, row)  
 | 
                        .eq(Loc::getBay1, loc.getBay1())  
 | 
                        .eq(Loc::getLev1, loc.getLev1()));  
 | 
                if (one == null) {  
 | 
                    continue;  
 | 
                }  
 | 
  
 | 
                if(one.getLocStsId() != LocStsType.O.val()) {  
 | 
                    flag = true;  
 | 
                    break;  
 | 
                }  
 | 
  
 | 
                allLocs.add(one);  
 | 
            }  
 | 
  
 | 
            if (flag) {  
 | 
                continue;  
 | 
            }  
 | 
  
 | 
            if (!allLocs.isEmpty()) {  
 | 
                defaultLoc = allLocs.get(0);  
 | 
                break;//找到一个完整空巷道  
 | 
            }  
 | 
        }  
 | 
  
 | 
        return defaultLoc;  
 | 
    }  
 | 
  
 | 
    //从库存或任务中匹配相邻库位(空托盘)  
 | 
    public Loc getNeighborEmptyLoc(Long taskType, Integer locTypeHeight) {  
 | 
        //从任务中进行匹配  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskType, 10));  
 | 
        for (Task task : list) {  
 | 
            String targetLoc = task.getTargetLoc();  
 | 
            Loc one = locService.getOne(new LambdaQueryWrapper<Loc>().eq(Loc::getLocNo, targetLoc));  
 | 
            if(one == null) {  
 | 
                continue;  
 | 
            }  
 | 
            locs.add(one);  
 | 
        }  
 | 
  
 | 
        Loc loc = filterLoc(taskType, locs);  
 | 
        if (loc != null) {  
 | 
            return loc;  
 | 
        }  
 | 
  
 | 
        //从库存中进行匹配  
 | 
        List<Loc> locs2 = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocStsId, LocStsType.D.val()));  
 | 
        Loc loc2 = filterLoc(taskType, locs2);  
 | 
        if (loc2 != null) {  
 | 
            return loc2;  
 | 
        }  
 | 
        return null;  
 | 
    }  
 | 
  
 | 
    //获取推荐库位(空托盘)  
 | 
    public List<Loc> getSuggestEmptyLoc(Long taskType, Integer locTypeHeight, List<Integer> laneRowList, Integer currentLev) {  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        //空托盘  
 | 
        List<SuggestLocRule> suggestLocRules = suggestLocRuleService.list(new LambdaQueryWrapper<SuggestLocRule>().eq(SuggestLocRule::getLocType, 0));  
 | 
        for (SuggestLocRule suggestLocRule : suggestLocRules) {  
 | 
            LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();  
 | 
            queryWrapper.ge(Loc::getRow1, suggestLocRule.getStartRow());  
 | 
            queryWrapper.le(Loc::getRow1, suggestLocRule.getTargetRow());  
 | 
            queryWrapper.ge(Loc::getBay1, suggestLocRule.getStartBay());  
 | 
            queryWrapper.le(Loc::getBay1, suggestLocRule.getTargetBay());  
 | 
            queryWrapper.ge(Loc::getLev1, suggestLocRule.getStartLev());  
 | 
            queryWrapper.le(Loc::getLev1, suggestLocRule.getTargetLev());  
 | 
            queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());  
 | 
  
 | 
            if (laneRowList != null && !laneRowList.isEmpty()) {  
 | 
                queryWrapper.in(Loc::getRow1, laneRowList);  
 | 
            }  
 | 
  
 | 
            if (currentLev != null) {  
 | 
                queryWrapper.eq(Loc::getLev1, currentLev);  
 | 
            }  
 | 
  
 | 
            List<Loc> list = locService.list(queryWrapper);  
 | 
            if (!list.isEmpty()) {  
 | 
                locs.addAll(list);  
 | 
            }  
 | 
        }  
 | 
        return locs;  
 | 
    }  
 | 
  
 | 
    //获取全局库位(完整巷道)  
 | 
    public List<Loc> getGlobalEmptyLoc(Long taskType, Integer locTypeHeight, List<Integer> laneRowList, Integer currentLev) {  
 | 
        List<Loc> locs = new ArrayList<>();  
 | 
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);  
 | 
        if (locTypeHeightType == null) {  
 | 
            throw new CoolException("库位高度类型异常");  
 | 
        }  
 | 
        //获取库位高度  
 | 
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));  
 | 
        //符合库位高度的库位集合  
 | 
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);  
 | 
  
 | 
        LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();  
 | 
        queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());  
 | 
        queryWrapper.in(Loc::getId, locIdList);  
 | 
  
 | 
        if (laneRowList != null && !laneRowList.isEmpty()) {  
 | 
            queryWrapper.in(Loc::getRow1, laneRowList);  
 | 
        }  
 | 
  
 | 
        if (currentLev != null) {  
 | 
            queryWrapper.eq(Loc::getLev1, currentLev);  
 | 
        }  
 | 
  
 | 
        List<Loc> list = locService.list(queryWrapper);  
 | 
        if (!list.isEmpty()) {  
 | 
            locs.addAll(list);  
 | 
        }  
 | 
        return locs;  
 | 
    }  
 | 
  
 | 
    // 重写ctu库获取库位  
 | 
    public Loc getGlobalEmptyLocToCtu(Integer locTypeHeight) {  
 | 
        LocTypeHeightType locTypeHeightType = LocTypeHeightType.get(locTypeHeight);  
 | 
        if (locTypeHeightType == null) {  
 | 
            throw new CoolException("库位高度类型异常");  
 | 
        }  
 | 
        //获取库位高度  
 | 
        LocType locType = locTypeService.getOne(new LambdaQueryWrapper<LocType>().eq(LocType::getFlag, locTypeHeightType.flag));  
 | 
        //符合库位高度的库位集合  
 | 
        List<Long> locIdList = locTypeBindService.getLocIdListByType(locType);  
 | 
  
 | 
        LambdaQueryWrapper<Loc> queryWrapper = new LambdaQueryWrapper<>();  
 | 
        queryWrapper.in(Loc::getId, locIdList);  
 | 
        queryWrapper.eq(Loc::getLocStsId, LocStsType.O.val());  
 | 
        queryWrapper.le(Loc::getRow1,26);  
 | 
        queryWrapper.orderByAsc(Loc::getLev1).orderByDesc(Loc::getBay1).orderByAsc(Loc::getRow1);  
 | 
        queryWrapper.last("limit 1");  
 | 
  
 | 
        return locService.getOne(queryWrapper);  
 | 
    }  
 | 
  
 | 
}  
 |