#
vincentlu
2025-12-16 920bb5635c88c2f2f9a21134c81ebbc344539987
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.zy.acs.manager.core.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.common.domain.AreaShapeDto;
import com.zy.acs.manager.common.domain.MapPointDto;
import com.zy.acs.manager.core.service.astart.CodeNodeType;
import com.zy.acs.manager.core.service.astart.MapDataDispatcher;
import com.zy.acs.manager.manager.entity.Area;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.service.AreaService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
@Service
public class AreaGovernService {
 
//    public static final Map<String, Set<Long>> CODE_AGV = new ConcurrentHashMap<>();
 
    public static final Map<Long, List<String>> AREA_CODE = new ConcurrentHashMap<>();
 
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
 
    @Autowired
    private AreaService areaService;
    @Autowired
    private MapDataDispatcher mapDataDispatcher;
 
    // launcher -------------------------------------------------------
    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        List<Area> areaList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getStatus, StatusType.ENABLE.val));
        if (Cools.isEmpty(areaList)) {
            return;
        }
 
        List<AreaNode> areaNodeList = new ArrayList<>();
        for (Area area : areaList) {
            AreaShapeDto shapeDto = JSON.parseObject(area.getShapeData(), AreaShapeDto.class);
            MapPointDto start = shapeDto.getStart();
            MapPointDto end = shapeDto.getEnd();
 
            double minX = Math.min(start.getX(), end.getX());
            double maxX = Math.max(start.getX(), end.getX());
            double minY = Math.min(start.getY(), end.getY());
            double maxY = Math.max(start.getY(), end.getY());
 
            areaNodeList.add(new AreaNode(area.getId(), minX, maxX, minY, maxY));
        }
 
        String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(MapDataDispatcher.MAP_DEFAULT_LEV);
        Double[][][] cdaMatrix = mapDataDispatcher.getCdaMatrix(MapDataDispatcher.MAP_DEFAULT_LEV);
        int rows = cdaMatrix.length;
        int cols = cdaMatrix[0].length;
 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                String code = codeMatrix[i][j];
                if (CodeNodeType.NONE.val.equals(code)) { continue; }
 
                Double x = cdaMatrix[i][j][0];
                Double y = cdaMatrix[i][j][1];
                if (x == null || y == null) { continue; }
 
                for (AreaNode areaNode : areaNodeList) {
                    if (this.inRect(x, y, areaNode.minX, areaNode.maxX, areaNode.minY, areaNode.maxY)) {
 
                        List<String> codeList = AREA_CODE.computeIfAbsent(areaNode.getId(), k -> new ArrayList<>());
                        codeList.add(code);
                    }
                }
            }
        }
 
//        for (Map.Entry<Long, List<String>> entry : AREA_CODE.entrySet()) {
//            Long areaId = entry.getKey();
//            List<String> codeList = entry.getValue();
//
//            for (String code : codeList) {
//                Set<Long> agvList = CODE_AGV.computeIfAbsent(code, k -> new HashSet<>());
//                // todo AreaAGV( man_area_agv )
//
//
//            }
//        }
 
 
//        for (Area area : areaList) {
//            AreaShapeDto shapeDto = JSON.parseObject(area.getShapeData(), AreaShapeDto.class);
//            MapPointDto start = shapeDto.getStart();
//            MapPointDto end = shapeDto.getEnd();
//
//            List<String> codeList = this.findCodesInArea(start, end);
//            log.info("codeList: {}", JSON.toJSONString(codeList));
//
//            String redisKey = area.getName() + (area.getZoneId() != null ? "_" + area.getZoneId() : "");
//            String areaDataStr = redis.getValue(RedisConstant.MAP_AREA_DATA_FLAG, redisKey);
//
//        }
 
        System.out.println(1);
    }
 
    // checkout list of area by code data
    public List<Long> queryAreas(String code) {
        List<Long> areaIds = new ArrayList<>();
        for (Map.Entry<Long, List<String>> entry : AREA_CODE.entrySet()) {
            List<String> codeList = entry.getValue();
            if (!Cools.isEmpty(codeList) && codeList.contains(code)) {
                areaIds.add(entry.getKey());
            }
        }
        return areaIds;
    }
 
    // reset and set new area
    public List<String> reSet(Area area) {
        AreaShapeDto shapeDto = JSON.parseObject(area.getShapeData(), AreaShapeDto.class);
        MapPointDto start = shapeDto.getStart();
        MapPointDto end = shapeDto.getEnd();
 
        List<String> codeList = this.findCodesInArea(start, end);
        AREA_CODE.remove(area.getId());
        AREA_CODE.put(area.getId(), codeList);
 
        return codeList;
    }
 
    // reset and set new area
    public Boolean removeArea(Long areaId) {
        if (null == areaId) { return false; }
        List<String> codeList = AREA_CODE.get(areaId);
        AREA_CODE.get(areaId).clear();
        AREA_CODE.remove(areaId);
        return Boolean.TRUE;
    }
 
    public List<String> findCodesInArea(MapPointDto start, MapPointDto end) {
        double minX = Math.min(start.getX(), end.getX());
        double maxX = Math.max(start.getX(), end.getX());
        double minY = Math.min(start.getY(), end.getY());
        double maxY = Math.max(start.getY(), end.getY());
 
        List<String> codeList = new ArrayList<>();
 
        String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(MapDataDispatcher.MAP_DEFAULT_LEV);
        Double[][][] cdaMatrix = mapDataDispatcher.getCdaMatrix(MapDataDispatcher.MAP_DEFAULT_LEV);
        int rows = cdaMatrix.length;
        int cols = cdaMatrix[0].length;
 
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                Double x = cdaMatrix[i][j][0];
                Double y = cdaMatrix[i][j][1];
 
                if (x == null || y == null) { continue; }
 
                if (this.inRect(x, y, minX, maxX, minY, maxY)) {
                    String code = codeMatrix[i][j];
                    if (!CodeNodeType.NONE.val.equals(code)) {
                        codeList.add(code);
                    }
                }
 
            }
        }
 
        return codeList;
    }
 
    private boolean inRect(double x, double y,
                           double minX, double maxX,
                           double minY, double maxY) {
        return x >= minX && x <= maxX && y >= minY && y <= maxY;
    }
 
    @Data
    public static class AreaNode {
 
        public Long id;
        public double minX;
        public double maxX;
        public double minY;
        public double maxY;
 
        public AreaNode(Long id, double minX, double maxX, double minY, double maxY) {
            this.id = id;
            this.minX = minX;
            this.maxX = maxX;
            this.minY = minY;
            this.maxY = maxY;
        }
 
    }
 
}