001/*
002 *  Copyright (c) 2022-2025, 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.query;
018
019import com.mybatisflex.core.paginate.Page;
020import com.mybatisflex.core.relation.RelationManager;
021import com.mybatisflex.core.util.LambdaGetter;
022
023import java.util.List;
024
025/**
026 * 使用 {@code Relations Query} 的方式进行关联查询。
027 *
028 * @author 王帅
029 * @since 2023-08-08
030 */
031public class RelationsBuilder<T> extends AbstractQueryBuilder<T> {
032
033    public RelationsBuilder(MapperQueryChain<T> delegate) {
034        super(delegate);
035    }
036
037    /**
038     * 忽略查询部分 {@code Relations} 注解标记的属性。
039     *
040     * @param fields 属性
041     * @return {@code Relations} 查询构建
042     */
043    public RelationsBuilder<T> ignoreRelations(String... fields) {
044        RelationManager.addIgnoreRelations(fields);
045        return this;
046    }
047
048    /**
049     * 忽略查询部分 {@code Relations} 注解标记的属性。
050     *
051     * @param fields 属性
052     * @return {@code Relations} 查询构建
053     */
054    public <L> RelationsBuilder<T> ignoreRelations(LambdaGetter<L>... fields) {
055        RelationManager.addIgnoreRelations(fields);
056        return this;
057    }
058
059    /**
060     * 设置父子关系查询中,默认的递归查询深度。
061     *
062     * @param maxDepth 查询深度
063     * @return {@code Relations} 查询构建
064     */
065    public RelationsBuilder<T> maxDepth(int maxDepth) {
066        RelationManager.setMaxDepth(maxDepth);
067        return this;
068    }
069
070    /**
071     * 添加额外的 {@code Relations} 查询条件。
072     *
073     * @param key   键
074     * @param value 值
075     * @return {@code Relations} 查询构建
076     */
077    public RelationsBuilder<T> extraConditionParam(String key, Object value) {
078        RelationManager.addExtraConditionParam(key, value);
079        return this;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public T one() {
087        return baseMapper().selectOneWithRelationsByQuery(queryWrapper());
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public <R> R oneAs(Class<R> asType) {
095        return baseMapper().selectOneWithRelationsByQueryAs(queryWrapper(), asType);
096    }
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public List<T> list() {
103        return baseMapper().selectListWithRelationsByQuery(queryWrapper());
104    }
105
106    /**
107     * {@inheritDoc}
108     */
109    @Override
110    public <R> List<R> listAs(Class<R> asType) {
111        return baseMapper().selectListWithRelationsByQueryAs(queryWrapper(), asType);
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    @Override
118    public Page<T> page(Page<T> page) {
119        return baseMapper().paginateWithRelations(page, queryWrapper());
120    }
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public <R> Page<R> pageAs(Page<R> page, Class<R> asType) {
127        return baseMapper().paginateWithRelationsAs(page, queryWrapper(), asType);
128    }
129
130}