@Autowired
public DoctorScheduleService(DoctorScheduleRepository doctorScheduleRepository,
DoctorRepository doctorRepository,
RoomRepository roomRepository) {
this.doctorScheduleRepository = doctorScheduleRepository;
this.doctorRepository = doctorRepository;
this.roomRepository = roomRepository;
}
// 获取所有排班信息
public List<DoctorSchedule> getAllDoctorSchedule() {
return new ArrayList<>(doctorScheduleRepository.findAll());
}
// 根据ID获取排班信息
public Optional<DoctorSchedule> getDoctorScheduleById(Long id) {
return doctorScheduleRepository.findById(id);
}
// 添加排班信息
public Optional<DoctorScheduleDTO> addDoctorSchedule(DoctorScheduleDTO doctorScheduleDTO) {
return Optional.ofNullable(doctorScheduleDTO)
.filter(dto -> dto.getDoctor_id() != null && dto.getRoom_id() != null)
.flatMap(dto -> {
Optional<Doctor> doctor = doctorRepository.findById(dto.getDoctor_id());
Optional<Room> room = roomRepository.findById(dto.getRoom_id());
try {
DoctorSchedule doctorSchedule = getDoctorSchedule(dto, doctor.get(), room.get());
// 检查appointments是否为空,如果为空,就将其设置为max_appointments的值
if (doctorSchedule.getAppointments() == null) {
doctorSchedule.setAppointments(doctorSchedule.getMax_appointments());
}
doctorScheduleRepository.save(doctorSchedule);
return Optional.of(getDoctorScheduleDTO(doctorSchedule));
} catch (Exception e) {
return Optional.empty();
}
});
}
// 删除排班信息
public void deleteDoctorSchedule(Long id) {
doctorScheduleRepository.findById(id)
.ifPresent(doctorScheduleRepository::delete);
}
// 更新排班信息
public Optional<DoctorScheduleDTO> updateDoctorSchedule(Long id, DoctorScheduleDTO doctorScheduleDTO) {
return doctorScheduleRepository.findById(id)
.flatMap(doctorSchedule -> {
updateDoctorScheduleFields(doctorSchedule, doctorScheduleDTO);
doctorScheduleRepository.save(doctorSchedule);
return Optional.of(getDoctorScheduleDTO(doctorSchedule));
});
}
// 根据医生ID获取排班信息
public List<DoctorScheduleDTO> findScheduleByDoctorId(Long doctor_id) {
return doctorScheduleRepository.findScheduleByDoctorId(doctor_id).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
// 根据诊室ID获取排班信息
public List<DoctorScheduleDTO> findScheduleByRoomId(Long room_id) {
return doctorScheduleRepository.findScheduleByRoomId(room_id).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
// 根据日期获取排班信息
public List<DoctorScheduleDTO> findScheduleByDate(Long doctor_id, Date date) {
return doctorScheduleRepository.findScheduleByDate(doctor_id, date).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
// 根据一周的开始日期和结束日期获取排班信息
public List<DoctorScheduleDTO> findScheduleByWeek(Long doctor_id, Date date) {
Date[] startOfWeekAndEndOfWeek = getMondayAndSundayOfWeek(date);
Date startOfWeek = startOfWeekAndEndOfWeek[0];
Date endOfWeek = startOfWeekAndEndOfWeek[1];
return doctorScheduleRepository.findScheduleByWeek(doctor_id, startOfWeek, endOfWeek).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
// 根据一个月的开始日期和结束日期获取排班信息
public List<DoctorScheduleDTO> findScheduleByMonth(Long doctor_id, Date date) {
Date[] startOfMonthAndEndOfMonth = getFirstAndLastDayOfMonth(date);
Date startOfMonth = startOfMonthAndEndOfMonth[0];
Date endOfMonth = startOfMonthAndEndOfMonth[1];
return doctorScheduleRepository.findScheduleByMonth(doctor_id, startOfMonth, endOfMonth).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
public List<DoctorScheduleDTO> getScheduleByDate(Long doctor_id,Date StartDate,Date EndDate){
return doctorScheduleRepository.findScheduleByStartToEnd(doctor_id,StartDate,EndDate).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
//根据门诊id和日期获取当日排班表
public List<DoctorScheduleDTO> getAllScheduleByClinicIdAndDate(Long clinic_id, Date date){
return doctorScheduleRepository.findScheduleByClinicIdAndDate(clinic_id,date).stream()
.map(this::getDoctorScheduleDTO)
.collect(Collectors.toList());
}
private static Date[] getMondayAndSundayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置日历为这一周的第一天(星期一)
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
Date monday = calendar.getTime();
// 设置日历为这一周的最后一天(星期天),注意这里需要加6天,因为set方法已经设置为了星期一
calendar.add(Calendar.DAY_OF_WEEK, 6);
Date sunday = calendar.getTime();
// 返回包含星期一和星期天日期的数组
return new Date[]{monday, sunday};
}
private static Date[] getFirstAndLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置日历为这个月的第一天(通常是1号)
calendar.set(Calendar.DAY_OF_MONTH, 1);
Date firstDay = calendar.getTime();
// 设置日历为这个月的最后一天
calendar.add(Calendar.MONTH, 1); // 下一个月的第一天
calendar.add(Calendar.DAY_OF_MONTH, -1); // 减去一天,即为这个月的最后一天
Date lastDay = calendar.getTime();
// 返回包含这个月第一天和最后一天日期的数组
return new Date[]{firstDay, lastDay};
}
// 私有方法,用于将DoctorSchedule实体转换为DoctorScheduleDTO
private DoctorScheduleDTO getDoctorScheduleDTO(DoctorSchedule doctorSchedule) {
DoctorScheduleDTO dto = new DoctorScheduleDTO();
dto.setSchedule_id(doctorSchedule.getSchedule_id());
dto.setDoctor_id(doctorSchedule.getDoctor().getDoctor_id());
dto.setRoom_id(doctorSchedule.getRoom().getRoom_id());
dto.setDate(doctorSchedule.getDate());
dto.setWork_time(doctorSchedule.getWork_time());
dto.setStart_time(doctorSchedule.getStart_time());
dto.setEnd_time(doctorSchedule.getEnd_time());
dto.setMax_appointments(doctorSchedule.getMax_appointments());
dto.setAppointments(doctorSchedule.getAppointments());
dto.setType(doctorSchedule.getType());
dto.setCost(setCost(doctorSchedule.getCost()));
dto.setSchedule_status(doctorSchedule.getSchedule_status());
return dto;
}
// 私有方法,用于将DoctorScheduleDTO转换为DoctorSchedule实体
private DoctorSchedule getDoctorSchedule(DoctorScheduleDTO dto, Doctor doctor, Room room) {
DoctorSchedule doctorSchedule = new DoctorSchedule();
doctorSchedule.setDoctor(doctor);
doctorSchedule.setRoom(room);
doctorSchedule.setDate(dto.getDate());
updateDoctorScheduleFields(doctorSchedule, dto);
return doctorSchedule;
}
// 私有方法,用于更新DoctorSchedule实体的字段
private void updateDoctorScheduleFields(DoctorSchedule doctorSchedule, DoctorScheduleDTO dto) {
Optional.ofNullable(dto.getDoctor_id())
.flatMap(doctorRepository::findById)
.ifPresent(doctorSchedule::setDoctor);
Optional.ofNullable(dto.getRoom_id())
.flatMap(roomRepository::findById)
.ifPresent(doctorSchedule::setRoom);
Optional.ofNullable(dto.getDate())
.map(date -> date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate())
.filter(localDate -> !localDate.isBefore(LocalDate.now()))
.ifPresent(localDate -> doctorSchedule.setDate(java.sql.Date.valueOf(localDate)));
Optional.ofNullable(dto.getWork_time())
.filter(workTime -> !workTime.isEmpty())
.ifPresent(doctorSchedule::setWork_time);
Optional.ofNullable(dto.getStart_time())
.filter(startTime -> !startTime.isEmpty())
.ifPresent(doctorSchedule::setStart_time);
Optional.ofNullable(dto.getEnd_time())
.filter(endTime -> !endTime.isEmpty())
.ifPresent(doctorSchedule::setEnd_time);
Optional.ofNullable(dto.getMax_appointments())
.filter(maxAppointments -> maxAppointments > 0)
.ifPresent(doctorSchedule::setMax_appointments);
Optional.ofNullable(dto.getAppointments())
.filter(appointments -> appointments >= 0)
.ifPresent(doctorSchedule::setAppointments);
Optional.ofNullable(dto.getType())
.filter(type -> !type.isEmpty())
.ifPresent(doctorSchedule::setType);
Optional.ofNullable(dto.getCost())
.filter(cost -> cost > 0)
.map(this::setCost)
.ifPresent(doctorSchedule::setCost);
Optional.ofNullable(dto.getSchedule_status())
.filter(status -> !status.isEmpty())
.ifPresent(doctorSchedule::setSchedule_status);
}
// 私有方法,用于设置价格精度
private double setCost(double cost) {
BigDecimal costBigDecimal = new BigDecimal(cost);
BigDecimal roundedCost = costBigDecimal.setScale(2, RoundingMode.HALF_UP);
return roundedCost.doubleValue();
}