package com.healthcare.ohctech.service.impl; import com.healthcare.ohctech.dto.ShiftDetailsDto; import com.healthcare.ohctech.entity.OhcType; import com.healthcare.ohctech.entity.Patient; import com.healthcare.ohctech.entity.ShiftDetails; import com.healthcare.ohctech.repository.OhcTypeRepo; import com.healthcare.ohctech.repository.PatientRepo; import com.healthcare.ohctech.repository.ShiftDetailsRepo; import com.healthcare.ohctech.service.ShiftDetailsService; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service public class ShiftDetailsServiceImpl implements ShiftDetailsService { private final ShiftDetailsRepo shiftDetailsRepo; private final PatientRepo patientRepo; private final OhcTypeRepo ohcTypeRepo; public ShiftDetailsServiceImpl(ShiftDetailsRepo shiftDetailsRepo, PatientRepo patientRepo, OhcTypeRepo ohcTypeRepo) { this.shiftDetailsRepo = shiftDetailsRepo; this.patientRepo = patientRepo; this.ohcTypeRepo = ohcTypeRepo; } @Override public ShiftDetails getShiftDetailsById(Long shiftId) { return shiftDetailsRepo.findById(shiftId) .orElseThrow(() -> new RuntimeException("Shift details not found for ID: " + shiftId)); } @Override public Page getAllShiftDetails(Pageable pageable) { return shiftDetailsRepo.findAll(pageable); } @Override public void addShiftDetails(ShiftDetailsDto shiftDetailsDto, Long userId) { ShiftDetails shiftDetails = convertToEntity(new ShiftDetails(), shiftDetailsDto); shiftDetails.setModifiedBy(userId); shiftDetailsRepo.save(shiftDetails); } @Override public void updateShiftDetails(ShiftDetailsDto shiftDetailsDto, Long userId) { Long shiftId = shiftDetailsDto.id(); ShiftDetails shiftDetails = shiftDetailsRepo.findById(shiftId) .orElseThrow(() -> new RuntimeException("Shift details not found for ID: " + shiftId)); convertToEntity(shiftDetails, shiftDetailsDto); shiftDetails.setModifiedBy(userId); shiftDetailsRepo.save(shiftDetails); } @Override public void deleteShiftDetails(Long shiftId) { ShiftDetails shiftDetails = shiftDetailsRepo.findById(shiftId) .orElseThrow(() -> new RuntimeException("Shift details not found for ID: " + shiftId)); shiftDetailsRepo.delete(shiftDetails); } private ShiftDetails convertToEntity(ShiftDetails shiftDetails, ShiftDetailsDto shiftDetailsDto) { shiftDetails.setShiftDate(shiftDetailsDto.shiftDate()); shiftDetails.setShiftStatus(shiftDetailsDto.shiftStatus()); Patient patient = patientRepo.findById(shiftDetailsDto.empId()) .orElseThrow(() -> new RuntimeException("Patient not found for ID: " + shiftDetailsDto.empId())); shiftDetails.setPatient(patient); OhcType ohcType = ohcTypeRepo.findById(shiftDetailsDto.ohcTypeId()) .orElseThrow(() -> new RuntimeException("OHC Type not found for ID: " + shiftDetailsDto.ohcTypeId())); shiftDetails.setOhcType(ohcType); return shiftDetails; } }