Search in sources :

Example 6 with Page

use of cn.hutool.db.Page in project Jpom by dromara.

the class NodeWelcomeController method getList.

private List<SystemMonitorLog> getList() {
    NodeModel node = getNode();
    String startDateStr = getParameter("time[0]");
    String endDateStr = getParameter("time[1]");
    if (StrUtil.hasEmpty(startDateStr, endDateStr)) {
        SystemMonitorLog systemMonitorLog = new SystemMonitorLog();
        systemMonitorLog.setNodeId(node.getId());
        return dbSystemMonitorLogService.queryList(systemMonitorLog, 500, new Order("monitorTime", Direction.DESC));
    }
    // 处理时间
    DateTime startDate = DateUtil.parse(startDateStr);
    long startTime = startDate.getTime();
    DateTime endDate = DateUtil.parse(endDateStr);
    if (startDate.equals(endDate)) {
        // 时间相等
        endDate = DateUtil.endOfDay(endDate);
    }
    long endTime = endDate.getTime();
    // 开启了节点信息采集
    Page pageObj = new Page(1, 5000);
    pageObj.addOrder(new Order("monitorTime", Direction.DESC));
    Entity entity = Entity.create();
    entity.set("nodeId", node.getId());
    entity.set(" MONITORTIME", ">= " + startTime);
    entity.set("MONITORTIME", "<= " + endTime);
    return dbSystemMonitorLogService.listPageOnlyResult(entity, pageObj);
}
Also used : Order(cn.hutool.db.sql.Order) Entity(cn.hutool.db.Entity) NodeModel(io.jpom.model.data.NodeModel) SystemMonitorLog(io.jpom.model.log.SystemMonitorLog) Page(cn.hutool.db.Page) DateTime(cn.hutool.core.date.DateTime)

Example 7 with Page

use of cn.hutool.db.Page in project Jpom by dromara.

the class BaseDbService method getLastTimeValue.

/**
 * 查询指定字段降序 指定条数对最后一个值
 *
 * @param timeColumn 时间字段
 * @param maxCount   最大数量
 * @param whereCon   添加查询条件回调
 * @return 时间
 */
protected long getLastTimeValue(String timeColumn, int maxCount, Consumer<Entity> whereCon) {
    Entity entity = Entity.create(super.getTableName());
    if (whereCon != null) {
        // 条件
        whereCon.accept(entity);
    }
    Page page = new Page(maxCount, 1);
    page.addOrder(new Order(timeColumn, Direction.DESC));
    PageResultDto<T> pageResult;
    try {
        pageResult = super.listPage(entity, page);
    } catch (java.lang.IllegalStateException illegalStateException) {
        return 0L;
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("查询数据错误 :{}", e.getMessage());
        return 0L;
    }
    if (pageResult.isEmpty()) {
        return 0L;
    }
    T entity1 = pageResult.get(0);
    Object fieldValue = ReflectUtil.getFieldValue(entity1, timeColumn);
    return Convert.toLong(fieldValue, 0L);
}
Also used : Order(cn.hutool.db.sql.Order) Entity(cn.hutool.db.Entity) Page(cn.hutool.db.Page)

Example 8 with Page

use of cn.hutool.db.Page in project Jpom by dromara.

the class BaseDbService method listPage.

/**
 * 通用的分页查询, 使用该方法查询,数据库表字段不能包含 "page", "limit", "order_field", "order", "total"
 * <p>
 * page=1&limit=10&order=ascend&order_field=name
 *
 * @param paramMap 请求参数
 * @return page
 */
public PageResultDto<T> listPage(Map<String, String> paramMap) {
    String orderField = paramMap.get("order_field");
    String order = paramMap.get("order");
    // 
    Page pageReq = this.parsePage(paramMap);
    Entity where = Entity.create();
    List<String> ignoreField = new ArrayList<>(10);
    // 查询条件
    for (Map.Entry<String, String> stringStringEntry : paramMap.entrySet()) {
        String key = stringStringEntry.getKey();
        String value = stringStringEntry.getValue();
        if (StrUtil.isEmpty(value)) {
            continue;
        }
        key = StrUtil.removeAll(key, "%");
        if (StrUtil.startWith(stringStringEntry.getKey(), "%") && StrUtil.endWith(stringStringEntry.getKey(), "%")) {
            where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '%{}%'", value));
        } else if (StrUtil.endWith(stringStringEntry.getKey(), "%")) {
            where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '{}%'", value));
        } else if (StrUtil.startWith(stringStringEntry.getKey(), "%")) {
            where.set(StrUtil.format("`{}`", key), StrUtil.format(" like '%{}'", value));
        } else if (StrUtil.containsIgnoreCase(key, "time") && StrUtil.contains(value, "~")) {
            // 时间筛选
            String[] val = StrUtil.splitToArray(value, "~");
            if (val.length == 2) {
                DateTime startDateTime = DateUtil.parse(val[0], DatePattern.NORM_DATETIME_FORMAT);
                where.set(key, ">= " + startDateTime.getTime());
                DateTime endDateTime = DateUtil.parse(val[1], DatePattern.NORM_DATETIME_FORMAT);
                if (startDateTime.equals(endDateTime)) {
                    endDateTime = DateUtil.endOfDay(endDateTime);
                }
                // 防止字段重复
                where.set(key + " ", "<= " + endDateTime.getTime());
            }
        } else if (StrUtil.containsIgnoreCase(key, "time")) {
            // 时间筛选
            String timeKey = StrUtil.removeAny(key, "[0]", "[1]");
            if (ignoreField.contains(timeKey)) {
                continue;
            }
            String startTime = paramMap.get(timeKey + "[0]");
            String endTime = paramMap.get(timeKey + "[1]");
            if (StrUtil.isAllNotEmpty(startTime, endTime)) {
                DateTime startDateTime = DateUtil.parse(startTime, DatePattern.NORM_DATETIME_FORMAT);
                where.set(timeKey, ">= " + startDateTime.getTime());
                DateTime endDateTime = DateUtil.parse(endTime, DatePattern.NORM_DATETIME_FORMAT);
                if (startDateTime.equals(endDateTime)) {
                    endDateTime = DateUtil.endOfDay(endDateTime);
                }
                // 防止字段重复
                where.set(timeKey + " ", "<= " + endDateTime.getTime());
            }
            ignoreField.add(timeKey);
        } else if (StrUtil.endWith(key, ":in")) {
            String inKey = StrUtil.removeSuffix(key, ":in");
            where.set(StrUtil.format("`{}`", inKey), StrUtil.split(value, StrUtil.COMMA));
        } else {
            where.set(StrUtil.format("`{}`", key), value);
        }
    }
    // 排序
    if (StrUtil.isNotEmpty(orderField)) {
        orderField = StrUtil.removeAll(orderField, "%");
        pageReq.addOrder(new Order(orderField, StrUtil.equalsIgnoreCase(order, "ascend") ? Direction.ASC : Direction.DESC));
    }
    return this.listPage(where, pageReq);
}
Also used : Order(cn.hutool.db.sql.Order) Entity(cn.hutool.db.Entity) ArrayList(java.util.ArrayList) Page(cn.hutool.db.Page) Map(java.util.Map) DateTime(cn.hutool.core.date.DateTime)

