package cn.autoform.factory.product.impl; import cn.autoform.bean.CodeAndValueBean; import cn.autoform.bean.FormTable; import cn.autoform.bean.form.autowhere.ScreenCondition; import cn.autoform.bean.page.PageResult; import cn.autoform.db.entity.*; import cn.autoform.db.entity.form.FormCpnAttr; import cn.autoform.db.exten.*; import cn.autoform.factory.FormFactory; import cn.autoform.factory.product.ProductMethod; import cn.autoform.fw.exception.DuplicateFormNameException; import cn.autoform.fw.exception.FormClientException; import cn.autoform.fw.exception.RowOverLengthException; import cn.autoform.fw.utility.ConstMap; import cn.autoform.fw.utility.FormDataUtil; import cn.autoform.util.form.AutoConditionUtil; import cn.autoform.util.tool.*; import cn.autoform.web.client.CscApiService; import cn.autoform.web.client.FormClient; import cn.autoform.web.client.MasterClient; import cn.autoform.web.client.WorkflowClient; import cn.autoform.web.client.util.FormatParsingTool; import cn.autoform.web.client.util.WorkflowTool; import cn.autoform.web.mapper.datamanagement.DataManagementMapper; import cn.autoform.web.mapper.formTable.FormTableMapper; import cn.autoform.web.service.datamanagement.DataManagementService; import cn.autoform.web.service.formbase.FormBaseService; import cn.autoform.web.service.process.ProcessInstanceService; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; import lombok.Setter; 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 org.springframework.ui.ModelMap; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.TimeoutException; import java.util.stream.Stream; @Component @Slf4j public class ServicesProductMethod implements ProductMethod, ColumnConst { @Autowired @Setter private FormBaseService formBaseService = null; @Autowired private FormClient formClient; @Autowired private MasterClient masterClient; @Resource DataManagementMapper dataManagementMapper; @Autowired private FormFactory formFactory; @Autowired private CodeToNameTool codeToNameTool; @Autowired private DataManagementService dataManagementService = null; @Resource private FormTableMapper formTable; @Autowired private CscApiService cscApi; @Autowired private ProcessInstanceService processInstance; @Autowired private WorkflowClient workflowClient; @Autowired private WorkflowTool workflowTool; private static final Map DBTYPE = new HashMap<>(); private static final Map DBTYPELENGTH = 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); //自定义控件 DBTYPE.put("mytext",ConstMap.LONGTEXT); //自动生成编号控件 DBTYPE.put("auto",ConstMap.VARCHAR50); // 单行文本 DBTYPELENGTH.put("text", 150); // 单选按钮 DBTYPELENGTH.put("radio", 50); // 日期控件 DBTYPELENGTH.put("calendar", 30); // 复选框 // 子表单 DBTYPELENGTH.put("subform", 30); // 数字 DBTYPELENGTH.put("number", 30); // 条码 DBTYPELENGTH.put("barcode", 30); // 二维码 DBTYPELENGTH.put("qrcode", 200); } @Override public String createForm(FormBaseEntity formBase, JSONObject jsonObj) throws Exception { System.out.println("formBase:"+JSONTool.toJson(formBase)); System.out.println("jsonObj:"+jsonObj); String userName = jsonObj.getString("userName"); formBase.setCreateUser(userName); if(formTable.getFormTable(formBase.getFormID())==null){ formBaseService.addFormBase(formBase); }else{ int i = formBaseService.updateFormBaseInfo(formBase); System.out.println("更新了:"+i); } formBase.setFieldset(AutoConditionUtil.filterOption(formBase.getFieldset())); //过滤选项卡 JSONObject filedset = JSONObject.fromObject(formBase.getFieldset()); List fieldtypeList = new ArrayList<>(); //这是不被删除得子表单集合 Map> subFormObjcloumn = new HashMap<>(); Map> subFormObjFieldtype = new HashMap<>(); //更新后字段别名 List aliasValues = new ArrayList<>(); //更新后字段别名及类型 例:{子表单=subForm , ...} Map aliasValuesMap = new HashMap<>(); //Map<子表单号,该子表单字段别名> Map> aliasSubValues = new HashMap<>(); //<别名,类型> Map> aliasSubValuesMap = new HashMap<>(); //子表单号 List subFormNums = new ArrayList<>(); //Map<子表单号,子表单别名> Map subFormNames = new HashMap<>(); FormFieldEntity formFieldEntity = new FormFieldEntity(); // 表单ID formFieldEntity.setFormID(formBase.getFormID()); // 租户ID formFieldEntity.setTenantID(formBase.getTenantID()); // 控件状态 true formFieldEntity.setFieldIsShow("true"); // 控件状态 true formFieldEntity.setSearchIsShow("true"); // 作者 formFieldEntity.setCreateUser(userName); // 处理主表 getMainStream(filedset) .forEach(main->{ String columnOrderNum = Objects.toString(main.get(ConstMap.COLUMNORDERNUMKEY)); String alia = Objects.toString(Objects.toString(((Map)main.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS))); String type = Objects.toString(main.get(ConstMap.TAGTYPEKEY)); fieldtypeList.add(type); aliasValues.add(alia); aliasValuesMap.put(alia, type); subFormNums.add(Integer.valueOf(columnOrderNum)); // 添加字段信息到数据库 addFormField(formFieldEntity, main); }); // 处理子表 getChildStream(filedset) .forEach(child->{ String columnOrderNum = Objects.toString(child.get(ConstMap.COLUMNORDERNUMKEY)); String childAlia = Objects.toString(((Map)child.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS)); List alias = new ArrayList<>(); List types = new ArrayList<>(); Map values = new HashMap<>(); getChildStream(filedset, columnOrderNum) .forEach(childFields->{ String alia = Objects.toString(((Map)childFields.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS)); String type = Objects.toString(childFields.get(ConstMap.TAGTYPEKEY)); alias.add(alia); types.add(type); values.put(alia, type); // 添加字段信息到数据库 addFormField(formFieldEntity, childFields); }); subFormObjcloumn.put(columnOrderNum, alias); subFormObjFieldtype.put(columnOrderNum, types); aliasSubValues.put(Integer.valueOf(columnOrderNum), alias); aliasSubValuesMap.put(columnOrderNum, values); subFormNames.put(Integer.valueOf(columnOrderNum), childAlia); }); insertFormdataManager( fieldtypeList, subFormObjcloumn, subFormObjFieldtype, formBase.getFormID(), formBase.getTenantID(), userName, aliasValues, aliasSubValues, aliasValuesMap,aliasSubValuesMap, subFormNames); return "nice"; } /** * 表单数据管理表插入操作 * * @param fieldtypeList * @param subFormObjcloumn * @param subFormObjFieldtype * @param tenantID * @throws Exception */ private void insertFormdataManager(List fieldtypeList, Map> subFormObjcloumn, Map> subFormObjFieldtype, String formID, String tenantID, String userName, List aliasValues,Map> aliasSubValues, Map aliasValuesMap, Map> aliasSubValuesMap, Map subFormNames) throws Exception { TableStructure tableStructure = null; SubTableStructure subTableStructure = null; FormdataManagerEntity formdataManagerEntity = new FormdataManagerEntity(); formdataManagerEntity.setFormID(formID); formdataManagerEntity.setTenantID(tenantID); // 非子表单 formdataManagerEntity.setSubFormFlg("0"); formdataManagerEntity.setFormdataNum(0); // 查询主表单字段是否创建数据管理表 FormdataManagerEntity formdataManagerEntityRs = formBaseService.queryFormdataManagerNum(formdataManagerEntity); if (formdataManagerEntityRs == null) { // 数据表名 StringBuilder sbTableName = new StringBuilder(); sbTableName.append("formdata").append(FormDataUtil.getFormTableName(tenantID, formID)); String tableName = sbTableName.toString(); if (formBaseService.querySubFormName(tableName) != 0) { throw new DuplicateFormNameException(); } formdataManagerEntity.setFormdatatable(tableName); formdataManagerEntity.setCreateUser(userName); formBaseService.addFormdataManager(formdataManagerEntity); // 将表明、字段装入实体中。 tableStructure =new TableStructure(); tableStructure.setTableName(tableName); Map sqlData = createTable(tableName, fieldtypeList, aliasValues); tableStructure.setFields(sqlData); tableStructure.setTenantId(tenantID); tableStructure.setMapperID(formID); tableStructure.setFormId(formID); tableStructure.setCreatUser(userName); tableStructure.setFormDataNum(0); tableStructure.setFlag("create"); } else { tableStructure =new TableStructure(); tableStructure.setFlag("update"); tableStructure.setTenantId(tenantID); tableStructure.setMapperID(formID); tableStructure.setFormId(formID); tableStructure.setAliasValuesMap(aliasValuesMap); tableStructure.setAliasValues(aliasValues); tableStructure.setFieldtypeList(fieldtypeList); tableStructure.setTableName(formdataManagerEntityRs.getFormdatatable()); } // 删除画面侧删除的子表单表 FormdataManagerEntity formdataManagerEntitySubTemp = new FormdataManagerEntity(); formdataManagerEntitySubTemp.setFormID(formID); formdataManagerEntitySubTemp.setTenantID(tenantID); // 非子表单 formdataManagerEntitySubTemp.setSubFormFlg("1"); // 查询子表单字段是否创建数据管理表 System.out.println("查询子表单是否创建数据表参数:"+JSONTool.toJson(formdataManagerEntitySubTemp)); Map formdataManagerEntityRsSubMap = formBaseService .queryFormdataManagerNum2(formdataManagerEntitySubTemp); System.out.println("查询子表单是否创建数据表返回结果:"+JSONTool.toJson(formdataManagerEntityRsSubMap)); // 子表单插入 System.out.println("所有表单控件:"+JSONTool.toJson(subFormObjcloumn)); Iterator it = subFormObjcloumn.keySet().iterator(); List subformL = new ArrayList<>(); List subTableNames = new ArrayList<>(); while (it.hasNext()) { subTableStructure =new SubTableStructure(); subTableStructure.setSubFormObjcloumn(subFormObjcloumn); subTableStructure.setSubFormObjFieldtype(subFormObjFieldtype); String currentKey = it.next(); FormdataManagerEntity formdataManagerEntitySub = new FormdataManagerEntity(); formdataManagerEntitySub.setFormID(formID); formdataManagerEntitySub.setTenantID(tenantID); // 子表单标识 formdataManagerEntitySub.setSubFormFlg("1"); formdataManagerEntitySub.setFormdataNum(Integer.valueOf(currentKey)); // 数据表名 // StringBuilder subTableNameSub = new StringBuilder(); // subTableNameSub.append("formdata") // .append(FormDataUtil.getSubFormTableName(tenantID, formID, subFormNames.get(Integer.valueOf(currentKey)))); String tableNameSub = "formdata" + FormDataUtil.getSubFormTableName(tenantID, formID, subFormNames.get(Integer.valueOf(currentKey))); formdataManagerEntitySub.setFormdatatable(tableNameSub); // System.out.println("不被删除得子表单:"+tableNameSub); subTableNames.add(tableNameSub); // Map<表名, formDataNum> Map subFormNameAndFormDataNum = new HashMap<>(); subFormNameAndFormDataNum.put(tableNameSub, Integer.valueOf(currentKey)); // Map<表名, Map<字段名,类型>> Map> subFormNameAndColumnsMap = new HashMap<>(); subFormNameAndColumnsMap.put(tableNameSub, aliasSubValuesMap.get(currentKey)); // Map<表名, List<字段名>> Map> subFormNameColumns = new HashMap<>(); subFormNameColumns.put(tableNameSub, aliasSubValues.get(currentKey)); // Map<表名,List<类型>> Map> subformNameAndType = new HashMap<>(); subformNameAndType.put(tableNameSub, subFormObjFieldtype.get(currentKey)); subTableStructure.setSubFormNameAndColumnsMap(subFormNameAndColumnsMap); subTableStructure.setSubFormNameColumns(subFormNameColumns); subTableStructure.setSubformNameAndType(subformNameAndType); subTableStructure.setSubFormNameAndFormDataNum(subFormNameAndFormDataNum); System.out.println("查询是否创建表参数:"+JSONTool.toJson(formdataManagerEntitySub)); // 查询子表单字段是否创建数据管理表 FormdataManagerEntity formdataManagerEntityRsSub = formBaseService .queryFormdataManager(formdataManagerEntitySub); System.out.println("查询是否创建表单返回结果:"+JSONTool.toJson(formdataManagerEntityRsSub)); if (formdataManagerEntityRsSub == null) { System.out.println("子表单那表明:"+tableNameSub); // 判断表名是否存在 System.out.println("表名是否存在:"+JSONTool.toJson(tableNameSub)); if (formBaseService.querySubFormName(tableNameSub) != 0) { //将formdatamanager中该表状态设置为0 StringBuilder alterDataStateSql = new StringBuilder( "/*!mycat:sql=select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FORMID = '%s' */" + " update FORMDATAMANAGER set deleteflg = 0 WHERE FORMDATATABLE = '%s' AND TENANTID = '%s' AND FORMID = '%s'"); String alterSql = String.format(alterDataStateSql.toString(), tenantID, formID,tableNameSub,tenantID,formID); formBaseService.excuteAlterDataState(alterSql); // 删除已经存在的子表单表 formdataManagerEntityRsSubMap.remove(currentKey); subTableStructure.setSubAliasValues(aliasSubValues.get(currentKey)); subTableStructure.setMapperID(formID); subTableStructure.setTenantID(tenantID); subTableStructure.setAliasSubValuesMap(aliasSubValuesMap); subTableStructure.setTableName(tableNameSub); subformL.add(subTableStructure); } else{ formdataManagerEntitySub.setFormdatatable(tableNameSub); formdataManagerEntitySub.setDatarowNum(0); formdataManagerEntitySub.setCreateUser(userName); formBaseService.addFormdataManager(formdataManagerEntitySub); // 子表单数据表创建 Map subData = createTable(tableNameSub, subFormObjFieldtype.get(currentKey.toString()), aliasSubValues.get(Integer.valueOf(currentKey))); subTableStructure.setTableName(tableNameSub); subTableStructure.setCreatUser(userName); subTableStructure.setSubFormObjcloumn(subFormObjcloumn); subTableStructure.setFields(subData); subTableStructure.setMapperID(formID); subTableStructure.setTenantID(tenantID); subTableStructure.setAliasSubValuesMap(aliasSubValuesMap); subTableStructure.setFormDataNum(0); subformL.add(subTableStructure); } } else { // 删除已经存在的子表单表 formdataManagerEntityRsSubMap.remove(currentKey); subTableStructure.setSubAliasValues(aliasSubValues.get(currentKey)); subTableStructure.setMapperID(formID); subTableStructure.setTenantID(tenantID); subTableStructure.setAliasSubValuesMap(aliasSubValuesMap); subTableStructure.setTableName(formdataManagerEntityRsSub.getFormdatatable()); subformL.add(subTableStructure); } } // formdatamanager表中所有子表名 List formDataTable = formBaseService.selectFormDataTable(formID, tenantID); System.out.println("所有被删除得子表单:"+JSONTool.toJson(formDataTable)); // 现主表中不存在的子表单名 List updateStateNames = new ArrayList<>(); System.out.println("subTableNames:--"+subTableNames); for (String name : formDataTable) { if(!subTableNames.contains(name)) updateStateNames.add(name); } System.out.println("要删除得子表单:"+JSONTool.toJson(updateStateNames)); // 将现主表中不存在的子表单状态设置为1 for (String name : updateStateNames) { StringBuilder alterDataStateSql = new StringBuilder( "/*!mycat:sql=select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FORMID = '%s' */" + " update FORMDATAMANAGER set deleteflg = 1 WHERE FORMDATATABLE = '%s' AND TENANTID = '%s' AND FORMID = '%s'"); String alterSql = String.format(alterDataStateSql.toString(), tenantID, formID,name,tenantID,formID); System.out.println("删除子表单sql:"+alterSql); formBaseService.excuteAlterDataState(alterSql); } if(subformL.size()> 0) { tableStructure.setSubTableList(subformL); } //修改创建表位置 if(formTable.getFormTable(tableStructure.getFormId())==null){ if(tableStructure.getFlag() != null && tableStructure.getFlag().equals("update")){ this.formClient.updateTable(tableStructure); }else{ FormTable fo = new FormTable(); fo.setFormID(tableStructure.getFormId()); fo.setJsonObj(JSONTool.toJson(tableStructure, SerializerFeature.DisableCircularReferenceDetect)); fo.setFlag("services"); formTable.addFormTable(fo); } }else{ FormTable fo = new FormTable(); System.out.println("表单ID:"+tableStructure.getFormId()); System.out.println("建表数据:"+JSONTool.toJson(tableStructure, SerializerFeature.DisableCircularReferenceDetect)); fo.setFormID(tableStructure.getFormId()); fo.setFlag("services"); fo.setJsonObj(JSONTool.toJson(tableStructure, SerializerFeature.DisableCircularReferenceDetect)); formTable.addFormTable(fo); } // // Iterator delI = formdataManagerEntityRsSubMap.keySet().iterator(); // while (delI.hasNext()) { // Integer key = delI.next(); // FormdataManagerEntity formdataManagerEntityDel = new FormdataManagerEntity(); // formdataManagerEntityDel.setFormID(formID); // formdataManagerEntityDel.setTenantID(tenantID); // // 非子表单 // formdataManagerEntityDel.setSubFormFlg("1"); // formdataManagerEntityDel.setFormdataNum(key); // formBaseService.deleteFormdataManager(formdataManagerEntityDel); // StringBuilder showTableSql = new StringBuilder( // "/*!mycat:sql=select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ DROP TABLE IF EXISTS %s;"); // String dropSql = String.format(showTableSql.toString(), tenantID, formID, // formdataManagerEntityRsSubMap.get(key).getFormdatatable()); // formBaseService.excuteFormDataTable(dropSql); // } } /** * 创建表单数据表 * * @param tableName 表名 * @param fieldtypeList 表字段类型 * @return 创建表语句 * @throws RowOverLengthException */ private Map createTable(String tableName, List fieldtypeList, List aliasValues) throws RowOverLengthException { Map data = new HashMap<>(); if (!checkRowLength(fieldtypeList, 0)) { throw new RowOverLengthException(); } // StringBuilder createTableSql = new StringBuilder( // "/*!mycat:sql=select tenantid from FORMDATAMANAGER WHERE TENANTID = '%s' AND FROMID = '%s' */ create table "); // createTableSql.append(tableName); // createTableSql.append("("); // createTableSql.append("DATAROWNUM int not null,"); // createTableSql.append("TENANTID char(50) not null,"); // createTableSql.append("SUBDATAROWNUM int not null,"); // 表单数据表创建 for (int i = 0; i < aliasValues.size();i++) { if (DBTYPE.containsKey(fieldtypeList.get(i))) { // createTableSql.append(aliasValues.get(i)); // createTableSql.append(DBTYPE.get(fieldtypeList.get(i))); data.put(aliasValues.get(i) , DBTYPE.get(fieldtypeList.get(i))); // createTableSql.append(" NULL "); // createTableSql.append(ConstMap.COMMA); } } // createTableSql.append("DELETEFLG char(1) comment '0未删除,1删除',"); // createTableSql.append("CREATEUSER varchar(50),"); // createTableSql.append("CREATEDATETIME TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',"); // createTableSql.append("UPDATEUSER varchar(50),"); // createTableSql.append("UPDATEDATETIME TIMESTAMP NOT NULL DEFAULT now() on update now(),"); // createTableSql.append("primary key (DATAROWNUM,TENANTID,SUBDATAROWNUM)"); // createTableSql.append(");"); return data; } private boolean checkRowLength(List fieldtypeList, Integer oldLength) { int rowLength = 0; for (String type : fieldtypeList) { if (DBTYPELENGTH.containsKey(type)) { rowLength += DBTYPELENGTH.get(type); } } if ((rowLength + oldLength) > ConstMap.MAXROWLENGTH) { return false; } return true; } @Override public void formBaseUpdate(FormBaseEntity formBase, JSONObject jsonObj, JSONArray linkages) throws Exception { String formMode = jsonObj.getString("formMode");//5为表单模板.其余自己找吧... String formID = jsonObj.getString("formid"); String userName = jsonObj.getString("userName"); // 更新用户 formBase.setUpdateUser(userName); formBase.setLinkage(linkages.toString()); // 表单基本表更新操作 formBaseService.updateFormBaseInfo(formBase); // 删除表单字段 formBaseService.deleteFormField(formBase.getFormID(), formBase.getTenantID()); // 删除表单属性 formBaseService.deleteFormFieldProperty(formBase.getFormID(), formBase.getTenantID()); //过滤选项卡 String filedsetStr = AutoConditionUtil.filterOption(jsonObj.getString("filedset")); JSONObject filedset = JSONObject.fromObject(filedsetStr); List fieldtypeList = new ArrayList<>(); //这是不被删除得子表单集合 Map> subFormObjcloumn = new HashMap<>(); Map> subFormObjFieldtype = new HashMap<>(); //更新后字段别名 List aliasValues = new ArrayList<>(); //更新后字段别名及类型 例:{子表单=subForm , ...} Map aliasValuesMap = new HashMap<>(); //Map<子表单号,该子表单字段别名> Map> aliasSubValues = new HashMap<>(); //<别名,类型> Map> aliasSubValuesMap = new HashMap<>(); //子表单号 List subFormNums = new ArrayList<>(); //Map<子表单号,子表单别名> Map subFormNames = new HashMap<>(); FormFieldEntity formFieldEntity = new FormFieldEntity(); // 表单ID formFieldEntity.setFormID(formBase.getFormID()); // 租户ID formFieldEntity.setTenantID(formBase.getTenantID()); // 控件状态 true formFieldEntity.setFieldIsShow("true"); // 控件状态 true formFieldEntity.setSearchIsShow("true"); // 作者 formFieldEntity.setCreateUser(userName); // 处理主表 getMainStream(filedset) .forEach(main->{ String columnOrderNum = Objects.toString(main.get(ConstMap.COLUMNORDERNUMKEY)); String alia = Objects.toString(Objects.toString(((Map)main.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS))); String type = Objects.toString(main.get(ConstMap.TAGTYPEKEY)); fieldtypeList.add(type); aliasValues.add(alia); aliasValuesMap.put(alia, type); subFormNums.add(Integer.valueOf(columnOrderNum)); // 添加字段信息到数据库 addFormField(formFieldEntity, main); }); // 处理子表 getChildStream(filedset) .forEach(child->{ String columnOrderNum = Objects.toString(child.get(ConstMap.COLUMNORDERNUMKEY)); String childAlia = Objects.toString(((Map)child.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS)); List alias = new ArrayList<>(); List types = new ArrayList<>(); Map values = new HashMap<>(); getChildStream(filedset, columnOrderNum) .forEach(childFields->{ String alia = Objects.toString(((Map)childFields.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS)); String type = Objects.toString(childFields.get(ConstMap.TAGTYPEKEY)); alias.add(alia); types.add(type); values.put(alia, type); // 添加字段信息到数据库 addFormField(formFieldEntity, childFields); }); subFormObjcloumn.put(columnOrderNum, alias); subFormObjFieldtype.put(columnOrderNum, types); aliasSubValues.put(Integer.valueOf(columnOrderNum), alias); aliasSubValuesMap.put(columnOrderNum, values); subFormNames.put(Integer.valueOf(columnOrderNum), childAlia); }); insertFormdataManager(fieldtypeList, subFormObjcloumn, subFormObjFieldtype, formBase.getFormID(), formBase.getTenantID(), userName, aliasValues, aliasSubValues, aliasValuesMap, aliasSubValuesMap, subFormNames); if(!"5".equals(formMode)){ FormSetEntity createFormSetInfo = new FormSetEntity(); createFormSetInfo.setFormSetID(jsonObj.getString("formSetID")); createFormSetInfo.setTenantID(formBase.getTenantID()); createFormSetInfo.setFormName(jsonObj.getString("formName")); createFormSetInfo.setUpdateUser(userName); //追加表单集合 formBaseService.updateFormSetInfo(createFormSetInfo); FormVersionEntity formVersionEntity = new FormVersionEntity(); formVersionEntity.setFormID(formID); formVersionEntity.setFormSetID(jsonObj.getString("formSetID")); formVersionEntity.setFormVersion(jsonObj.getString("formVer")); formVersionEntity.setTenantID(formBase.getTenantID()); formBaseService.updateFormVersion(formVersionEntity); //追加表单版本 if (!StringUtils.isEmpty(formBase.getValidationRuleGroupID())) {//数据验证规则ID formBaseService.deleteFormValidation(formBase.getValidationRuleGroupID()); List listFormValidationEntity = new ArrayList<>(); for (int i = 0; i < linkages.size(); i++) { FormValidationEntity entity = new FormValidationEntity(); entity.setFormID(formID); entity.setOrderNum(i + 1); entity.setValidationRuleGroupID(formBase.getValidationRuleGroupID()); entity.setTenantID(formBase.getTenantID()); entity.setRuleset(linkages.getString(i)); entity.setCreateUser(userName); entity.setUpdateUser(userName); listFormValidationEntity.add(entity); } formBaseService.insertFormValidation(listFormValidationEntity); } } // 查看表单集合内是否存在formSetID } private void addFormField(FormFieldEntity formFieldEntity, Map main) { String columnOrderNum = Objects.toString(main.get(ConstMap.COLUMNORDERNUMKEY)); String parentFormNum = Objects.toString(main.get("parentsubFormNum")); String alia = Objects.toString(Objects.toString(((Map)main.get(ConstMap.TAGATTRIBUTE)).get(ConstMap.ALIAS))); String type = Objects.toString(main.get(ConstMap.TAGTYPEKEY)); // 字段编号 formFieldEntity.setColumnOrderNum(Integer.valueOf(columnOrderNum)); // 字段类型 formFieldEntity.setFieldtype(type); // 上层子表单 formFieldEntity.setParentsubFormNum(Integer.valueOf(parentFormNum)); // 表示顺序 formFieldEntity.setFieldOrderNum(Integer.valueOf(Objects.toString(main.get("fieldOrderNum")))); // 字段别名 formFieldEntity.setFieldKey(alia); formBaseService.addFormField(formFieldEntity); Map fieldPropertyObj = (Map) main.get(ConstMap.TAGATTRIBUTE); Iterator itSub = fieldPropertyObj.keySet().iterator(); List fieldPropertyEntitys = new ArrayList<>(); while (itSub.hasNext()) { String currentKey = itSub.next().toString(); FieldPropertyEntity fieldPropertyEntity = new FieldPropertyEntity(); String value = Objects.toString(fieldPropertyObj.get(currentKey)); // 表单ID fieldPropertyEntity.setFormID(formFieldEntity.getFormID()); // 租户ID fieldPropertyEntity.setTenantID(formFieldEntity.getTenantID()); // 字段顺番 fieldPropertyEntity.setColumnOrderNum(Integer.valueOf(columnOrderNum)); // 属性名 fieldPropertyEntity.setProperty(currentKey); // 属性值 fieldPropertyEntity.setValue(value); // 作成者 fieldPropertyEntity.setCreateUser(formFieldEntity.getCreateUser()); fieldPropertyEntitys.add(fieldPropertyEntity); } formBaseService.addFormFieldProperty(fieldPropertyEntitys); } private Stream> getChildStream(JSONObject fields){ return ((Collection>)fields.values()).stream() .filter(f->Objects.equals(f.get("tag_Type"), "subform")); } private Stream> getChildStream(JSONObject fields, String columnOrderNum){ return ((Collection>)fields.values()).stream() .filter(cf->!Objects.equals(cf.get("parentsubFormNum"), 0)) .filter(cf->Objects.equals(columnOrderNum, Objects.toString(cf.get("parentsubFormNum")))); } private Stream> getMainStream(JSONObject fields) { return ((Collection>)fields.values()).stream() .filter(f->Objects.equals(f.get("parentsubFormNum"), 0)); } private Iterator getKeys(JSONObject filedset) { Set s = new TreeSet<>((c1, c2)-> Integer.valueOf(c1.substring(7)).compareTo(Integer.valueOf(c2.substring(7)))); s.addAll(filedset.keySet()); return s.iterator(); } @SuppressWarnings("unchecked") @Override public ServicesFormData removeFormData(String formID, String tenantID, JSONArray datarowNumArray, String userName) throws Exception { //结束当前流程 datarowNumArray.stream().forEach(o->{ try { processOver(formID,tenantID,Integer.parseInt(Objects.toString(o,"0"))); } catch (TimeoutException e) { e.printStackTrace(); } }); 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("formdata" + formDataMangers.get(i).getFormdatatable().substring(8)); formData.setFormName("formdata" + formDataMangers.get(i).getFormdatatable().substring(8)); }else{ subForm.setFormID("formdata" + formDataMangers.get(i).getFormdatatable().substring(8)); subForm.setFormName("formdata" + formDataMangers.get(i).getFormdatatable().substring(8)); subFormList.add(subForm); } formDataMangers.get(i).getFormdatatable(); } formData.setSubFormList(subFormList); formData.setDataIds(datarowNumArray); formData.setTenantID(tenantID); formData.setUserName(userName); this.formClient.deleteFormDataClient(formData); return null; } @Override public PageResult> queryFormDataPage(String formID, String tenantID, int pageNum, int pageSize, String where, boolean dataPower) throws FormClientException { List formFieldsList = formBaseService.getFormFieldEntitys(tenantID, formID); if(ObjectUtil.empty(formFieldsList)) return new PageResult<>(null); // 获得自定义值 Map customs = new HashMap<>(); formFieldsList.stream() .filter(field->Objects.equals(field.getDataSourceType(), ConstMap.CUSTOM) && ObjectUtil.notEmpty(field.getDataSourceData())) .forEach(field->{ Map customVals = new HashMap<>(); customs.put(field.getFieldKey(), customVals); System.out.println(field.getDataSourceData().getClass()); field.getDataSourceData().stream() .filter(data->data instanceof FormCpnAttr.Custom) .map(data->(FormCpnAttr.Custom) data) .forEach(data->{ customVals.put(Objects.toString(data.getCode(), ""), Objects.toString(data.getItemtext(), "")); }); }); log.debug("所有自定义值:{}", JSONTool.toJson(customs)); SelectForm mainForm = new SelectForm(); mainForm.setDataPower(dataPower); mainForm.setFormName(RequestContext.getFormName(tenantID, formID)); String formName = mainForm.getFormName(); Map fields = new ModelMap(getColumn(formName, "processLog"), "processLog") .addAttribute(getColumn(formName, "processTask"), "processTask") .addAttribute(getColumn(formName, "processUser"), "processUser") .addAttribute(getColumn(formName, "processFlag"), "processFlag") .addAttribute(getColumn(formName, PROCESS_STATE), PROCESS_STATE) .addAttribute(getFormCreateDateTime(formName), CREATEDATETIME) .addAttribute(getColumn(formName, CREATEUSER), CREATEUSER) .addAttribute(getColumn(formName, SOLIDIFICATION), SOLIDIFICATION) .addAttribute(getColumn(formName, "htmlUrl"), "htmlUrl") ; mainForm.setFields(fields); formFieldsList.stream() .filter(form->form.getParentsubFormNum() == 0 && DBTYPE.containsKey(form.getFieldtype())) .filter(form->!fields.containsValue(form.getFieldKey())) .forEach(form->fields.put(getColumn(formName, form.getFieldKey()), form.getFieldKey())); mainForm.setTenantID(tenantID); mainForm.setFormID(formID); mainForm.setStartRow(pageSize * (pageNum - 1)); mainForm.setEndRow(pageSize); mainForm.setScreenCondition(where); PageResult> map = formClient.queryFormDataPage(mainForm); if(ObjectUtil.empty(map) || ObjectUtil.empty(map.getList())) { return map == null ? new PageResult<>() : map; } // this.codeToNameTool.toDo(map.getList()); Map userCache = new HashMap<>(); map.getList().stream().forEach(o->{ // 显示状态名 o.put("processStatE",o.get(PROCESS_STATE)); o.entrySet().forEach(entry->{ Object name = Optional.ofNullable(customs.get(entry.getKey())).orElse(Collections.EMPTY_MAP).get(entry.getValue()); if(ObjectUtil.notEmpty(name)){ entry.setValue(name); } }); String code = Objects.toString(o.get(CREATEUSER),""); if(ObjectUtil.empty(code)){ return; } String name = userCache.get(code); if(ObjectUtil.empty(name)){ name = cscApi.getUserName(code); userCache.put(code, name); } o.put(CREATEUSER, name); o.put("openId", code); }); return map; } @Override public List> queryFormDataList(String formID, String tenantID, String where) throws FormClientException{ List formFieldsList = dataManagementMapper.queryFormFields3(formID, tenantID); if(ObjectUtil.empty(formFieldsList)) return new ArrayList<>(); Map fields = new HashMap<>(); SelectForm mainForm = new SelectForm(); mainForm.setFields(fields); mainForm.setFormName(RequestContext.getFormName(tenantID, formID)); mainForm.setTenantID(tenantID); mainForm.setFormID(formID); mainForm.setScreenCondition(where); formFieldsList.stream().filter(form->form.getParentsubFormNum() == 0 && DBTYPE.containsKey(form.getFieldtype())) .forEach(form->fields.put(form.getFieldKey(), form.getFieldKey())); return formClient.queryFormDataList(mainForm); } @SuppressWarnings("unchecked") @Override public Map queryFormDataList(String formID, String tenantID, Integer datarowNum,String formdataNum) throws Exception { List formFieldsList = formBaseService.getFormFieldEntitys(tenantID, formID); List subForm = new ArrayList<>() , mytexts = new ArrayList<>(); SelectForm selectForm = new SelectForm(); selectForm.setDataRowNum(datarowNum); selectForm.setTenantID(tenantID); selectForm.setFormName(RequestContext.getFormName(tenantID, formID)); Map selectMap = new ModelMap(CREATEUSER, CREATEUSER) .addAttribute(CREATEDATETIME_SQL, CREATEDATETIME) .addAttribute(SOLIDIFICATION, SOLIDIFICATION) .addAttribute(PROCESS_FLAG, PROCESS_FLAG) .addAttribute(PROCESS_STATE, PROCESS_STATE) .addAttribute("htmlUrl", "htmlUrl"); selectForm.setFields(selectMap); formFieldsList.stream().forEach(field->{ if(selectMap.containsValue(field.getFieldKey())){ return; } // 缓存子表单数据 if(Objects.equals(field.getFieldtype(), ConstMap.SUBFORM)){ subForm.add(field); }else if(ObjectUtil.empty(field.getParentsubFormNum())){ selectMap.put(field.getFieldKey(), field.getFieldKey()); } // 缓存关联控件数据 if(Objects.equals(field.getFieldtype(), ConstMap.MYTEXT)){ mytexts.add(field); } }); List subList = new ArrayList<>(); selectForm.setSubForm(subList); subForm.stream().forEach(sub->{ SubForm subFormInfo = new SubForm(); subList.add(subFormInfo); subFormInfo.setSubDataRowNum(selectForm.getDataRowNum()); subFormInfo.setFormName(RequestContext.getFormChildName(selectForm.getFormName(), sub.getFieldKey())); subFormInfo.setFormDataNum(sub.getColumnOrderNum()); Map selectSub = new HashMap<>(); subFormInfo.setFields(selectSub); formFieldsList.stream() .filter(field->Objects.equals(field.getParentsubFormNum(), sub.getColumnOrderNum())) .forEach(subField-> selectSub.put(subField.getFieldKey(), subField.getFieldKey())); }); //原先返回为rs Map sf = this.formClient.queryFormDataOne(selectForm); //用户名name转code if(sf.get("main")!=null){ Map main = JSONTool.toObj(JSONTool.toJson(sf.get("main")),Map.class); Map sub = JSONTool.toObj(JSONTool.toJson(sf.get("subs")),Map.class); String uName = cscApi.getUserName(main.get(CREATEUSER)); log.debug("查询用户:{}", uName); main.put(CREATEUSER, uName!=null?uName:main.get(CREATEUSER)); // 添加子表单 subForm.stream().forEach(subObj-> main.put(String.format("FIELD%d", subObj.getColumnOrderNum()), subObj.getFieldKey())); sf.put("main", main); sf.put("subs", sub); } return sf; } @Override public IOType getProductMethodThod() { return IOType.services; } @Override public List getFormFieldMainValue(String formID, String tenantID,String field, String tableName) throws Exception { return this.formClient.getFormFieldMainValue(field, tableName, formID, tenantID); } @Override public List getMasterBaseDataListService(String tenantID, String name) throws TimeoutException, FormClientException { MasterData masterData = new MasterData(); masterData.setTenantID(tenantID); // masterData.setFormID(formID); masterData.setName(name); return masterClient.queryClientMasterDefines(masterData); } @Override public List getMasterBaseElementListService(String formID, String tenantID, String code) throws Exception { MasterData masterData = new MasterData(); masterData.setCode(code); masterData.setTenantID(tenantID); masterData.setFormID(formID); return this.masterClient.queryClientMasterElements(masterData); } @Override public Map getMasterBaseElementListServiceForNew(String tenantID, String code, String callbackId,String codes,String value,String isAll) throws Exception { JSONObject jsonObj = JSONObject.fromObject(code); String defindCode = null; String eleCode = null; if (jsonObj.has("defindCode")) { defindCode = jsonObj.getString("defindCode"); } if (jsonObj.has("eleCode")) { eleCode = jsonObj.getString("eleCode"); } if (defindCode != null && eleCode != null) { String eleName = null; if (jsonObj.has(ConstMap.ELENAME)) { eleName = jsonObj.getString(ConstMap.ELENAME); } String parentCode = null; if (jsonObj.has("parentCode")) { parentCode = jsonObj.getString("parentCode"); } Map rsMap = new HashMap<>(); //将需要的参数放入到实体中 MasterValue eleCodeMasterValue = new MasterValue(); eleCodeMasterValue.setTenantID(tenantID); eleCodeMasterValue.setDefineCode(defindCode); eleCodeMasterValue.setVariableField(eleCode); eleCodeMasterValue.setELE_CODE(eleCode); //全部的参数 最新的 MasterValue MasterValue = new MasterValue(); MasterValue.setTenantID(tenantID); MasterValue.setDefineCode(defindCode); MasterValue.setELE_CODE(eleCode); MasterValue.setParentCode(parentCode); MasterValue.setEleName(eleName); MasterValue.setIsAll(isAll); MasterValue.setValue(value); List result1 = this.masterClient.queryClientMasterValues(MasterValue); if(ObjectUtil.empty(result1)) { rsMap.put(callbackId, new ArrayList<>()); }else{ rsMap.put(callbackId, result1); } if(!"".equals(codes) && !ObjectUtil.empty(codes)){ if(JSONTool.toList(codes, String.class).size()>0){ MasterValue.setCodes(codes); MasterValue.setValue(null); List result = this.masterClient.queryClientMasterValues(MasterValue); System.out.println("元素列表:"+JSONTool.toJson(result)); rsMap.put("selectSeache", result); } } return rsMap; } return null; } @Override public Object addFormData(AllFormData formData) throws TimeoutException, FormClientException { return formClient.addFormDataClient(formData.getFormData()); } @Override public void updataFormData(AllFormData formData) throws TimeoutException, FormClientException { formClient.updateFormDataClient(formData.getFormData()); } @SuppressWarnings("unchecked") @Override public List> getSubFormDataListService(String formID, String tenantID, Integer formdataNum, Integer datarowNum) throws Exception { Map value = this.queryFormDataList(formID, tenantID, datarowNum, Objects.toString(formdataNum, "")); Map subs = JSONTool.toObj(JSONTool.toJson(value.get("subs")), new TypeReference>(){}); Iterator next = subs.keySet().iterator(); List> result = new ArrayList<>(); while(next.hasNext()){ Object key = next.next(); result = (List>) subs.get(key); // Collections.reverse(result); 去掉子表单数据排序 break; } return result; } @Override public List queryMasterValues(String tenantID, String defineCode, String eleCode) throws Exception { MasterValue eleCodeMasterValue = new MasterValue(); eleCodeMasterValue.setTenantID(tenantID); eleCodeMasterValue.setDefineCode(defineCode); eleCodeMasterValue.setVariableField(eleCode); eleCodeMasterValue.setELE_CODE(eleCode); // return this.masterClient.queryClientMasterValues(eleCodeMasterValue); return null; } @Override public String getUserName(String formID, String tenantID, Integer dataRowNum) { try { return this.formClient.getUserName(formID, tenantID, dataRowNum); } catch (TimeoutException | FormClientException e) { e.printStackTrace(); } return null; } // public String getProcessInfo(String state){ // switch(state){ // case "10": // return "未提交"; // case "20": // return "审核中"; // case "30": // return "审批结束"; // } // return "数据有误"; // } /** * 取出固化判断条件 * @return */ @SuppressWarnings("unchecked") public Map getSoFlag(FormFieldEntity form){ Map map = new HashMap<>(); Map data = JSONTool.toObj(form.getFieldset(),Map.class); data.keySet().stream().forEach(k->{ Map map1 = JSONTool.toObj(data.get(k).toString(),Map.class); System.out.println(JSONTool.toJson(map1)); Map map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class); String flag = Objects.toString(map2.get("solidiFication"),""); map.put(Objects.toString(map2.get(ALIAS),null),!"".equals(flag)?flag:"false" ); }); return map; } /** * 匹配表单固化字段 * @return */ @SuppressWarnings("unchecked") public String soField( List map,Object dataRowNum,String key){ for(Map f : map){ if(Objects.equals(dataRowNum,f.get(DATAROWNUM))){ return Objects.toString(f.get(key),""); } } return null; } public static void main(String[] args) { ScreenCondition screenCondition = new ScreenCondition(); screenCondition.setCount("processFlag !=1"); screenCondition.setChild(new ArrayList()); String where2 = FormatParsingTool.parsing(screenCondition); System.out.println(where2); } public void processOver(String formId,String tenantId,Integer dataRowNum) throws TimeoutException { System.out.println("当前结束流程id为:"+dataRowNum); ProcessInstance process = new ProcessInstance(formId,tenantId,dataRowNum); ProcessInstance pro = processInstance.selectInst(process); if(pro!= null && pro.getProcessInstId()!=null && !Objects.equals(pro.getProcessInstId(),"") ){ JSONObject jsonObject = new JSONObject(); jsonObject.put("processInstId", pro.getProcessInstId()); workflowClient.instOver(workflowTool.getProcess(jsonObject)); } } }