|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2024, baomidou (jobob@qq.com). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.baomidou.mybatisplus.core.injector.methods; |
| 17 | + |
| 18 | +import com.baomidou.mybatisplus.core.enums.SqlMethod; |
| 19 | +import com.baomidou.mybatisplus.core.injector.AbstractMethod; |
| 20 | +import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; |
| 21 | +import com.baomidou.mybatisplus.core.metadata.TableInfo; |
| 22 | +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| 23 | +import com.baomidou.mybatisplus.core.toolkit.Constants; |
| 24 | +import com.baomidou.mybatisplus.core.toolkit.StringPool; |
| 25 | +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; |
| 26 | +import org.apache.ibatis.mapping.MappedStatement; |
| 27 | +import org.apache.ibatis.mapping.SqlSource; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static java.util.stream.Collectors.joining; |
| 32 | +import static java.util.stream.Collectors.toList; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * 根据 ID 集合删除 |
| 37 | + * |
| 38 | + * @author nieqiurong |
| 39 | + * @since 3.5.7 |
| 40 | + */ |
| 41 | +public class DeleteByIds extends AbstractMethod { |
| 42 | + |
| 43 | + public DeleteByIds() { |
| 44 | + this(SqlMethod.DELETE_BY_IDS.getMethod()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param name 方法名 |
| 49 | + * @since 3.5.0 |
| 50 | + */ |
| 51 | + public DeleteByIds(String name) { |
| 52 | + super(name); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { |
| 57 | + String sql; |
| 58 | + SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_IDS; |
| 59 | + if (tableInfo.isWithLogicDelete()) { |
| 60 | + sql = logicDeleteScript(tableInfo, sqlMethod); |
| 61 | + SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class); |
| 62 | + return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource); |
| 63 | + } else { |
| 64 | + sqlMethod = SqlMethod.DELETE_BY_IDS; |
| 65 | + sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), |
| 66 | + SqlScriptUtils.convertForeach( |
| 67 | + SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())", |
| 68 | + "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"), |
| 69 | + COLL, null, "item", COMMA)); |
| 70 | + SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class); |
| 71 | + return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param tableInfo 表信息 |
| 77 | + * @return 逻辑删除脚本 |
| 78 | + * @since 3.5.0 |
| 79 | + */ |
| 80 | + public String logicDeleteScript(TableInfo tableInfo, SqlMethod sqlMethod) { |
| 81 | + List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream() |
| 82 | + .filter(TableFieldInfo::isWithUpdateFill) |
| 83 | + .filter(f -> !f.isLogicDelete()) |
| 84 | + .collect(toList()); |
| 85 | + String sqlSet = "SET "; |
| 86 | + if (CollectionUtils.isNotEmpty(fieldInfos)) { |
| 87 | + sqlSet += SqlScriptUtils.convertIf(fieldInfos.stream() |
| 88 | + .map(i -> i.getSqlSet(Constants.ENTITY + StringPool.DOT)).collect(joining(EMPTY)), String.format("%s != null", Constants.ENTITY), true); |
| 89 | + } |
| 90 | + sqlSet += StringPool.EMPTY + tableInfo.getLogicDeleteSql(false, false); |
| 91 | + return String.format(sqlMethod.getSql(), tableInfo.getTableName(), |
| 92 | + sqlSet, tableInfo.getKeyColumn(), SqlScriptUtils.convertForeach( |
| 93 | + SqlScriptUtils.convertChoose("@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())", |
| 94 | + "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"), |
| 95 | + COLL, null, "item", COMMA), |
| 96 | + tableInfo.getLogicDeleteSql(true, true)); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments