1
0
Fork 0

feat(screen): 优化日期格式化处理

- 在 DateUtils 中添加 formatToYearMonth 方法,用于将日期对象格式化为年月字符串
- 在 QscTaxBizTopBo 中为 month 字段添加 DateTimeFormat 注解,指定格式为 yyyy-MM
- 修改 QscTaxBizTopServiceImpl 和 QscTaxBizVolServiceImpl,使用新的 formatToYearMonth 方法进行日期格式化
- 移除 QscTaxBizVolServiceImpl 中冗余的 formatToYearMonth 方法
master
karlkyo 9 months ago
parent b3a2a18141
commit bb57e681d3

@ -165,4 +165,38 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
/**
*
*
* @param date
* @return yyyy-MM
*/
public static String formatToYearMonth(Object date) {
if (date == null) {
return null;
}
// 如果已经是字符串格式且符合yyyy-MM格式直接返回
if (date instanceof String) {
String dateStr = (String) date;
if (dateStr.matches("\\d{4}-\\d{2}")) {
return dateStr;
}
// 尝试解析字符串日期
Date parsedDate = parseDate(dateStr);
if (parsedDate != null) {
return parseDateToStr(YYYY_MM, parsedDate);
}
}
// 如果是Date类型直接格式化
if (date instanceof Date) {
return parseDateToStr(YYYY_MM, (Date) date);
}
// 尝试解析其他类型的日期
Date parsedDate = parseDate(date);
if (parsedDate != null) {
return parseDateToStr(YYYY_MM, parsedDate);
}
return null;
}
}

@ -11,6 +11,7 @@ import java.util.Date;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ziyun.common.core.domain.BaseEntity;
import org.springframework.format.annotation.DateTimeFormat;
/**
* qsc_tax_biz_top
@ -33,6 +34,7 @@ public class QscTaxBizTopBo extends BaseEntity {
*
*/
@NotNull(message = "时间维度不能为空", groups = { AddGroup.class, EditGroup.class })
@DateTimeFormat(pattern = "yyyy-MM")
private Date month;
/**

@ -15,6 +15,7 @@ import com.ziyun.screen.domain.vo.QscTaxBizTopVo;
import com.ziyun.screen.domain.QscTaxBizTop;
import com.ziyun.screen.mapper.QscTaxBizTopMapper;
import com.ziyun.screen.service.IQscTaxBizTopService;
import com.ziyun.common.utils.DateUtils;
import java.util.List;
import java.util.Map;
@ -63,7 +64,10 @@ public class QscTaxBizTopServiceImpl implements IQscTaxBizTopService {
private LambdaQueryWrapper<QscTaxBizTop> buildQueryWrapper(QscTaxBizTopBo bo) {
Map<String, Object> params = bo.getParams();
LambdaQueryWrapper<QscTaxBizTop> lqw = Wrappers.lambdaQuery();
lqw.eq(bo.getMonth() != null, QscTaxBizTop::getMonth, bo.getMonth());
// 如果month不为空则按年月格式进行模糊查询
if (bo.getMonth() != null) {
lqw.like(QscTaxBizTop::getMonth, DateUtils.formatToYearMonth(bo.getMonth()));
}
lqw.like(StringUtils.isNotBlank(bo.getBizName()), QscTaxBizTop::getBizName, bo.getBizName());
lqw.eq(bo.getBizVol() != null, QscTaxBizTop::getBizVol, bo.getBizVol());
lqw.eq(bo.getBizRatio() != null, QscTaxBizTop::getBizRatio, bo.getBizRatio());

@ -66,7 +66,7 @@ public class QscTaxBizVolServiceImpl implements IQscTaxBizVolService {
LambdaQueryWrapper<QscTaxBizVol> lqw = Wrappers.lambdaQuery();
// 如果bizMonthly不为空则按年月格式进行模糊查询
if (bo.getBizMonthly() != null) {
lqw.like(QscTaxBizVol::getBizMonthly, formatToYearMonth(bo.getBizMonthly()));
lqw.like(QscTaxBizVol::getBizMonthly, DateUtils.formatToYearMonth(bo.getBizMonthly()));
}
lqw.eq(bo.getBizMonthlyVol() != null, QscTaxBizVol::getBizMonthlyVol, bo.getBizMonthlyVol());
lqw.eq(StringUtils.isNotBlank(bo.getBizMonthlyRatio()), QscTaxBizVol::getBizMonthlyRatio, bo.getBizMonthlyRatio());
@ -74,32 +74,6 @@ public class QscTaxBizVolServiceImpl implements IQscTaxBizVolService {
lqw.eq(bo.getDeptId() != null, QscTaxBizVol::getDeptId, bo.getDeptId());
return lqw;
}
/**
*
*
* @param date
* @return yyyy-MM
*/
private String formatToYearMonth(Object date) {
if (date == null) {
return null;
}
// 如果已经是字符串格式且符合yyyy-MM格式直接返回
if (date instanceof String) {
String dateStr = (String) date;
if (dateStr.matches("\\d{4}-\\d{2}")) {
return dateStr;
}
}
// 尝试解析日期
Date parsedDate = DateUtils.parseDate(date);
if (parsedDate != null) {
return DateUtils.parseDateToStr(DateUtils.YYYY_MM, parsedDate);
}
// 如果无法解析,返回原始字符串
return date.toString();
}
/**
*
*/

Loading…
Cancel
Save