فهرست منبع

通用删除接口

shiqian 4 سال پیش
والد
کامیت
b7aa50094b

+ 22 - 0
boman-api/boman-domain/src/main/java/com.boman.domain/dto/DeleteDto.java

@@ -0,0 +1,22 @@
+package com.boman.domain.dto;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.Data;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * @author shiqian
+ * @date 2021年06月30日 16:42
+ **/
+@Data
+public class DeleteDto {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(DeleteDto.class);
+
+    private String tableName;
+
+    private JSONObject condition;
+}

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

@@ -2,6 +2,7 @@ package com.boman.web.core.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import com.boman.domain.dto.AjaxResult;
+import com.boman.domain.dto.DeleteDto;
 import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.dto.InsertListDto;
 import com.boman.web.core.service.common.ICommonService;
@@ -100,4 +101,27 @@ public class CommonController {
         return commonService.insertList(dto);
     }
 
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    @PostMapping("delete")
+    public int delete(@RequestBody DeleteDto dto) {
+        return commonService.delete(dto);
+    }
+
+
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    @PostMapping("update")
+    public int update(@RequestBody DeleteDto dto) {
+        return commonService.delete(dto);
+    }
+
 }

+ 20 - 25
boman-web-core/src/main/java/com/boman/web/core/mapper/StandardlyMapper.java

