package cn.autoform.web.service.datamanagement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestBody; import com.alibaba.fastjson.TypeReference; import cn.autoform.bean.BudgetData; import cn.autoform.bean.BudgetTitle; import cn.autoform.bean.FormSolidiFication; import cn.autoform.bean.ProcessBudget; import cn.autoform.bean.ReleData; import cn.autoform.bean.TableName; import cn.autoform.bean.form.AssociatedFormData; import cn.autoform.bean.form.CpnType; import cn.autoform.bean.form.JoinFormData; import cn.autoform.bean.form.RequestFormBase; import cn.autoform.bean.page.PageResult; import cn.autoform.db.entity.FieldPropertyEntity; import cn.autoform.db.entity.FormDataMangerEntity; import cn.autoform.db.entity.FormFieldEntity; import cn.autoform.db.exten.ServicesFormData; import cn.autoform.db.exten.SelectForm; import cn.autoform.db.exten.SubForm; import cn.autoform.factory.FormFactory; import cn.autoform.fw.exception.FormClientException; import cn.autoform.fw.exception.RestResult; import cn.autoform.fw.utility.ConstMap; import cn.autoform.fw.utility.FormDataUtil; import cn.autoform.util.tool.JSONTool; import cn.autoform.util.tool.RequestContext; import cn.autoform.web.client.FormClient; import cn.autoform.web.client.util.auto.OnlyCharacters; import cn.autoform.web.client.util.auto.ParamFactory; import cn.autoform.web.mapper.datamanagement.DataManagementMapper; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Service public class DataManagementService { private static final Map DBTYPE = new HashMap<>(); static { // 单行文本 DBTYPE.put("text", ConstMap.VARCHAR150); // 单选按钮 DBTYPE.put("radio", ConstMap.VARCHAR50); // 下拉列表框 DBTYPE.put("dropdownlist", ConstMap.LONGTEXT); // 日期控件 DBTYPE.put("calendar", ConstMap.VARCHAR30); // 复选框 DBTYPE.put("checkbox", ConstMap.LONGTEXT); // 子表单 DBTYPE.put("subform", ConstMap.VARCHAR30); // 数字 DBTYPE.put("number", ConstMap.VARCHAR30); // 多行文本 DBTYPE.put("textarea", ConstMap.LONGTEXT); // HTML DBTYPE.put("htmlediter", ConstMap.LONGTEXT); // 条码 DBTYPE.put("barcode", ConstMap.VARCHAR30); // 二维码 DBTYPE.put("qrcode", ConstMap.VARCHAR200); // 图片 DBTYPE.put("image", ConstMap.LONGTEXT); // 文件上传 DBTYPE.put("fileupload", ConstMap.LONGTEXT); // 数型控件 DBTYPE.put("tree", ConstMap.LONGTEXT); // 金额组件 DBTYPE.put("amount", ConstMap.LONGTEXT); } @Autowired private DataManagementMapper dataManagementMapper = null; @Autowired private FormClient formClient = null; // @Autowired // private FormFactory formFactory; public List queryFormFields(String formID, String tenantID) { return dataManagementMapper.queryFormFields(formID, tenantID); } public List getFormFieldMain(String formID, String tenantID) { return dataManagementMapper.getFormFieldMain(formID, tenantID); } public Map queryFormFieldsAboutData(String formID, String tenantID, Integer columnOrderNum) { return dataManagementMapper.queryFormFieldsAboutData(formID, tenantID, columnOrderNum); } public Map queryFormFields2(String formID, String tenantID) { Map info = dataManagementMapper.queryFormFields2(formID, tenantID); // 过滤选项卡。 info.entrySet().stream().forEach(entry->{ if(Objects.equals(entry.getValue().getFieldtype(), CpnType.subform.toString())) { entry.getValue().setParentsubFormNum(0); } }); return info; } public Map queryFormdataManagers2(String formID, String tenantID) { return dataManagementMapper.queryFormdataManagers2(formID, tenantID); } public void excuteFormDataInsert(String excuteSql) { dataManagementMapper.excuteFormDataInsert(excuteSql); } public FormDataMangerEntity queryFormdataManagersFor(String formID, String tenantID) { return dataManagementMapper.queryFormdataManagersFor(formID, tenantID); } public void updateFormdataManagersFor(FormDataMangerEntity entity) { dataManagementMapper.updateFormdataManagersFor(entity); } //根据formID查询基本表的表名 public String getBaseTableName(String formID, String tenantID){ return dataManagementMapper.getBaseTableName(formID, tenantID); } public List queryCloudMainValue(String excuteSql){ return dataManagementMapper.queryCloudMainValue(excuteSql); } public List queryServicesMainValue(String sql){ return null; } //查询控件属性 public List queryFormFieldProperty(String formID, String tenantID, Integer columnOrderNum){ return dataManagementMapper.queryFormFieldProperty(formID, tenantID, columnOrderNum); } public List queryFormFieldProperty(String formID, String tenantID, String field){ return dataManagementMapper.queryFormFieldPropertyByField(formID, tenantID, field); } //查询表中下拉框,等多选主数据相关控件 public List queryFormMainField(String formID, String tenantID){ return dataManagementMapper.queryFormMainField(formID, tenantID); } public List queryFormSubField(String formID, String tenantID){ return dataManagementMapper.queryFormSubField(formID, tenantID); } public List queryFormAllField(String formID, String tenantID){ return dataManagementMapper.queryFormAllField(formID, tenantID); } public SelectForm queryFormDataList(String formID, String tenantID) { SelectForm mainForm = new SelectForm(); mainForm.setTenantID(tenantID); // 取得表单数据管理 List formDataMangerList = dataManagementMapper.queryFormdataMangers(formID, tenantID); if (formDataMangerList == null || formDataMangerList.isEmpty()) { return null; } List formFieldsList = dataManagementMapper.queryFormFields3(formID, tenantID); StringBuilder sqlSelectMain = new StringBuilder( "/*!mycat:sql= select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ SELECT %s FROM "); for (FormDataMangerEntity formDataMangerEntity : formDataMangerList) { if (formDataMangerEntity.getFormdataNum() == 0) { sqlSelectMain.append(formDataMangerEntity.getFormdatatable()); mainForm.setFormName("_" + tenantID + "_" + formDataMangerEntity.getFormdatatable().substring(8)); } } StringBuilder sqlSelcetFields = new StringBuilder(); sqlSelcetFields.append(" DATAROWNUM,"); Map fields = new HashMap<>(); for (FormFieldEntity formFieldEntity : formFieldsList) { if (formFieldEntity.getParentsubFormNum() == 0 && DBTYPE.containsKey(formFieldEntity.getFieldtype())) { if (!ConstMap.SUBFORM.equals(formFieldEntity.getFieldtype())) { String fieldName = ConstMap.FIELD + formFieldEntity.getColumnOrderNum(); sqlSelcetFields.append(" IFNULL("); sqlSelcetFields.append(fieldName); sqlSelcetFields.append(" , '') AS '"); sqlSelcetFields.append(fieldName); sqlSelcetFields.append("',"); fields.put(fieldName,fieldName); } else { sqlSelcetFields.append(formFieldEntity.getColumnOrderNum()); sqlSelcetFields.append(" AS"); sqlSelcetFields.append(" FIELD"); sqlSelcetFields.append(formFieldEntity.getColumnOrderNum()); sqlSelcetFields.append(","); //fields.put(formFieldEntity.getColumnOrderNum().toString(), " FIELD" + formFieldEntity.getColumnOrderNum()); } } } sqlSelcetFields.deleteCharAt(sqlSelcetFields.length() - 1); StringBuilder whereSql = new StringBuilder(" WHERE "); whereSql.append(" deleteFlg = '0'"); whereSql.append(" AND TENANTID = '"); whereSql.append(tenantID); whereSql.append("'"); sqlSelectMain.append(whereSql); mainForm.setFields(fields); return mainForm; //dataManagementMapper.queryFormdatas(executeSql); } public Map queryFormDataList(String formID, String tenantID, Integer datarowNum) { SelectForm selectForm = new SelectForm(); selectForm.setDataRowNum(datarowNum); selectForm.setTenantID(tenantID); // 取得表单数据管理 List formDataMangerList = dataManagementMapper.queryFormdataMangers(formID, tenantID); List formFieldsList = dataManagementMapper.queryFormFields3(formID, tenantID); StringBuilder sqlSelectMain = new StringBuilder( "/*!mycat:sql= select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ SELECT %s FROM "); for (FormDataMangerEntity formDataMangerEntity : formDataMangerList) { if (formDataMangerEntity.getFormdataNum() == 0) { sqlSelectMain.append(formDataMangerEntity.getFormdatatable()); selectForm.setFormName("_" + tenantID + "_" + formDataMangerEntity.getFormdatatable().substring(8)); } } Map selectMap = new HashMap<>(); StringBuilder sqlSelcetFields = new StringBuilder(); sqlSelcetFields.append(" DATAROWNUM,"); for (FormFieldEntity formFieldEntity : formFieldsList) { if (formFieldEntity.getParentsubFormNum() == 0 && DBTYPE.containsKey(formFieldEntity.getFieldtype())) { if (!ConstMap.SUBFORM.equals(formFieldEntity.getFieldtype())) { String fieldName = ConstMap.FIELD + formFieldEntity.getColumnOrderNum(); sqlSelcetFields.append(" IFNULL("); sqlSelcetFields.append(fieldName); sqlSelcetFields.append(" , '') AS "); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append(","); selectMap.put(fieldName,formFieldEntity.getFieldKey()); } else { sqlSelcetFields.append("'"); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append("'"); sqlSelcetFields.append(" AS "); sqlSelcetFields.append(ConstMap.FIELD + formFieldEntity.getColumnOrderNum()); sqlSelcetFields.append(","); selectMap.put(String.format("'%s'", formFieldEntity.getFieldKey()), ConstMap.FIELD + formFieldEntity.getColumnOrderNum()); } } } selectForm.setFields(selectMap); // sqlSelcetFields.deleteCharAt(sqlSelcetFields.length() - 1); // // StringBuilder whereSql = new StringBuilder(" WHERE "); // whereSql.append(" deleteFlg = '0'"); // whereSql.append(" AND TENANTID = '"); // whereSql.append(tenantID); // whereSql.append("'"); // // whereSql.append(" AND SUBDATAROWNUM = 0 AND "); // whereSql.append(" DATAROWNUM = "); // whereSql.append(datarowNum); // sqlSelectMain.append(whereSql); // String executeSql = String.format(sqlSelectMain.toString(), tenantID, formID, sqlSelcetFields.toString()); Map data = new HashMap<>(); // data.put("main", dataManagementMapper.queryFormdatas(executeSql).get(0)); data.put("selectForm", selectForm); return data; } /** * 删除表单数据 * * @param formID 表单ID * @param tenantID 租户ID * @param datarowNum 数据行番 */ public ServicesFormData removeFormData(String formID, String tenantID, JSONArray datarowNumArray) { List formDataMangers = dataManagementMapper.queryFormdataMangers(formID, tenantID); ServicesFormData formData = new ServicesFormData(); SubForm subForm = null; List subFormList = new ArrayList(); for(int i= 0; i< formDataMangers.size(); i++) { subForm = new SubForm(); if(i == 0) { formData.setFormID("_" + tenantID + "_" + formDataMangers.get(i).getFormdatatable().substring(8)); }else{ subForm.setFormID("_" + tenantID + "_" + formDataMangers.get(i).getFormdatatable().substring(8)); subFormList.add(subForm); } formDataMangers.get(i).getFormdatatable(); } formData.setSubFormList(subFormList); StringBuilder updateSql = new StringBuilder(); updateSql.append( "/*!mycat:sql= select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ UPDATE "); updateSql.append(" SET DELETEFLG = '1' WHERE "); updateSql.append(" TENANTID = '%s' AND DATAROWNUM IN ("); for (Object dataRowNum : datarowNumArray) { updateSql.append(dataRowNum); updateSql.append(ConstMap.COMMA); } updateSql.deleteCharAt(updateSql.length() - 1); updateSql.append(")"); return formData; } /** * 删除表单数据 * * @param sql */ public void removeFormData(String sql) { dataManagementMapper.removeFormData(sql); } public void updateFormData(String updateSql) { dataManagementMapper.updateFormData(updateSql); } public String getTitleProperty(String formID, String tenantID, Integer columnOrderNum){ List file = queryFormFieldProperty(formID, tenantID, columnOrderNum); String field = null; try{ field = file .stream() .filter(fieldP -> "title".equals(fieldP.getProperty())) .collect(Collectors.toList()) .get(0) .getValue(); }catch(Exception e){ return ""; } return field; } public List> querySubFormDataList(String formID, String tenantID, Integer formdataNum, Integer datarowNum) { SubForm subForm = new SubForm(); subForm.setDataRowNum(datarowNum); subForm.setSubDataRowNum(datarowNum); subForm.setFormDataNum(formdataNum); // 取得表单数据管理 List formDataMangerList = dataManagementMapper.queryFormdataMangers1(formID, tenantID, formdataNum); if (formDataMangerList.isEmpty()) { return null; } List formFieldsList = dataManagementMapper.queryFormFields3(formID, tenantID); StringBuilder sqlSelectMain = new StringBuilder( "/*!mycat:sql= select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ SELECT %s FROM "); for (FormDataMangerEntity formDataMangerEntity : formDataMangerList) { if (formDataMangerEntity.getFormdataNum() == formdataNum) { subForm.setFormName("formdata" + formDataMangerEntity.getFormdatatable().substring(8)); sqlSelectMain.append(formDataMangerEntity.getFormdatatable()); } } StringBuilder sqlSelcetFields = new StringBuilder(); sqlSelcetFields.append(" DATAROWNUM,"); Map fieldMap = new HashMap<>(); for (FormFieldEntity formFieldEntity : formFieldsList) { if (formFieldEntity.getParentsubFormNum() == formdataNum && DBTYPE.containsKey(formFieldEntity.getFieldtype())) { if (!ConstMap.SUBFORM.equals(formFieldEntity.getFieldtype())) { String fieldName = ConstMap.FIELD + formFieldEntity.getColumnOrderNum(); sqlSelcetFields.append(" IFNULL("); sqlSelcetFields.append(fieldName); sqlSelcetFields.append(" , '') AS "); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append(","); fieldMap.put(formFieldEntity.getFieldKey(), formFieldEntity.getFieldKey()); } else { sqlSelcetFields.append(formFieldEntity.getColumnOrderNum()); sqlSelcetFields.append(" AS "); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append(","); } } } subForm.setFields(fieldMap); sqlSelcetFields.deleteCharAt(sqlSelcetFields.length() - 1); StringBuilder whereSql = new StringBuilder(" WHERE "); whereSql.append(" deleteFlg = '0'"); whereSql.append(" AND DATAROWNUM = "); whereSql.append(datarowNum); whereSql.append(" AND TENANTID = '"); whereSql.append(tenantID); whereSql.append("'"); // sqlSelectMain.append(sqlSelcetFields); sqlSelectMain.append(whereSql); List> lis = new ArrayList<>(); lis.add(new ModelMap("subForm",subForm)); return lis; } public List> queryCloudSubFormDataList(String formID, String tenantID, Integer formdataNum, Integer datarowNum) { // 取得表单数据管理 List formDataMangerList = dataManagementMapper.queryFormdataMangers1(formID, tenantID, formdataNum); if (formDataMangerList.isEmpty()) { return null; } List formFieldsList = dataManagementMapper.queryFormFields3(formID, tenantID); StringBuilder sqlSelectMain = new StringBuilder( "/*!mycat:sql= select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ SELECT %s FROM "); for (FormDataMangerEntity formDataMangerEntity : formDataMangerList) { if (formDataMangerEntity.getFormdataNum() == formdataNum) { sqlSelectMain.append(formDataMangerEntity.getFormdatatable()); } } StringBuilder sqlSelcetFields = new StringBuilder(); sqlSelcetFields.append(" DATAROWNUM,"); for (FormFieldEntity formFieldEntity : formFieldsList) { if (formFieldEntity.getParentsubFormNum() == formdataNum && DBTYPE.containsKey(formFieldEntity.getFieldtype())) { if (!ConstMap.SUBFORM.equals(formFieldEntity.getFieldtype())) { // String fieldName = ConstMap.FIELD + formFieldEntity.getColumnOrderNum(); sqlSelcetFields.append(" IFNULL("); // sqlSelcetFields.append(fieldName); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append(" , '') AS '"); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append("',"); } else { sqlSelcetFields.append(formFieldEntity.getColumnOrderNum()); sqlSelcetFields.append(" AS '"); sqlSelcetFields.append(formFieldEntity.getFieldKey()); sqlSelcetFields.append("',"); } } } sqlSelcetFields.deleteCharAt(sqlSelcetFields.length() - 1); StringBuilder whereSql = new StringBuilder(" WHERE "); whereSql.append(" deleteFlg = '0'"); whereSql.append(" AND PARENTDATAROWNUM = "); whereSql.append(datarowNum); whereSql.append(" AND TENANTID = '"); whereSql.append(tenantID); whereSql.append("'"); sqlSelectMain.append(whereSql); String executeSql = String.format(sqlSelectMain.toString(), tenantID, formID, sqlSelcetFields.toString()); return dataManagementMapper.queryFormdatas(executeSql); } public void updateFormFieldState(FormFieldEntity formFieldentity) { dataManagementMapper.updateFormFieldState(formFieldentity); } public void updateMainFormFieldService(FormFieldEntity formFieldentity) { dataManagementMapper.updateMainFormFieldService(formFieldentity); } public List> queryCodeAutoAttr(String formID, String tenantID,String columnordernum){ return dataManagementMapper.queryCodeAutoAttr(formID, tenantID,columnordernum); } public List> queryAutoAttr(String formID, String tenantID){ return dataManagementMapper.queryAutoAttr(formID, tenantID); } public Integer addFormStateData(Map formState){ return dataManagementMapper.addFormStateData(formState); } /** * 根据别名查询表单中对应控件属性 * @param formID * @param tenantID * @param fieldKey * @return */ public FormFieldEntity queryFormFieldPropertyByFieldKey(String formID, String tenantID, String fieldKey){ return dataManagementMapper.queryFormFieldPropertyByFieldKey(formID, tenantID, fieldKey); } /** * 得到自动生成number * @param formID * @param tenantID * @param field * @param dataRowNum * @return */ public String getAutoNumber(String formID, String tenantID, String field ,Integer dataRowNum){ StringBuilder prfix = new StringBuilder(); Map param = new HashMap<>(); queryFormFieldProperty(formID, tenantID, field) .stream().filter(formField -> "prefixs".equals(formField.getProperty()) || "autoTypeNumber".equals(formField.getProperty()) ||"autoTypePrefix".equals(formField.getProperty())) .forEach(formField -> { if("prefixs".equals(formField.getProperty())){ List> prefixs = JSONTool.toObj(formField.getValue(),new TypeReference>>(){}); prfix.append(prefixs.get(0).get("itemtext_1") + "$in{" + prefixs.get(0).get("itemtext_2") +"}"); }else{ param.put(formField.getProperty(), formField.getValue()); } }); if(!param.isEmpty()){ prfix.append( param.get("autoTypePrefix")+ "$in{" + param.get("autoTypeNumber") +"}"); } ParamFactory.setBegain(dataRowNum.toString()); return OnlyCharacters.getOnlyStr(prfix.toString()); } /** * 获得关联函数对应数据 * @param associatedFormData * @return * @throws TimeoutException * @throws FormClientException */ public Map getAssociateData(BudgetData budgetData) throws TimeoutException, FormClientException{ // associatedFormData.setPageNum(RequestContext.getPageNum()); // associatedFormData.setPageSize(RequestContext.getPageSize()); return this.formClient.getAssociateData(budgetData); } /** * 获得关联函数标题 * @param key * @return * @throws TimeoutException * @throws FormClientException */ public List getAssociateTitle(Map key) throws TimeoutException, FormClientException{ return this.formClient.getAssociateTitle(key); } /** * 获得表单上下文 * @param formID * @param tenantID * @return */ public Map getFormContext(String formID, String tenantID){ return this.dataManagementMapper.getFormContext(formID, tenantID); } /** * 获得表单数据上下文 * @param formID * @param tenantID * @return */ public Map getFormDataContext(RequestFormBase requestFormBase){ String formName = FormDataUtil.getFormTableName(requestFormBase.getTenantID(), requestFormBase.getFormID()); return this.dataManagementMapper.getFormDataContext(requestFormBase.getFormID(),requestFormBase.getTenantID() ,requestFormBase.getDatarowNum(), formName); } public Object getBudget(ProcessBudget processBudget) throws TimeoutException, FormClientException{ return this.formClient.getBudget(processBudget); } /** * 查询控件别名 * @param formID * @param tenantID * @return */ public List> queryFormFiled(String formID,String tenantID){ return dataManagementMapper.queryFormFiled(formID, tenantID); } /** * 表单固化准备参数 * @throws TimeoutException */ public FormSolidiFication formSolidiFication(String formId,Integer dataRowNum,String tenantId) throws TimeoutException{ List formDataMangerList = dataManagementMapper.queryFormdataMangers(formId, tenantId); FormSolidiFication fs = new FormSolidiFication(); fs.setFormId(formId); fs.setTenantId(tenantId); fs.setDataRowNum(dataRowNum); for(FormDataMangerEntity f: formDataMangerList){ if(f.getFormdataNum()!=0){ fs.getTableList().add(new TableName(f.getFormdataNum(),f.getFormdatatable())); } } return fs; } }