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);
}
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);
}
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);
}
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;
}
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();
}
Aggregations