package com.huaheng.pc.config.location.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huaheng.common.utils.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.domain.LocationInfo;
import com.huaheng.pc.config.location.mapper.LocationMapper;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.LocationTypeService;
import com.huaheng.pc.config.points.domain.Points;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.*;

@Service("LocationService")
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService {

    @Resource
    private LocationService locationService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationMapper locationMapper;
    @Resource
    private TaskHeaderService taskHeaderService;

    /**
     * //新增库位,需要建立code,并判断是否重复
     * 1. 库区和库位没有绝对关系。 先建立库区,然后在库区的位置进行建立库位类型。
     * TODO:胡海--在判断库位类型和库区是否匹配,应该用库位类型表进行判断。库位类型在库区存在即可
     *
     * @param location
     * @return
     */
    @Override
    public AjaxResult addsave(Location location) {

        //库区与库位类型的匹配判断
        if (!location.getZoneCode().equals(location.getLocationType())) {
            throw new ServiceException(location.getLocationType() + "的库位类型与" + location.getZoneCode() + "库区不匹配");
        }
        //TODO:胡海-- 库位编码长度应该是弹性的,即我们的库位是由4个维度组成的,正常情况下  平库(行列) 立体仓库(行列层) AGV料架库(行列层格)
        // 这里可以判断下 然后决定库位编码长度。
        //创建库位编码code
        String prefix = location.getLocationType();
        String code = MessageFormat.format("{0}{1}_{2}_{3}",
                prefix,
                String.format("%02d", location.getIRow()),
                String.format("%02d", location.getIColumn()),
                String.format("%02d", location.getILayer()));

        //判断code是否重复
        LambdaQueryWrapper<Location> lam = Wrappers.lambdaQuery();
        lam.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getCode, code);
        if (this.getOne(lam) != null) {
            return AjaxResult.error("该位置已有库位生成,请重新输入位置");
        }

        //插入数据库
        location.setCode(code);
        location.setWarehouseCode(ShiroUtils.getWarehouseCode());
        location.setCreatedBy(ShiroUtils.getLoginName());
        Boolean flag = this.save(location);
        if (flag == false) {
            return AjaxResult.error("新增库位失败,插入数据库时失败");
        }
        return AjaxResult.success("新增库位成功");
    }

    /**
     * 批量新增库位
     *
     * @param location
     * @return boolean
     */
    @Override
    public boolean insertLocation(Location location) {
        /* 判断库位类型编码是否存在*/
        LambdaQueryWrapper<LocationType> typeLambda = Wrappers.lambdaQuery();
        typeLambda.eq(LocationType::getCode, location.getLocationType())
                .select(LocationType::getCode);
        List<Map<String, Object>> list = locationTypeService.listMaps(typeLambda);
        if (list.size() < 1) {
            throw new ServiceException("库位类型编码不存在");
        }

        /* 判断库区编码是否存在*/
        LambdaQueryWrapper<Zone> zoneLambda = Wrappers.lambdaQuery();
        zoneLambda.eq(Zone::getCode, location.getZoneCode())
                .select(Zone::getCode);

        list = zoneService.listMaps(zoneLambda);
        if (list.size() < 1) {
            throw new ServiceException("库区编码不存在");
        }
        //TODO:胡海--在判断库位类型和库区是否匹配,应该用库位类型表进行判断。库位类型在库区存在即可
        if (!location.getZoneCode().equals(location.getLocationType())) {
            throw new ServiceException(location.getLocationType() + "的库位类型与" + location.getZoneCode() + "库区不匹配");
        }

        List<Location> locations = new ArrayList<>();
        for (int i = 1; i <= location.getIRow().intValue(); i++) {
            for (int j = 1; j <= location.getIColumn().intValue(); j++) {
                for (int k = 1; k <= location.getILayer().intValue(); k++) {
                    for (int m = 1; m <= location.getIGrid().intValue(); m++) {
                        //Integer roadway = (j / 2) + 1;
                        Location param = new Location();
                        param.setIRow(i);
                        param.setIColumn(j);
                        param.setILayer(k);
                        param.setIGrid(m);
                        param.setRoadway(location.getRoadway());
                        param.setZoneCode(location.getZoneCode());
                        param.setLocationType(location.getLocationType());
                        param.setStatus(location.getStatus());
                        param.setWarehouseCode(ShiroUtils.getWarehouseCode());
                        param.setCreatedBy(ShiroUtils.getLoginName());
                        param.setLastUpdatedBy(ShiroUtils.getLoginName());
                        String code = MessageFormat.format("{0}{1}_{2}_{3}",
                                location.getLocationType(),
                                String.format("%02d", i),
                                String.format("%02d", j),
                                String.format("%02d", k));
                        //查询该库位编码是否存在
                        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
                        queryWrapper.eq(Location::getCode, code)
                                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
                        List<Location> locationList = locationService.list(queryWrapper);
                        if (locationList.size() == 0) {
                            param.setCode(code);
                            locations.add(param);
                        }
                    }
                }
            }
        }
        int num = 0;
        List<Location> locations1 = new ArrayList<>();
        if (locations.size() > 1000) {
            for (Location item : locations) {
                num++;
                locations1.add(item);
                if (num % 1000 == 0 || num == locations.size()) {
                    locationMapper.addList(locations1);
                    locations1 = new ArrayList<>();
                }
            }
        } else {
            locationMapper.addList(locations);
        }

        List<Integer> integerList = new ArrayList<>();
        for (Location location2 : locations) {
            if (integerList.size() == 0) {
                integerList.add(location2.getIRow());
            } else {
                for (int i = 0; i < integerList.size(); i++) {
                    if (integerList.get(i).intValue() == location2.getIRow().intValue()) {
                        break;
                    } else {
                        if (i == (integerList.size() - 1)) {
                            integerList.add(location2.getIRow());
                        }
                    }
                }
            }
        }
        if (integerList.size() == 4) {
            for (Location location3 : locations) {
                for (int i = 0; i < integerList.size(); i++) {
                    if (location3.getIRow().intValue() == integerList.get(i).intValue()) {
                        if (i == 0 || i == (integerList.size() - 1)) {
                            location3.setRowFlag(1);
                        } else {
                            location3.setRowFlag(0);
                        }
                    }
                }
            }
            locationMapper.updateList(locations);
        }
        return true;
    }

    @Override
    public boolean addBatchSave(String prefix, Integer firstRow, Integer lastRow, Integer firstColumn, Integer lastColumn, Integer firstLayer, Integer lastLayer,
                                Integer firstGrid, Integer lastGrid, String roadWay, String status, String zoneCode, String locationType, String high) {
        LambdaQueryWrapper<LocationType> typeLambda = Wrappers.lambdaQuery();
        typeLambda.eq(LocationType::getCode, locationType)
                .select(LocationType::getCode);
        List<Map<String, Object>> list = locationTypeService.listMaps(typeLambda);
        if (list.size() < 1) {
            throw new ServiceException("库位类型编码不存在");
        }

        /* 判断库区编码是否存在*/
        LambdaQueryWrapper<Zone> zoneLambda = Wrappers.lambdaQuery();
        zoneLambda.eq(Zone::getCode, zoneCode)
                .select(Zone::getCode);

        list = zoneService.listMaps(zoneLambda);
        if (list.size() < 1) {
            throw new ServiceException("库区编码不存在");
        }
        //TODO:胡海--在判断库位类型和库区是否匹配,应该用库位类型表进行判断。库位类型在库区存在即可
       /* if(!zoneCode.equals(locationType)){
            throw new ServiceException(locationType+"的库位类型与"+ zoneCode +"库区不匹配");
        }*/
        LambdaQueryWrapper<Zone> zoneLambdaQueryWrapper = Wrappers.lambdaQuery();
        zoneLambdaQueryWrapper.eq(Zone::getWarehouseCode, ShiroUtils.getWarehouseCode());
        zoneLambdaQueryWrapper.eq(Zone::getCode, zoneCode);
        Zone zone = zoneService.getOne(zoneLambdaQueryWrapper);
        String area = zone.getArea();
        List<Location> locations = new ArrayList<>(); // 为零?
        for (int i = firstRow.intValue(); i <= lastRow.intValue(); i++) {
            for (int j = firstColumn.intValue(); j <= lastColumn.intValue(); j++) {
                for (int k = firstLayer.intValue(); k <= lastLayer.intValue(); k++) {
                    for (int m = firstGrid.intValue(); m <= lastGrid.intValue(); m++) {
                        //Integer roadway = (j / 2) + 1;
                        Location param = new Location();
                        param.setIRow(i);
                        param.setIColumn(j);
                        param.setILayer(k);
                        param.setIGrid(m);
                        param.setRoadway(roadWay);
                        param.setArea(area);
                        param.setZoneCode(zoneCode);
                        param.setLocationType(locationType);
                        param.setStatus(status);
                        param.setHigh(Integer.parseInt(high));
                        param.setWarehouseCode(ShiroUtils.getWarehouseCode());
                        param.setCreatedBy(ShiroUtils.getLoginName());
                        param.setLastUpdatedBy(ShiroUtils.getLoginName());
                        String code = MessageFormat.format("{0}{1}{2}{3}",
                                prefix,
                                String.format("%02d", i),
                                String.format("%02d", j),
                                String.format("%02d", k));
                        //查询该库位编码是否存在
                        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
                        queryWrapper.eq(Location::getCode, code)
                                .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
                        List<Location> locationList = locationService.list(queryWrapper);
                        if (locationList.size() == 0) {
                            param.setCode(code);
                            locations.add(param);
                        }
                    }
                }
            }
        }

        int num = 0;
        List<Location> locations1 = new ArrayList<>();
        if (locations.size() > 1000) {
            for (Location item : locations) {
                num++;
                locations1.add(item);
                if (num % 1000 == 0 || num == locations.size()) {
                    locationMapper.addList(locations1);
                    locations1 = new ArrayList<>();
                }
            }
        } else {
            locationMapper.addList(locations);
        }

        List<Integer> integerList = new ArrayList<>();
        for (Location location2 : locations) {
            if (integerList.size() == 0) {
                integerList.add(location2.getIRow());
            } else {
                for (int i = 0; i < integerList.size(); i++) {
                    if (integerList.get(i).intValue() == location2.getIRow().intValue()) {
                        break;
                    } else {
                        if (i == (integerList.size() - 1)) {
                            integerList.add(location2.getIRow());
                        }
                    }
                }
            }
        }
        if (integerList.size() == 4) {
            for (Location location3 : locations) {
                for (int i = 0; i < integerList.size(); i++) {
                    if (location3.getIRow().intValue() == integerList.get(i).intValue()) {
                        if (i == 0 || i == (integerList.size() - 1)) {
                            location3.setRowFlag(1);
                        } else {
                            location3.setRowFlag(0);
                        }
                    }
                }
            }
            locationMapper.updateList(locations);
        }

        return true;
    }

    /**
     * 根据库位编码对库位表格的状态进行更新
     *
     * @param locationCode
     * @param status
     */
    @Override
    public void updateStatus(String locationCode, String status) {
        if (StringUtils.isNotEmpty(locationCode)) {
            locationMapper.updateStatus(ShiroUtils.getWarehouseCode(), locationCode, status);
        }
    }

    @Override
    public void updateStatus(String locationCode, String status, String warehouseCode) {
        if (StringUtils.isNotEmpty(locationCode)) {
            locationMapper.updateStatus(warehouseCode, locationCode, status);
        }
    }

    /**
     * 通过定位规则查找库位
     *
     * @param locatingRule
     * @return
     */
    @Override
    public String position(String locatingRule) {
        return locationMapper.position(locatingRule).getCode();
    }

    /**
     * 修改容器和库位状态
     */
    @Override
    public void updateContainerCodeAndStatus(String locationCode, String containerCode, String status) {
        if (StringUtils.isNotEmpty(locationCode) || StringUtils.isNotEmpty(containerCode)) {
            LambdaUpdateWrapper<Location> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(Location::getContainerCode, containerCode)
                    .set(Location::getStatus, status)
                    .eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                    .eq(Location::getCode, locationCode);
            this.update(updateWrapper);
        }
    }

    @Override
  	public void updateContainerCodeAndStatus(String locationCode, String containerCode,
                                             String status, String warehouseCode) {
        if (StringUtils.isNotEmpty(locationCode) || StringUtils.isNotEmpty(containerCode)) {
            LambdaUpdateWrapper<Location> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(Location::getContainerCode, containerCode)
                    .set(Location::getStatus, status)
                    .eq(Location::getWarehouseCode, warehouseCode)
                    .eq(Location::getCode, locationCode);
            this.update(updateWrapper);
        }
    }

    /**
     * 获取库区最大行列
     * @param type
     * @return
     */
    @Override
    public LocationInfo getAllLocation(String type) {
        if (StringUtils.isNotEmpty(type)) {
            Location location = locationMapper.getAllLocation(ShiroUtils.getWarehouseCode(), type);
            LocationInfo locationInfo = new LocationInfo();
            locationInfo.setMaxRow(location.getIRow());
            locationInfo.setMaxLine(location.getIColumn());
            locationInfo.setMaxLayer(location.getILayer());
            locationInfo.setMaxGrid(location.getIGrid());
            int minRow = locationMapper.getFirstRowOfZone(ShiroUtils.getWarehouseCode(), type);
            locationInfo.setMinRow(minRow);
            return locationInfo;
        }
        return null;
    }

    /**
     * 验证库位合法性
     *
     * @param code
     * @return
     */
    @Override
    public boolean checkLocation(String code) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getDeleted, false)
                .eq(Location::getCode, code);
        List<Location> locations = list(queryWrapper);
        if (locations.size() >= 1) {
            Location location = locations.get(0);
            String containerCode = location.getContainerCode();
            if (containerCode != null && containerCode.length() > 0) {
                throw new ServiceException("货架上已有容器");
            }
            String status = location.getStatus();
            if (!QuantityConstant.STATUS_LOCATION_EMPTY.equals(status)) {
                throw new ServiceException("库位状态不为空");
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean getFreeLocation(String materialCode, String batch) {
        LambdaQueryWrapper<Location> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .eq(Location::getContainerCode, "")
                .eq(Location::getDeleted, false)
                .eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY);
        List<Location> locations = locationService.list(lambdaQueryWrapper);
        if (locations != null && locations.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询库位利用率
     *
     * @return
     */
    @Override
    public List<LinkedHashMap<String, Object>> getLocationProp() {
        return locationMapper.getLocationProp();
    }

    /**
     * 根据库位编码查询库位信息
     *
     * @param
     * @return
     */
    @Override
    public Location getLocationByCode(String locationCode) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getCode, locationCode);
        queryWrapper.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
        return getOne(queryWrapper);
    }

    @Override
    public Location getLocationByCode(String locationCode, String warehouseCode) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getCode, locationCode);
        queryWrapper.eq(Location::getWarehouseCode, warehouseCode);
        return getOne(queryWrapper);
    }

    @Override
    public List<Location> pickLocation() {
        return locationMapper.pickLocation();
    }

    @Override
    public List<Location> selectContainerEmpty(String warehouseCode) {
        return locationMapper.selectContainerEmpty(warehouseCode);
    }

    @Override
    public Location getInsideNear(Location location) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, location.getWarehouseCode());
        queryWrapper.eq(Location::getZoneCode, location.getZoneCode());
        queryWrapper.eq(Location::getRoadway, location.getRoadway());
        queryWrapper.eq(Location::getIColumn, location.getIColumn());
        queryWrapper.eq(Location::getILayer, location.getILayer());
        queryWrapper.eq(Location::getLocationType, location.getLocationType());
        queryWrapper.eq(Location::getRowFlag, QuantityConstant.ROW_IN);
        List<Location> locationList = list(queryWrapper);
        for (Location location1 : locationList) {
            int diff = Math.abs(location1.getIRow().intValue() - location.getIRow().intValue());
            if (diff == 1) {
                return location1;
            }
        }
        return null;
    }

    @Override
    public Location getOutSideNear(Location location) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, location.getWarehouseCode());
        queryWrapper.eq(Location::getZoneCode, location.getZoneCode());
        queryWrapper.eq(Location::getRoadway, location.getRoadway());
        queryWrapper.eq(Location::getIColumn, location.getIColumn());
        queryWrapper.eq(Location::getILayer, location.getILayer());
        queryWrapper.eq(Location::getLocationType, location.getLocationType());
        queryWrapper.eq(Location::getRowFlag, QuantityConstant.ROW_OUT);
        List<Location> locationList = list(queryWrapper);
        for (Location location1 : locationList) {
            int diff = Math.abs(location1.getIRow().intValue() - location.getIRow().intValue());
            if (diff == 1) {
                return location1;
            }
        }
        return null;
    }

    @Override
    public Location getNear(Location location) {
        int rowFlag = location.getRowFlag().intValue();
        Location locaiton2 = null;
        if (rowFlag == 1) {
            locaiton2 = getInsideNear(location);
        } else {
            locaiton2 = getOutSideNear(location);
        }
        return locaiton2;
    }

    private boolean getOutSideCanMove(Location location) {
        Location locaiton2 = getInsideNear(location);
        if (!locaiton2.getStatus().equals(QuantityConstant.STATUS_LOCATION_EMPTY)) {
            return false;
        }
        if (!locaiton2.getContainerCode().equals("")) {
            return false;
        }
        return true;
    }

    @Override
    public Location getEmptyOutSideLocation(Location location) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, location.getWarehouseCode());
        queryWrapper.eq(Location::getRoadway, location.getRoadway());
        queryWrapper.eq(Location::getHigh, location.getHigh());
        queryWrapper.eq(Location::getRowFlag, QuantityConstant.ROW_OUT);
        queryWrapper.eq(Location::getContainerCode, "");
        queryWrapper.eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY);
        queryWrapper.eq(Location::getLocationType, location.getLocationType());
        List<Location> locationList = list(queryWrapper);
        List<Location> removeLocationList = new ArrayList<>();
        int column = location.getIColumn();
        Collections.sort(locationList, new Comparator<Location>() {
            @Override
            public int compare(Location o1, Location o2) {
                int dvalue1 = Math.abs(o1.getIColumn() - column);
                int dvalue2 = Math.abs(o2.getIColumn() - column);
                return dvalue1 - dvalue2;
            }
        });
        int removeSize = 0;
        for (int i = 0; i < locationList.size(); i++) {
            Location location1 = locationList.get(i);
            if (taskHeaderService.getUncompleteTaskInNear(location1) > 0) {
                removeLocationList.add(location1);
                removeSize++;
            } else if (!getOutSideCanMove(location1)) {
                removeLocationList.add(location1);
                removeSize++;
            }
            if (i - removeSize >= 3) {
                break;
            }
        }
        locationList.removeAll(removeLocationList);
        if (locationList == null || locationList.size() <= 0) {
            return null;
        }
        Location location1 = locationList.get(0);
        return location1;
    }


    @Override
    public Location getEmptyInsideLocation(Location location) {
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, location.getWarehouseCode());
        queryWrapper.eq(Location::getRoadway, location.getRoadway());
        queryWrapper.eq(Location::getRowFlag, QuantityConstant.ROW_IN);
        queryWrapper.eq(Location::getHigh, location.getHigh());
        queryWrapper.eq(Location::getContainerCode, "");
        queryWrapper.eq(Location::getStatus, QuantityConstant.STATUS_LOCATION_EMPTY);
        queryWrapper.eq(Location::getLocationType, location.getLocationType());
        List<Location> locationList = list(queryWrapper);
        List<Location> removeLocaationList = new ArrayList<>();
        int column = location.getIColumn();
        Collections.sort(locationList, new Comparator<Location>() {
            @Override
            public int compare(Location o1, Location o2) {
                int dvalue1 = Math.abs(o1.getIColumn() - column);
                int dvalue2 = Math.abs(o2.getIColumn() - column);
                return dvalue1 - dvalue2;
            }
        });
        int removeSize = 0;
        for (int i = 0; i < locationList.size(); i++) {
            Location location1 = locationList.get(i);
            if (taskHeaderService.getUncompleteTaskInNear(location1) > 0) {
                removeLocaationList.add(location1);
                removeSize++;
            }
            if (i - removeSize >= 3) {
                break;
            }
        }
        locationList.removeAll(removeLocaationList);
        if (locationList == null || locationList.size() <= 0) {
            return null;
        }
        Location location1 = locationList.get(0);
        return location1;
    }

    @Override
    public Location getEmptyLocation(Location location) {
        Location location1 = getEmptyOutSideLocation(location);
        if (location1 == null) {
            location1 = getEmptyInsideLocation(location);
        }
        return location1;
    }

    @Override
    public int getFirstRowOfZone(String warehouseCode, String locationType) {
        return locationMapper.getFirstRowOfZone(warehouseCode, locationType);
    }

    @Override
    public int getLastRowOfZone(String warehouseCode, String locationType) {
        return locationMapper.getLastRowOfZone(warehouseCode, locationType);
    }

    @Override
    public List<Location> remoteList(String type) {
        LambdaQueryWrapper<Location> wrapper = Wrappers.lambdaQuery();
        if ("status".equals(type)) {
            wrapper.eq(Location::getStatus, "lock");
        } else if ("container".equals(type)) {
            wrapper.isNotNull(Location::getContainerCode)
                    .ne(Location::getContainerCode, "");
        }
        return list(wrapper);
    }

    @Override
    public void upstatus(String code) {
        Location user = new Location();
        user.setStatus("empty");
        //修改条件s
        UpdateWrapper<Location> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("containerCode", code);
        int update = locationMapper.update(user, userUpdateWrapper);
//        System.out.println(update);
    }
    @Override
    public int locationSIsnull(){
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
        queryWrapper.eq(Location::getRoadway, 2);
        queryWrapper.eq(Location::getZoneCode,"LK");
        queryWrapper.eq(Location::getContainerCode,"").or().isNull(Location::getContainerCode);
        int count=this.count();
        return count;
    }
    @Override
    public Location getLocationOfRule(int roadWay){
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Location::getWarehouseCode, ShiroUtils.getWarehouseCode());
        queryWrapper.eq(Location::getStatus,QuantityConstant.STATUS_LOCATION_EMPTY);
        queryWrapper.eq(Location::getRoadway, roadWay);
        queryWrapper.eq(Location::getZoneCode,"LK");
        queryWrapper.eq(Location::getContainerCode,"").or().isNull(Location::getContainerCode);
        queryWrapper.orderByAsc(Location::getIColumn,Location::getIRow);
        queryWrapper.last("limit 1");
        Location location=this.getOne(queryWrapper);
        return location;
    }

    @Override
    public Location selectFirstEntity(Location temp1){
        LambdaQueryWrapper<Location> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotEmpty(temp1.getWarehouseCode()),Location::getWarehouseCode, temp1.getWarehouseCode());
        queryWrapper.eq(StringUtils.isNotEmpty(temp1.getCode()),Location::getStatus,temp1.getCode());
        queryWrapper.eq(StringUtils.isNull(temp1.getIColumn()),Location::getIColumn,temp1.getIColumn());
        queryWrapper.eq(StringUtils.isNotEmpty(temp1.getRoadway()),Location::getRoadway,temp1.getRoadway());
        queryWrapper.eq(StringUtils.isNull(temp1.getILayer()),Location::getILayer,temp1.getILayer());

        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public Location selectAddress(Location location) {
        return locationMapper.selectAddress(location);
    }
    @Override
    public List<Location> selectEmptyAddress() {
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setIsLocked(2);
        return locationMapper.selectEmptyAddress(points);
    }

    @Override
    public List<Location> getRoadwayByLocationType(String locationType) {
        QueryWrapper<Location>  qw  = new QueryWrapper<>();
        qw.select("DISTINCT locationType,roadway").lambda().eq(Location::getLocationType,locationType);

        List<Location> list = this.list(qw);
        return list;
    }
}