完善表单数据操作工具类
This commit is contained in:
@@ -1,16 +1,66 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import com.seeyon.cap4.form.api.FormApi4Cap4;
|
||||
import com.seeyon.cap4.form.bean.FormBean;
|
||||
import com.seeyon.cap4.form.bean.FormFieldBean;
|
||||
import com.seeyon.cap4.form.bean.FormTableBean;
|
||||
import com.seeyon.ctp.common.AppContext;
|
||||
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
|
||||
import com.seeyon.ctp.common.exceptions.BusinessException;
|
||||
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumBean;
|
||||
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
|
||||
import com.seeyon.ctp.util.JDBCAgent;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class EnumMapUtils {
|
||||
|
||||
public static String getMasterTableEnumItemValue(String formNo,String fieldDisplay, String targetValue) {
|
||||
if(targetValue == null || "null".equals(targetValue) || "".equals(targetValue)){
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
FormFieldBean beanByDisplay = masterTableBean.getFieldBeanByDisplay(fieldDisplay);
|
||||
if(beanByDisplay == null || beanByDisplay.getEnumId() == 0l) {
|
||||
return "";
|
||||
}
|
||||
return getEnumItemValueByEnumId(targetValue, beanByDisplay.getEnumId());
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static String getEnumItemIdByGroupNameAndItemShowValue(String enumGroupName, String targetValue) {
|
||||
String sql = "SELECT * FROM ctp_enum ce let join ctp_enum_item cei on ce.`ID` = cei.REF_ENUMID where ce.`ENUMNAME` = ? and CEI.`SHOWVALUE` = ?";
|
||||
if(StringUtils.isAnyBlank(enumGroupName, targetValue)){
|
||||
return "";
|
||||
}
|
||||
Long enumId = null;
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
agent.execute(sql, Arrays.asList(enumGroupName,targetValue));
|
||||
List<Map<String, Object>> list = (List<Map<String, Object>>) agent.resultSetToList();
|
||||
if(list == null || list.size() == 0) {
|
||||
return "";
|
||||
}
|
||||
Map map = list.get(0);
|
||||
enumId = (Long)map.get("ID");
|
||||
return enumId == null ? "" : enumId + "";
|
||||
} catch (Exception e) {
|
||||
|
||||
}finally {
|
||||
agent.close();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
public static String getEnumItemValue(String rootPCode, String groupValue, String targetValue) {
|
||||
if(targetValue == null || "null".equals(targetValue) || "".equals(targetValue)){
|
||||
return "";
|
||||
@@ -66,21 +116,19 @@ public class EnumMapUtils {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getEnumItemValueByEnumIdAndEnumValue(String value,long enumId) {
|
||||
public static String getEnumShowValue(Object enumItemId) throws BusinessException {
|
||||
if(enumItemId == null) {
|
||||
return "";
|
||||
}
|
||||
Long temp = enumItemId instanceof Long ? (Long)enumItemId : (enumItemId instanceof String ? Long.parseLong((String)enumItemId) : null);
|
||||
if(temp == null) {
|
||||
return "";
|
||||
}
|
||||
EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
|
||||
CtpEnumBean ctpEnumBean = enumManagerNew.getEnum(enumId);
|
||||
if(ctpEnumBean == null) {
|
||||
CtpEnumItem ctpEnumItem = enumManagerNew.getCtpEnumItem(temp);
|
||||
if(ctpEnumItem == null) {
|
||||
return "";
|
||||
}
|
||||
List<CtpEnumItem> ctpEnumItems = ctpEnumBean.getItems();
|
||||
if(ctpEnumBean.getItems() == null) {
|
||||
return "";
|
||||
}
|
||||
for (CtpEnumItem enumItem : ctpEnumItems) {
|
||||
if(enumItem.getValue().equals(value)) {
|
||||
return enumItem.getId() + "";
|
||||
}
|
||||
}
|
||||
return "";
|
||||
return ctpEnumItem.getShowvalue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class FormColumn {
|
||||
|
||||
private String id;
|
||||
private List<FormFieldVo> vos;
|
||||
private List<FormFieldVo> vos = new ArrayList<>();
|
||||
private Map<String, Object> fieldsMap = new HashMap<>(); // 缓存字段map
|
||||
|
||||
public FormColumn() {}
|
||||
|
||||
public FormColumn(String id, List<FormFieldVo> vos) {
|
||||
this.id = id;
|
||||
setVos(vos);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
@@ -12,6 +24,7 @@ public class FormColumn {
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
fieldsMap.put("id", id); // 同步到 map
|
||||
}
|
||||
|
||||
public List<FormFieldVo> getVos() {
|
||||
@@ -19,6 +32,45 @@ public class FormColumn {
|
||||
}
|
||||
|
||||
public void setVos(List<FormFieldVo> vos) {
|
||||
this.vos = vos;
|
||||
this.vos = vos != null ? vos : new ArrayList<>();
|
||||
rebuildFieldsMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字段 map,按 displayName 映射 value
|
||||
*/
|
||||
public Map<String, Object> getFieldsMap() {
|
||||
return fieldsMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 displayName 获取值
|
||||
*/
|
||||
public Object getValue(String displayName) {
|
||||
return fieldsMap.get(displayName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新某个字段的值,同时同步到 map
|
||||
*/
|
||||
public void setValue(String displayName, Object value) {
|
||||
for (FormFieldVo vo : vos) {
|
||||
if (displayName.equals(vo.getDisplayName())) {
|
||||
vo.setValue(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fieldsMap.put(displayName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部方法:根据 vos 重建 map
|
||||
*/
|
||||
private void rebuildFieldsMap() {
|
||||
fieldsMap.clear();
|
||||
for (FormFieldVo vo : vos) {
|
||||
fieldsMap.put(vo.getDisplayName(), vo.getValue());
|
||||
}
|
||||
fieldsMap.put("id", id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,629 +0,0 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.seeyon.aicloud.common.JsonUtils;
|
||||
import com.seeyon.cap4.form.api.FormApi4Cap4;
|
||||
import com.seeyon.cap4.form.bean.FormBean;
|
||||
import com.seeyon.cap4.form.bean.FormFieldBean;
|
||||
import com.seeyon.cap4.form.bean.FormTableBean;
|
||||
import com.seeyon.ctp.common.AppContext;
|
||||
import com.seeyon.ctp.common.exceptions.BusinessException;
|
||||
import com.seeyon.ctp.util.JDBCAgent;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import www.seeyon.com.utils.StringUtil;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FormDataOperator {
|
||||
|
||||
public void updateMasterForm(String formNo, List<FormUpdateField> updateFieldVos, List<FormWhereCondition> conditionVos) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
if (updateFieldVos == null) {
|
||||
throw new IllegalArgumentException("要修改的字段为空");
|
||||
}
|
||||
for (FormUpdateField fieldVo : updateFieldVos) {
|
||||
FormFieldBean bean = masterTableBean.getFieldBeanByDisplay(fieldVo.getDisplay());
|
||||
if (bean == null) {
|
||||
continue;
|
||||
}
|
||||
if(bean.getInputType().equals("image") || bean.getInputType().equals("attachment")) {
|
||||
|
||||
}
|
||||
fieldVo.fieldName(bean.getColumnName());
|
||||
}
|
||||
List<FormUpdateField> updateFields = updateFieldVos.stream().filter(u -> u.getFieldName() != null).collect(Collectors.toList());
|
||||
for (FormWhereCondition conditionVo : conditionVos) {
|
||||
FormFieldBean bean = masterTableBean.getFieldBeanByDisplay(conditionVo.getDisplay());
|
||||
if (bean == null) {
|
||||
if (conditionVo.getDisplay().equals("ID") || conditionVo.getDisplay().equals("id")) {
|
||||
conditionVo.setFieldName(conditionVo.getDisplay());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
conditionVo.setFieldName(bean.getColumnName());
|
||||
}
|
||||
List<FormWhereCondition> conditions = conditionVos.stream().filter(c -> c.getFieldName() != null).collect(Collectors.toList());
|
||||
Map<String, Object> map = generateSql(updateFields, masterTableBean.getTableName(), conditions);
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
agent.execute((String) map.get("sql"), (List<Object>) map.get("params"));
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
}
|
||||
|
||||
public Long countCondition(String formNo,List<FormWhereCondition> conditionVos) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
String tableName = masterTableBean.getTableName();
|
||||
for (FormWhereCondition conditionVo : conditionVos) {
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(conditionVo.getDisplay());
|
||||
if (fieldBeanByDisplay == null) {
|
||||
continue;
|
||||
}
|
||||
conditionVo.setFieldName(fieldBeanByDisplay.getColumnName());
|
||||
}
|
||||
|
||||
Map<String, Object> generateSql = generateSql(conditionVos, tableName);
|
||||
String sql = (String) generateSql.get("sql");
|
||||
List<Object> params = (List<Object>) generateSql.get("params");
|
||||
JDBCAgent jdbcAgent = new JDBCAgent();
|
||||
try {
|
||||
jdbcAgent.execute(sql, params);
|
||||
return (Long) jdbcAgent.resultSetToMap().get("countsize");
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
jdbcAgent.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// public void refreshForm(String id, String formNo, String formLoginName, HttpFormRestApiConfig config) throws BusinessException, IOException {
|
||||
// FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
// FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
// FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
// List<Object> dataList = new ArrayList<>();
|
||||
// Map<String,Object> temp1 = new HashMap<>();
|
||||
// Map<String,Object> temp2 = new HashMap<>();
|
||||
// temp1.put("masterTable",temp2);
|
||||
// temp2.put("name",masterTableBean.getTableName());
|
||||
// Map<String, Object> recordMap = new HashMap<>(3); // 容量=3 (2/0.75≈2.67)
|
||||
// recordMap.put("id", id);
|
||||
// recordMap.put("fields", new ArrayList<>());
|
||||
// temp2.put("record", recordMap);
|
||||
// temp2.put("changedFields",new ArrayList<>());
|
||||
// dataList.add(temp1);
|
||||
// String token = getToken(config.getTokenUrl(),config.getRestUserName(),config.getRestPwd(),formLoginName);
|
||||
// Map beanMap = new HashMap();
|
||||
// beanMap.put("formCode", formNo);
|
||||
// beanMap.put("loginName", formLoginName);
|
||||
// beanMap.put("doTrigger", "true");
|
||||
// beanMap.put("rightId", config.getRightId());
|
||||
// beanMap.put("dataList",dataList);
|
||||
// Map<String, String> header = new HashMap<>();
|
||||
// header.put("token", token);
|
||||
// String url = config.getBaseUrl() + "/seeyon/rest/cap4/form/soap/batch-update";
|
||||
// String response = HttpClient.httpPostRaw(url, JsonUtils.toJSONString(beanMap), header, "UTF-8");
|
||||
// System.out.println(response);
|
||||
// }
|
||||
// private String getToken(String oatokenurl,String restName,String restPassword,String loginName) throws FileNotFoundException, IOException {
|
||||
// String address = oatokenurl + restName + "/" + restPassword;
|
||||
// if(StringUtil.isNotEmpty(loginName)){
|
||||
// address = address +"?loginName="+loginName;
|
||||
// }
|
||||
// DefaultHttpClient client = new DefaultHttpClient();
|
||||
// String result = "";
|
||||
// HttpGet get = new HttpGet(address);
|
||||
// // 添加 Headers 信息
|
||||
// get.addHeader(new BasicHeader("Accept", "application/json"));
|
||||
// try {
|
||||
// HttpResponse res = client.execute(get);
|
||||
// if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
// result = EntityUtils.toString(res.getEntity());
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// throw new RuntimeException(e);
|
||||
// }
|
||||
// String token = "";
|
||||
// if(result.contains("{")) {
|
||||
// JSONObject jsObj = JSONObject.parseObject(result);
|
||||
// token = jsObj.get("id").toString();
|
||||
// }else {
|
||||
// token = result;
|
||||
// }
|
||||
// return token;
|
||||
// }
|
||||
|
||||
public List<FormColumn> queryFormDataCondition(String formNo, List<String> queryColumnVos, List<FormWhereCondition> conditionVos) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
List<String> queryColumns = new ArrayList<>();
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
String tableName = masterTableBean.getTableName();
|
||||
Map<String, FormFieldBean> fieldMap4Name = masterTableBean.getFieldMap4Name();
|
||||
if (queryColumnVos != null) {
|
||||
for (String queryColumnVo : queryColumnVos) {
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(queryColumnVo);
|
||||
if (fieldBeanByDisplay == null) {
|
||||
continue;
|
||||
}
|
||||
queryColumns.add(fieldBeanByDisplay.getColumnName());
|
||||
}
|
||||
}
|
||||
for (FormWhereCondition conditionVo : conditionVos) {
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(conditionVo.getDisplay());
|
||||
if (fieldBeanByDisplay == null) {
|
||||
if (conditionVo.getDisplay().equals("ID") || conditionVo.getDisplay().equals("id")) {
|
||||
conditionVo.setFieldName(conditionVo.getDisplay());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
conditionVo.setFieldName(fieldBeanByDisplay.getColumnName());
|
||||
}
|
||||
|
||||
Map<String, Object> generateSql = generateSql(queryColumns, conditionVos, tableName);
|
||||
String sql = (String) generateSql.get("sql");
|
||||
List<Object> params = (List<Object>) generateSql.get("params");
|
||||
JDBCAgent jdbcAgent = new JDBCAgent();
|
||||
List<FormColumn> columns = new ArrayList<>();
|
||||
try {
|
||||
jdbcAgent.execute(sql, params);
|
||||
List list = jdbcAgent.resultSetToList();
|
||||
for (Object o : list) {
|
||||
FormColumn column = new FormColumn();
|
||||
Map<String, Object> columnMap = (Map<String, Object>) o;
|
||||
List<FormFieldVo> vos = new ArrayList<>();
|
||||
for (String key : columnMap.keySet()) {
|
||||
FormFieldVo fieldVo = new FormFieldVo();
|
||||
if (fieldMap4Name.containsKey(key)) {
|
||||
FormFieldBean fieldBean = fieldMap4Name.get(key);
|
||||
fieldVo.setDisplayName(fieldBean.getDisplay());
|
||||
fieldVo.setValue(columnMap.get(key));
|
||||
vos.add(fieldVo);
|
||||
}
|
||||
}
|
||||
column.setVos(vos);
|
||||
if (columnMap.get("id") != null) {
|
||||
column.setId(columnMap.get("id") + "");
|
||||
}
|
||||
columns.add(column);
|
||||
}
|
||||
return columns;
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
} finally {
|
||||
jdbcAgent.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String, Object> generateSql(List<FormWhereCondition> conditions, String tableName) {
|
||||
if (tableName == null) {
|
||||
throw new IllegalArgumentException("tableName cannot be null or empty");
|
||||
}
|
||||
StringBuilder sqlBuilder = new StringBuilder("SELECT count(*) as countSize ");
|
||||
sqlBuilder.append(" from " + tableName);
|
||||
List<Object> params = new ArrayList<>();
|
||||
sqlBuilder.append(buildWhereClause(conditions,params));
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sql", sqlBuilder.toString());
|
||||
result.put("params", params);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> generateSql(List<String> queryColumn, List<FormWhereCondition> conditions, String tableName) {
|
||||
if (tableName == null) {
|
||||
throw new IllegalArgumentException("tableName cannot be null or empty");
|
||||
}
|
||||
StringBuilder sqlBuilder = new StringBuilder("SELECT ");
|
||||
if (queryColumn == null || queryColumn.isEmpty()) {
|
||||
sqlBuilder.append("*");
|
||||
}
|
||||
|
||||
for (int i = 0; i < queryColumn.size(); i++) {
|
||||
sqlBuilder.append(queryColumn.get(i));
|
||||
if (queryColumn.size() > 1 && i >= 0 && i < queryColumn.size() - 1) {
|
||||
sqlBuilder.append(",");
|
||||
}
|
||||
}
|
||||
|
||||
if (queryColumn.size() > 0) {
|
||||
sqlBuilder.append(",`ID`");
|
||||
}
|
||||
|
||||
sqlBuilder.append(" from " + tableName);
|
||||
List<Object> params = new ArrayList<>();
|
||||
sqlBuilder.append(buildWhereClause(conditions,params));
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sql", sqlBuilder.toString());
|
||||
result.put("params", params);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> generateSql(List<FormUpdateField> fieldValues, String tableName, List<FormWhereCondition> conditions) {
|
||||
if (fieldValues == null || fieldValues.isEmpty()) {
|
||||
throw new IllegalArgumentException("Field values cannot be null or empty");
|
||||
}
|
||||
if (tableName == null || tableName.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("Table name cannot be null or empty");
|
||||
}
|
||||
StringBuilder sqlBuilder = new StringBuilder("UPDATE ").append(tableName).append(" SET ");
|
||||
List<Object> params = new ArrayList<>();
|
||||
// Build the SET clause
|
||||
int fieldCount = 0;
|
||||
for (FormUpdateField updateField : fieldValues) {
|
||||
if (updateField.getValue() == null || "".equals(updateField.getValue()) || "null".equals(updateField.getValue())) {
|
||||
continue;
|
||||
}
|
||||
if (fieldCount > 0) {
|
||||
sqlBuilder.append(", ");
|
||||
}
|
||||
if (updateField.getValue() instanceof String && ((String) updateField.getValue()).startsWith("DATE>")) {
|
||||
String oldValue = (String) updateField.getValue();
|
||||
sqlBuilder.append(updateField.getFieldName()).append(" = TO_DATE( ?, 'YYYY-MM-DD')");
|
||||
updateField.setValue(oldValue.substring(oldValue.indexOf(">") + 1));
|
||||
} else {
|
||||
sqlBuilder.append(updateField.getFieldName()).append(" = ?");
|
||||
}
|
||||
params.add(updateField.getValue());
|
||||
fieldCount++;
|
||||
}
|
||||
String whereClauseStr = buildWhereClause(conditions,params);
|
||||
// Append the WHERE clause
|
||||
sqlBuilder.append(whereClauseStr);
|
||||
// Create result map
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sql", sqlBuilder.toString());
|
||||
result.put("params", params);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String,List<Object>> getMainFormTableAttachments(String formNo,Map<String,Object> sourceMap) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
Map<String,List<Object>> attachmentTempMap = new HashMap<>();
|
||||
Set<String> removeKey = new HashSet<>();
|
||||
for (String key : sourceMap.keySet()) {
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(key);
|
||||
if(fieldBeanByDisplay == null ){
|
||||
continue;
|
||||
}
|
||||
if(fieldBeanByDisplay.getInputType().equals("image") ||
|
||||
fieldBeanByDisplay.getInputType().equals("attachment")) {
|
||||
attachmentTempMap.put(fieldBeanByDisplay.getDisplay(),(List<Object>)sourceMap.get(key));
|
||||
removeKey.add(key);
|
||||
}
|
||||
}
|
||||
for (String key : removeKey) {
|
||||
sourceMap.remove(key);
|
||||
}
|
||||
return attachmentTempMap;
|
||||
}
|
||||
|
||||
public Map<String,List<Object>> getSubFormTableAttachments(String formNo,Map<String,Object> sourceMap) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
List<FormTableBean> subTableBean = cap4FormBean.getSubTableBean();
|
||||
Map<String,List<Object>> attachmentTempMap = new HashMap<>();
|
||||
String tableName = null;
|
||||
for (String key : sourceMap.keySet()) {
|
||||
tableName = key;
|
||||
}
|
||||
String finalTableName = tableName;
|
||||
FormTableBean tableBean = subTableBean.stream().filter(s->s.getTableName().equals(finalTableName)).collect(Collectors.toList()).get(0);
|
||||
List<Map<String,Object>> tableData = (List<Map<String,Object>>)sourceMap.get(tableName);
|
||||
Set<String> removeKey = new HashSet<>();
|
||||
for (Map<String, Object> rowData : tableData) {
|
||||
for (String key : rowData.keySet()) {
|
||||
FormFieldBean fieldBeanByDisplay = tableBean.getFieldBeanByDisplay(key);
|
||||
if(fieldBeanByDisplay == null ){
|
||||
continue;
|
||||
}
|
||||
if(fieldBeanByDisplay.getInputType().equals("image") ||
|
||||
fieldBeanByDisplay.getInputType().equals("attachment")) {
|
||||
attachmentTempMap.put(fieldBeanByDisplay.getDisplay(),(List<Object>)sourceMap.get(key));
|
||||
removeKey.add(key);
|
||||
}
|
||||
}
|
||||
for (String key : removeKey) {
|
||||
rowData.remove(key);
|
||||
}
|
||||
removeKey.clear();
|
||||
}
|
||||
return attachmentTempMap;
|
||||
}
|
||||
|
||||
public void addSubTableRecord(String subTableName,List<Object> data,String formNo,String formId) {
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
List<FormTableBean> tableBean = cap4FormBean.getSubTableBean();
|
||||
for (FormTableBean bean : tableBean) {
|
||||
if(!bean.getTableName().equals(subTableName)) {
|
||||
continue;
|
||||
}
|
||||
for (Object column : data) {
|
||||
Map<String,Object> map = (Map<String,Object>) column;
|
||||
Map<String,Object> tempMap = new HashMap<>();
|
||||
for (String key : map.keySet()) {
|
||||
FormFieldBean fieldBeanByDisplay = bean.getFieldBeanByDisplay(key);
|
||||
if(fieldBeanByDisplay == null) {
|
||||
continue;
|
||||
}
|
||||
tempMap.put(fieldBeanByDisplay.getColumnName(),map.get(key));
|
||||
}
|
||||
tempMap.put("formmain_id",formId);
|
||||
tempMap.put("sort",1);
|
||||
tempMap.put("ID",Math.abs(UUID.randomUUID().getLeastSignificantBits()));
|
||||
Map<String, Object> insertSql = generateInsertSql(tempMap, subTableName);
|
||||
agent.execute((String)insertSql.get("sql"),(List<Object>)insertSql.get("params"));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void rebuildSubTableRecord(String subTableName,List<Object> data,String formNo,String formId) {
|
||||
String deleteSql = "delete from " + subTableName + " where formmain_id = ? ";
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
agent.execute(deleteSql, Arrays.asList(formId));
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
List<FormTableBean> tableBean = cap4FormBean.getSubTableBean();
|
||||
for (FormTableBean bean : tableBean) {
|
||||
if(!bean.getTableName().equals(subTableName)) {
|
||||
continue;
|
||||
}
|
||||
for (Object column : data) {
|
||||
Map<String,Object> map = (Map<String,Object>) column;
|
||||
Map<String,Object> tempMap = new HashMap<>();
|
||||
for (String key : map.keySet()) {
|
||||
FormFieldBean fieldBeanByDisplay = bean.getFieldBeanByDisplay(key);
|
||||
if(fieldBeanByDisplay == null) {
|
||||
continue;
|
||||
}
|
||||
tempMap.put(fieldBeanByDisplay.getColumnName(),map.get(key));
|
||||
}
|
||||
tempMap.put("formmain_id",formId);
|
||||
tempMap.put("sort",1);
|
||||
tempMap.put("ID",Math.abs(UUID.randomUUID().getLeastSignificantBits()));
|
||||
Map<String, Object> insertSql = generateInsertSql(tempMap, subTableName);
|
||||
agent.execute((String)insertSql.get("sql"),(List<Object>)insertSql.get("params"));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void handleEnumDeptEtc(Map<String,Object> map,String formNo) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
for (String key : map.keySet()) {
|
||||
FormFieldBean fieldBean = masterTableBean.getFieldBeanByDisplay(key);
|
||||
if(fieldBean == null) {
|
||||
continue;
|
||||
}
|
||||
switch (fieldBean.getInputType()) {
|
||||
case "select": if(fieldBean.getEnumId() != 0l) {
|
||||
String enumItemId = EnumMapUtils.getEnumItemValueByEnumId((String)map.get(key),fieldBean.getEnumId());
|
||||
map.put(key,enumItemId);
|
||||
} break;
|
||||
case "image":break;
|
||||
case "attachment": break;
|
||||
case "account": break;
|
||||
case "department": break;
|
||||
case "radio": break;
|
||||
case "member":break;
|
||||
case "checkbox":
|
||||
case "date":
|
||||
case "text":break;
|
||||
default: break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void handleSubTableEnumDeptEtc(Map<String,Object> map,String formNo) throws BusinessException {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
List<FormTableBean> subTableBean = cap4FormBean.getSubTableBean();
|
||||
for (FormTableBean tableBean : subTableBean) {
|
||||
String tableName = null;
|
||||
for (String key : map.keySet()) {
|
||||
tableName = key;
|
||||
}
|
||||
if(!tableBean.getTableName().equals(tableName)) {
|
||||
continue;
|
||||
}
|
||||
List<Map<String,Object>> subDatas = (List<Map<String, Object>>) map.get(tableName);
|
||||
for (Map<String, Object> subData : subDatas) {
|
||||
for (String key : subData.keySet()) {
|
||||
FormFieldBean fieldBean = tableBean.getFieldBeanByDisplay(key);
|
||||
if(fieldBean == null) {
|
||||
continue;
|
||||
}
|
||||
switch (fieldBean.getInputType()) {
|
||||
case "select": if(fieldBean.getEnumId() != 0l) {
|
||||
String enumItemId = EnumMapUtils.getEnumItemValueByEnumId((String)subData.get(key),fieldBean.getEnumId());
|
||||
subData.put(key,enumItemId);
|
||||
} break;
|
||||
case "image":break;
|
||||
case "attachment": break;
|
||||
case "account": break;
|
||||
case "department": break;
|
||||
case "radio": break;
|
||||
case "member":break;
|
||||
case "checkbox":
|
||||
case "date":
|
||||
case "text":break;
|
||||
default: break;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMasterTableFile(String displayName,String formNo) {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
try {
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(displayName);
|
||||
return fieldBeanByDisplay.getInputType().equals("image") || fieldBeanByDisplay.getInputType().equals("attachment");
|
||||
} catch (BusinessException e) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Map<String, Object> generateInsertSql(Map<String, Object> data, String tableName) {
|
||||
if (tableName == null || tableName.isEmpty()) {
|
||||
throw new IllegalArgumentException("tableName cannot be null or empty");
|
||||
}
|
||||
if (data == null || data.isEmpty()) {
|
||||
throw new IllegalArgumentException("data cannot be null or empty");
|
||||
}
|
||||
StringBuilder sqlBuilder = new StringBuilder("INSERT INTO " + tableName + " (");
|
||||
List<Object> params = new ArrayList<>();
|
||||
// 拼接字段名
|
||||
StringBuilder columns = new StringBuilder();
|
||||
// 拼接字段值
|
||||
StringBuilder values = new StringBuilder();
|
||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||
// 拼接字段名
|
||||
if (columns.length() > 0) {
|
||||
columns.append(", ");
|
||||
}
|
||||
columns.append(entry.getKey());
|
||||
// 拼接值,使用占位符 ?
|
||||
if (values.length() > 0) {
|
||||
values.append(", ");
|
||||
}
|
||||
values.append("?");
|
||||
// 将值加入 params 列表
|
||||
params.add(entry.getValue());
|
||||
}
|
||||
// 完善 SQL 语句
|
||||
sqlBuilder.append(columns).append(") VALUES (").append(values).append(");");
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sql", sqlBuilder.toString());
|
||||
result.put("params", params);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态生成 WHERE 子句
|
||||
* @param conditions 条件集合
|
||||
* @param params 参数列表(引用传递)
|
||||
* @return 拼接后的 WHERE 子句(包含 WHERE 关键字)
|
||||
*/
|
||||
private String buildWhereClause(List<FormWhereCondition> conditions, List<Object> params) {
|
||||
if (conditions == null || conditions.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder whereClause = new StringBuilder(" WHERE ");
|
||||
int conditionIndex = 0;
|
||||
|
||||
for (FormWhereCondition condition : conditions) {
|
||||
// 处理括号起始
|
||||
if (condition.isStartWithBracket()) {
|
||||
whereClause.append("(");
|
||||
}
|
||||
|
||||
// 字段名校验
|
||||
String fieldName = condition.getFieldName();
|
||||
ClauseFactor factor = condition.getClauseFactor();
|
||||
String operator = parseOperator(factor);
|
||||
|
||||
// 构建条件表达式
|
||||
String conditionExpr;
|
||||
if (factor != null && factor.isNullType()) {
|
||||
// 处理 NULL/NOT NULL 条件(无需参数)
|
||||
conditionExpr = String.format("%s %s", fieldName, operator);
|
||||
} else {
|
||||
// 处理普通条件(带占位符)
|
||||
conditionExpr = String.format("%s %s ?", fieldName, operator);
|
||||
// 处理函数模板(如 TO_DATE)
|
||||
if (condition.getIndex() != null) {
|
||||
conditionExpr = conditionExpr.replace("?", condition.getIndex());
|
||||
}
|
||||
// 添加参数值
|
||||
params.add(condition.getValue());
|
||||
}
|
||||
|
||||
whereClause.append(conditionExpr);
|
||||
|
||||
// 处理括号闭合
|
||||
if (condition.isEndWithBracket()) {
|
||||
whereClause.append(")");
|
||||
}
|
||||
|
||||
// 添加连接符(AND/OR)
|
||||
if (conditionIndex < conditions.size() - 1) {
|
||||
whereClause.append(" ").append(condition.getConcatFactor()).append(" ");
|
||||
}
|
||||
conditionIndex++;
|
||||
}
|
||||
|
||||
return whereClause.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验字段名合法性(防止 SQL 注入)
|
||||
*/
|
||||
private String validateFieldName(String fieldName) {
|
||||
if (!fieldName.matches("^[a-zA-Z_][a-zA-Z0-9_]*$")) {
|
||||
throw new IllegalArgumentException("非法字段名: " + fieldName);
|
||||
}
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析运算符映射(eq -> =, lt -> < 等)
|
||||
*/
|
||||
private String parseOperator(ClauseFactor factor) {
|
||||
if(factor == null) {
|
||||
return "=";
|
||||
}
|
||||
switch (factor) {
|
||||
case EQ: return "=";
|
||||
case GT: return ">";
|
||||
case GE: return ">=";
|
||||
case LT: return "<";
|
||||
case LE: return "<=";
|
||||
case LIKE: return "LIKE";
|
||||
case NULL: return "IS NULL"; // 空值判断
|
||||
case NOT_NULL: return "IS NOT NULL"; // 非空判断
|
||||
default: throw new UnsupportedOperationException("不支持的运算符: " + factor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
599
src/main/java/com/seeyon/utils/form/FormTableExecutor.java
Normal file
599
src/main/java/com/seeyon/utils/form/FormTableExecutor.java
Normal file
@@ -0,0 +1,599 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import com.seeyon.aicloud.common.JsonUtils;
|
||||
import com.seeyon.cap4.form.api.FormApi4Cap4;
|
||||
import com.seeyon.cap4.form.bean.FormBean;
|
||||
import com.seeyon.cap4.form.bean.FormFieldBean;
|
||||
import com.seeyon.cap4.form.bean.FormTableBean;
|
||||
import com.seeyon.ctp.common.AppContext;
|
||||
import com.seeyon.ctp.common.exceptions.BusinessException;
|
||||
import com.seeyon.ctp.util.JDBCAgent;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FormTableExecutor {
|
||||
|
||||
/* ========== 表定位 ========== */
|
||||
|
||||
private static final Log log = LogFactory.getLog(FormTableExecutor.class);
|
||||
|
||||
/* ========== 表定位 ========== */
|
||||
|
||||
public TableContext master(String formNo) throws BusinessException {
|
||||
FormBean form = getForm(formNo);
|
||||
return new TableContext(form.getMasterTableBean());
|
||||
}
|
||||
|
||||
public TableContext sub(String formNo, String subTable) throws BusinessException {
|
||||
FormBean form = getForm(formNo);
|
||||
return form.getSubTableBean().stream()
|
||||
.filter(t -> t.getTableName().equals(subTable) || t.getDisplay().equals(subTable))
|
||||
.findFirst()
|
||||
.map(TableContext::new)
|
||||
.orElseThrow(() -> new BusinessException("未找到子表:" + subTable));
|
||||
}
|
||||
|
||||
private FormBean getForm(String formNo) throws BusinessException {
|
||||
FormApi4Cap4 api = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
return api.getFormByFormCode(formNo);
|
||||
}
|
||||
|
||||
private void fillUpdateFields(TableContext ctx, List<FormUpdateField> updateFields) {
|
||||
if (updateFields == null) return;
|
||||
|
||||
for (FormUpdateField c : updateFields) {
|
||||
FormFieldBean field = ctx.getTableBean().getFieldBeanByDisplay(c.getDisplay());
|
||||
if (field != null) {
|
||||
c.setFieldName(field.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fillConditionFields(TableContext ctx, List<FormWhereCondition> conditions) {
|
||||
if (conditions == null) return;
|
||||
|
||||
for (FormWhereCondition c : conditions) {
|
||||
FormFieldBean field = ctx.getTableBean().getFieldBeanByDisplay(c.getDisplay());
|
||||
if (field != null) {
|
||||
c.setFieldName(field.getColumnName());
|
||||
} else if ("ID".equalsIgnoreCase(c.getDisplay())) {
|
||||
c.setFieldName("ID");
|
||||
} else if ("formmain_id".equalsIgnoreCase(c.getDisplay())) {
|
||||
c.setFieldName("formmain_id");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> resolveQueryColumns(TableContext ctx, List<String> displays) {
|
||||
if (displays == null) return Collections.emptyList();
|
||||
return displays.stream()
|
||||
.map(ctx.getTableBean()::getFieldBeanByDisplay)
|
||||
.filter(Objects::nonNull)
|
||||
.map(FormFieldBean::getColumnName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/* ========== 查询方法 ========== */
|
||||
|
||||
public List<FormColumn> query(TableContext ctx,
|
||||
List<String> queryDisplays,
|
||||
List<FormWhereCondition> conditions,
|
||||
boolean changeEnum) {
|
||||
|
||||
fillConditionFields(ctx, conditions);
|
||||
List<String> columns = resolveQueryColumns(ctx, queryDisplays);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.SELECT);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setQueryColumns(columns);
|
||||
param.setConditions(conditions);
|
||||
Map<String, Object> sqlMap = generateSql(param);
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
|
||||
try {
|
||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
List<Map<String, Object>> rows = agent.resultSetToList();
|
||||
|
||||
return rows.stream()
|
||||
.map(row -> buildFormColumn(row, ctx, changeEnum))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e) {
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public FormColumn queryOne(TableContext ctx,
|
||||
List<FormWhereCondition> conditions,
|
||||
boolean changeEnum) {
|
||||
|
||||
fillConditionFields(ctx, conditions);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.SELECT);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setConditions(conditions);
|
||||
Map<String, Object> sqlMap = generateSql(param);
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
|
||||
try {
|
||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
Map<String, Object> row = agent.resultSetToMap();
|
||||
if (row == null) return null;
|
||||
|
||||
return buildFormColumn(row, ctx, changeEnum);
|
||||
} catch (Exception e) {
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<FormColumn> pageQuery(TableContext ctx,
|
||||
List<String> displays,
|
||||
List<FormWhereCondition> conditions,
|
||||
int pageNo,
|
||||
int pageSize,
|
||||
boolean changeEnum) {
|
||||
|
||||
fillConditionFields(ctx, conditions);
|
||||
List<String> columns = resolveQueryColumns(ctx, displays);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.SELECT);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setQueryColumns(columns);
|
||||
param.setConditions(conditions);
|
||||
param.setPageNo(pageNo);
|
||||
param.setPageSize(pageSize);
|
||||
Map<String, Object> sqlMap = generateSql(param);;
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
|
||||
try {
|
||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
List<Map<String, Object>> rows = agent.resultSetToList();
|
||||
|
||||
return rows.stream()
|
||||
.map(r -> buildFormColumn(r, ctx, changeEnum))
|
||||
.collect(Collectors.toList());
|
||||
} catch (Exception e){
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
public long count(TableContext ctx,List<String> countField,List<FormWhereCondition> conditions) {
|
||||
fillConditionFields(ctx, conditions);
|
||||
List<String> countColumn = resolveQueryColumns(ctx,countField);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.COUNT);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setConditions(conditions);
|
||||
param.setCountField(countColumn.size() > 0 ? countColumn.get(0) : null);
|
||||
Map<String, Object> sqlMap = generateSql(param);
|
||||
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
return (Long) agent.resultSetToMap().get("countnum");
|
||||
} catch (Exception e) {
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
return 0L;
|
||||
} finally {
|
||||
agent.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int update(TableContext ctx, List<FormUpdateField> fields, List<FormWhereCondition> conditions) {
|
||||
if (fields == null || fields.isEmpty()) throw new IllegalArgumentException("更新字段不能为空");
|
||||
if (conditions == null || conditions.isEmpty()) throw new IllegalArgumentException("UPDATE必须带条件");
|
||||
fillConditionFields(ctx, conditions);
|
||||
fillUpdateFields(ctx,fields);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.UPDATE);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setConditions(conditions);
|
||||
param.setUpdateFields(fields);
|
||||
Map<String, Object> sqlMap = generateSql(param);
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
} catch (Exception e){
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
}finally {
|
||||
agent.close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int delete(TableContext ctx, List<FormWhereCondition> conditions) {
|
||||
if (conditions == null || conditions.isEmpty()) throw new IllegalArgumentException("DELETE必须带条件");
|
||||
fillConditionFields(ctx, conditions);
|
||||
SqlBuildParam param = new SqlBuildParam();
|
||||
param.setSqlType(SqlType.DELETE);
|
||||
param.setTableName(ctx.getTableName());
|
||||
param.setConditions(conditions);
|
||||
|
||||
Map<String, Object> sqlMap = generateSql(param);
|
||||
JDBCAgent agent = new JDBCAgent();
|
||||
try {
|
||||
return agent.execute((String) sqlMap.get("sql"), (List<Object>) sqlMap.get("params"));
|
||||
}catch (Exception e){
|
||||
log.error("执行sql为: " + sqlMap.get("sql"));
|
||||
log.error("执行sql参数为: " + JsonUtils.toJSONString(sqlMap.get("params")));
|
||||
log.error(e.getMessage(),e);
|
||||
}finally {
|
||||
agent.close();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* ========== 构建 FormColumn ========== */
|
||||
|
||||
private FormColumn buildFormColumn(Map<String, Object> row,
|
||||
TableContext ctx,
|
||||
boolean changeEnum) {
|
||||
FormColumn column = new FormColumn();
|
||||
try {
|
||||
List<FormFieldVo> vos = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Object> e : row.entrySet()) {
|
||||
FormFieldBean field = ctx.getFieldMap().get(e.getKey());
|
||||
if (field == null) continue;
|
||||
|
||||
Object value = e.getValue();
|
||||
if (changeEnum && field.isEnumField()) {
|
||||
value = EnumMapUtils.getEnumShowValue(value);
|
||||
}
|
||||
|
||||
// 枚举、部门、成员等特殊字段处理
|
||||
value = handleFieldSpecialType(field, value);
|
||||
|
||||
FormFieldVo vo = new FormFieldVo();
|
||||
vo.setDisplayName(field.getDisplay());
|
||||
vo.setValue(value);
|
||||
vos.add(vo);
|
||||
}
|
||||
|
||||
column.setVos(vos);
|
||||
|
||||
Object idVal = row.get("ID");
|
||||
if (idVal == null) idVal = row.get("id");
|
||||
if (idVal != null) column.setId(String.valueOf(idVal));
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("构建 FormColumn 失败", e);
|
||||
}
|
||||
return column;
|
||||
}
|
||||
|
||||
private Object handleFieldSpecialType(FormFieldBean field, Object value) throws BusinessException {
|
||||
if (value == null) return null;
|
||||
|
||||
switch (field.getInputType()) {
|
||||
case "select":
|
||||
if (field.getEnumId() != 0L) {
|
||||
return EnumMapUtils.getEnumItemValueByEnumId(String.valueOf(value), field.getEnumId());
|
||||
}
|
||||
break;
|
||||
case "department":
|
||||
case "account":
|
||||
case "member":
|
||||
case "radio":
|
||||
case "checkbox":
|
||||
case "date":
|
||||
case "text":
|
||||
case "image":
|
||||
case "attachment":
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean isMasterTableFile(String displayName, String formNo) {
|
||||
FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
|
||||
try {
|
||||
FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
|
||||
FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
|
||||
FormFieldBean fieldBeanByDisplay = masterTableBean.getFieldBeanByDisplay(displayName);
|
||||
return fieldBeanByDisplay != null &&
|
||||
("image".equals(fieldBeanByDisplay.getInputType()) ||
|
||||
"attachment".equals(fieldBeanByDisplay.getInputType()));
|
||||
} catch (BusinessException e) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private Map<String, Object> buildCountSql(
|
||||
String countField,
|
||||
String tableName,
|
||||
List<FormWhereCondition> conditions) {
|
||||
|
||||
StringBuilder sql = new StringBuilder("SELECT count(*) as countnum FROM ")
|
||||
.append(tableName);
|
||||
|
||||
List<Object> params = new ArrayList<>();
|
||||
sql.append(buildWhereClause(conditions, params));
|
||||
|
||||
return buildResult(sql, params);
|
||||
}
|
||||
|
||||
private Map<String, Object> buildSelectSql(
|
||||
String tableName,
|
||||
List<String> queryColumns,
|
||||
List<FormWhereCondition> conditions,
|
||||
Integer limit,
|
||||
Integer offset,
|
||||
String orderField) {
|
||||
|
||||
StringBuilder sql = new StringBuilder("SELECT ");
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
if (queryColumns == null || queryColumns.isEmpty()) {
|
||||
sql.append("*");
|
||||
} else {
|
||||
for (int i = 0; i < queryColumns.size(); i++) {
|
||||
sql.append(queryColumns.get(i));
|
||||
if (i < queryColumns.size() - 1) {
|
||||
sql.append(",");
|
||||
}
|
||||
}
|
||||
sql.append(",ID");
|
||||
}
|
||||
|
||||
sql.append(" FROM ").append(tableName);
|
||||
sql.append(buildWhereClause(conditions, params));
|
||||
if(orderField == null) {
|
||||
sql.append(" order by start_date desc");
|
||||
}else {
|
||||
sql.append(" order by "+ orderField +" desc");
|
||||
}
|
||||
if(limit != null && offset != null) {
|
||||
sql.append(" "+ offset + "," + limit);
|
||||
}
|
||||
sql.append(";");
|
||||
return buildResult(sql, params);
|
||||
}
|
||||
|
||||
private Map<String, Object> buildUpdateSql(
|
||||
String tableName,
|
||||
List<FormUpdateField> fields,
|
||||
List<FormWhereCondition> conditions) {
|
||||
|
||||
if (fields == null || fields.isEmpty()) {
|
||||
throw new IllegalArgumentException("Update fields cannot be empty");
|
||||
}
|
||||
if (conditions == null || conditions.isEmpty()) {
|
||||
throw new IllegalArgumentException("UPDATE must have WHERE conditions");
|
||||
}
|
||||
|
||||
StringBuilder sql = new StringBuilder("UPDATE ")
|
||||
.append(tableName)
|
||||
.append(" SET ");
|
||||
|
||||
List<Object> params = new ArrayList<>();
|
||||
int count = 0;
|
||||
|
||||
for (FormUpdateField f : fields) {
|
||||
if (f.getValue() == null || "".equals(f.getValue()) || "null".equals(f.getValue())) {
|
||||
continue;
|
||||
}
|
||||
if (count++ > 0) {
|
||||
sql.append(", ");
|
||||
}
|
||||
if (f.getValue() instanceof String && ((String) f.getValue()).startsWith("DATE>")) {
|
||||
sql.append(f.getFieldName()).append(" = TO_DATE(?, 'YYYY-MM-DD')");
|
||||
params.add(((String) f.getValue()).substring(5));
|
||||
} else {
|
||||
sql.append(f.getFieldName()).append(" = ?");
|
||||
params.add(f.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0) {
|
||||
throw new IllegalArgumentException("No valid update fields");
|
||||
}
|
||||
|
||||
sql.append(buildWhereClause(conditions, params));
|
||||
return buildResult(sql, params);
|
||||
}
|
||||
|
||||
private Map<String, Object> buildDeleteSql(
|
||||
String tableName,
|
||||
List<FormWhereCondition> conditions) {
|
||||
|
||||
if (conditions == null || conditions.isEmpty()) {
|
||||
throw new IllegalArgumentException("DELETE must have WHERE conditions");
|
||||
}
|
||||
|
||||
StringBuilder values = new StringBuilder();
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
StringBuilder sql = new StringBuilder("DELETE FROM ")
|
||||
.append(tableName);
|
||||
|
||||
sql.append(buildWhereClause(conditions, params));
|
||||
return buildResult(sql, params);
|
||||
}
|
||||
|
||||
private Map<String, Object> buildInsertSql(
|
||||
String tableName,
|
||||
Map<String, Object> data) {
|
||||
|
||||
if (data == null || data.isEmpty()) {
|
||||
throw new IllegalArgumentException("Insert data cannot be empty");
|
||||
}
|
||||
|
||||
StringBuilder columns = new StringBuilder();
|
||||
StringBuilder values = new StringBuilder();
|
||||
List<Object> params = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, Object> e : data.entrySet()) {
|
||||
if (columns.length() > 0) {
|
||||
columns.append(",");
|
||||
values.append(",");
|
||||
}
|
||||
columns.append(e.getKey());
|
||||
values.append("?");
|
||||
params.add(e.getValue());
|
||||
}
|
||||
|
||||
StringBuilder sql = new StringBuilder("INSERT INTO ")
|
||||
.append(tableName)
|
||||
.append(" (")
|
||||
.append(columns)
|
||||
.append(") VALUES (")
|
||||
.append(values)
|
||||
.append(")");
|
||||
|
||||
return buildResult(sql, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 动态生成 WHERE 子句
|
||||
* @param conditions 条件集合
|
||||
* @param params 参数列表(引用传递)
|
||||
* @return 拼接后的 WHERE 子句(包含 WHERE 关键字)
|
||||
*/
|
||||
private String buildWhereClause(List<FormWhereCondition> conditions, List<Object> params) {
|
||||
if (conditions == null || conditions.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
StringBuilder whereClause = new StringBuilder(" WHERE ");
|
||||
int conditionIndex = 0;
|
||||
|
||||
for (FormWhereCondition condition : conditions) {
|
||||
// 处理括号起始
|
||||
if (condition.isStartWithBracket()) {
|
||||
whereClause.append("(");
|
||||
}
|
||||
|
||||
// 字段名校验
|
||||
String fieldName = condition.getFieldName();
|
||||
ClauseFactor factor = condition.getClauseFactor();
|
||||
String operator = parseOperator(factor);
|
||||
|
||||
// 构建条件表达式
|
||||
String conditionExpr;
|
||||
if (factor != null && factor.isNullType()) {
|
||||
// 处理 NULL/NOT NULL 条件(无需参数)
|
||||
conditionExpr = String.format("%s %s", fieldName, operator);
|
||||
} else {
|
||||
// 处理普通条件(带占位符)
|
||||
conditionExpr = String.format("%s %s ?", fieldName, operator);
|
||||
// 处理函数模板(如 TO_DATE)
|
||||
if (condition.getIndex() != null) {
|
||||
conditionExpr = conditionExpr.replace("?", condition.getIndex());
|
||||
}
|
||||
// 添加参数值
|
||||
params.add(condition.getValue());
|
||||
}
|
||||
|
||||
whereClause.append(conditionExpr);
|
||||
|
||||
// 处理括号闭合
|
||||
if (condition.isEndWithBracket()) {
|
||||
whereClause.append(")");
|
||||
}
|
||||
|
||||
// 添加连接符(AND/OR)
|
||||
if (conditionIndex < conditions.size() - 1) {
|
||||
whereClause.append(" ").append(condition.getConcatFactor()).append(" ");
|
||||
}
|
||||
conditionIndex++;
|
||||
}
|
||||
|
||||
return whereClause.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析运算符映射(eq -> =, lt -> < 等)
|
||||
*/
|
||||
private String parseOperator(ClauseFactor factor) {
|
||||
if(factor == null) {
|
||||
return "=";
|
||||
}
|
||||
switch (factor) {
|
||||
case EQ: return "=";
|
||||
case GT: return ">";
|
||||
case GE: return ">=";
|
||||
case LT: return "<";
|
||||
case LE: return "<=";
|
||||
case LIKE: return "LIKE";
|
||||
case NULL: return "IS NULL"; // 空值判断
|
||||
case NOT_NULL: return "IS NOT NULL"; // 非空判断
|
||||
default: throw new UnsupportedOperationException("不支持的运算符: " + factor);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> buildResult(StringBuilder sql, List<Object> params) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sql", sql.toString());
|
||||
result.put("params", params);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> generateSql(SqlBuildParam param) {
|
||||
|
||||
if (param == null || param.getSqlType() == null) {
|
||||
throw new IllegalArgumentException("SqlBuildParam or SqlType cannot be null");
|
||||
}
|
||||
|
||||
switch (param.getSqlType()) {
|
||||
case COUNT:
|
||||
return buildCountSql(
|
||||
param.getCountField(),
|
||||
param.getTableName(),
|
||||
param.getConditions()
|
||||
);
|
||||
case SELECT:
|
||||
return buildSelectSql(
|
||||
param.getTableName(),
|
||||
param.getQueryColumns(),
|
||||
param.getConditions(),
|
||||
param.getLimit(),
|
||||
param.getOffset(),
|
||||
param.getOrderField()
|
||||
);
|
||||
case UPDATE:
|
||||
return buildUpdateSql(
|
||||
param.getTableName(),
|
||||
param.getUpdateFields(),
|
||||
param.getConditions()
|
||||
);
|
||||
case INSERT:
|
||||
return buildInsertSql(
|
||||
param.getTableName(),
|
||||
param.getInsertData()
|
||||
);
|
||||
case DELETE:
|
||||
return buildDeleteSql(
|
||||
param.getTableName(),
|
||||
param.getConditions()
|
||||
);
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unsupported SqlType: " + param.getSqlType());
|
||||
}
|
||||
}
|
||||
}
|
||||
114
src/main/java/com/seeyon/utils/form/SqlBuildParam.java
Normal file
114
src/main/java/com/seeyon/utils/form/SqlBuildParam.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SqlBuildParam {
|
||||
|
||||
private SqlType sqlType;
|
||||
// 公共
|
||||
private String tableName;
|
||||
// SELECT / COUNT
|
||||
private List<String> queryColumns;
|
||||
private List<FormWhereCondition> conditions;
|
||||
// UPDATE
|
||||
private List<FormUpdateField> updateFields;
|
||||
// INSERT
|
||||
private Map<String, Object> insertData;
|
||||
private Integer pageNo;
|
||||
private Integer pageSize;
|
||||
private String orderField;
|
||||
private String countField;
|
||||
|
||||
public SqlType getSqlType() {
|
||||
return sqlType;
|
||||
}
|
||||
|
||||
public void setSqlType(SqlType sqlType) {
|
||||
this.sqlType = sqlType;
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public List<String> getQueryColumns() {
|
||||
return queryColumns;
|
||||
}
|
||||
|
||||
public void setQueryColumns(List<String> queryColumns) {
|
||||
this.queryColumns = queryColumns;
|
||||
}
|
||||
|
||||
public List<FormWhereCondition> getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
public void setConditions(List<FormWhereCondition> conditions) {
|
||||
this.conditions = conditions;
|
||||
}
|
||||
|
||||
public List<FormUpdateField> getUpdateFields() {
|
||||
return updateFields;
|
||||
}
|
||||
|
||||
public void setUpdateFields(List<FormUpdateField> updateFields) {
|
||||
this.updateFields = updateFields;
|
||||
}
|
||||
|
||||
public Map<String, Object> getInsertData() {
|
||||
return insertData;
|
||||
}
|
||||
|
||||
public void setInsertData(Map<String, Object> insertData) {
|
||||
this.insertData = insertData;
|
||||
}
|
||||
|
||||
public Integer getPageNo() {
|
||||
return pageNo;
|
||||
}
|
||||
|
||||
public void setPageNo(Integer pageNo) {
|
||||
this.pageNo = pageNo;
|
||||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public String getOrderField() {
|
||||
return orderField;
|
||||
}
|
||||
|
||||
public void setOrderField(String orderField) {
|
||||
this.orderField = orderField;
|
||||
}
|
||||
public Integer getLimit() {
|
||||
if(this.pageSize == null) {
|
||||
return null;
|
||||
}
|
||||
return this.pageSize;
|
||||
}
|
||||
public Integer getOffset() {
|
||||
if(this.pageSize == null) {
|
||||
return null;
|
||||
}
|
||||
return (this.pageNo - 1) * this.pageSize;
|
||||
}
|
||||
|
||||
public String getCountField() {
|
||||
return countField;
|
||||
}
|
||||
|
||||
public void setCountField(String countField) {
|
||||
this.countField = countField;
|
||||
}
|
||||
}
|
||||
9
src/main/java/com/seeyon/utils/form/SqlType.java
Normal file
9
src/main/java/com/seeyon/utils/form/SqlType.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
public enum SqlType {
|
||||
SELECT,
|
||||
COUNT,
|
||||
UPDATE,
|
||||
INSERT,
|
||||
DELETE
|
||||
}
|
||||
23
src/main/java/com/seeyon/utils/form/TableContext.java
Normal file
23
src/main/java/com/seeyon/utils/form/TableContext.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.seeyon.utils.form;
|
||||
|
||||
import com.seeyon.cap4.form.bean.FormFieldBean;
|
||||
import com.seeyon.cap4.form.bean.FormTableBean;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class TableContext {
|
||||
|
||||
private final FormTableBean tableBean;
|
||||
private final String tableName;
|
||||
private final Map<String, FormFieldBean> fieldMap;
|
||||
|
||||
public TableContext(FormTableBean tableBean) {
|
||||
this.tableBean = tableBean;
|
||||
this.tableName = tableBean.getTableName();
|
||||
this.fieldMap = tableBean.getFieldMap4Name();
|
||||
}
|
||||
|
||||
public FormTableBean getTableBean() { return tableBean; }
|
||||
public String getTableName() { return tableName; }
|
||||
public Map<String, FormFieldBean> getFieldMap() { return fieldMap; }
|
||||
}
|
||||
Reference in New Issue
Block a user