13693261870
2024-09-13 8df2806dfed8ed0fbd326a8bbc83c0f61839103b
src/main/java/com/se/simu/service/SedbService.java
@@ -1,12 +1,22 @@
package com.se.simu.service;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.se.simu.domain.SeDb;
import com.se.simu.domain.SeField;
import com.se.simu.domain.SeLayer;
import com.se.simu.helper.RsaHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
 * SDDB服务类
@@ -16,9 +26,10 @@
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class SedbService {
    @Value("${sedb.url}")
    String url;
    @Value("${sedb.host}")
    String host;
    @Value("${sedb.user}")
    String user;
@@ -26,23 +37,152 @@
    @Value("${sedb.pwd}")
    String pwd;
    @Value("${sedb.dbName}")
    String dbName;
    @Value("${sedb.pageSize}")
    Integer pageSize;
    @Value("#{'${sedb.layerNames}'}")
    List<String> layerNames;
    @Value("#{'${sedb.sysFields}'}")
    List<String> sysFields;
    String password;
    @Resource
    RestTemplate restTemplate;
    public String getToken() {
        //http://106.120.22.26:8013/account-service/security/publickey
        String key = getPublicKey();
    public String test() throws Exception {
        String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496";
        String token = getToken();
        return key;
        SeDb db = getSeDb(token);
        db.setBbox(bbox);
        List<SeLayer> layers = getLayers(token, db.getDbid());
        queryData(token, db, layers);
        return db.getDbid();
    }
    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() {
        String uri = url + "account-service/security/publickey";
        //{"datetime":"2024-09-12 17:24:38","code":200,"data":"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtFwJCh2taVTEi05C8qT2oG7Y+rDmJhlO4zicpSeRtiro9LsytePeWI7BXM6sfDU0WeKun1izawcfgGkZgnoJuMBluAOKI1tL0uCrR+DreNLqMVtnXHwoWEIk/hGJedDWaf3q22aGDyEB5h9qCq0JklSShP1Ih4ppap4LmgxdTPQIDAQAB"}
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class);
        return obj.getStr("data");
    }
    public SeDb getSeDb(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 data = obj.getJSONArray("data");
        List<SeDb> list = JSONUtil.toList(data, SeDb.class);
        if (CollectionUtils.isEmpty(list)) return null;
        return list.stream().filter(db -> dbName.equals(db.getName())).findFirst().orElse(null);
    }
    public List<SeLayer> 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<SeLayer> 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");
                List<SeField> fields = JSONUtil.toList(jb.getJSONArray("fields"), SeField.class);
                fields = fields.stream().filter(f -> !sysFields.contains(f.getName())).collect(Collectors.toList());
                layers.add(new SeLayer(id, name, dataType, fields));
            }
        }
        return layers;
    }
    public void queryData(String token, SeDb db, List<SeLayer> layers) throws Exception {
        for (SeLayer layer : layers) {
            int count = getCount(token, db, layer);
            if (0 == count) throw new Exception(layer.getName() + ",图层数据为空");
            int pageCount = (count - 1) / pageSize + 1;
            for (int i = 0; i < pageCount; i++) {
                JSONArray data = query(token, db, layer, i + 1, pageSize);
                if (null != data && data.size() > 0) {
                    layer.addData(data);
                }
            }
        }
    }
    public int getCount(String token, SeDb db, SeLayer layer) {
        Map<String, Object> map = new HashMap<>(6);
        map.put("token", token);
        map.put("dbid", db.getDbid());
        map.put("bbox", db.getBbox());
        map.put("layerid", layer.getId());
        map.put("returnCountOnly", true);
        map.put("inSR", 4326);
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
        if (null == obj || 200 != obj.getInt("code")) return 0;
        return obj.getInt("data");
    }
    public JSONArray query(String token, SeDb db, SeLayer layer, int start, int count) {
        Map<String, Object> map = new HashMap<>(9);
        map.put("token", token);
        map.put("start", start);
        map.put("count", count);
        map.put("dbid", db.getDbid());
        map.put("bbox", db.getBbox());
        map.put("containCount", false);
        map.put("layerid", layer.getId());
        map.put("querytype", layer.getQueryType());
        map.put("inSR", 4326);
        JSONObject obj = restTemplate.postForObject(host + "geo-service/entitydbdata/layer/query", map, JSONObject.class);
        if (null == obj || 200 != obj.getInt("code")) return null;
        JSONObject data = obj.getJSONObject("data");
        return data.getJSONArray("features");
    }
}