Demo.java
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
67
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;
}
}