13693261870
2024-09-13 d68ba69e9f4d8edb7b62da57749de052f203c9f0
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
package com.se.simu.service;
 
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.se.simu.domain.SedbLayer;
import com.se.simu.helper.RsaHelper;
import com.se.simu.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * SDDB服务类
 *
 * @author WWW
 * @date   2024-09-12
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class SedbService {
    @Value("${sedb.host}")
    String host;
 
    @Value("${sedb.user}")
    String user;
 
    @Value("${sedb.pwd}")
    String pwd;
 
    @Value("#{'${sedb.layerNames}'}")
    List<String> layerNames;
 
    String password;
 
    @Resource
    RestTemplate restTemplate;
 
    public String test() throws Exception {
        String token = getToken();
 
        String dbid = getDbId(token);
 
        List<SedbLayer> layers = getLayers(token, dbid);
 
        return dbid;
    }
 
    public String getToken() throws Exception {
        Map<String, Object> map = new HashMap<>(2);
        map.put("userid", user);
        map.put("password", getPassword());
 
        JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class);
 
        JSONObject data = obj.getJSONObject("data");
 
        return data.getStr("token");
    }
 
    public String getPassword() throws Exception {
        if (StringUtils.isEmpty(password)) {
            String key = getPublicKey();
            RsaHelper.setPublicKey(key);
            password = RsaHelper.encrypt(pwd);
        }
 
        return password;
    }
 
    public String getPublicKey() {
        //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"}
        JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class);
 
        return obj.getStr("data");
    }
 
    public String getDbId(String token) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("token", token);
 
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydb/list/canview", map, JSONObject.class);
 
        JSONArray arr = obj.getJSONArray("data");
        if (null == arr || arr.size() == 0) {
            return null;
        }
 
        JSONObject dbObj = arr.getJSONObject(0);
 
        return dbObj.getStr("dbid");
    }
 
    public List<SedbLayer> getLayers(String token, String dbid) {
        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", host, dbid, token);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
 
        JSONObject data = obj.getJSONObject("data");
        JSONArray arr = data.getJSONArray("layers");
        if (null == arr || arr.size() == 0) {
            return null;
        }
 
        List<SedbLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
 
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("id");
                Integer dataType = jb.getInt("_data_type");
                JSONArray fields = jb.getJSONArray("fields");
 
                layers.add(new SedbLayer(id, name, dataType, fields));
            }
        }
 
        return layers;
    }
 
    public List<SedbLayer> getLayers2(String token, String dbid) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
 
        String date = StringHelper.YMD_FORMAT.format(calendar.getTime());
        String uri = String.format("%sgeo-service/statis/layer/data/info?dbid=%s&token=%s&caldate=%s", host, dbid, token, date);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
 
        JSONArray arr = obj.getJSONArray("data");
        if (null == arr || arr.size() == 0) {
            return null;
        }
 
        List<SedbLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
 
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("layerid");
                layers.add(new SedbLayer(id, name));
            }
        }
 
        return layers;
    }
}