郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Service.ServiceImpl;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gk.hotwork.Domain.FourColorPoint;
import com.gk.hotwork.Domain.FourColorPointCoord;
import com.gk.hotwork.Domain.dto.FourColorMapDto;
import com.gk.hotwork.Domain.dto.FourColorPointLocateDto;
import com.gk.hotwork.Service.FourColorMapService;
import com.gk.hotwork.Service.FourColorPointCoordService;
import com.gk.hotwork.Service.FourColorPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
public class FourColorMapServiceImpl implements FourColorMapService {
 
    @Autowired
    private FourColorPointService pointService;
 
    @Autowired
    private FourColorPointCoordService coordService;
 
    @Transactional
    @Override
    public String insertOneMapPoint(FourColorMapDto mapDto) {
        String result = null;
        String dtoCheckResult = checkFourColorMapDto(mapDto);
        if(dtoCheckResult == null){
            int step = 1;
            FourColorPoint point = new FourColorPoint();
            point.setId(mapDto.getId());
            point.setEtype(mapDto.getEtype());
            point.setName(mapDto.getName());
            point.setType(mapDto.getType());
            point.setText(mapDto.getText());
            point.setShapeType(mapDto.getShapeType());
            point.setClampToGround(mapDto.isClampToGround());
            point.setGeoType(mapDto.getGeoType());
            point.setColor(mapDto.getColor());
            if(pointService.getById(point.getId()) != null){
                result = "ID已存在";
                return result;
            }
            if(pointService.save(point) == true){
                step = 2;
            }else {
                throw new RuntimeException("数据库保存点位失败");
            }
            if(step == 2){
                List<FourColorPointCoord> coordList = new ArrayList<>();
                for(FourColorPointLocateDto locateDto : mapDto.getLocations()){
                    FourColorPointCoord coord = new FourColorPointCoord();
                    coord.setPointId(mapDto.getId());
                    coord.setLon(locateDto.getLon());
                    coord.setLat(locateDto.getLat());
                    coord.setHei(locateDto.getHei());
                    coordList.add(coord);
                }
                if(coordService.saveBatch(coordList) == true){
                    step = 3;
                }else {
                    result = "业务错误";
                    throw new RuntimeException("数据库保存定位数据失败");
                }
            }
        }else {
            result = dtoCheckResult;
        }
        return result;
    }
 
    @Transactional
    @Override
    public boolean deleteMap(String id) {
        boolean result = false;
        FourColorPoint point = pointService.getById(id);
        if(point == null || point.getId()==null || point.getId().isEmpty()){
            return false;
        }
        //1、删除位置信息
        List<FourColorPointCoord> coordList = coordService.findCoordListByPointId(id);
        int step = 1;
        if(coordList != null && coordList.size() > 0){
            List<Long> idList = new ArrayList<>();
            for(FourColorPointCoord coord : coordList){
                idList.add(coord.getId());
            }
            if(coordService.removeByIds(idList) == true){
                step = 2;
            }else {
                throw new RuntimeException("数据库删除定位数据失败");
            }
        }
        //2、删除地图信息
        if(step == 2){
            if(pointService.removeById(point)){
                result = true;
            }else {
                throw new RuntimeException("数据库删除地图失败");
            }
        }
        return result;
    }
 
    @Override
    public JSONObject getOnePointJsonById(String id) {
        JSONObject mapJson = null;
        FourColorPoint point = pointService.getById(id);
        List<FourColorPointCoord> coordList = coordService.findCoordListByPointId(id);
        if(point != null && !point.getId().isEmpty()){
            mapJson = packageMapDto(point,coordList);
        }
        return mapJson;
    }
 
    @Override
    public List<JSONObject> getPointListJson(int etype) {
        //非60万吨和130万吨类型
        if(etype != 1 && etype != 2){
            return null;
        }
        List<JSONObject> mapDtoListJson = null;
        List<FourColorPoint> pointList = pointService.findPointListByEtype(etype);
        if(pointList != null && pointList.size() > 0){
            mapDtoListJson = new ArrayList<>();
            for(FourColorPoint point : pointList){
                List<FourColorPointCoord> coordList = coordService.findCoordListByPointId(point.getId());
                JSONObject mapDtoJson = packageMapDto(point,coordList);
                if(mapDtoJson != null && !mapDtoJson.isEmpty()){
                    mapDtoListJson.add(mapDtoJson);
                }
            }
        }
        return mapDtoListJson;
    }
 
    private String checkFourColorMapDto(FourColorMapDto dto){
        if(dto == null)
            return "参数不能为空";
        if(dto.getId() == null || dto.getId().isEmpty())
            return "ID不能为空";
        if(dto.getEtype() == null || dto.getEtype() <= 0)
            return "企业类型不能为空";
        if(dto.getEtype() != 1 && dto.getEtype() !=2)
            return "企业类型错误";
        if(dto.getName() == null || dto.getName().isEmpty())
            return "名字不能为空";
        if(dto.getColor() == null || dto.getColor().isEmpty())
            return "颜色不能为空";
        if(dto.getText() == null || dto.getText().isEmpty())
            return "文字说明不能为空";
        if(dto.getGeoType() == null || dto.getGeoType().isEmpty())
            return "绘图形状不能为空";
        if(dto.getType() == null || dto.getType().isEmpty())
            return "四色图形状不能为空";
        if(dto.getShapeType() == null || dto.getShapeType().isEmpty())
            return "四色图形状不能为空";
        if(dto.getLocations() == null || dto.getLocations().size() <= 0)
            return "坐标不能为空";
        return null;
    }
 
    private JSONObject packageMapDto(FourColorPoint point,List<FourColorPointCoord> coordList){
        if(point == null)
            return null;
        JSONObject obj = JSONUtil.createObj();
        obj.put("type",point.getType());
        JSONObject prop = JSONUtil.createObj();
        prop.put("attr",JSONUtil.createObj().put("name",point.getName()).put("id",point.getId()));
        prop.put("type",point.getShapeType());
        prop.put("style",JSONUtil.createObj().put("text",point.getText()).put("clampToGround",point.isClampToGround()).put("color",point.getColor()));
        obj.put("properties",prop);
        JSONObject geoLocate = JSONUtil.createObj();
        geoLocate.put("type",point.getGeoType());
        if(coordList != null && coordList.size() > 0){
            JSONArray coordArray = JSONUtil.createArray();
            for(FourColorPointCoord coord : coordList){
                JSONArray co = JSONUtil.createArray();
                co.add(coord.getLon());
                co.add(coord.getLat());
                co.add(coord.getHei());
                coordArray.add(co);
            }
            geoLocate.put("coordinates",coordArray);
        }
        obj.put("geometry",geoLocate);
        return obj;
    }
 
}