package com.ruoyi.common.utils;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import org.apache.poi.ss.formula.functions.T;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDate;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.ZoneId;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @author :yys
|
* @date : 2025/9/15 15:31
|
*/
|
public class OrderUtils {
|
|
|
/**
|
* 查询当天(基于createTime字段)的记录数量
|
* @param mapper 实体类对应的BaseMapper
|
* @param <T> 实体类泛型
|
* @return 当天记录数量
|
*/
|
public static <T> String countTodayByCreateTime(BaseMapper<T> mapper,String preFix) {
|
// 获取当天开始时间(00:00:00)
|
LocalDateTime todayStart = LocalDateTime.of(
|
LocalDateTime.now().toLocalDate(),
|
LocalTime.MIN
|
);
|
// 获取当天结束时间(23:59:59.999)
|
LocalDateTime todayEnd = LocalDateTime.of(
|
LocalDateTime.now().toLocalDate(),
|
LocalTime.MAX
|
);
|
|
// 转换为Date类型(如果实体类中createTime是LocalDateTime可直接使用)
|
Date startDate = Date.from(todayStart.atZone(ZoneId.systemDefault()).toInstant());
|
Date endDate = Date.from(todayEnd.atZone(ZoneId.systemDefault()).toInstant());
|
|
// 构建查询条件
|
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
queryWrapper.ge("create_time", startDate) // 大于等于当天开始
|
.lt("create_time", endDate); // 小于当天结束(避免毫秒精度问题)
|
|
// 执行查询
|
Long aLong = mapper.selectCount(queryWrapper);
|
// 拼接订单编号 preFix + 时间(yyyyMMdd) + 订单数量(001)
|
return preFix + LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE).replaceAll("-", "") + String.format("%03d", (aLong + 1));}
|
}
|