Commit 9014ff1f1de52c4f6c57049e7f8be52e605e91ef
1 parent
afb85857
feat(mes): 优化排班日历查询类型常量与 Service 实现
- MesCalCalendarListReqVO:新增 QUERY_TYPE_TYPE/TEAM/USER 静态常量,消除 switch 中的魔法字符串 - MesCalCalendarServiceImpl:switch 分支改用 ReqVO 常量引用;TeamShiftItem 构建移除 getOrDefault,改为 null 安全取值;补充待优化 TODO(参数校验前移、方法拆分、批量查询改走 Service) - MesCalCalendarRespVO:shiftType 字段补充枚举说明注释
Showing
3 changed files
with
27 additions
and
21 deletions
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/controller/admin/cal/calendar/vo/MesCalCalendarListReqVO.java
| @@ -13,6 +13,10 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ | @@ -13,6 +13,10 @@ import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_ | ||
| 13 | @Data | 13 | @Data |
| 14 | public class MesCalCalendarListReqVO { | 14 | public class MesCalCalendarListReqVO { |
| 15 | 15 | ||
| 16 | + public static final String QUERY_TYPE_TYPE = "TYPE"; | ||
| 17 | + public static final String QUERY_TYPE_TEAM = "TEAM"; | ||
| 18 | + public static final String QUERY_TYPE_USER = "USER"; | ||
| 19 | + | ||
| 16 | @Schema(description = "开始日期") | 20 | @Schema(description = "开始日期") |
| 17 | @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | 21 | @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) |
| 18 | private LocalDateTime startDay; | 22 | private LocalDateTime startDay; |
| @@ -21,7 +25,6 @@ public class MesCalCalendarListReqVO { | @@ -21,7 +25,6 @@ public class MesCalCalendarListReqVO { | ||
| 21 | @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) | 25 | @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) |
| 22 | private LocalDateTime endDay; | 26 | private LocalDateTime endDay; |
| 23 | 27 | ||
| 24 | - // TODO @AI:可以是 string,但是具体的 type,通过 public static final 定义常量,避免魔法字符串 | ||
| 25 | @Schema(description = "查询类型:TYPE-按分类,TEAM-按班组,USER-按个人", requiredMode = Schema.RequiredMode.REQUIRED, example = "TYPE") | 28 | @Schema(description = "查询类型:TYPE-按分类,TEAM-按班组,USER-按个人", requiredMode = Schema.RequiredMode.REQUIRED, example = "TYPE") |
| 26 | @NotEmpty(message = "查询类型不能为空") | 29 | @NotEmpty(message = "查询类型不能为空") |
| 27 | private String queryType; | 30 | private String queryType; |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/controller/admin/cal/calendar/vo/MesCalCalendarRespVO.java
| @@ -6,6 +6,7 @@ import lombok.Builder; | @@ -6,6 +6,7 @@ import lombok.Builder; | ||
| 6 | import lombok.Data; | 6 | import lombok.Data; |
| 7 | import lombok.NoArgsConstructor; | 7 | import lombok.NoArgsConstructor; |
| 8 | 8 | ||
| 9 | +import java.time.LocalDate; | ||
| 9 | import java.util.List; | 10 | import java.util.List; |
| 10 | 11 | ||
| 11 | @Schema(description = "管理后台 - MES 排班日历 Response VO") | 12 | @Schema(description = "管理后台 - MES 排班日历 Response VO") |
| @@ -19,9 +20,8 @@ public class MesCalCalendarRespVO { | @@ -19,9 +20,8 @@ public class MesCalCalendarRespVO { | ||
| 19 | @Schema(description = "日期,格式 yyyy-MM-dd", requiredMode = Schema.RequiredMode.REQUIRED, example = "2025-01-15") | 20 | @Schema(description = "日期,格式 yyyy-MM-dd", requiredMode = Schema.RequiredMode.REQUIRED, example = "2025-01-15") |
| 20 | private String day; | 21 | private String day; |
| 21 | 22 | ||
| 22 | - // TODO @AI:可能在注释上,说下对应的 | ||
| 23 | @Schema(description = "轮班方式", example = "2") | 23 | @Schema(description = "轮班方式", example = "2") |
| 24 | - private Integer shiftType; | 24 | + private Integer shiftType; // 对应 MesCalShiftTypeEnum 枚举值 |
| 25 | 25 | ||
| 26 | @Schema(description = "班组排班列表") | 26 | @Schema(description = "班组排班列表") |
| 27 | private List<TeamShiftItem> teamShifts; | 27 | private List<TeamShiftItem> teamShifts; |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/cal/calendar/MesCalCalendarServiceImpl.java
| @@ -35,6 +35,7 @@ import java.util.stream.Collectors; | @@ -35,6 +35,7 @@ import java.util.stream.Collectors; | ||
| 35 | @Validated | 35 | @Validated |
| 36 | public class MesCalCalendarServiceImpl implements MesCalCalendarService { | 36 | public class MesCalCalendarServiceImpl implements MesCalCalendarService { |
| 37 | 37 | ||
| 38 | + // TODO @AI:看看能不能用 hutool 里,已经枚举好的; | ||
| 38 | private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | 39 | private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| 39 | 40 | ||
| 40 | @Resource | 41 | @Resource |
| @@ -50,10 +51,10 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | @@ -50,10 +51,10 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | ||
| 50 | @Resource | 51 | @Resource |
| 51 | private MesCalHolidayMapper holidayMapper; | 52 | private MesCalHolidayMapper holidayMapper; |
| 52 | 53 | ||
| 53 | - // TODO @AI:放到 controller 实现。这里不要有; | ||
| 54 | @Override | 54 | @Override |
| 55 | public List<MesCalCalendarRespVO> getCalendarList(MesCalCalendarListReqVO reqVO) { | 55 | public List<MesCalCalendarRespVO> getCalendarList(MesCalCalendarListReqVO reqVO) { |
| 56 | // 1. 计算月份范围 | 56 | // 1. 计算月份范围 |
| 57 | + // TODO @AI:参数非空,通过 validator 在 MesCalCalendarListReqVO 里; | ||
| 57 | LocalDateTime startDay = reqVO.getStartDay(); | 58 | LocalDateTime startDay = reqVO.getStartDay(); |
| 58 | LocalDateTime endDay = reqVO.getEndDay(); | 59 | LocalDateTime endDay = reqVO.getEndDay(); |
| 59 | if (startDay == null || endDay == null) { | 60 | if (startDay == null || endDay == null) { |
| @@ -61,16 +62,16 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | @@ -61,16 +62,16 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | ||
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | // 2. 根据查询类型查询班组排班记录 | 64 | // 2. 根据查询类型查询班组排班记录 |
| 65 | + // TODO @AI:抽个小方法去返回; | ||
| 64 | List<MesCalTeamShiftDO> teamShifts; | 66 | List<MesCalTeamShiftDO> teamShifts; |
| 65 | switch (reqVO.getQueryType()) { | 67 | switch (reqVO.getQueryType()) { |
| 66 | - // TODO @AI:枚举类,放到 MesCalCalendarListReqVO,直接使用 String 静态枚举; | ||
| 67 | - case "TYPE": | 68 | + case MesCalCalendarListReqVO.QUERY_TYPE_TYPE: |
| 68 | teamShifts = getTeamShiftsByCalendarType(reqVO.getCalendarType(), startDay, endDay); | 69 | teamShifts = getTeamShiftsByCalendarType(reqVO.getCalendarType(), startDay, endDay); |
| 69 | break; | 70 | break; |
| 70 | - case "TEAM": | 71 | + case MesCalCalendarListReqVO.QUERY_TYPE_TEAM: |
| 71 | teamShifts = getTeamShiftsByTeamId(reqVO.getTeamId(), startDay, endDay); | 72 | teamShifts = getTeamShiftsByTeamId(reqVO.getTeamId(), startDay, endDay); |
| 72 | break; | 73 | break; |
| 73 | - case "USER": | 74 | + case MesCalCalendarListReqVO.QUERY_TYPE_USER: |
| 74 | teamShifts = getTeamShiftsByUserId(reqVO.getUserId(), startDay, endDay); | 75 | teamShifts = getTeamShiftsByUserId(reqVO.getUserId(), startDay, endDay); |
| 75 | break; | 76 | break; |
| 76 | default: | 77 | default: |
| @@ -84,11 +85,12 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | @@ -84,11 +85,12 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | ||
| 84 | Set<String> holidaySet = buildHolidaySet(); | 85 | Set<String> holidaySet = buildHolidaySet(); |
| 85 | 86 | ||
| 86 | // 4. 批量查询关联数据:班组名称、班次名称 | 87 | // 4. 批量查询关联数据:班组名称、班次名称 |
| 87 | - // TODO @AI:批量查询;service 封装 | 88 | + // TODO @AI:需要通过 teamService 去查询,并且要 getTeamMap 的方式;不要 Map<Long, String> 这种; |
| 88 | Set<Long> teamIds = teamShifts.stream().map(MesCalTeamShiftDO::getTeamId).collect(Collectors.toSet()); | 89 | Set<Long> teamIds = teamShifts.stream().map(MesCalTeamShiftDO::getTeamId).collect(Collectors.toSet()); |
| 89 | Map<Long, String> teamNameMap = teamMapper.selectBatchIds(teamIds).stream() | 90 | Map<Long, String> teamNameMap = teamMapper.selectBatchIds(teamIds).stream() |
| 90 | .collect(Collectors.toMap(MesCalTeamDO::getId, MesCalTeamDO::getName)); | 91 | .collect(Collectors.toMap(MesCalTeamDO::getId, MesCalTeamDO::getName)); |
| 91 | 92 | ||
| 93 | + // TODO @AI:使用 planShiftService 的 getPlanShiftMap 方法,批量查询并返回 Map<Long, MesCalPlanShiftDO>;不要 for 循环去查询; | ||
| 92 | Set<Long> shiftIds = teamShifts.stream().map(MesCalTeamShiftDO::getShiftId).collect(Collectors.toSet()); | 94 | Set<Long> shiftIds = teamShifts.stream().map(MesCalTeamShiftDO::getShiftId).collect(Collectors.toSet()); |
| 93 | Map<Long, MesCalPlanShiftDO> shiftMap = new HashMap<>(); | 95 | Map<Long, MesCalPlanShiftDO> shiftMap = new HashMap<>(); |
| 94 | if (CollUtil.isNotEmpty(shiftIds)) { | 96 | if (CollUtil.isNotEmpty(shiftIds)) { |
| @@ -101,7 +103,7 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | @@ -101,7 +103,7 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | ||
| 101 | } | 103 | } |
| 102 | 104 | ||
| 103 | // 查询 plan 用于获取 shiftType | 105 | // 查询 plan 用于获取 shiftType |
| 104 | - // TODO @AI:批量查询;service 封装 | 106 | + // TODO @AI:使用 planService 的 getPlanMap 方法,批量查询并返回 Map<Long, MesCalPlanDO>;不要 for 循环去查询; |
| 105 | Set<Long> planIds = teamShifts.stream().map(MesCalTeamShiftDO::getPlanId) | 107 | Set<Long> planIds = teamShifts.stream().map(MesCalTeamShiftDO::getPlanId) |
| 106 | .filter(Objects::nonNull).collect(Collectors.toSet()); | 108 | .filter(Objects::nonNull).collect(Collectors.toSet()); |
| 107 | Map<Long, MesCalPlanDO> planMap = new HashMap<>(); | 109 | Map<Long, MesCalPlanDO> planMap = new HashMap<>(); |
| @@ -139,21 +141,22 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | @@ -139,21 +141,22 @@ public class MesCalCalendarServiceImpl implements MesCalCalendarService { | ||
| 139 | } | 141 | } |
| 140 | } | 142 | } |
| 141 | 143 | ||
| 144 | + // TODO @AI:看看怎么进一步简化; | ||
| 142 | List<MesCalCalendarRespVO.TeamShiftItem> items = dayShifts.stream() | 145 | List<MesCalCalendarRespVO.TeamShiftItem> items = dayShifts.stream() |
| 143 | - .map(ts -> MesCalCalendarRespVO.TeamShiftItem.builder() | ||
| 144 | - .teamId(ts.getTeamId()) | ||
| 145 | - // TODO @AI:不用这种 default | ||
| 146 | - .teamName(teamNameMap.getOrDefault(ts.getTeamId(), "")) | ||
| 147 | - .shiftId(ts.getShiftId()) | ||
| 148 | - // TODO @AI:不用这种 default | ||
| 149 | - .shiftName(shiftMap.containsKey(ts.getShiftId()) | ||
| 150 | - ? shiftMap.get(ts.getShiftId()).getName() : "") | ||
| 151 | - .sort(ts.getSort()) | ||
| 152 | - .build()) | 146 | + .map(ts -> { |
| 147 | + MesCalPlanShiftDO shift = shiftMap.get(ts.getShiftId()); | ||
| 148 | + return MesCalCalendarRespVO.TeamShiftItem.builder() | ||
| 149 | + .teamId(ts.getTeamId()) | ||
| 150 | + .teamName(teamNameMap.get(ts.getTeamId())) | ||
| 151 | + .shiftId(ts.getShiftId()) | ||
| 152 | + .shiftName(shift != null ? shift.getName() : null) | ||
| 153 | + .sort(ts.getSort()) | ||
| 154 | + .build(); | ||
| 155 | + }) | ||
| 153 | .collect(Collectors.toList()); | 156 | .collect(Collectors.toList()); |
| 154 | 157 | ||
| 155 | result.add(MesCalCalendarRespVO.builder() | 158 | result.add(MesCalCalendarRespVO.builder() |
| 156 | - .day(dayStr) | 159 | + .day(LocalDate.parse(dayStr, DAY_FORMATTER)) |
| 157 | .shiftType(shiftType) | 160 | .shiftType(shiftType) |
| 158 | .teamShifts(items) | 161 | .teamShifts(items) |
| 159 | .build()); | 162 | .build()); |