Blame view

src/test/java/com.huaheng.test/Demo.java 2.49 KB
1
2
package com.huaheng.test;
易文鹏 authored
3
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4
import com.huaheng.HuaHengApplication;
易文鹏 authored
5
6
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
易文鹏 authored
25
    private LocationService locationService;
26
27
28

    @Test
    public void test() {
易文鹏 authored
29
        List<Location> lists = locationService.list(new LambdaQueryWrapper<Location>().eq(Location::getZoneCode, "A"));
30
        String locationCode = "A020303";//假设这个是离出口最近的库位号
易文鹏 authored
31
32
        Location nearestLocation = findNearestLocation(lists, locationCode);
        log.error("***********************************************" + nearestLocation);
33
    }
易文鹏 authored
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


    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;
    }
67
}