需求:查询按照指定时间的前一天的数据,举个例子:传入2023/2/27 ,需要获取到2023/2/26 的数据,如果这一天没有数据,那就需要往前找,直到找不到为止;
分析:因为是从数据库根据时间查找的,就按照数据库中每条数据的时间倒叙排序一下,将符合条件的数据返回即可,奉上代码:
@Override
public R<ChronicChatLog> queryReferralTime(Date incomeTime, String referralId) {
//获取到incomeTime的前一天时间
DateTime offsetDay = DateUtil.offsetDay(incomeTime, -1);
//运用LambdaQueryWrapper条件构造器查询
ChronicChatLog one = this.getOne(new LambdaQueryWrapper<ChronicChatLog>()
.eq(ChronicChatLog::getSignId,referralId)
//查询小于传入时间的前一天时间的数据
.le(ChronicChatLog::getChatTime, offsetDay)
//将数据库中的日期倒叙排序一遍
.orderByDesc(ChronicChatLog::getChatTime)
.last("limit 1"));
return R.ok(one);
}