forked from hugh/open-JSD-8686
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.
207 lines
6.3 KiB
207 lines
6.3 KiB
package com.fr.plugin.function.function; |
|
|
|
import com.fr.json.JSONArray; |
|
import com.fr.json.JSONObject; |
|
import com.fr.plugin.context.PluginContexts; |
|
import com.fr.plugin.function.config.PluginSimpleConfig; |
|
import com.fr.plugin.function.utils.HttpUtils; |
|
import com.fr.plugin.function.utils.Utils; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
import com.fr.script.AbstractFunction; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import com.fr.stable.fun.Authorize; |
|
|
|
@FunctionRecorder |
|
@Authorize(callSignKey = "com.fr.plugin.xxxx.bkfunction") |
|
public class AuthFunction extends AbstractFunction { |
|
|
|
@Override |
|
public Object run(Object[] objects) { |
|
PluginSimpleConfig psc = PluginSimpleConfig.getInstance(); |
|
String remarks = String.valueOf(objects[0]); |
|
String ucid = String.valueOf(objects[1]); |
|
JSONArray remarkStr = getRemarkStr(remarks); |
|
String result = http(remarkStr,ucid,psc); |
|
String returnResult = "插件未授权"; |
|
System.out.println(PluginContexts.currentContext().isAvailable()); |
|
if(PluginContexts.currentContext().isAvailable()){ |
|
returnResult = getResult(result,psc); |
|
} |
|
return returnResult; |
|
} |
|
|
|
private static JSONArray getRemarkStr(String remarks){ |
|
String[] remark = remarks.split(","); |
|
JSONArray jsonArray = new JSONArray(); |
|
for(int i=0;i<remark.length;i++){ |
|
jsonArray.add(remark[i]); |
|
} |
|
|
|
return jsonArray; |
|
} |
|
|
|
/** |
|
* 发送http请求 |
|
* @param resMarks |
|
* @param ucid |
|
* @return |
|
*/ |
|
private static String http(JSONArray resMarks,String ucid,PluginSimpleConfig psc){ |
|
String url = psc.getUrl(); |
|
JSONObject param = new JSONObject(); |
|
param.put("projectValue",psc.getProjectValue()); |
|
param.put("systemSource",psc.getSystemSource()); |
|
param.put("resourceTypeValue",psc.getResourceTypeValue()); |
|
param.put("resMarks",resMarks); |
|
param.put("option",psc.getOption()); |
|
param.put("ucid",ucid); |
|
|
|
return HttpUtils.HttpPostJson(url,param.toString(),null); |
|
} |
|
|
|
/** |
|
* 获取请求结果 |
|
* @param resultStr |
|
* @return |
|
*/ |
|
private static String getResult(String resultStr,PluginSimpleConfig psc){ |
|
if(Utils.isNullStr(resultStr)){ |
|
return ""; |
|
} |
|
|
|
JSONObject resultobj = new JSONObject(resultStr); |
|
String code = resultobj.getString("code"); |
|
if(!"10001".equals(code)){ |
|
return "查询数据异常!"; |
|
} |
|
|
|
JSONArray datas = resultobj.getJSONArray("datas"); |
|
|
|
if(datas == null || datas.size() <= 0){ |
|
return " and 1 = 1"; |
|
} |
|
|
|
Map<String,Integer> level = new HashMap<String,Integer>(); |
|
level.put(psc.getCorp(),1); |
|
level.put(psc.getArea(),2); |
|
level.put(psc.getCity(),3); |
|
level.put(psc.getProject(),4); |
|
|
|
int minLevel = 1; |
|
String result = ""; |
|
for(int i = 0;i<datas.size();i++){ |
|
JSONObject data = datas.getJSONObject(i); |
|
JSONArray fieldRecords = data.getJSONArray("fieldRecords"); |
|
|
|
if(fieldRecords == null || fieldRecords.size() <= 0){ |
|
result += " and 1 = 1"; |
|
continue; |
|
} |
|
String oneResult = ""; |
|
for(int j = 0;j < fieldRecords.size();j++){ |
|
JSONObject fieldRecord = fieldRecords.getJSONObject(j); |
|
JSONArray fieldValues = fieldRecord.getJSONArray("fieldValues"); |
|
if(fieldValues == null || fieldValues.size() <= 0){ |
|
result += " and 1 = 1"; |
|
break; |
|
} |
|
|
|
String field =fieldRecord.getString("resField"); |
|
|
|
if(!level.containsKey(field)){ |
|
continue; |
|
} |
|
|
|
int levelF = level.get(field); |
|
|
|
if(levelF >= minLevel){ |
|
minLevel = levelF; |
|
oneResult = " and "+field + getValue(fieldValues); |
|
} |
|
} |
|
|
|
result += oneResult; |
|
} |
|
|
|
return result; |
|
} |
|
|
|
private static String getValue(JSONArray fieldValues){ |
|
String fieldV = " in ("; |
|
|
|
for(int i = 0;i < fieldValues.size();i++){ |
|
String field = "'"+fieldValues.getString(i)+"'"; |
|
|
|
if(i != 0){ |
|
field = ","+field; |
|
} |
|
|
|
fieldV +=field; |
|
} |
|
|
|
return fieldV + ")"; |
|
} |
|
|
|
|
|
private static String getResult2(String resultStr){ |
|
if(Utils.isNullStr(resultStr)){ |
|
return ""; |
|
} |
|
|
|
JSONObject resultobj = new JSONObject(resultStr); |
|
String code = resultobj.getString("code"); |
|
if(!"10001".equals(code)){ |
|
return "查询数据异常!"; |
|
} |
|
|
|
JSONArray datas = resultobj.getJSONArray("datas"); |
|
|
|
if(datas == null || datas.size() <= 0){ |
|
return " and 1 = 1"; |
|
} |
|
|
|
Map<String,Integer> level = new HashMap<String,Integer>(); |
|
level.put("corp_code",1); |
|
level.put("area_corp_code",2); |
|
level.put("city_corp_code",3); |
|
level.put("market_project_code",4); |
|
|
|
int minLevel = 1; |
|
String result = ""; |
|
for(int i = 0;i<datas.size();i++){ |
|
JSONObject data = datas.getJSONObject(i); |
|
JSONArray fieldRecords = data.getJSONArray("fieldRecords"); |
|
|
|
if(fieldRecords == null || fieldRecords.size() <= 0){ |
|
result += " and 1 = 1"; |
|
continue; |
|
} |
|
String oneResult = ""; |
|
for(int j = 0;j < fieldRecords.size();j++){ |
|
JSONObject fieldRecord = fieldRecords.getJSONObject(j); |
|
JSONArray fieldValues = fieldRecord.getJSONArray("fieldValues"); |
|
if(fieldValues == null || fieldValues.size() <= 0){ |
|
result += " and 1 = 1"; |
|
break; |
|
} |
|
|
|
String field =fieldRecord.getString("resField"); |
|
|
|
if(!level.containsKey(field)){ |
|
continue; |
|
} |
|
|
|
int levelF = level.get(field); |
|
if(levelF >= minLevel){ |
|
minLevel = levelF; |
|
oneResult = " and "+field + getValue(fieldValues); |
|
} |
|
} |
|
|
|
result += oneResult; |
|
} |
|
|
|
return result; |
|
} |
|
}
|
|
|