2 Commits 6038a56b07 ... 38b2f83733

Tác giả SHA1 Thông báo Ngày
  fanxp 38b2f83733 Merge remote-tracking branch 'origin/dev' into dev 3 tháng trước cách đây
  fanxp 792d873dca form 3 tháng trước cách đây

+ 40 - 57
src/main/java/com/xjrsoft/module/form/service/impl/FormExecuteServiceImpl.java

@@ -6,12 +6,7 @@ import cn.hutool.core.util.ClassUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.db.Db;
-import cn.hutool.db.Entity;
-import cn.hutool.db.GlobalDbConfig;
-import cn.hutool.db.Page;
-import cn.hutool.db.PageResult;
-import cn.hutool.db.Session;
+import cn.hutool.db.*;
 import cn.hutool.db.meta.Column;
 import cn.hutool.db.meta.JdbcType;
 import cn.hutool.db.meta.MetaUtil;
@@ -37,19 +32,7 @@ import com.xjrsoft.common.utils.LocalDateTimeUtil;
 import com.xjrsoft.module.app.entity.AppFuncDesign;
 import com.xjrsoft.module.app.service.IAppFuncDesignService;
 import com.xjrsoft.module.authority.utils.AuthorityUtil;
-import com.xjrsoft.module.form.dto.AppFormExecuteAddOrUpdateDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteDeleteDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteInfoDto;
-import com.xjrsoft.module.form.dto.AppFormExecuteListDto;
-import com.xjrsoft.module.form.dto.AppFormExecutePageDto;
-import com.xjrsoft.module.form.dto.FormExecuteAddOrUpdateDto;
-import com.xjrsoft.module.form.dto.FormExecuteDeleteDto;
-import com.xjrsoft.module.form.dto.FormExecuteInfoDto;
-import com.xjrsoft.module.form.dto.FormExecuteListDto;
-import com.xjrsoft.module.form.dto.FormExecutePageDto;
-import com.xjrsoft.module.form.dto.FormExecuteWorkflowAddDto;
-import com.xjrsoft.module.form.dto.FormExecuteWorkflowInfoDto;
-import com.xjrsoft.module.form.dto.FormExecuteWorkflowUpdateDto;
+import com.xjrsoft.module.form.dto.*;
 import com.xjrsoft.module.form.entity.FormDesignConfig;
 import com.xjrsoft.module.form.entity.FormRelease;
 import com.xjrsoft.module.form.entity.FormReleaseConfig;
@@ -61,12 +44,7 @@ import com.xjrsoft.module.form.utils.FormDataTransUtil;
 import com.xjrsoft.module.form.vo.DeskColumnsVo;
 import com.xjrsoft.module.form.vo.DeskTableInfoVo;
 import com.xjrsoft.module.generator.constant.ComponentTypeConstant;
-import com.xjrsoft.module.generator.entity.ColumnConfig;
-import com.xjrsoft.module.generator.entity.ListConfig;
-import com.xjrsoft.module.generator.entity.QueryConfig;
-import com.xjrsoft.module.generator.entity.TableConfig;
-import com.xjrsoft.module.generator.entity.TableFieldConfig;
-import com.xjrsoft.module.generator.entity.TableStructureConfig;
+import com.xjrsoft.module.generator.entity.*;
 import com.xjrsoft.module.generator.utils.GeneratorUtil;
 import com.xjrsoft.module.system.service.ICodeRuleService;
 import lombok.AllArgsConstructor;
@@ -95,17 +73,10 @@ import javax.sql.DataSource;
 import java.sql.Timestamp;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
+
 /**
  * @Author: tzx
  * @Date: 2022/5/11 15:05
@@ -470,7 +441,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         FormTemplate template = formTemplateMapper.selectById(formRelease.getFormId());
 
         long id = insertFormData(formData, template);
-        if(id != 0){
+        if (id != 0) {
             return true;
         }
         return false;
@@ -501,7 +472,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         template.setFormJson(configJson);
 
         long id = insertFormData(formData, template);
-        if(id != 0){
+        if (id != 0) {
             return true;
         }
         return false;
@@ -524,11 +495,13 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
             Table tableMeta = SqlRunnerAdapter.db().getTableMeta(tableName);
             Collection<Column> columns = tableMeta.getColumns();
             Optional<Column> pk = columns.stream().filter(Column::isPk).findFirst();
+            // 根据表单配置获取字典数据
+            Map<String, Map<String, Object>> formDicData = FormDataTransUtil.formConfigGetDicData(formDesignConfig);
 
             for (Map<String, Object> data : dataList) {
                 Entity where = Entity.create(tableName);
                 // 处理字段值
-                Map<String, Object> toSaveFormData = handleFormDataForSave(data, formDesignConfig, tableName, new ArrayList<>());
+                Map<String, Object> toSaveFormData = handleFormDataForSave(data, formDesignConfig, tableName, new ArrayList<>(), formDicData);
 
                 //formData 默认插入雪花Id主键
                 pk.ifPresent(column -> toSaveFormData.put(column.getName(), IdUtil.getSnowflakeNextId()));
@@ -660,7 +633,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
 
                             long count = SqlRunnerAdapter.db().selectCount("select count(*) from " + childTableConfig.get().getTableName() + " where " + childTableConfig.get().getRelationField() + " in " + keyValuesStr);
 
-                            if(count > 0){
+                            if (count > 0) {
                                 Boolean result = SqlRunnerAdapter.db().dynamicDelete(childTableConfig.get().getTableName(), childDeleteWhere);
                                 if (!result) {
                                     throw new MyException("删除数据失败");
@@ -698,7 +671,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
 
                             long count = SqlRunnerAdapter.db().selectCount("select count(*) from " + childTableConfig.get().getTableName() + " where " + childTableConfig.get().getRelationField() + " in " + keyValuesStr);
 
-                            if(count > 0){
+                            if (count > 0) {
                                 Boolean result = SqlRunnerAdapter.db().dynamicDelete(childTableConfig.get().getTableName(), childDeleteWhere);
                                 if (!result) {
                                     throw new MyException("删除数据失败");
@@ -921,7 +894,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
             // 自动编码code
             List<String> autoCodeList = new ArrayList<>();
             // 处理字段值
-            Map<String, Object> toSaveFormData = handleFormDataForSave(formData, formDesignConfig, tableName, autoCodeList);
+            Map<String, Object> toSaveFormData = handleFormDataForSave(formData, formDesignConfig, tableName, autoCodeList, null);
             //formData 默认插入雪花Id主键
             if (pk.isPresent()) {
                 formData.put(pk.get().getName(), keyValue);
@@ -1010,7 +983,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
             wrapperChildEntity(tableConfigList, childMaps, formData, formDesignConfig, keyValue2, null);
 
             // 处理字段值
-            formData = handleFormDataForSave(formData, formDesignConfig, tableName, null);
+            formData = handleFormDataForSave(formData, formDesignConfig, tableName, null, null);
 //            for (Map.Entry<String, Object> entry : formData.entrySet()) {
 //                if (entry.getValue().toString().isEmpty()) {
 //                    entry.setValue(null);
@@ -1099,7 +1072,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
             Map<String, List<Map<String, Object>>> childFormData = wrapperChildEntity(tableConfigList, childMaps, newFormData, formDesignConfig, keyValue, autoCodeList);
 
             // 处理字段值
-            newFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, autoCodeList);
+            newFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, autoCodeList, null);
 
             //formData 默认插入雪花Id主键
             if (pk.isPresent()) {
@@ -1183,7 +1156,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
             Map<String, List<Map<String, Object>>> childFormData = wrapperChildEntity(tableConfigList, childMaps, newFormData, formDesignConfig, keyValue, null);
 
             // 处理字段值
-            newFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, null);
+            newFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, null, null);
 
             //此时的formData 已经是剔除了子表单数据了
             newFormData.remove(pk.getName());
@@ -1287,7 +1260,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
                 Map<String, List<Map<String, Object>>> childFormData = wrapperChildEntity(tableConfigList, childMaps, newFormData, formDesignConfig, keyValue, null);
 
                 // 处理字段值
-                Map<String, Object> toSaveFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, null);
+                Map<String, Object> toSaveFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, null, null);
                 //此时的formData 已经是剔除了子表单数据了
                 toSaveFormData.remove(pk.getName());
                 entity.putAll(toSaveFormData);
@@ -1299,12 +1272,12 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
                 findwhere.putAll(entity);
                 Map<String, Object> objectMap = SqlRunnerAdapter.db().dynamicSelectOne(tableName, findwhere);
                 Boolean isUpdate = false;
-                if(objectMap == null){
+                if (objectMap == null) {
                     isUpdate = true;
                 }
 
                 Boolean result = null;
-                if(isUpdate){
+                if (isUpdate) {
                     result = SqlRunnerAdapter.db().dynamicUpdate(tableName, entity, where);
                     if (!result) {
                         throw new MyException("更新数据失败");
@@ -1320,7 +1293,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
                         String keyValuesStr = "(" + keyValue + ")";
 
                         long count = SqlRunnerAdapter.db().selectCount("select * from " + childTableConfig.get().getTableName() + " where " + childTableConfig.get().getRelationField() + " in " + keyValuesStr);
-                        if(count > 0){
+                        if (count > 0) {
                             Entity childDeleteWhere = Entity.create(tableMap.getKey()).set(childTableConfig.get().getRelationField(), keyValue);
                             result = SqlRunnerAdapter.db().dynamicDelete(childTableConfig.get().getTableName(), childDeleteWhere);
                             if (!result) {
@@ -1361,7 +1334,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
                 Map<String, List<Map<String, Object>>> childFormData = wrapperChildEntity(tableConfigList, childMaps, newFormData, formDesignConfig, keyValue, autoCodeList);
                 System.out.println("insertOrUpdateFormDataByWorkflow 3");
                 // 处理字段值
-                Map<String, Object> toSaveFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, autoCodeList);
+                Map<String, Object> toSaveFormData = handleFormDataForSave(newFormData, formDesignConfig, tableName, autoCodeList, null);
                 toSaveFormData.put(pk.getName(), keyValue);
                 formData.put(pk.getName(), keyValue);
                 //此时的formData 已经是剔除了子表单数据了
@@ -1598,7 +1571,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         for (Map<String, Object> item : result) {
             Entity entity = Entity.create();
             for (String key : item.keySet()) {
-                entity.set(key,item.get(key));
+                entity.set(key, item.get(key));
             }
             //将所有查询的数据id 转string
             entity.set(pkColumn.getName(), entity.get(pkColumn.getName()).toString());
@@ -1909,16 +1882,16 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         }
         plainSelect.setWhere(queryExpression);
 
-        com.baomidou.mybatisplus.extension.plugins.pagination.Page mybatisPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(page.getPageNumber()+1, page.getPageSize());
+        com.baomidou.mybatisplus.extension.plugins.pagination.Page mybatisPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page(page.getPageNumber() + 1, page.getPageSize());
 
         IPage<Map<String, Object>> pageResult = SqlRunnerAdapter.db().
-                selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPageNumber()+1, page.getPageSize()), plainSelect.toString());
+                selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPageNumber() + 1, page.getPageSize()), plainSelect.toString());
 
         List<Entity> rData = new ArrayList<>();
         for (Map<String, Object> item : pageResult.getRecords()) {
             Entity entity = Entity.create();
             for (String key : item.keySet()) {
-                entity.set(key,item.get(key));
+                entity.set(key, item.get(key));
             }
             //将所有查询的数据id 转string
             entity.set(pkColumn.getName(), entity.get(pkColumn.getName()).toString());
@@ -1926,9 +1899,9 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
         }
 
         PageOutput<Entity> pageOutput = new PageOutput<>();
-        pageOutput.setPageSize((int)pageResult.getSize());
-        pageOutput.setCurrentPage((int)pageResult.getCurrent());
-        pageOutput.setTotal((int)pageResult.getTotal());
+        pageOutput.setPageSize((int) pageResult.getSize());
+        pageOutput.setCurrentPage((int) pageResult.getCurrent());
+        pageOutput.setTotal((int) pageResult.getTotal());
         pageOutput.setList(rData);
 
         return pageOutput;
@@ -2175,7 +2148,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
 
             List<Entity> childEntities = new ArrayList<>();
             for (Map<String, Object> stringObjectMap : childMap) {
-                Map<String, Object> handedMap = handleFormDataForSave(stringObjectMap, formDesignConfig, tableConfig.getTableName(), autoCodeList);
+                Map<String, Object> handedMap = handleFormDataForSave(stringObjectMap, formDesignConfig, tableConfig.getTableName(), autoCodeList, null);
                 Entity entity = Entity.create(tableConfig.getTableName());
                 entity.putAll(handedMap);
 
@@ -2283,7 +2256,7 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
      * @param formDesignConfig
      */
     private Map<String, Object> handleFormDataForSave(Map<String, Object> formData, FormDesignConfig formDesignConfig,
-                                                      String tableName, List<String> autoCodeList) {
+                                                      String tableName, List<String> autoCodeList, Map<String, Map<String, Object>> formDicData) {
         Map<String, Object> resultData = new HashMap<>(formData.size());
         Map<String, List<ComponentConfig>> componentListMap = GeneratorUtil.buildFormComponentList(formDesignConfig.getFormJson().getList());
         List<ComponentConfig> configList = componentListMap.get(tableName);
@@ -2367,6 +2340,16 @@ public class FormExecuteServiceImpl implements IFormExecuteService {
                 if (autoCodeList != null)
                     autoCodeList.add(MapUtils.getString(options, ComponentTypeConstant.AUTO_CODE_RULE));
             }
+            // 针对导入数据做处理()
+            if (formDicData != null) {
+                if (formDicData.containsKey(bindField)) {
+                    Map<String, Object> dicData = formDicData.get(bindField);
+                    List<Object> mapKey = FormDataTransUtil.getMapKey(dicData, formData.get(bindField));
+                    if (!mapKey.isEmpty()) {
+                        resultData.put(bindField, mapKey.stream().findFirst().get());
+                    }
+                }
+            }
         }
         return resultData;
     }

