cn.schoolwow.util.domain.query.jsonarray.QueryJSONArrayResponse Maven / Gradle / Ivy
package cn.schoolwow.util.domain.query.jsonarray;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.util.module.query.jsonarray.domain.JSONArrayQueryOption;
import cn.schoolwow.util.module.query.jsonarray.flow.get.GetJSONArrayMultipleFieldValueListBusiness;
import cn.schoolwow.util.module.query.jsonarray.flow.get.GetJSONArraySingleFieldValueListBusiness;
import cn.schoolwow.util.module.query.jsonarray.flow.group.GroupJSONArrayByFieldBusiness;
import cn.schoolwow.util.module.query.jsonarray.service.FilterJSONArrayCompositeBusiness;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class QueryJSONArrayResponse {
private JSONArrayQueryOption jsonQueryOption;
public QueryJSONArrayResponse(JSONArrayQueryOption jsonQueryOption) {
this.jsonQueryOption = jsonQueryOption;
}
/**获取过滤后的列表*/
public JSONObject getObject(){
JSONArray array = getArray();
if(null==array||array.size()==0){
return null;
}
return array.getJSONObject(0);
}
/**获取过滤后的列表*/
public JSONObject checkObject(){
JSONArray array = getArray();
Map conditionMap = new HashMap<>();
conditionMap.putAll(jsonQueryOption.fieldValueMap);
conditionMap.putAll(jsonQueryOption.fieldValueListMap);
if(null==array||array.size()==0){
throw new IllegalArgumentException("符合条件的实例为空!" + "查询条件:"+conditionMap);
}
if(array.size()>1){
throw new IllegalArgumentException("期望获取1个实例,实际个数:"+array.size()+"!查询条件:"+conditionMap);
}
return array.getJSONObject(0);
}
/**获取过滤后的列表*/
public JSONArray getArray(){
FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayCompositeBusiness())
.putCurrentCompositeFlowData("jsonQueryOption", jsonQueryOption)
.execute();
JSONArray filterArray = (JSONArray) flowContext.getData("filterArray");
return filterArray;
}
/**获取指定字段的值*/
public List getSingleFieldValueList(String fieldName){
FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayCompositeBusiness())
.next(new GetJSONArraySingleFieldValueListBusiness())
.putCurrentCompositeFlowData("jsonQueryOption", jsonQueryOption)
.putCurrentCompositeFlowData("fieldName", fieldName)
.execute();
List fieldValueList = (List) flowContext.getData("fieldValueList");
return fieldValueList;
}
/**获取指定多个字段的值*/
public List getMultipleFieldValueList(String... fieldNames){
FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayCompositeBusiness())
.next(new GetJSONArrayMultipleFieldValueListBusiness())
.putCurrentCompositeFlowData("jsonQueryOption", jsonQueryOption)
.putCurrentCompositeFlowData("fieldNames", fieldNames)
.execute();
List fieldValueList = (List) flowContext.getData("fieldValueList");
return fieldValueList;
}
/**根据字段字段分组*/
public JSONObject groupByFieldName(String fieldName){
FlowContext flowContext = QuickFlowBuilder.quickFlow().startFlow(new FilterJSONArrayCompositeBusiness())
.next(new GroupJSONArrayByFieldBusiness())
.putCurrentCompositeFlowData("jsonQueryOption", jsonQueryOption)
.putCurrentCompositeFlowData("fieldName", fieldName)
.execute();
JSONObject fieldJSONArrayMap = (JSONObject) flowContext.getData("fieldJSONArrayMap");
return fieldJSONArrayMap;
}
}