You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
8.6 KiB

package com.eco.plugin.xxx.bjgjdata.utils;
import com.eco.plugin.xxx.bjgjdata.config.PluginSimpleConfig;
import com.eco.plugin.xxx.bjgjdata.db.bean.DBEntity;
import com.eco.plugin.xxx.bjgjdata.db.bean.OrgEntity;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
import java.util.*;
public class DataUtils {
private static String getToken() throws Exception {
PluginSimpleConfig psc = PluginSimpleConfig.getInstance();
String apikey = psc.getApikey();
String clientid = psc.getClientId();
String secret = psc.getSecret();
String tokenurl = psc.getTokenurl();
Map<String,String> header = new HashMap<String,String>();
header.put("apikey",apikey);
header.put("Content-Type","application/json");
JSONObject param = new JSONObject();
param.put("clientId",clientid);
param.put("clientSecret",secret);
String result = HttpUtils.HttpPostJson(tokenurl,param.toString(),header);
if(Utils.isNullStr(result)){
throw new Exception("获取token异常!");
}
JSONObject resultJson = new JSONObject(result);
int code = resultJson.getInt("code");
if(code != 0 ){
String msg = resultJson.getString("message");
throw new Exception(msg);
}
String token = resultJson.getJsonObject("data").getString("accessToken");
return token;
}
public static void getData(String tableName, String updateTime, int pageNum, String token, PluginSimpleConfig psc, List<DBEntity> dbentity) throws Exception {
if(Utils.isNullStr(token)){
token = "Bearer "+getToken();
}
JSONObject param = new JSONObject();
param.put("pageNo",pageNum);
param.put("pageSize",1000);
param.put("tableAlias",tableName);
param.put("updateTime",updateTime);
String apikey = psc.getApikey();
Map<String,String> header = new HashMap<String,String>();
header.put("apikey",apikey);
header.put("Content-Type","application/json");
header.put("Authorization",token);
String dataurl = psc.getDataurl();
String result = HttpUtils.HttpPostJson(dataurl,param.toString(),header);
if(Utils.isNullStr(result)){
throw new Exception("获取数据异常");
}
JSONObject resultJson = new JSONObject(result);
int code = resultJson.getInt("code");
if(code != 0 ){
String msg = resultJson.getString("message");
throw new Exception(msg);
}
JSONObject data = resultJson.getJSONObject("data");
int total = data.getInt("pageCount");
if(total == 0){
dbentity = new ArrayList<DBEntity>();
return ;
}
JSONArray dataList = data.getJSONArray("dataList");
arrayToList(dataList,dbentity,tableName);
if(total == pageNum){
return ;
}
getData(tableName,updateTime,pageNum+1,token,psc,dbentity);
}
public static void main(String[] args) {
JSONObject json = new JSONObject();
String a = json.getString("a");
System.out.println();
}
private static void arrayToList(JSONArray dataList, List<DBEntity> dbentity,String tablename) {
for(int i=0;i<dataList.length();i++){
JSONObject json = dataList.getJSONObject(i);
Date now = new Date();
DBEntity dbEntity = new DBEntity();
if("RY".equals(tablename)){
String rybh = json.getString("rybh");
String rymc = json.getString("rymc");
String wlkh = json.getString("wlkh");
String fgsmc = json.getString("fgsmc");
String jgmc = json.getString("jgmc");
String jgid = json.getString("jgid");
dbEntity.setId(UUID.randomUUID().toString());
dbEntity.setRybh(rybh);
dbEntity.setRymc(rymc);
dbEntity.setWlkh(wlkh);
dbEntity.setFgsmc(fgsmc);
dbEntity.setJgmc(jgmc);
dbEntity.setCreatetime(now);
dbEntity.setUpdatetime(now);
dbEntity.setJgid(jgid);
dbEntity.setType("1");
}else{
String WBRYID = json.getString("WBRYID");
String rybh = json.getString("RYBH");
String rymc = json.getString("RYMC");
String WBGSID = json.getString("WBGSID");
String WBGSMC = json.getString("WBGSMC");
String jgmc = json.getString("WBJGMC");
String jgid = json.getString("WBJGID");
dbEntity.setId(UUID.randomUUID().toString());
dbEntity.setRybh(rybh);
dbEntity.setRymc(rymc);
dbEntity.setJgmc(jgmc);
dbEntity.setCreatetime(now);
dbEntity.setUpdatetime(now);
dbEntity.setJgid(jgid);
dbEntity.setWBRYID(WBRYID);
dbEntity.setWbgsid(WBGSID);
dbEntity.setWbgsmc(WBGSMC);
dbEntity.setType("2");
}
dbentity.add(dbEntity);
}
}
private static void arrayToOrgList(JSONArray dataList, List<OrgEntity> dbentity,String tablename) {
for(int i=0;i<dataList.length();i++){
Date now = new Date();
JSONObject json = dataList.getJSONObject(i);
OrgEntity dbEntity = new OrgEntity();
if("JG".equals(tablename)){
String jgid = json.getString("jgid");
String jgbm = json.getString("jgbm");
String jgmc = json.getString("jgmc");
String zgjgid = json.getString("zgjgid");
String px = json.getString("px");
String jgzt = json.getString("jgzt");
dbEntity.setId(UUID.randomUUID().toString());
dbEntity.setJgbm(jgbm);
dbEntity.setZgjgid(zgjgid);
dbEntity.setPx(px);
dbEntity.setJgzt(jgzt);
dbEntity.setJgmc(jgmc);
dbEntity.setCreatetime(now);
dbEntity.setUpdatetime(now);
dbEntity.setJgid(jgid);
dbEntity.setType("1");
}else{
String jgid = json.getString("WBJGID");
String jgbm = json.getString("JGBM");
String jgmc = json.getString("JGMC");
String zgjgid = json.getString("ZGJGID");
String SJJGS = json.getString("SJJGS");
dbEntity.setId(UUID.randomUUID().toString());
dbEntity.setJgbm(jgbm);
dbEntity.setZgjgid(zgjgid);
dbEntity.setSjjgs(SJJGS);
dbEntity.setJgmc(jgmc);
dbEntity.setCreatetime(now);
dbEntity.setUpdatetime(now);
dbEntity.setJgid(jgid);
dbEntity.setType("2");
}
dbentity.add(dbEntity);
}
}
public static void getOrgData(String tableName, String updateTime, int pageNum, String token, PluginSimpleConfig psc, List<OrgEntity> dbentity) throws Exception{
if(Utils.isNullStr(token)){
token = "Bearer "+getToken();
}
JSONObject param = new JSONObject();
param.put("pageNo",pageNum);
param.put("pageSize",1000);
param.put("tableAlias",tableName);
param.put("updateTime",updateTime);
String apikey = psc.getApikey();
Map<String,String> header = new HashMap<String,String>();
header.put("apikey",apikey);
header.put("Content-Type","application/json");
header.put("Authorization",token);
String dataurl = psc.getDataurl();
String result = HttpUtils.HttpPostJson(dataurl,param.toString(),header);
if(Utils.isNullStr(result)){
throw new Exception("获取数据异常");
}
JSONObject resultJson = new JSONObject(result);
int code = resultJson.getInt("code");
if(code != 0 ){
String msg = resultJson.getString("message");
throw new Exception(msg);
}
JSONObject data = resultJson.getJSONObject("data");
int total = data.getInt("pageCount");
if(total == 0){
dbentity = new ArrayList<OrgEntity>();
return ;
}
JSONArray dataList = data.getJSONArray("dataList");
arrayToOrgList(dataList,dbentity,tableName);
if(total == pageNum){
return ;
}
getOrgData(tableName,updateTime,pageNum+1,token,psc,dbentity);
}
}