Przeglądaj źródła

新增根据外键修改数据

Gogs 4 lat temu
rodzic
commit
2af584d01b

+ 24 - 0
boman-web-core/src/main/java/com/boman/web/core/controller/ObjController.java

@@ -257,6 +257,30 @@ public class ObjController {
         return tableServiceCmdService.objectSubmit(condition, true);
     }
 
+    /**
+     * 功能描述: 根据外键修改信息
+     *
+     * {
+     *     "table":"boman_message_receive",
+     *     "commitData":[
+     *         {
+     *             "foreignKeyName":"message_id",
+     *             "foreignKey": 3,
+     *             "updateData":{
+     *                 "visible": "Y"
+     *             }
+     *         }
+     *     ]
+     * }
+     *
+     * @param condition condition
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    @ApiOperation(value = "根据外键修改数据接口")
+    @PostMapping("/updateDataByForeignKey")
+    public AjaxResult genReceiverMsg(@RequestBody FormDataDto condition) {
+        return tableServiceCmdService.genReceiverMsg(condition);
+    }
 
     /**
      * 功能描述: 反提交接口, 更改的字段类型和字段值都是一致的

+ 33 - 0
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -93,6 +93,10 @@ public interface StandardlyMapper {
     int updateByIdList(@Param("tableName") String tableName, @Param("pkName") String pkName
             , @Param("idList") List<Long> idList, @Param("model") JSONObject models);
 
+    @UpdateProvider(type = SqlProvider.class, method = "updateByForeignKey")
+    int updateByForeignKey(@Param("tableName") String tableName, @Param("foreignKeyName") String foreignKeyName
+            , @Param("foreignKeyList") List<Long> foreignKeyList, @Param("model") JSONObject models);
+
     @Select("select id from ${tableName} where ${akColumnName} = #{akColumnValue}")
     Long selectIdByAkColumn(@Param("tableName") String tableName, @Param("akColumnName") String akColumnName, @Param("akColumnValue") String akColumnValue);
 
@@ -409,6 +413,35 @@ public interface StandardlyMapper {
 
         }
 
+        public String updateByForeignKey(Map<String, Object> para) {
+            String tableName = (String) para.get("tableName");
+            String foreignKeyName = (String) para.get("foreignKeyName");
+            List<Long> foreignKeyList = (List<Long>) para.get("foreignKeyList");
+            JSONObject models = (JSONObject) para.get("model");
+
+            StringBuilder wholeSql = new StringBuilder();
+            wholeSql.append("update ").append(tableName).append(" set ");
+            for (Map.Entry<String, Object> entry : models.entrySet()) {
+                String key = entry.getKey();
+                Object value = entry.getValue();
+                wholeSql.append(key).append(covert(EQ, CHAR, key, value)).append(" , ");
+            }
+
+            wholeSql = new StringBuilder(StringUtils.substringBeforeLast(wholeSql.toString(), ","));
+            wholeSql.append(" where ").append(foreignKeyName).append(" in ( ");
+
+            for (Long id : foreignKeyList) {
+                wholeSql.append(id);
+                wholeSql.append(",");
+            }
+
+            wholeSql.setCharAt(wholeSql.length() - 1, ')');
+            String sqlStr = wholeSql.toString();
+            LOGGER.info("批量更新的sql语句为: {}, \r\n 更改的数据为: {}, \r\n foreignKeyList: {}", sqlStr, models, foreignKeyList);
+            return sqlStr;
+
+        }
+
         public String delete(Map<String, Object> para) {
             Long[] ids = (Long[]) para.get("ids");
             String tableName = (String) para.get("tableName");

+ 37 - 0
boman-web-core/src/main/java/com/boman/web/core/service/TableServiceCmdService.java

@@ -693,6 +693,43 @@ public class TableServiceCmdService {
         return AjaxResult.success(commitData);
     }
 
+    /**
+     * 功能描述: 修改数据
+     *
+     * @param condition condition
+     * @return com.boman.domain.dto.AjaxResult
+     */
+    public AjaxResult genReceiverMsg(FormDataDto condition) {
+        GenTable genTable = getTableFromRedisByTableName(RedisKey.TABLE_INFO, condition.getTable());
+        String tableName = requireNonNull(genTable.getTableName(), "tableName = [" + genTable.getTableName() + "] 此表不存在");
+
+        List<JSONObject> commitData = condition.getCommitData();
+        for(JSONObject commitDatum : commitData) {
+            Long foreignKey = Long.valueOf(commitDatum.get("foreignKey").toString());
+            String foreignKeyName = (String) commitDatum.get("foreignKeyName");
+            this.executeData(tableName, foreignKeyName, foreignKey, (Map<String, Object>) commitDatum.get("updateData"));
+        }
+
+        return AjaxResult.success(commitData);
+    }
+
+    /**
+     * 功能描述: 修改数据
+     *
+     * @param tableName   tableName
+     * @param foreignKeyName 外键名
+     * @param foreignKey  外键值
+     * @param commitDatum 返回给前台的数据
+     */
+    private void executeData(String tableName, String foreignKeyName, Long foreignKey, Map<String, Object> commitDatum) {
+        int effective = submitService.executeData(tableName, foreignKeyName, foreignKey, new JSONObject(commitDatum));
+        if (effective > 0) {
+            commitDatum.put(SubmitConstant.SUBMIT_RESULT, SubmitConstant.SUCCESS);
+        } else {
+            commitDatum.put(SubmitConstant.SUBMIT_RESULT, SubmitConstant.FAIL);
+        }
+    }
+
     /**
      * 功能描述: 处理提交和反提交
      *

+ 6 - 0
boman-web-core/src/main/java/com/boman/web/core/service/submit/BaseSubmitServiceImpl.java

@@ -37,4 +37,10 @@ public class BaseSubmitServiceImpl implements IBaseSubmitService {
         requireNonNull(model, "提交的数据为空");
         return mapper.updateByIdList(tableName, pkName, Lists.newArrayList(id), model);
     }
+
+    @Override
+    public int executeData(String tableName, String foreignKeyName, Long foreignKey, JSONObject model) {
+        requireNonNull(model, "提交的数据为空");
+        return mapper.updateByForeignKey(tableName, foreignKeyName, Lists.newArrayList(foreignKey), model);
+    }
 }

+ 10 - 0
boman-web-core/src/main/java/com/boman/web/core/service/submit/IBaseSubmitService.java

@@ -18,4 +18,14 @@ public interface IBaseSubmitService {
      * @return int
      */
     int handlerSubmit(String tableName, String pkName, JSONObject model, Long id);
+
+    /**
+     * 根据外键修改数据
+     * @param tableName
+     * @param foreignKeyName
+     * @param foreignKey
+     * @param model
+     * @return
+     */
+    int executeData(String tableName, String foreignKeyName, Long foreignKey, JSONObject model);
 }