Demo.java 2.49 KB
package com.huaheng.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.HuaHengApplication;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * @author yiwenpeng
 * @date 2023/7/20 09:49
 */
@Slf4j
@SpringBootTest(classes = HuaHengApplication.class)
@RunWith(SpringRunner.class)
public class Demo {
    @Autowired
    private LocationService locationService;

    @Test
    public void test() {
        List<Location> lists = locationService.list(new LambdaQueryWrapper<Location>().eq(Location::getZoneCode, "A"));
        String locationCode = "A020303";//假设这个是离出口最近的库位号
        Location nearestLocation = findNearestLocation(lists, locationCode);
        log.error("***********************************************" + nearestLocation);
    }


    public static Location findNearestLocation(List<Location> locations, String locationCode) {
        if (locations == null || locations.isEmpty() || locationCode == null) {
            return null;
        }
        Location targetLocation = null;
        int minDistance = Integer.MAX_VALUE;

        int targetRow = Integer.parseInt(locationCode.substring(1, 3)); // Extract row number from locationCode
        int targetColumn = Integer.parseInt(locationCode.substring(3, 5)); // Extract column number from locationCode
        int targetLayer = Integer.parseInt(locationCode.substring(5, 7)); // Extract layer number from locationCode

        for (Location location : locations) {
            if (location.getCode().equals(locationCode)) {
                continue; // Skip the location with the same code as the target code
            }

            int rowDistance = Math.abs(targetRow - location.getIRow());
            int columnDistance = Math.abs(targetColumn - location.getIColumn());
            int layerDistance = Math.abs(targetLayer - location.getILayer());

            int totalDistance = rowDistance + columnDistance + layerDistance;

            if (totalDistance < minDistance) {
                minDistance = totalDistance;
                targetLocation = location;
            }
        }

        return targetLocation;
    }

}