diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/PrimaryKeyLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/PrimaryKeyLogicDeleteProcessor.java new file mode 100644 index 00000000..40f041fa --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/impl/PrimaryKeyLogicDeleteProcessor.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.core.logicdelete.impl; + +import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.exception.FlexAssert; +import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor; +import com.mybatisflex.core.table.IdInfo; +import com.mybatisflex.core.table.TableInfo; + +import java.util.List; + +import static com.mybatisflex.core.constant.SqlConsts.EQUALS; + +/** + * 主键逻辑删除处理器。 + * + * @author 王帅 + * @see I7O1VV + * @since 2023-07-26 + */ +public class PrimaryKeyLogicDeleteProcessor extends AbstractLogicDeleteProcessor { + + @Override + public String buildLogicDeletedSet(String logicColumn, TableInfo tableInfo, IDialect dialect) { + List primaryKeyList = tableInfo.getPrimaryKeyList(); + FlexAssert.notEmpty(primaryKeyList, "Must have one primary key."); + String column = primaryKeyList.get(0).getColumn(); + return dialect.wrap(logicColumn) + EQUALS + dialect.wrap(column); + } + + /** + * 正常未删除的值为 0 (或者可配置为其他值)。 + */ + @Override + public Object getLogicNormalValue() { + Object normalValueOfLogicDelete = FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete(); + if (normalValueOfLogicDelete instanceof Number) { + return normalValueOfLogicDelete; + } + return 0; + } + + /** + * 逻辑删除后,则更新逻辑删除字段值为主键的值。 + */ + @Override + public Object getLogicDeletedValue() { + return null; + } + +}