浏览代码

getTableByTableName接口,和objectSve中编辑接口更改

shiqian 4 年之前
父节点
当前提交
74111d4d28

+ 9 - 1
boman-common/boman-common-core/src/main/java/com/boman/common/core/utils/obj/ObjectUtils.java

@@ -159,5 +159,13 @@ public class ObjectUtils {
     }
 
 
-
+    /**
+     * 功能描述: 是否小于0
+     *
+     * @param aLong aLong
+     * @return boolean true小于0
+     */
+    public static boolean ltZero(Long aLong) {
+        return null != aLong && aLong < 0;
+    }
 }

+ 1 - 1
boman-modules/boman-gen/src/main/java/com/boman/gen/controller/MyController.java

@@ -50,7 +50,7 @@ public class MyController extends BaseController {
     @GetMapping("/loadTable")
     public AjaxResult loadTable(GenTable genTable) {
         List<GenTable> tableList = genTableService.selectGenTableList(genTable);
-        requiredNonNull(tableList);
+        requireNonNull(tableList);
 
         // load table and tableColumn
         List<Long> tableIdList = tableList.stream().map(GenTable::getTableId).collect(Collectors.toList());

+ 47 - 13
boman-modules/boman-system/src/main/java/com/boman/system/common/TableServiceCmdService.java

@@ -3,7 +3,6 @@ package com.boman.system.common;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.boman.common.core.utils.SecurityUtils;
 import com.boman.common.core.utils.collection.CollectionUtils;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.common.core.web.domain.AjaxResult;
@@ -25,10 +24,8 @@ import java.sql.Timestamp;
 import java.util.*;
 import java.util.stream.Collectors;
 
-import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
-import static com.boman.common.core.utils.obj.ObjectUtils.requiredNonNull;
-import static com.boman.system.common.FormDataConstant.CONDITION;
-import static com.boman.system.common.FormDataConstant.SHOW_DATA;
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
+import static com.boman.system.common.FormDataConstant.*;
 
 /**
  * @author shiqian
@@ -48,6 +45,8 @@ public class TableServiceCmdService {
     private IBaseSelectService selectService;
     @Resource
     private IBaseSubmitService submitService;
+    @Resource
+    private IBaseUpdateService updateService;
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TableServiceCmdService.class);
 
@@ -60,6 +59,7 @@ public class TableServiceCmdService {
     }
 
     public final AjaxResult objectSave(BaseTableSaveDTO baseTableSaveDTO) {
+        AjaxResult result = AjaxResult.success();
         BaseTableDTO baseTableDTO = packTableDTO(baseTableSaveDTO);
         TableServiceContext context = TableServiceContext.createFrom(baseTableDTO);
         // 拿到pkName和maxId
@@ -71,15 +71,33 @@ public class TableServiceCmdService {
         List<String> allColumnNameList = columns.stream()
                 .map(GenTableColumn::getColumnName)
                 .collect(Collectors.toList());
-        Long maxId = IdUtils.getMaxId(baseTableDTO.getTable(), pkName);
-        RowResult rowResult = saveService.insertRow(context.getRealTableName(), pkName, maxId, context.getRows().get(0), allColumnNameList);
-        if (RowResult.checkSuccess(rowResult)) {
-            LOGGER.info("保存成功,封装到数据库的数据为: {}", JSON.toJSONString(rowResult.getData()));
-        } else {
-            LOGGER.error("保存失败,保持的原始数据为: {}", JSON.toJSONString(baseTableSaveDTO));
+
+        // 新增
+        if (ObjectUtils.ltZero(baseTableDTO.getObjId())) {
+            Long maxId = IdUtils.getMaxId(baseTableDTO.getTable(), pkName);
+            RowResult rowResult = saveService.insertRow(context.getRealTableName(), pkName, maxId, context.getRows().get(0), allColumnNameList);
+            if (RowResult.checkSuccess(rowResult)) {
+                LOGGER.info("保存成功,封装到数据库的数据为: {}", JSON.toJSONString(rowResult.getData()));
+            } else {
+                LOGGER.error("保存失败,保持的原始数据为: {}", JSON.toJSONString(baseTableSaveDTO));
+                result = AjaxResult.error();
+            }
+        }else {
+            JSONObject commitData = context.getRows().get(0).getCommitData();
+
+            // 编辑
+            int effective = updateService.updateByIdList(context, pkName
+                    , Collections.singletonList(baseTableDTO.getObjId()), commitData);
+            if (effective > 0) {
+                LOGGER.info("修改成功,封装到数据库的数据为: {}", JSON.toJSONString(context.getRows().get(0).getCommitData()));
+            } else {
+                LOGGER.error("修改失败,前台传来的原始数据为: {}", JSON.toJSONString(context.getRows().get(0).getCommitData()));
+                result = AjaxResult.error();
+            }
         }
 
-        return AjaxResult.success(rowResult);
+        return result;
+
     }
 
     /**
@@ -202,7 +220,7 @@ public class TableServiceCmdService {
                 // long string collection 暂时只作此三种类型判断
                 if (column.getColumnName().equalsIgnoreCase(key) && ObjectUtils.isNotEmpty(value)) {
                     // columnType 作为判断需不需要转义的一个标准,防止索引失效
-                    result.put(key, packValue(String.valueOf(value), column.getQueryType(), column.getColumnType()));
+                    result.put(key, Lists.newArrayList(String.valueOf(value), column.getQueryType(), column.getColumnType()));
                     break;
                 }
             }
@@ -362,4 +380,20 @@ public class TableServiceCmdService {
         return requireNonNull(genTable);
     }
 
+    /**
+     * 功能描述: 反提交接口, 更改的字段类型和字段值都是一致的
+     *                {
+     *                    "table": "sys_config",
+     *                }
+     *
+     *
+     * @param condition condition
+     * @return com.boman.common.core.web.domain.AjaxResult
+     */
+    public AjaxResult getByTableName(BaseTableSaveDTO condition) {
+//        requiredNonNull(condition.getTable(), "表名为空");
+        GenTable genTable = getTableFromRedisByTableName(RedisKey.TABLE_INFO, condition.getTable());
+        return AjaxResult.success(genTable);
+    }
 }
+

+ 43 - 0
boman-modules/boman-system/src/main/java/com/boman/system/controller/TableController.java

@@ -0,0 +1,43 @@
+package com.boman.system.controller;
+
+import com.boman.common.core.web.domain.AjaxResult;
+import com.boman.system.common.BaseTableSaveDTO;
+import com.boman.system.common.TableServiceCmdService;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+
+/**
+ * @author shiqian
+ * @description
+ * @date 2021年03月22日 09:19
+ **/
+@RestController
+@RequestMapping("/p/cs/table")
+public class TableController {
+
+    @Autowired
+    private TableServiceCmdService tableServiceCmdService;
+
+    /**
+     * 功能描述: 反提交接口, 更改的字段类型和字段值都是一致的
+     *                {
+     *                    "table": "sys_config",
+     *                }
+     *
+     *
+     * @param condition condition
+     * @return com.boman.common.core.web.domain.AjaxResult
+     */
+    @ApiOperation(value = "根据表名获取表单所有信息")
+    @PostMapping("/getByTableName")
+    public AjaxResult getByTableName(@RequestBody BaseTableSaveDTO condition) {
+        return tableServiceCmdService.getByTableName(condition);
+    }
+
+
+}

+ 20 - 7
boman-modules/boman-system/src/main/java/com/boman/system/mapper/StandardlyMapper.java

@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.boman.common.core.utils.obj.ObjectUtils;
 import com.boman.system.common.FormDataConstant;
-import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.ibatis.annotations.*;
 import org.apache.ibatis.annotations.Param;
@@ -584,6 +583,12 @@ public interface StandardlyMapper {
             return result;
         }
 
+        /**
+         * 功能描述: 判断传过来的值是否需要转义
+         *
+         * @param packCondition 封装的条件
+         * @param wholeSql      sql
+         */
         private void packCondition(JSONObject packCondition, StringBuilder wholeSql) {
             if (ObjectUtils.isNotEmpty(packCondition)) {
                 wholeSql.append(" where ");
@@ -591,18 +596,26 @@ public interface StandardlyMapper {
                 for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
                     String key = entry.getKey();
                     Object valueObj = entry.getValue();
-                    String valueStr = ((String) valueObj);
-                    // {@link com.boman.system.common.TableServiceCmdService.packValue} 这里是拼参数的地方
-                    String[] split = valueStr.split("_");
-                    String value = split[0];
-                    String queryType = split[1];
-                    String columnType = split[2];
+                    List<String> types = ((List<String>) valueObj);
+                    // {@link com.boman.system.common.TableServiceCmdService.packColCondition} 这里是拼参数的地方
+                    String value = types.get(0);
+                    String queryType = types.get(1);
+                    String columnType = types.get(2);
                     conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
                 }
                 wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
             }
         }
 
+        /**
+         * 功能描述: 仅此用来拼装查询条件,不做他用
+         *
+         * @param queryType  like > < =
+         * @param columnType varchar char textarea timestamp
+         * @param key        key
+         * @param value      value
+         * @return java.lang.String
+         */
         private String covert(String queryType, String columnType, String key, String value) {
             // false 不需要转义
             boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)

+ 4 - 3
boman-modules/boman-system/src/main/java/com/boman/system/service/IBaseUpdateService.java

@@ -1,6 +1,7 @@
 package com.boman.system.service;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.system.common.TableServiceContext;
 
 import java.util.List;
 
@@ -13,11 +14,11 @@ public interface IBaseUpdateService {
     /**
      * 功能描述: 批量修改, 默认需要更改的model,更改的字段类型和字段值都是一致的
      *
-     * @param tableName  表名
+     * @param context    用到其中的表名和所有的列(用来判断是否需要转义字符)
      * @param pkName     主键名
      * @param idList     idList
-     * @param commitData 需要更改的model
+     * @param commitData 需要更改的model, 已经转义好了,不需要到mapper层再去转义
      * @return int
      */
-    int updateByIdList(String tableName, String pkName, List<Long> idList, JSONObject commitData);
+    int updateByIdList(TableServiceContext context, String pkName, List<Long> idList, JSONObject commitData);
 }

+ 39 - 7
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/BaseUpdateServiceImpl.java

@@ -1,6 +1,9 @@
 package com.boman.system.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.common.core.utils.obj.ObjectUtils;
+import com.boman.gen.domain.GenTableColumn;
+import com.boman.system.common.TableServiceContext;
 import com.boman.system.mapper.StandardlyMapper;
 import com.boman.system.service.IBaseUpdateService;
 import org.slf4j.Logger;
@@ -9,8 +12,12 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Map;
 
+import static com.boman.common.core.utils.obj.ObjectUtils.escapeStr;
 import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
+import static com.boman.system.common.FormDataConstant.*;
+import static com.boman.system.common.FormDataConstant.TIMESTAMP;
 
 /**
  * @author shiqian
@@ -25,21 +32,46 @@ public class BaseUpdateServiceImpl implements IBaseUpdateService {
     private StandardlyMapper mapper;
 
     /**
-     * 功能描述: 批量修改
+     * 功能描述: 批量修改, 默认需要更改的model,更改的字段类型和字段值都是一致的
      *
-     * @param tableName  表名
+     * @param context    用到其中的表名和所有的列(用来判断是否需要转义字符)
      * @param pkName     主键名
      * @param idList     idList
-     * @param commitData 需要更改的model
+     * @param commitData 需要更改的model, 已经转义好了,不需要到mapper层再去转义
      * @return int
      */
     @Override
-    public int updateByIdList(String tableName, String pkName, List<Long> idList, JSONObject commitData) {
-        requireNonNull(tableName, "表名为空");
+    public int updateByIdList(TableServiceContext context, String pkName, List<Long> idList, JSONObject commitData) {
+        requireNonNull(context.getRealTableName(), "表名为空");
         requireNonNull(pkName, "主键名称为空");;
-        requireNonNull(idList);
+        requireNonNull(idList, "需要修改的idList为空");
         requireNonNull(commitData, "需要更改的参数为空");
 
-        return mapper.updateByIdList(tableName, pkName, idList, commitData);
+        commitData = ObjectUtils.ifNullSetEmpty(escapeByQueryType(context.getTable().getColumns(), commitData));
+        return mapper.updateByIdList(context.getRealTableName(), pkName, idList, commitData);
+    }
+
+    private JSONObject escapeByQueryType(List<GenTableColumn> columns, JSONObject commitData) {
+        requireNonNull(columns);
+        JSONObject result = new JSONObject(columns.size());
+        for (Map.Entry<String, Object> entry : commitData.entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+            for (GenTableColumn column : columns) {
+                if (column.getColumnName().equalsIgnoreCase(key) && ObjectUtils.isNotEmpty(value)) {
+                    value = covert(column.getColumnType(), String.valueOf(value));
+                    result.put(key, value);
+                }
+            }
+        }
+
+        return result;
+    }
+
+    private String covert(String columnType, String value) {
+        // false 不需要转义
+        boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR)
+                || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
+        return needEscape ? escapeStr(value) : value;
     }
 }