排班按月份查询重构

This commit is contained in:
dy 2025-08-06 11:37:29 +08:00
parent b91201cf7f
commit 0a18e0c1a6

View File

@ -2,6 +2,7 @@ package org.dromara.property.service.impl;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
@ -44,6 +45,7 @@ public class AttendanceArrangementServiceImpl implements IAttendanceArrangementS
private final AttendanceGroupMapper attendanceGroupMapper;
// private final AttendanceArrangementGroupMapper arrangementGroupMapper;
private final AttendanceWeekSetMapper weekSetMapper;
@ -125,7 +127,7 @@ public class AttendanceArrangementServiceImpl implements IAttendanceArrangementS
vo.setShift(attendanceShift);
}else if (Objects.equals(attendanceGroup.getAttendanceType(), StatusConstant.SHIFTSCHEDULE)) {
} else if (Objects.equals(attendanceGroup.getAttendanceType(), StatusConstant.SHIFTSCHEDULE)) {
//3.2排班制
//将startDate作为第一天endDate作为最后一天循环判断当前日期是第几天取出当前天数的班次信息
@ -163,7 +165,6 @@ public class AttendanceArrangementServiceImpl implements IAttendanceArrangementS
}
//根据cycleDay查询出当前日期的班次信息
AttendanceScheduleCycle cycle = scheduleCycleMapper.selectOne(Wrappers.<AttendanceScheduleCycle>lambdaQuery().eq(AttendanceScheduleCycle::getGroupId, groupId).eq(AttendanceScheduleCycle::getDayNumber, cycleDay));
//将cycle存到vo中
@ -260,85 +261,153 @@ public class AttendanceArrangementServiceImpl implements IAttendanceArrangementS
public TableDataInfo<AttendanceArrangementVo> queryList(AttendanceArrangementBo bo) {
LocalDate month = bo.getMonth().atEndOfMonth();
// 1. 拿到日历的开始时间和结束时间
LocalDate calendarStartDate = month.withDayOfMonth(1);
LocalDate calendarEndDate = month.withDayOfMonth(calendarStartDate.lengthOfMonth());
// 2. 根据日历的开始时间和结束时间查询出每一天的排班信息
// 2.1 循环日历的开始时间和结束时间判断 calendarStartDate 在哪些排班中
List<AttendanceArrangementVo> result = new ArrayList<>();
//使用mybatisplus的between查询在calendarStartDate和calendarEndDate之间的排班信息
List<AttendanceArrangementVo> startResult = baseMapper.selectVoList(Wrappers.<AttendanceArrangement>lambdaQuery()
.between(AttendanceArrangement::getStartDate, calendarStartDate.atStartOfDay(ZoneId.systemDefault()).toInstant(),
calendarEndDate.atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant().minusSeconds(1)));
//查询结束时间在calendarStartDate和calendarEndDate之间的排班信息
List<AttendanceArrangement> endResult = baseMapper.selectList(Wrappers.<AttendanceArrangement>lambdaQuery()
.between(AttendanceArrangement::getEndDate, calendarStartDate.atStartOfDay(ZoneId.systemDefault()).toInstant(),
calendarEndDate.atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant().minusSeconds(1)));
for (LocalDate date = calendarStartDate; date.isBefore(calendarEndDate); date = date.plusDays(1)) {
// 2.2 根据当前日期查询在开始时间和结束时间之间的排班信息
//查询当前日期在哪些排班中当前日期大于等于排班开始日期小于等于排班结束日期
// 查询指定日期在哪些排班中
List<AttendanceArrangement> arrangementList = baseMapper.selectList(
Wrappers.<AttendanceArrangement>lambdaQuery()
.between(AttendanceArrangement::getStartDate, date.atStartOfDay(ZoneId.systemDefault()).toInstant(),
date.atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant().minusSeconds(1)));
//合并两个list并去重
List<AttendanceArrangementVo> result = new ArrayList<>(startResult.stream().distinct().toList());
result.addAll(endResult.stream().map(vo -> {
AttendanceArrangementVo newVo = MapstructUtils.convert(vo, AttendanceArrangementVo.class);
newVo.setStartDate(vo.getStartDate());
newVo.setEndDate(vo.getEndDate());
return newVo;
}).toList());
// //根据arrangementList查询出排班的id
// List<Long> scheduleIdList = arrangementList.stream().map(AttendanceArrangement::getId).toList();
// //根据排班的id查询出排班的人员详细信息
// List<AttendanceUserGroup> userGroupList = userGroupMapper.selectList(Wrappers.<AttendanceUserGroup>lambdaQuery().in(AttendanceUserGroup::getScheduleId, scheduleIdList));
//去掉result中id重复的排班信息
result = result.stream().distinct().toList();
// 根据排班查询出考勤组id
List<Long> groupIds = arrangementList.stream()
.map(AttendanceArrangement::getGroupId)
.distinct()
.toList();
// 如果当前日期没有排班则跳过当前日期
if (groupIds.isEmpty()) {
continue;
}
// 循环 groupIds循环和 AttendanceGroup 中的 id 做对比如果有相同的则取出 attendanceGroup 中的所有数据
for (Long groupId : groupIds) {
AttendanceGroup attendanceGroup = attendanceGroupMapper.selectById(groupId);
if (attendanceGroup != null) {
// 创建 AttendanceArrangementVo 对象并设置属性
AttendanceArrangementVo vo = new AttendanceArrangementVo();
vo.setId(arrangementList.get(0).getId());
vo.setStartDate(arrangementList.get(0).getStartDate());
vo.setEndDate(arrangementList.get(0).getEndDate());
//获取到第一个排班的id
Long arrangementId = arrangementList.get(0).getId();
//根据arrangementId查询出人员组的详细信息
AttendanceGroup group = attendanceGroupMapper.selectById(arrangementId);
//将group存到vo中
vo.setAttendanceGroup(group);
//根据arrangementList查询出排班的id
List<Long> scheduleIdList = arrangementList.stream().map(AttendanceArrangement::getId).distinct().toList();
//根据排班的id查询出排班的人员详细信息
List<AttendanceUserGroup> userGroupList = userGroupMapper.selectList(Wrappers.<AttendanceUserGroup>lambdaQuery().in(AttendanceUserGroup::getScheduleId, scheduleIdList));
//新建attendanceGroup对象将排班信息和考勤组信息添加到vo中
vo.setAttendanceGroup(attendanceGroup);
//将排班人员信息添加到排班信息中
vo.setUserGroupList(userGroupList);
result.add(vo);
//根据排班查询出考勤组id
List<Long> groupIds = result.stream().map(AttendanceArrangementVo::getGroupId).distinct().toList();
// 循环 groupIds循环和 AttendanceGroup 中的 id 做对比如果有相同的则取出 attendanceGroup 中的所有数据
for (Long groupId : groupIds) {
AttendanceGroup attendanceGroup = attendanceGroupMapper.selectById(groupId);
if (attendanceGroup != null) {
// 循环排班列表将排班信息和考勤组信息添加到vo中
for (AttendanceArrangementVo vo : result) {
if (vo.getGroupId().equals(groupId)) {
vo.setAttendanceGroup(attendanceGroup);
}
}
}
}
// 循环排班列表查询排班人员信息
for (AttendanceArrangementVo vo : result) {
// 根据排班查询出人员组的详细信息
List<AttendanceUserGroup> userGroupList = userGroupMapper.selectList(Wrappers.<AttendanceUserGroup>lambdaQuery().eq(AttendanceUserGroup::getScheduleId, vo.getId()));
// 将排班人员信息添加到排班信息中
vo.setUserGroupList(userGroupList);
}
// LocalDate month = bo.getMonth().atEndOfMonth();
// // 1. 拿到日历的开始时间和结束时间
// LocalDate calendarStartDate = month.withDayOfMonth(1);
// LocalDate calendarEndDate = month.withDayOfMonth(calendarStartDate.lengthOfMonth());
//
// // 2. 根据日历的开始时间和结束时间查询出每一天的排班信息
// // 2.1 循环日历的开始时间和结束时间判断 calendarStartDate 在哪些排班中
// List<AttendanceArrangementVo> result = new ArrayList<>();
//
//
// //查询所有排班信息
// List<AttendanceArrangement> arrangementList1 = baseMapper.selectList();
// //取出排班的开始时间和结束时间
// List<LocalDate> startDateList = arrangementList1.stream().map(AttendanceArrangement::getStartDate).toList();
// List<LocalDate> endDateList = arrangementList1.stream().map(AttendanceArrangement::getEndDate).toList();
//
// int count = 0;
//
// for (LocalDate date = calendarStartDate; date.isBefore(calendarEndDate); date = date.plusDays(1)) {
// // 2.2 根据当前日期查询在开始时间和结束时间之间的排班信息
// //查询当前日期在哪些排班中当前日期大于等于排班开始日期小于等于排班结束日期
//
// if (count >= startDateList.size() || count >= endDateList.size()){
// continue;
// }
//
//
//
//// // 查询指定日期在哪些排班中
//// List<AttendanceArrangement> arrangementList = baseMapper.selectList(
//// Wrappers.<AttendanceArrangement>lambdaQuery()
//// .between(AttendanceArrangement::getStartDate, date.atStartOfDay(ZoneId.systemDefault()).toInstant(),
//// date.atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant().minusSeconds(1)));
//
//
// // 查询指定日期在哪些排班中
// List<AttendanceArrangement> arrangementList = baseMapper.selectList(
// Wrappers.<AttendanceArrangement>lambdaQuery()
// .between(AttendanceArrangement::getStartDate, startDateList.get(count).atStartOfDay(ZoneId.systemDefault()).toInstant(),
// endDateList.get(count).atStartOfDay(ZoneId.systemDefault()).plusDays(1).toInstant().minusSeconds(1)));
//
//
//
//
//// //根据arrangementList查询出排班的id
//// List<Long> scheduleIdList = arrangementList.stream().map(AttendanceArrangement::getId).toList();
//// //根据排班的id查询出排班的人员详细信息
//// List<AttendanceUserGroup> userGroupList = userGroupMapper.selectList(Wrappers.<AttendanceUserGroup>lambdaQuery().in(AttendanceUserGroup::getScheduleId, scheduleIdList));
//
//
// // 根据排班查询出考勤组id
// List<Long> groupIds = arrangementList.stream()
// .map(AttendanceArrangement::getGroupId)
// .distinct()
// .toList();
//
// // 如果当前日期没有排班则跳过当前日期
// if (groupIds.isEmpty()) {
// count++;
// continue;
// }
//
// // 循环 groupIds循环和 AttendanceGroup 中的 id 做对比如果有相同的则取出 attendanceGroup 中的所有数据
// for (Long groupId : groupIds) {
// AttendanceGroup attendanceGroup = attendanceGroupMapper.selectById(groupId);
// if (attendanceGroup != null) {
// // 创建 AttendanceArrangementVo 对象并设置属性
// AttendanceArrangementVo vo = new AttendanceArrangementVo();
// vo.setId(arrangementList.get(0).getId());
// vo.setStartDate(arrangementList.get(0).getStartDate());
// vo.setEndDate(arrangementList.get(0).getEndDate());
//
// //获取到第一个排班的id
// Long arrangementId = arrangementList.get(0).getId();
// //根据arrangementId查询出人员组的详细信息
// AttendanceGroup group = attendanceGroupMapper.selectById(arrangementId);
// //将group存到vo中
// vo.setAttendanceGroup(group);
//
// //根据arrangementList查询出排班的id
// List<Long> scheduleIdList = arrangementList.stream().map(AttendanceArrangement::getId).distinct().toList();
// //根据排班的id查询出排班的人员详细信息
// List<AttendanceUserGroup> userGroupList = userGroupMapper.selectList(Wrappers.<AttendanceUserGroup>lambdaQuery().in(AttendanceUserGroup::getScheduleId, scheduleIdList));
//
//
// //新建attendanceGroup对象将排班信息和考勤组信息添加到vo中
// vo.setAttendanceGroup(attendanceGroup);
// //将排班人员信息添加到排班信息中
// vo.setUserGroupList(userGroupList);
//
// result.add(vo);
// }
// }
//
// count++;
// }
return TableDataInfo.build(result);
// // 计算交集的开始时间取较晚的开始时间和结束时间取较早的结束时间
// Date overlapStart = calendarStartDate.after(startDate)
// ? calendarStartDate : startDate;
// Date overlapEnd = calendarEndTimeDate.before(endDate)
// ? calendarEndTimeDate : endDate;
//根据交集的时间范围查询排班信息
}