fix: 计划日期转换为 string

This commit is contained in:
xiaozzzi 2024-02-06 12:21:18 +03:00
parent a99d226481
commit 10fc6d7f26
4 changed files with 30 additions and 23 deletions

View File

@ -8,7 +8,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
@ -32,7 +31,7 @@ public class PlanController {
* @param month 查询的月份, 会查询该月的所有每日计划
*/
@GetMapping("/list/day")
public R<Map<Date, List<PlanDayRes>>> days(String month) {
public R<Map<String, List<PlanDayRes>>> days(String month) {
return R.ok(baseService.listDay(month, AuthContext.getUserId()));
}

View File

@ -28,12 +28,19 @@ public class PlanService extends ServiceImpl<PlanMapper, PlanEntity> {
/**
* 按月查询每日计划
*/
public Map<Date, List<PlanDayRes>> listDay(String month, Long userId) {
public Map<String, List<PlanDayRes>> listDay(String month, Long userId) {
PlanEntity where = new PlanEntity();
where.setPlanMonth(month);
where.setUserId(userId);
where.setType(PlanTypeEnum.DAY.getType());
return PlanUtil.sortToTreeMap(BeanUtil.toList(baseMapper.listAll(where), PlanDayRes.class), false);
List<PlanDayRes> list = new ArrayList<>();
for (PlanEntity plan : baseMapper.listAll(where)) {
PlanDayRes res = BeanUtil.toObj(plan, PlanDayRes.class);
res.setPlanDate(DateUtils.toYMD(plan.getPlanDate()) + " 00:00:00");
list.add(res);
}
return PlanUtil.sortToTreeMap(list, false);
}
/**

View File

@ -3,7 +3,6 @@ package com.blossom.backend.server.plan.pojo;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 每日计划
@ -34,7 +33,7 @@ public class PlanDayRes implements Serializable {
/**
* 日期
*/
private Date planDate;
private String planDate;
/**
* 计划开始时间
*/

View File

@ -4,10 +4,12 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.blossom.backend.server.plan.pojo.PlanDayRes;
import com.blossom.common.base.util.DateUtils;
import com.blossom.common.base.util.SortUtil;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/**
@ -22,8 +24,8 @@ public class PlanUtil {
*
* @param plans 计划列表
*/
private static TreeMap<Date, List<PlanDayRes>> byDay(List<PlanDayRes> plans) {
TreeMap<Date, List<PlanDayRes>> map = new TreeMap<>();
private static TreeMap<String, List<PlanDayRes>> byDay(List<PlanDayRes> plans) {
TreeMap<String, List<PlanDayRes>> map = new TreeMap<>();
for (PlanDayRes plan : plans) {
List<PlanDayRes> list = map.getOrDefault(plan.getPlanDate(), new ArrayList<>());
list.add(plan);
@ -57,7 +59,7 @@ public class PlanUtil {
if (CollUtil.isEmpty(plans)) {
return plans;
}
Map<Date, List<PlanDayRes>> byDay = byDay(plans);
Map<String, List<PlanDayRes>> byDay = byDay(plans);
Map<Long, List<PlanDayRes>> byGroupId = byGroupId(plans);
// 先为所有计划设置默认排序
@ -105,13 +107,13 @@ public class PlanUtil {
* @param plans 计划列表, 传入的计划列表需要提前按 {@link PlanDayRes#getPlanDate()} 升序
* @return treeMap key 为日期(), value 为该日期计划数组
*/
public static TreeMap<Date, List<PlanDayRes>> sortToTreeMap(List<PlanDayRes> plans, boolean debug) {
public static TreeMap<String, List<PlanDayRes>> sortToTreeMap(List<PlanDayRes> plans, boolean debug) {
if (CollUtil.isEmpty(plans)) {
return new TreeMap<>();
}
Map<Date, List<PlanDayRes>> map = sort(plans).stream()
Map<String, List<PlanDayRes>> map = sort(plans).stream()
.collect(Collectors.groupingBy(PlanDayRes::getPlanDate));
TreeMap<Date, List<PlanDayRes>> treeMap = MapUtil.sort(map);
TreeMap<String, List<PlanDayRes>> treeMap = MapUtil.sort(map);
treeMap.forEach((k, v) -> {
v = v.stream().sorted((p1, p2) -> SortUtil.intSort.compare(p1.getSort(), p2.getSort())).collect(Collectors.toList());
treeMap.put(k, v);
@ -127,9 +129,9 @@ public class PlanUtil {
/**
* 控制台打印计划的排序
*/
private static void debugTreeMap(TreeMap<Date, List<PlanDayRes>> treeMap) {
for (Date date : treeMap.keySet()) {
System.out.print(DateUtils.format(date, DateUtils.PATTERN_YYYYMMDD) + "|");
private static void debugTreeMap(TreeMap<String, List<PlanDayRes>> treeMap) {
for (String date : treeMap.keySet()) {
System.out.print(date + "|");
}
System.out.println();
for (int i = 0; i < treeMap.values().size(); i++) {
@ -151,7 +153,7 @@ public class PlanUtil {
* @param date 日期
* @param sort 顺序
*/
private static PlanDayRes getHolderPlan(Date date, int sort) {
private static PlanDayRes getHolderPlan(String date, int sort) {
PlanDayRes holder = new PlanDayRes();
holder.setId(sort * -1L - 1);
holder.setGroupId(0L);
@ -166,31 +168,31 @@ public class PlanUtil {
PlanDayRes p1_1 = new PlanDayRes();
p1_1.setId(1L);
p1_1.setGroupId(1L);
p1_1.setPlanDate(DateUtils.parse("2023-09-01", DateUtils.PATTERN_YYYYMMDD));
p1_1.setPlanDate("2023-09-01");
plans.add(p1_1);
PlanDayRes p1_2 = new PlanDayRes();
p1_2.setId(2L);
p1_2.setGroupId(1L);
p1_2.setPlanDate(DateUtils.parse("2023-09-02", DateUtils.PATTERN_YYYYMMDD));
p1_2.setPlanDate("2023-09-02");
plans.add(p1_2);
PlanDayRes p2 = new PlanDayRes();
p2.setId(3L);
p2.setGroupId(2L);
p2.setPlanDate(DateUtils.parse("2023-09-01", DateUtils.PATTERN_YYYYMMDD));
p2.setPlanDate("2023-09-01");
plans.add(p2);
PlanDayRes p3_1 = new PlanDayRes();
p3_1.setId(4L);
p3_1.setGroupId(3L);
p3_1.setPlanDate(DateUtils.parse("2023-09-02", DateUtils.PATTERN_YYYYMMDD));
p3_1.setPlanDate("2023-09-02");
plans.add(p3_1);
PlanDayRes p3_2 = new PlanDayRes();
p3_2.setId(4L);
p3_2.setGroupId(3L);
p3_2.setPlanDate(DateUtils.parse("2023-09-03", DateUtils.PATTERN_YYYYMMDD));
p3_2.setPlanDate("2023-09-03");
plans.add(p3_2);
sortToTreeMap(plans, true);