package com.zrar.blh;
import com.zrar.easyweb.core.util.PropertyManager;
import com.zrar.easyweb.persistence.core.IBaseZrarDao;
import com.zrar.easyweb.web.core.annotation.BLH;
import com.zrar.easyweb.web.core.annotation.Mapping;
import com.zrar.easyweb.web.core.event.IZrarRequest;
import com.zrar.easyweb.web.core.event.IZrarResponse;
import com.zrar.easyweb.web.core.event.impl.ZrarResponse;
import com.zrar.util.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import java.util.*;
@BLH
@Mapping("/TreeBLH")
public class TreeBLH {
private static String Client_Id = PropertyManager.getProperty("Client_Id",new String[0]);
private static String Client_secret = PropertyManager.getProperty("Client_secret",new String[0]);
private static String zsksbUrl = PropertyManager.getProperty("zsksbUrl",new String[0]);
private static String categoryCodeCode = PropertyManager.getProperty("categoryCode",new String[0]);
//账号信息
public static final Map<String, String> TOKEN_MAP = new HashMap<String, String>(5);
static {
TOKEN_MAP.put("client_id", Client_Id);
TOKEN_MAP.put("client_secret", Client_secret);
TOKEN_MAP.put("grant_type", "client_credentials");
}
@Resource
private IBaseZrarDao dao;
@Mapping("/get_kinfo_category")
public IZrarResponse get_kinfo_category(IZrarRequest req){
IZrarResponse res = new ZrarResponse();
//获取整个map参数值,对参数值进行拼接在方法后面,拼接方式如下方法
List<String> keys = new ArrayList<String>(TOKEN_MAP.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = TOKEN_MAP.get(key);
if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
prestr = prestr + key + "=" + value;
} else {
prestr = prestr + key + "=" + value + "&";
}
}
//参数拼接好之后进行请求获取access_token
String result= HttpRequest.sendPost(zsksbUrl+"/get_kinfo_category", prestr, "utf-8");
JSONObject jo=getJSONObject(result);
String token = jo.getJSONObject("custom").getString("access_token");
token = "Bearer " + token;//请求方式要求是这样,进行拼接。
Map<String , String > map = new HashMap<String, String>();
map.put("Authorization",token);
//获取整个数据(json形式)
String data = HttpRequest.sendPostOne(zsksbUrl+"/get_kinfo_category","", map, "utf-8");
JSONObject datajson=getJSONObject(data);
List list=new ArrayList();
//list数据,进行遍历
JSONArray jsonArray = datajson.getJSONObject("custom").getJSONArray("infoList");
for (int i = 0 ; i < jsonArray.size() ; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String categoryCode = jsonObject.getString("categoryCode");
if (categoryCodeCode.equals(categoryCode)){
getChildList(jsonObject,list);//递归遍历树
}
}
//遍历后的树塞在list中,从list中获取数据,数据保存到库中。
for(int i = 0;i < list.size();i++) {
Map<String, String> maplist = (Map<String, String>) list.get(i);
String typecode = maplist.get("typecode").toString();
String typename = maplist.get("typename").toString();
String typeuuid = maplist.get("typeuuid").toString();
String sql = "insert into zl_type_gk(typeuuid,typecode,typename) values (?,?,?)";
dao.update(sql,typeuuid,typecode,typename);
res.addMessage("保存成功!");
}
return res;
}
/**
* @Description:递归遍历树
* @Author: Simajilai
* @Date: 2021/7/22 9:26
*/
public void getChildList(JSONObject jsonObject,List list){
String categoryCode = jsonObject.getString("categoryCode");
String categoryName = jsonObject.getString("categoryName");
String categoryGuid = jsonObject.getString("categoryGuid");
String isLeaf = jsonObject.getString("isLeaf");
if ("0".equals(isLeaf)){//自己本身不是叶子节点,含有叶子节点
JSONArray childList = jsonObject.getJSONArray("childList");
for (int j = 0 ; j < childList.size() ; j++){
JSONObject jsonObject1 = childList.getJSONObject(j);
getChildList(jsonObject1,list);
}
}
Map map = new HashMap();
map.put("typecode",categoryCode);
map.put("typename",categoryName);
map.put("typeuuid",categoryGuid);
list.add(map);
}
/**
* 将字符串转成json对象
* @param jostr
* @return
*/
private JSONObject getJSONObject(String jostr){
JSONObject jo=null;
if(StringUtils.isNotEmpty(jostr)){//如果字符串为空则返回null
try{
jo=JSONObject.fromObject(jostr);
}catch(Exception e){
e.printStackTrace();
}
}
return jo;
}
}
从服务器接口拉数据保存至库中
最新推荐文章于 2022-10-01 09:45:00 发布