001/*
002 *  Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package com.mybatisflex.core.activerecord;
018
019import com.mybatisflex.core.BaseMapper;
020import com.mybatisflex.core.activerecord.query.FieldsQuery;
021import com.mybatisflex.core.activerecord.query.QueryModel;
022import com.mybatisflex.core.activerecord.query.RelationsQuery;
023import com.mybatisflex.core.query.MapperQueryChain;
024import com.mybatisflex.core.query.QueryWrapper;
025import com.mybatisflex.core.util.SqlUtil;
026
027import java.io.Serializable;
028import java.util.Optional;
029
030/**
031 * Active Record 模型。
032 *
033 * @param <T> 实体类类型
034 * @author 王帅
035 * @since 2023-07-24
036 */
037@SuppressWarnings({"unused", "unchecked"})
038public abstract class Model<T extends Model<T>>
039    extends QueryModel<T>
040    implements MapperModel<T>, MapperQueryChain<T>, Serializable {
041
042    /**
043     * 根据实体类构建的条件删除数据。
044     *
045     * @return {@code true} 删除成功,{@code false} 删除失败
046     */
047    public boolean remove() {
048        return SqlUtil.toBool(baseMapper().deleteByQuery(queryWrapper()));
049    }
050
051    /**
052     * 根据实体类构建的条件删除数据,结果使用 {@link Optional} 返回源对象回调,删除成功返回
053     * {@code Optional.of(this)},删除失败返回 {@code Optional.empty()}。
054     *
055     * @return {@link Optional} 链式调用
056     */
057    public Optional<T> removeOpt() {
058        return remove() ? Optional.of((T) this) : Optional.empty();
059    }
060
061    /**
062     * 根据实体类构建的条件更新数据(自动忽略 {@code null} 值)。
063     *
064     * @return {@code true} 更新成功,{@code false} 更新失败
065     */
066    public boolean update() {
067        return update(true);
068    }
069
070    /**
071     * 根据实体类构建的条件更新数据(自动忽略 {@code null} 值),结果使用 {@link Optional}
072     * 返回源对象回调,更新成功返回 {@code Optional.of(this)},更新失败返回
073     * {@code Optional.empty()}。
074     *
075     * @return {@link Optional} 链式调用
076     */
077    public Optional<T> updateOpt() {
078        return updateOpt(true);
079    }
080
081    /**
082     * 根据实体类构建的条件更新数据,并设置是否忽略 {@code null} 值。
083     *
084     * @param ignoreNulls 是否忽略 {@code null} 值
085     * @return {@code true} 更新成功,{@code false} 更新失败
086     */
087    public boolean update(boolean ignoreNulls) {
088        return SqlUtil.toBool(baseMapper().updateByQuery((T) this, ignoreNulls, queryWrapper()));
089    }
090
091    /**
092     * 根据实体类构建的条件更新数据,并设置是否忽略 {@code null} 值,结果使用 {@link Optional}
093     * 返回源对象回调,更新成功返回 {@code Optional.of(this)},更新失败返回
094     * {@code Optional.empty()}。
095     *
096     * @param ignoreNulls 是否忽略 {@code null} 值
097     * @return {@link Optional} 链式调用
098     */
099    public Optional<T> updateOpt(boolean ignoreNulls) {
100        return update(ignoreNulls) ? Optional.of((T) this) : Optional.empty();
101    }
102
103    @Override
104    public BaseMapper<T> baseMapper() {
105        return MapperModel.super.baseMapper();
106    }
107
108    @Override
109    public QueryWrapper toQueryWrapper() {
110        return queryWrapper();
111    }
112
113    @Override
114    public FieldsQuery<T> withFields() {
115        return new FieldsQuery<>(this);
116    }
117
118    @Override
119    public RelationsQuery<T> withRelations() {
120        return new RelationsQuery<>(this);
121    }
122
123}