| | |
| | | |
| | | 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.HashMap; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * SDDB服务类 |
| | |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class SedbService { |
| | | @Value("${sedb.host}") |
| | | String host; |
| | |
| | | @Value("${sedb.pwd}") |
| | | String pwd; |
| | | |
| | | @Value("#{'${sedb.layerNames}'}") |
| | | List<String> layerNames; |
| | | |
| | | String password; |
| | | |
| | | @Resource |
| | | RestTemplate restTemplate; |
| | | |
| | | public String test() { |
| | | public String test() throws Exception { |
| | | String token = getToken(); |
| | | |
| | | String dbid = getDbId(token); |
| | | |
| | | System.out.println(dbid); |
| | | 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() { |
| | |
| | | JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class); |
| | | |
| | | return obj.getStr("data"); |
| | | } |
| | | |
| | | public String getToken() { |
| | | String key = getPublicKey(); |
| | | |
| | | Map<String, Object> map = new HashMap<>(2); |
| | | map.put("userid", user); |
| | | map.put("password", pwd); |
| | | |
| | | JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class); |
| | | |
| | | JSONObject data = obj.getJSONObject("data"); |
| | | |
| | | return data.getStr("token"); |
| | | } |
| | | |
| | | public String getDbId(String token) { |
| | |
| | | |
| | | 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; |
| | | } |
| | | } |