public AttendanceVo getEmployeeMonthlyAttendance2(String type) {
AttendanceVo attendanceVo = new AttendanceVo(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
List<Attach2Vo> attendanceList = new ArrayList<>(); // 用于返回的考勤列表
// 两个容器,一个用于存储迟到的订单,另一个用于存储缺勤的订单
List<StoreOrder> lateOrders = new ArrayList<>(); // 用于存储迟到的订单
List<StoreOrder> absentOrders = new ArrayList<>(); // 用于存储缺勤的订单
Integer deliveryId = frontTokenComponent.getUserId(); // 获取当前登录的用户ID
Date currentDate = new Date(); // 获取当前日期
Calendar calendar = Calendar.getInstance(); // 创建Calendar对象
calendar.setTime(currentDate); // 设置当前日期
calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为当月的第一天
Date firstDayOfMonth = calendar.getTime(); // 获取当月的第一天
calendar.add(Calendar.MONTH, 1); // 当月加1
calendar.add(Calendar.DAY_OF_MONTH, -1); // 天数减1,得到当月的最后一天
Date lastDayOfMonth = calendar.getTime(); // 获取当月的最后一天
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 创建日期格式化对象
// 查询当月的订单
List<StoreOrder> orders = storeOrderDao.findEmployeeMonthlyOrders(deliveryId, sdf.format(firstDayOfMonth), sdf.format(lastDayOfMonth));
Map<String, List<StoreOrder>> ordersByDate = new HashMap<>(); // 按日期存储订单的Map
// 将订单按日期分组
for (StoreOrder order : orders) {
String date = sdf.format(order.getCreateTime());
ordersByDate.computeIfAbsent(date, k -> new ArrayList<>()).add(order);
}
// 遍历每一个日期,并检查该日期下的订单状态
for (Map.Entry<String, List<StoreOrder>> entry : ordersByDate.entrySet()) {
String date = entry.getKey();
List<StoreOrder> dailyOrders = entry.getValue();
String absenceFlag = "0"; // 缺勤标记,0代表正常,1代表不正常(迟到或缺勤)
// 检查每一个订单
for (StoreOrder order : dailyOrders) {
if (order.getStaffLate() == 1) { // 如果订单标记为迟到
absenceFlag = "1"; // 标记为不正常
lateOrders.add(order); // 添加到迟到订单列表
}
// 检查是否有缺勤记录
List<StaffAbsenceOrder> absenceRecords = staffAbsenceOrderDao.selectBatchIds(Collections.singleton(order.getDeliveryId()));
if (!absenceRecords.isEmpty()) {
absenceFlag = "1"; // 标记为不正常
//用缺勤表中的order_id,去eb_store_order查询对应的数据
// 提取所有相关的id
List<String> absenceIds = absenceRecords.stream()
.map(StaffAbsenceOrder::getOrderId)
.collect(Collectors.toList());
// 使用这些id去查询eb_store_order表
List<StoreOrder> relatedStoreOrders = storeOrderDao.selectBatchIds(absenceIds);
absentOrders.addAll(relatedStoreOrders); // 添加到缺勤订单列表
}
}
// 添加到考勤列表
attendanceList.add(new Attach2Vo(date, absenceFlag));
}
if ("late".equals(type)) {
attendanceVo.setLateDetails(lateOrders); // set the lateDetails
} else if ("absent".equals(type)) {
attendanceVo.setAbsenceDetails(absentOrders); // set the absenceDetails
} else {
attendanceVo.setLateDetails2(attendanceList); // set the lateDetails2
}
return attendanceVo;
}
}
public class AttendanceVo {
@ApiModelProperty(value = "一月每天的出勤")
private List<Attach2Vo> lateDetails2;
@ApiModelProperty(value = "迟到订单的详细信息,仅当type=late时填充")
private List<StoreOrder> lateDetails;
@ApiModelProperty(value = "缺勤订单的详细信息,仅当type=absent时填充")
private List<StoreOrder> absenceDetails;
}