Example 9 with Page

use of cn.hutool.db.Page in project Jpom by dromara.

the class BaseDbCommonService method listPage.

/**
 * 分页查询
 *
 * @param where 条件
 * @param page  分页
 * @return 结果
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public PageResultDto<T> listPage(Entity where, Page page) {
    if (!DbConfig.getInstance().isInit()) {
        // ignore
        log.error("The database is not initialized, this execution will be ignored:{},{}", this.tClass, this.getClass());
        return PageResultDto.EMPTY;
    }
    where.setTableName(getTableName());
    PageResult<Entity> pageResult;
    Db db = Db.use();
    db.setWrapper((Character) null);
    try {
        pageResult = db.page(where, page);
    } catch (Exception e) {
        throw warpException(e);
    }
    // 
    List<T> list = pageResult.stream().map(entity -> {
        T entityToBean = this.entityToBean(entity, this.tClass);
        this.fillSelectResult(entityToBean);
        return entityToBean;
    }).collect(Collectors.toList());
    PageResultDto<T> pageResultDto = new PageResultDto(pageResult);
    pageResultDto.setResult(list);
    if (pageResultDto.isEmpty() && pageResultDto.getPage() > 1) {
        Assert.state(pageResultDto.getTotal() <= 0, "筛选的分页有问题,当前页码查询不到任何数据");
    }
    return pageResultDto;
}
Also used : PageResultDto(io.jpom.model.PageResultDto) PageResult(cn.hutool.db.PageResult) ExceptionUtil(cn.hutool.core.exceptions.ExceptionUtil) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) BeanUtil(cn.hutool.core.bean.BeanUtil) Page(cn.hutool.db.Page) Order(cn.hutool.db.sql.Order) JpomRuntimeException(io.jpom.system.JpomRuntimeException) LinkedHashMap(java.util.LinkedHashMap) TypeUtil(cn.hutool.core.util.TypeUtil) Map(java.util.Map) PageUtil(cn.hutool.core.util.PageUtil) DbConfig(io.jpom.system.db.DbConfig) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Db(cn.hutool.db.Db) Consumer(java.util.function.Consumer) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Condition(cn.hutool.db.sql.Condition) CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Entity(cn.hutool.db.Entity) Assert(org.springframework.util.Assert) Entity(cn.hutool.db.Entity) Db(cn.hutool.db.Db) JdbcSQLNonTransientException(org.h2.jdbc.JdbcSQLNonTransientException) JpomRuntimeException(io.jpom.system.JpomRuntimeException) PageResultDto(io.jpom.model.PageResultDto)

Example 10 with Page

use of cn.hutool.db.Page in project Jpom by dromara.

the class BaseDbCommonService method queryList.

/**
 * 查询列表
 *
 * @param data   数据
 * @param count  查询数量
 * @param orders 排序
 * @return List
 */
public List<T> queryList(T data, int count, Order... orders) {
    Entity where = this.dataBeanToEntity(data);
    Page page = new Page(1, count);
    page.addOrder(orders);
    PageResultDto<T> tPageResultDto = this.listPage(where, page);
    return tPageResultDto.getResult();
}
Also used : Entity(cn.hutool.db.Entity) Page(cn.hutool.db.Page)

Aggregations

Page (cn.hutool.db.Page)10 Entity (cn.hutool.db.Entity)9 Map (java.util.Map)5 StrUtil (cn.hutool.core.util.StrUtil)4 Order (cn.hutool.db.sql.Order)4 JSONObject (com.alibaba.fastjson.JSONObject)4 PageResultDto (io.jpom.model.PageResultDto)4 ClassFeature (io.jpom.permission.ClassFeature)4 Feature (io.jpom.permission.Feature)4 MethodFeature (io.jpom.permission.MethodFeature)4 JpomRuntimeException (io.jpom.system.JpomRuntimeException)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Assert (org.springframework.util.Assert)4 GetMapping (org.springframework.web.bind.annotation.GetMapping)4 Convert (cn.hutool.core.convert.Convert)3 FileUtil (cn.hutool.core.io.FileUtil)3 Tuple (cn.hutool.core.lang.Tuple)3 Validator (cn.hutool.core.lang.Validator)3