+ 100 - 0
src/main/java/com/xjrsoft/module/form/utils/FormDataTransUtil.java

@@ -54,6 +54,92 @@ public final class FormDataTransUtil {
         areaService = SpringUtil.getBean(IAreaService.class);
     }
 
+    /**
+     * 根据表单配置获取数据字典
+     */
+    public static Map<String, Map<String, Object>> formConfigGetDicData(FormDesignConfig formDesignConfig) {
+        List<DictionaryDetail> detailList = null;
+        Map<String, Object> userMap = null;
+        Map<String, Object> deptMap = null;
+        Map<String, Object> areaMap = null;
+        Map<String, Map<String, Object>> apiDataMap = new HashMap<>();
+        List<ComponentConfig> componentConfigList = GeneratorUtil.getFormComponentListWithMain(formDesignConfig.getFormJson().getList());
+        Map<String, Map<String, Object>> fieldValuesMap = new HashMap<>(componentConfigList.size());
+        for (ComponentConfig componentConfig : componentConfigList) {
+            String type = componentConfig.getType();
+            String bindField = componentConfig.getBindField();
+            Map<String, Object> options = componentConfig.getOptions();
+            Integer infoType = MapUtils.getInteger(options, "infoType");
+            boolean isCascade = StrUtil.equalsIgnoreCase(ComponentTypeConstant.CASCADE, type);
+            if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.CHECKBOX, type)
+                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.SELECT, type)
+                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_SELECT, type)
+                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.ASSOCIATE_POPUP, type)
+                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.MULTIPLE_POPUP, type)
+                    || StrUtil.equalsIgnoreCase(ComponentTypeConstant.RADIO, type)
+                    || isCascade) {
+                String datasourceType = MapUtils.getString(options, "datasourceType");
+                if (StrUtil.equalsIgnoreCase(datasourceType, "dic")) {
+                    if (detailList == null) {
+                        detailList = redisUtil.get(GlobalConstant.DIC_DETAIL_CACHE_KEY, new TypeReference<List<DictionaryDetail>>() {});
+                    }
+                    Map<String, Object> detailMap = detailList.stream().filter(x -> StrUtil.equalsIgnoreCase(x.getItemId().toString(), MapUtils.getString(options, "itemId"))).collect(Collectors.toMap(DictionaryDetail::getValue, DictionaryDetail::getName, (e1, e2) -> e1));
+                    fieldValuesMap.put(bindField, detailMap);
+                }else if(StrUtil.equalsIgnoreCase(ComponentTypeConstant.SELECT, type) && StrUtil.equalsIgnoreCase(datasourceType, "staticData")){ // 处理下拉静态数据
+                    List<JSONObject> staticOptions = Convert.toList(JSONObject.class, options.get("staticOptions"));
+                    Map<String, Object> dataMap = staticOptions.stream().collect(Collectors.toMap(data -> data.get("value").toString(), data -> data.get("label"), (e1, e2) -> e1));
+                    fieldValuesMap.put(bindField, dataMap);
+                }else if (StrUtil.equalsIgnoreCase(datasourceType, "api") || isCascade) {
+                    String apiId = MapUtils.getString(MapUtils.getMap(options, "apiConfig"), "apiId");
+                    if (apiDataMap.get(apiId) == null) {
+                        Object resultApiData = magicApiService.executeApi(apiId);
+                        List<Map<String, Object>> apiDataList = null;
+                        if (resultApiData instanceof PageResult) {
+                            apiDataList = (List<Map<String, Object>>) ((PageResult) resultApiData).getList();
+                        } else if (resultApiData instanceof List) {
+                            apiDataList = (List<Map<String, Object>>) resultApiData;
+                        }
+                        if (isCascade) apiDataList = treeToList(apiDataList);
+                        if (apiDataList != null ){
+                            apiDataMap.put(apiId, apiDataList.stream()
+                                    .filter(x -> ObjectUtil.isNotNull(x.get("value")))
+                                    .collect(Collectors.toMap(data -> data.get("value").toString(), data -> data.get("label"), (e1, e2) -> e1)));
+                        }
+                    }
+                    fieldValuesMap.put(bindField, apiDataMap.get(apiId));
+                }
+            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.AREA, type)) {
+                // TODO 待优化
+                Set<Long> areaValueList = new LinkedHashSet<>();
+                if (CollectionUtils.isNotEmpty(areaValueList)) {
+                    List<Area> areaList = areaService.list(Wrappers.<Area>query().lambda().in(Area::getId, areaValueList));
+                    areaMap = areaList.stream().collect(Collectors.toMap(area -> area.getId().toString(), Area::getName, (e1, e2) -> e1));
+                } else {
+                    areaMap = new HashMap<>(0);
+                }
+                fieldValuesMap.put(bindField, areaMap);
+            }else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.ORGANIZATION, type)
+                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(1))) {
+                if (deptMap == null) {
+                    List<Department> departmentList = redisUtil.get(GlobalConstant.DEP_CACHE_KEY, new TypeReference<List<Department>>() {});
+                    deptMap = departmentList.stream().collect(Collectors.toMap(department -> department.getId().toString(), Department::getName, (e1, e2) -> e1));
+                }
+                fieldValuesMap.put(bindField, deptMap);
+            } else if (StrUtil.equalsIgnoreCase(ComponentTypeConstant.USER, type)
+                    || (StrUtil.equalsIgnoreCase(ComponentTypeConstant.INFO, type) && infoType.equals(0))) {
+                if (userMap == null) {
+                    List<User> userList = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {});
+                    userMap = userList.stream()
+                            .filter(user -> user.getId() != null)
+                            .filter(user -> user.getName() != null)
+                            .collect(Collectors.toMap(user -> user.getId().toString(), User::getName, (e1, e2) -> e1));
+                }
+                fieldValuesMap.put(bindField, userMap);
+            }
+        }
+        return fieldValuesMap;
+    }
+
     public static void transData(List<Entity> dataList, FormDesignConfig formDesignConfig) {
         if (CollectionUtils.isEmpty(dataList)) {
             return;
@@ -401,4 +487,18 @@ public final class FormDataTransUtil {
         }
         return resultList;
     }
+
+    /**
+     * 根据value获取key
+     */
+    public static List<Object> getMapKey(Map map, Object value){
+        List<Object> keyList = new ArrayList<>();
+        for(Object key: map.keySet()){
+            if(map.get(key).equals(value)){
+                keyList.add(key);
+            }
+        }
+        return keyList;
+    }
+
 }