package com.tianjian.property.jieshuns.service;
import java.util.Map;
import java.util.Properties;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tianjian.property.jieshuns.utils.CommonServices;
import com.tianjian.property.jieshuns.utils.ConfigHelpers;
import com.tianjian.property.jieshuns.utils.Utils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class SaveorUpdateServices extends CommonServices {
@Override
public String buildRequestParam(Map params) {
System.out.println("------params66------"+params.get("jsonusers"));
ConfigHelpers con=new ConfigHelpers();
Map map=con.getproper("/config/"+baseDir+"saveorupdate");
Properties prop= ConfigHelpers.getPropertiess(map);
JsonObject jsonParam = new JsonObject();
JsonObject attributes = new JsonObject();
// jsonParam
attributes.addProperty("serviceId", prop.getProperty("serviceId"));//JsonObject对象addProperty第二个参数是普通的String。
attributes.addProperty("requestType", prop.getProperty("requestType"));
System.out.println("------params------"+params.get("jsonusers"));
//Map型参数转为JSONObject
net.sf.json.JSONObject attribute012;
attribute012= (JSONObject) params.get("jsonusers");
System.out.println("------attribute012------"+attribute012);
String attribute013= attribute012.toString();
//JSONArray attribute123;
//attribute123=attribute012.getJSONArray("jsonusers");
// System.out.println("------attribute123------"+attribute123);
//JsonArray attribute;
//attribute= (JsonArray) params.get("jsonusers");
//System.out.println("------attributes001------"+attribute);
//JsonElement jsonelement = null;
String attributes01= attribute012.get("jsonusers").toString();
System.out.println("------attributes01------"+attributes01);
String attributes02=attributes01.replaceAll("\\\\", "");
System.out.println("------attributes02------"+attributes02);
//JSONObject转为JsonObject ,通过先转成对应的String然后转为JsonObject
JsonObject json=new JsonParser().parse(attribute013).getAsJsonObject();
System.out.println("--555----json------"+json);
JsonElement dataItems=json.get("jsonusers");
System.out.println("--555----dataItems------"+dataItems);
JsonElement value = null;
attributes.add("attributes", dataItems);//JsonObject对象add第二个参数是JsonElement的对象。
jsonParam.add("attributes", attributes);
System.out.println("---facebook------jsonParam:"+jsonParam);
System.out.println("---facebook------attributes:"+attributes);
//return jsonParam.toString();
return attributes.toString();
}
@Override
public String extractResult(CloseableHttpResponse response)
throws Exception {
int statusCode = response.getStatusLine().getStatusCode();
String dataItem="";
System.out.println("0666666"+statusCode);
if (statusCode == HttpStatus.SC_OK) {
String results = EntityUtils.toString(response.getEntity());
JsonObject json = new JsonParser().parse(results).getAsJsonObject();
int resultCode = json.get("resultCode").getAsInt();
System.out.println("1666666"+json);
System.out.println("2666666"+resultCode);
if (resultCode == 0) {
JsonElement dataItems = json.get("dataItems");
System.out.println("666666"+dataItems);
JsonArray jsonarray=json.getAsJsonArray("dataItems");
JsonObject dds=(JsonObject) jsonarray.get(0);
JsonObject attributes=dds.getAsJsonObject("attributes");
dataItem=attributes.toString();
System.out.println("ReplacePayFee-SUCCESS:成功!同步成功信息:" + dataItems.toString());
}
} else {
System.out.println("ReplacePayFee-ERROR:执行失败!" + "\tstatusCode:" + statusCode);
}
return dataItem;
}
}