001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
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 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
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 */
016package org.dromara.warm.flow.core.orm.agent;
017
018import org.dromara.warm.flow.core.orm.service.IWarmService;
019import org.dromara.warm.flow.core.utils.CollUtil;
020import org.dromara.warm.flow.core.utils.ObjectUtil;
021import org.dromara.warm.flow.core.utils.page.OrderBy;
022import org.dromara.warm.flow.core.utils.page.Page;
023
024import java.util.List;
025
026/**
027 * 查询代理层处理
028 *
029 * @author warm
030 * @since 2023-03-17
031 */
032public class WarmQuery<T> implements OrderBy {
033
034    /**
035     * 排序字段
036     */
037    private String orderBy;
038
039    /**
040     * 排序的方向desc或者asc
041     */
042    private String isAsc = "desc";
043
044    private IWarmService<T> warmService;
045
046    public WarmQuery(IWarmService<T> warmService) {
047        this.warmService = warmService;
048    }
049
050    /**
051     * 查询列表
052     *
053     * @param entity 实体列表
054     * @return 集合
055     */
056    public Page<T> page(T entity, Page<T> page) {
057        if (ObjectUtil.isNull(page)) {
058            page = new Page<>(1, 10, orderBy, isAsc);
059        }
060        return warmService.page(entity, page.setOrderBy(orderBy).setIsAsc(isAsc));
061    }
062
063    /**
064     * 查询列表
065     *
066     * @param entity 实体列表
067     * @return 集合
068     */
069    public List<T> list(T entity) {
070        return warmService.list(entity, this);
071    }
072
073    /**
074     * 查询列表
075     *
076     * @param entity 实体列表
077     * @return 集合
078     */
079    public T getOne(T entity) {
080        List<T> list = warmService.list(entity, this);
081        return CollUtil.getOne(list);
082    }
083
084    /**
085     * id设置正序排列
086     *
087     * @return 集合
088     */
089    public WarmQuery<T> orderById() {
090        this.orderBy = "id";
091        return this;
092    }
093
094    /**
095     * 创建时间设置正序排列
096     *
097     * @return 集合
098     */
099    public WarmQuery<T> orderByCreateTime() {
100        this.orderBy = "create_time";
101        return this;
102    }
103
104    /**
105     * 更新时间设置正序排列
106     *
107     * @return 集合
108     */
109    public WarmQuery<T> orderByUpdateTime() {
110        this.orderBy = "update_time";
111        return this;
112    }
113
114    /**
115     * 设置正序排列
116     *
117     * @return 集合
118     */
119    public WarmQuery<T> desc() {
120        this.isAsc = "desc";
121        return this;
122    }
123
124    /**
125     * 设置正序排列
126     *
127     * @param orderByField 排序字段
128     * @return 集合
129     */
130    public WarmQuery<T> orderByAsc(String orderByField) {
131        this.orderBy = orderByField;
132        this.isAsc = "asc";
133        return this;
134    }
135
136    /**
137     * 设置倒序排列
138     *
139     * @param orderByField 排序字段
140     * @return 集合
141     */
142    public WarmQuery<T> orderByDesc(String orderByField) {
143        this.orderBy = orderByField;
144        this.isAsc = "desc";
145        return this;
146    }
147
148    /**
149     * 用户自定义排序方案
150     *
151     * @param orderByField 排序字段
152     * @return 集合
153     */
154    public WarmQuery<T> orderBy(String orderByField) {
155        this.orderBy = orderByField;
156        return this;
157    }
158
159    @Override
160    public String getOrderBy() {
161        return orderBy;
162    }
163
164    public void setOrderBy(String orderBy) {
165        this.orderBy = orderBy;
166    }
167
168    @Override
169    public String getIsAsc() {
170        return isAsc;
171    }
172
173    public WarmQuery<T> setIsAsc(String isAsc) {
174        this.isAsc = isAsc;
175        return this;
176    }
177
178    public IWarmService<T> getWarmService() {
179        return warmService;
180    }
181
182    public void setWarmService(IWarmService<T> warmService) {
183        this.warmService = warmService;
184    }
185}