@@ -193,6 +193,17 @@ public interface StandardlyMapper {
     @Select("select * from ${tableName} order by create_time desc limit 1")
     JSONObject getNewest(@Param("tableName") String tableName);
 
+    /**
+     * 功能描述: 通用删除数据  {@link SqlProvider#delete(java.util.Map)}
+     *
+     * @param tableName        tableName
+     * @param condition        condition
+     * @param packColCondition packColCondition
+     * @return int
+     */
+    @SelectProvider(type = SqlProvider.class, method = "delete")
+    Integer delete(@Param("tableName") String tableName, @Param("condition") JSONObject condition, @Param("packColCondition") JSONObject packColCondition);
+
     @SuppressWarnings("unchecked")
     class SqlProvider {
         static final String[] READONLY_COLUMNS = new String[]{"OWNERID", "OWNERNAME", "OWNERENAME", "CREATIONDATE", "ID"};
@@ -443,34 +454,18 @@ public interface StandardlyMapper {
         }
 
         public String delete(Map<String, Object> para) {
-            Long[] ids = (Long[]) para.get("ids");
             String tableName = (String) para.get("tableName");
-            String pkName = (String) para.get("pkName");
-            if (isNullOrEmpty(tableName)) {
-                throw new IllegalArgumentException("tableName 无效");
-            } else if (ids == null) {
-                throw new IllegalArgumentException("ids 无效");
-            } else if (ids.length == 0) {
-                return "";
-            } else if (ids.length == 1) {
-                return "DELETE FROM " + tableName + " where " + pkName + " = #{ids[0]}";
-            } else {
-                StringBuffer sql = new StringBuffer();
-                sql.append("DELETE FROM ");
-                sql.append(tableName);
-                sql.append(" WHERE ");
-                sql.append(pkName + " in (");
+            JSONObject condition = (JSONObject) para.get("condition");
+            JSONObject packColCondition = (JSONObject) para.get("packColCondition");
 
-                for (int i = 0; i < ids.length; ++i) {
-                    sql.append(String.format("#{ids[%d]}", i));
-                    sql.append(",");
-                }
+            StringBuilder sql = new StringBuilder();
+            sql.append("DELETE FROM ");
+            sql.append(tableName);
+            packCondition(packColCondition, sql);
 
-                sql.setCharAt(sql.length() - 1, ')');
-                String sqlStr = sql.toString();
-                LOGGER.info("删除的sql语句为: {} \r\n idList: {}", sqlStr, ids);
-                return sqlStr;
-            }
+            String sqlStr = sql.toString();
+            LOGGER.info("通用删除的sql语句为: {} \r\n condition: {}", sqlStr, condition);
+            return sqlStr;
         }
 
         public String updates(Map<String, Object> para) {

+ 18 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/CommonServiceImpl.java

@@ -4,9 +4,11 @@ import com.alibaba.fastjson.JSONObject;
 import com.boman.common.redis.RedisKey;
 import com.boman.common.redis.service.RedisService;
 import com.boman.domain.GenTable;
+import com.boman.domain.dto.DeleteDto;
 import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.dto.InsertListDto;
 import com.boman.web.core.service.TableServiceCmdService;
+import com.boman.web.core.service.delete.IBaseDeleteService;
 import com.boman.web.core.service.save.IBaseSaveService;
 import com.boman.web.core.service.select.IBaseSelectService;
 import com.boman.web.core.utils.IdUtils;
@@ -33,6 +35,8 @@ public class CommonServiceImpl implements ICommonService {
     private TableServiceCmdService cmdService;
     @Resource
     private IBaseSaveService saveService;
+    @Resource
+    private IBaseDeleteService deleteService;
 
     /**
      * 功能描述: 根据表名和id查找
@@ -123,4 +127,18 @@ public class CommonServiceImpl implements ICommonService {
 
         return saveService.insertList(tableName, dataList);
     }
+
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    @Override
+    public int delete(DeleteDto dto) {
+        String tableName = requireNonNull(dto.getTableName(), "tableName is empty");
+        JSONObject condition = requireNonNull(dto.getCondition(), "condition is empty");
+
+        return deleteService.delete(tableName, condition);
+    }
 }

+ 9 - 0
boman-web-core/src/main/java/com/boman/web/core/service/common/ICommonService.java

@@ -1,6 +1,7 @@
 package com.boman.web.core.service.common;
 
 import com.alibaba.fastjson.JSONObject;
+import com.boman.domain.dto.DeleteDto;
 import com.boman.domain.dto.FormDataDto;
 import com.boman.domain.dto.InsertListDto;
 
@@ -65,4 +66,12 @@ public interface ICommonService {
      * @return int
      */
     int insertList(InsertListDto dto);
+
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param dto dto
+     * @return int
+     */
+    int delete(DeleteDto dto);
 }

+ 30 - 1
boman-web-core/src/main/java/com/boman/web/core/service/delete/BaseDeleteServiceImpl.java

@@ -1,14 +1,23 @@
 package com.boman.web.core.service.delete;
 
 import com.alibaba.fastjson.JSONObject;
-import com.boman.common.core.utils.obj.ObjectUtils;
+import com.boman.common.redis.RedisKey;
+import com.boman.common.redis.service.RedisService;
+import com.boman.domain.GenTable;
+import com.boman.domain.GenTableColumn;
 import com.boman.web.core.domain.RowResult;
 import com.boman.web.core.mapper.StandardlyMapper;
+import com.boman.web.core.utils.ColumnUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
+import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
+import static com.boman.common.core.utils.obj.ObjectUtils.requireNonNull;
+
 /**
  * @author shiqian
  * @date 2021年03月26日 11:16
@@ -20,6 +29,8 @@ public class BaseDeleteServiceImpl implements IBaseDeleteService {
 
     @Autowired
     private StandardlyMapper mapper;
+    @Autowired
+    private RedisService redisService;
 
     /**
      * 功能描述: 在此不对入参进行校验,所以在调用此方法之前一定要对参数进行校验,以免报错
@@ -64,5 +75,23 @@ public class BaseDeleteServiceImpl implements IBaseDeleteService {
         return delete > 0 ? RowResult.ok() : RowResult.fail();
     }
 
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param tableName tableName
+     * @param condition condition
+     * @return int
+     */
+    @Override
+    public int delete(String tableName, JSONObject condition) {
+        requireNonNull(tableName, "tableName is empty");
+        requireNonNull(condition, "condition is empty");
 
+        GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
+        List<GenTableColumn> columns = genTable.getColumns();
+        ColumnUtils.checkColumn(condition, columns);
+        JSONObject packColCondition = ColumnUtils.packColCondition(columns, condition);
+        Integer delete = mapper.delete(tableName, condition, packColCondition);
+        return isEmpty(delete) ? 0 : delete;
+    }
 }

+ 8 - 1
boman-web-core/src/main/java/com/boman/web/core/service/delete/IBaseDeleteService.java

@@ -42,5 +42,12 @@ public interface IBaseDeleteService {
      */
     RowResult objectLogicDelete(Long[] idArr, String tableName, String pkName, JSONObject model);
 
-
+    /**
+     * 功能描述: 通用删除数据
+     *
+     * @param tableName tableName
+     * @param condition condition
+     * @return int
+     */
+    int delete(String tableName, JSONObject condition);
 }