package cn.autoform.util.tool.impl;
|
|
import cn.autoform.bean.CodeAndValueBean;
|
import cn.autoform.db.entity.FieldPropertyEntity;
|
import cn.autoform.db.entity.FormFieldEntity;
|
import cn.autoform.factory.FormFactory;
|
import cn.autoform.fw.utility.ConstMap;
|
import cn.autoform.util.tool.*;
|
import cn.autoform.web.service.datamanagement.DataManagementService;
|
import cn.autoform.web.service.datasource.DatasourceService;
|
import lombok.extern.slf4j.Slf4j;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 数据管理,查询列表,将code转换成name
|
* @author yuezhw
|
* @data 2017-10-20
|
*/
|
@SuppressWarnings("unchecked")
|
@Slf4j
|
@Component
|
public class CodeToNameToolImpl implements CodeToNameTool, ColumnConst {
|
|
private final static String TEXTCOLUMN = "textcolumn";
|
private final static String VALUECOLUMN = "valuecolumn";
|
private final static String DATASOURCETYPE = "datasourcetype";
|
private final static String API_URL = "apiUrl";
|
private final static String API_IDKEY = "apiIdKey";
|
private final static String API_PIDKEY = "apiPIdKey";
|
private final static String ITEM = "item_1";
|
|
private static final Map<String ,WhereToGo> CHOOSE_DATASOURCETYPE = new HashMap<>();
|
|
{
|
//转换到主数据
|
CHOOSE_DATASOURCETYPE.put(ConstMap.MASTERDATA, ()->{
|
String code = this.fields.stream().
|
filter(o->"masterData".equals(o.getProperty())).
|
collect(Collectors.toList()).get(0).getValue();
|
if(code == null)
|
return null;
|
String callbackId = ITEM;
|
Map<String, List<CodeAndValueBean>> value;
|
try {
|
value = (Map<String, List<CodeAndValueBean>>) this.formFactory
|
.createFormMethod()
|
.getMasterBaseElementListServiceForNew(this.tenantID, code, callbackId,null,null,null);
|
return value.get(ITEM);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
this.a++;
|
System.out.println("------------------------------------------------------");
|
System.out.println(this.a+"次");
|
System.out.println("-------------------------------------------------------");
|
return null;
|
});
|
|
//转换到数据源
|
CHOOSE_DATASOURCETYPE.put(ConstMap.DATASOURCE, ()->{
|
try {
|
Map<String, FieldPropertyEntity> fieldPropertyMap = new HashMap<>();
|
// 数据源
|
this.fields.stream().filter(o->Objects.equals(o.getProperty(), ConstMap.DATASOURCE)//过滤出需要的参数
|
||Objects.equals(o.getProperty(),TEXTCOLUMN)
|
||Objects.equals(o.getProperty(), TEXTCOLUMN))
|
.forEach(o->{
|
fieldPropertyMap.put(o.getProperty(), o);
|
|
});
|
String datasource = fieldPropertyMap.get(ConstMap.DATASOURCE).getValue();
|
String text = fieldPropertyMap.get(TEXTCOLUMN).getValue();
|
String value = fieldPropertyMap.get(VALUECOLUMN).getValue();
|
|
StringBuilder sendSearchCol = new StringBuilder();
|
sendSearchCol.append(text);
|
sendSearchCol.append(ConstMap.COMMA);
|
sendSearchCol.append(value);
|
|
Map<String, List<String>> datasourceList = this.datasourceService.getColumnsValues(this.tenantID, this.formID, datasource,
|
sendSearchCol.toString());
|
List<String> codeList = datasourceList.get(text);
|
List<String> textList = datasourceList.get(value);
|
List<CodeAndValueBean> addList = new ArrayList<>();
|
for (int i = 0; i < codeList.size(); i++) {
|
CodeAndValueBean codeAndvaule = new CodeAndValueBean();
|
codeAndvaule.setCode(codeList.get(i));
|
codeAndvaule.setValue(textList.get(i));
|
addList.add(codeAndvaule);
|
}
|
return addList;
|
} catch (Exception e) {
|
log.error("---------> 数据源 ERROR!", e);
|
}
|
return null;
|
});
|
|
//转换到CUSTOM自定义
|
CHOOSE_DATASOURCETYPE.put(ConstMap.CUSTOM, ()->{
|
return null;
|
});
|
|
//转换到API
|
CHOOSE_DATASOURCETYPE.put(ConstMap.APIURL, ()->{
|
|
Map<String, FieldPropertyEntity> fieldPropertyMap = new HashMap<>();
|
// 数据源
|
this.fields.stream().filter(o->Objects.equals(o.getProperty(), API_URL)//过滤出需要的参数
|
||Objects.equals(o.getProperty(),API_IDKEY)
|
||Objects.equals(o.getProperty(), API_PIDKEY))
|
.forEach(o->{
|
fieldPropertyMap.put(o.getProperty(), o);
|
|
});
|
|
String apiUrl = fieldPropertyMap.get(API_URL).getValue();
|
String code = fieldPropertyMap.get(API_IDKEY).getValue();
|
String text = fieldPropertyMap.get(API_PIDKEY).getValue();
|
BufferedReader reader =null;
|
try {
|
URL url = new URL(apiUrl);
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
con.setRequestMethod("GET");
|
con.connect();
|
reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
|
String line;
|
StringBuilder buffer = new StringBuilder();
|
while ((line = reader.readLine()) != null) {
|
buffer.append(line);
|
}
|
reader.close();
|
con.disconnect();
|
String res = buffer.toString();
|
JSONArray jsonObjAPi = JSONArray.fromObject(res);
|
if (jsonObjAPi.isEmpty()) {
|
List<CodeAndValueBean> addList = new ArrayList<>();
|
for (Object jsonObjSub : jsonObjAPi) {
|
JSONObject jsonObjSubcov = (JSONObject) jsonObjSub;
|
String codeRs = jsonObjSubcov.getString(code);
|
String textRs = jsonObjSubcov.getString(text);
|
CodeAndValueBean codeAndvaule = new CodeAndValueBean();
|
codeAndvaule.setCode(codeRs);
|
codeAndvaule.setValue(textRs);
|
addList.add(codeAndvaule);
|
}
|
return addList;
|
}
|
} catch (Exception e) {
|
log.error("---------> API URL ERROR!", e);
|
|
}finally{
|
if(reader != null){
|
reader.close();
|
}
|
}
|
return null;
|
});
|
}
|
|
Integer a = 0;
|
@Resource
|
DataManagementService dataManagementService;
|
|
@Autowired
|
private DatasourceService datasourceService = null;
|
|
@Autowired
|
private FormFactory formFactory;
|
|
private String formID;
|
|
private List<FieldPropertyEntity> fields;
|
|
private String tenantID;
|
|
//默认是不查
|
private boolean queryMain = true;
|
|
public void setQueryMain(boolean queryMain) {
|
this.queryMain = queryMain;
|
}
|
|
@Override
|
public void toDo(List<Map<String,Object>> resultList) {
|
this.formID = RequestContext.getFormId();
|
this.tenantID = RequestContext.getTenantId();
|
List<FormFieldEntity> formMainField = null;
|
if(queryMain){
|
formMainField = this.queryFormMainField(formID, tenantID);
|
}else{
|
formMainField = this.queryFormSubField(formID, tenantID);
|
}
|
if(!formMainField.isEmpty())
|
formMainField.stream().forEach(fromField ->{
|
try {
|
//查询一列主数据
|
List<CodeAndValueBean> cANDv = checkData_1(getDataSourseType(queryFormFieldProperty(formID, tenantID ,fromField.getColumnOrderNum())));
|
if(cANDv != null){
|
resultList.stream() //把结果集按列替换
|
.filter(keyValue -> // && Objects.equals(keyValue.get(PROCESS_STATE), "10")
|
keyValue.containsKey(fromField.getFieldKey()))
|
.forEach(result ->{
|
StringBuilder name = new StringBuilder();
|
if( !ObjectUtil.empty(result.get(fromField.getFieldKey()))
|
&& result.get(fromField.getFieldKey()).toString().contains(ConstMap.SPLITSTR)){
|
String[] codes = result.get(fromField.getFieldKey()).toString().split(ConstMap.SPLITSTR);
|
for(String cd : codes){
|
name.append(returnName(cANDv, cd) + ",");
|
}
|
name.deleteCharAt(name.length() - 1);
|
}else{
|
if(result.get(fromField.getFieldKey()) != null){
|
name.append(returnName(cANDv,result.get(fromField.getFieldKey()).toString()));
|
}
|
}
|
if(!ObjectUtil.empty(result.get(fromField.getFieldKey()))){
|
result.put(fromField.getFieldKey(), name.toString());
|
}
|
});
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
});
|
this.queryMain = true;
|
}
|
|
//查询主表中相关控件
|
public List<FormFieldEntity> queryFormMainField(String formID, String tenantID){
|
return dataManagementService.queryFormMainField(formID, tenantID);
|
}
|
|
//查询非主表中相关控件
|
public List<FormFieldEntity> queryFormSubField(String formID, String tenantID){
|
return dataManagementService.queryFormSubField(formID, tenantID);
|
}
|
|
//查询非主表中相关控件
|
public List<FormFieldEntity> queryFormAllField(String formID, String tenantID){
|
return dataManagementService.queryFormAllField(formID, tenantID);
|
}
|
|
//查询控件属性
|
public List<FieldPropertyEntity> queryFormFieldProperty(String formID, String tenantID, Integer columnOrderNum){
|
return dataManagementService.queryFormFieldProperty(formID, tenantID, columnOrderNum);
|
}
|
|
|
//检查数据格式 如果符合要求,那么就返回name
|
public List<CodeAndValueBean> checkData_1(String datasourseName) throws Exception{
|
//控件绑定的数据源 : 主数据、API接口、自定义数据源
|
List<CodeAndValueBean> cANDv = null;
|
if(datasourseName != null){
|
cANDv = CHOOSE_DATASOURCETYPE.get(datasourseName).goGoGo();
|
}
|
return cANDv;
|
}
|
|
//返回需要的name
|
@SuppressWarnings("unused")
|
public String returnName(List<CodeAndValueBean> cANDv, String code){
|
String name;
|
if(StringUtils.isEmpty(code))
|
return name = "";
|
CodeAndValueBean codeAndValueBean =
|
cANDv
|
.stream()
|
.filter(codeV ->Objects.equals(code,codeV.getCode()))
|
.findFirst()
|
.orElse(null);
|
name = codeAndValueBean != null ? codeAndValueBean.getValue():code;
|
return name;
|
}
|
|
//获取选中数据类型
|
public String getDataSourseType(List<FieldPropertyEntity> fields){
|
this.fields = fields;
|
Map<String, String> result = new HashMap<>();
|
fields.stream()
|
.filter(propery ->"datasourcetype".equals(propery.getProperty()))
|
.forEach(o ->{
|
result.put(DATASOURCETYPE, o.getValue());
|
});
|
|
return result.get(DATASOURCETYPE);
|
}
|
|
}
|
@FunctionalInterface
|
interface WhereToGo{
|
|
List<CodeAndValueBean> goGoGo() throws IOException;
|
}
|