ohctechv3/.svn/pristine/ac/ac625186e36dbc2fd23a67cbdb299485b99a250d.svn-base
2024-10-28 15:03:36 +05:30

164 lines
8.5 KiB
Plaintext

package com.healthcare.ohctech.service.impl;
import com.healthcare.ohctech.dto.PrescriptionMasterDto;
import com.healthcare.ohctech.dto.TaskFrequencyMasterDto;
import com.healthcare.ohctech.entity.*;
import com.healthcare.ohctech.repository.*;
import com.healthcare.ohctech.service.PrescriptionMasterService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
public class PrescriptionMasterServiceImpl implements PrescriptionMasterService {
private final PrescriptionMasterRepo prescriptionMasterRepo;
private final ItemRepo itemRepo;
private final PatientRepo patientRepo;
private final AilmentRepo ailmentRepo;
private final AbnormalityRepo abnormalityRepo;
private final MedicineTimingRepo medicineTimingRepo;
private final MedicineFrequencyRepo medicineFrequencyRepo;
private final HealthAdviceRepo healthAdviceRepo;
private final OhcTypeRepo ohcTypeRepo;
private final RouteOfAdministrationRepo routeOfAdministrationRepo;
public PrescriptionMasterServiceImpl(PrescriptionMasterRepo prescriptionMasterRepo, ItemRepo itemRepo, PatientRepo patientRepo, AilmentRepo ailmentRepo, AbnormalityRepo abnormalityRepo, MedicineTimingRepo medicineTimingRepo, MedicineFrequencyRepo medicineFrequencyRepo, HealthAdviceRepo healthAdviceRepo, OhcTypeRepo ohcTypeRepo, RouteOfAdministrationRepo routeOfAdministrationRepo) {
this.prescriptionMasterRepo = prescriptionMasterRepo;
this.itemRepo = itemRepo;
this.patientRepo = patientRepo;
this.ailmentRepo = ailmentRepo;
this.abnormalityRepo = abnormalityRepo;
this.medicineTimingRepo = medicineTimingRepo;
this.medicineFrequencyRepo = medicineFrequencyRepo;
this.healthAdviceRepo = healthAdviceRepo;
this.ohcTypeRepo = ohcTypeRepo;
this.routeOfAdministrationRepo = routeOfAdministrationRepo;
}
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}
@Override
public PrescriptionMaster getPrescriptionMasterById(Long prescriptionId) {
return prescriptionMasterRepo.findById(prescriptionId)
.orElseThrow(() -> new RuntimeException("Prescription not found for ID: " + prescriptionId));
}
@Override
public Page<PrescriptionMaster> getAllPrescriptionMasters(Pageable pageable) {
return prescriptionMasterRepo.findAll(pageable);
}
@Override
public List<PrescriptionMaster> addPrescriptionMaster(PrescriptionMasterDto prescriptionMasterDto, Long userId) {
List<PrescriptionMaster> prescriptionMasters = new ArrayList<>();
for (PrescriptionMasterDto.MedicineDto medicineDto : prescriptionMasterDto.medicines()) {
PrescriptionMaster prescriptionMaster = new PrescriptionMaster();
validateHealthAdvices(medicineDto.healthAdvices());
convertToEntity(prescriptionMaster, medicineDto, prescriptionMasterDto);
prescriptionMaster.setModifiedBy(userId);
prescriptionMasters.add(prescriptionMaster);
}
return prescriptionMasterRepo.saveAll(prescriptionMasters);
}
private void validateHealthAdvices(List<Long> healthAdvices) {
if (healthAdvices != null) {
for (Long id : healthAdvices) {
if (!healthAdviceRepo.existsById(id)) {
throw new RuntimeException("Health Advice not found for ID: " + id);
}
}
}
}
@Override
public PrescriptionMaster updatePrescriptionMaster(PrescriptionMasterDto prescriptionMasterDto, Long userId) {
Long prescriptionId = prescriptionMasterDto.id();
validateHealthAdvices(prescriptionMasterDto.medicines().get(0).healthAdvices());
PrescriptionMaster prescriptionMaster = prescriptionMasterRepo.findById(prescriptionId)
.orElseThrow(() -> new ResourceNotFoundException("Prescription not found for ID: " + prescriptionId));
if (!prescriptionMasterDto.medicines().isEmpty()) {
convertToEntity(prescriptionMaster, prescriptionMasterDto.medicines().get(0), prescriptionMasterDto);
}
prescriptionMaster.setModifiedBy(userId);
return prescriptionMasterRepo.save(prescriptionMaster);
}
public void updatePrescriptionMasters(List<PrescriptionMasterDto> prescriptionMasterDtos, Long userId) {
for (PrescriptionMasterDto dto : prescriptionMasterDtos) {
updatePrescriptionMaster(dto, userId);
}
}
@Override
public void deletePrescriptionMaster(Long prescriptionId) {
PrescriptionMaster prescriptionMaster = prescriptionMasterRepo.findById(prescriptionId)
.orElseThrow(() -> new RuntimeException("Prescription not found for ID: " + prescriptionId));
prescriptionMasterRepo.delete(prescriptionMaster);
}
private PrescriptionMaster convertToEntity(PrescriptionMaster prescriptionMaster, PrescriptionMasterDto.MedicineDto medicineDto, PrescriptionMasterDto prescriptionMasterDto) {
Ailment ailment = ailmentRepo.findById(prescriptionMasterDto.diagnosis())
.orElseThrow(() -> new RuntimeException("Diagnosis not found for ID: " + prescriptionMasterDto.diagnosis()));
Abnormality abnormality = abnormalityRepo.findById(prescriptionMasterDto.chronicIllness())
.orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + prescriptionMasterDto.chronicIllness()));
MedicineTiming medicineTiming = medicineTimingRepo.findById(medicineDto.medicineTiming())
.orElseThrow(() -> new RuntimeException("Medicine Timing not found for ID: " + medicineDto.medicineTiming()));
MedicineFrequency medicineFrequency = medicineFrequencyRepo.findById(medicineDto.medicineFrequency())
.orElseThrow(() -> new RuntimeException("Medicine Frequency not found for ID: " + medicineDto.medicineFrequency()));
RouteOfAdministration routeOfAdministration = routeOfAdministrationRepo.findById(medicineDto.adminRoute())
.orElseThrow(() -> new RuntimeException("Route of Administration not found for ID: " + medicineDto.adminRoute()));
OhcType ohcType = ohcTypeRepo.findById(prescriptionMasterDto.ohcType())
.orElseThrow(() -> new RuntimeException("Ohc Type not found for ID: " + prescriptionMasterDto.ohcType()));
// Item medicineNames = itemRepo.findByIdAndStatus(medicineDto.medicineName(), "1")
// .orElseThrow(() -> new RuntimeException("Item with status 1 not Found for medicine Id " + medicineDto.medicineName()));
List<Item> availableMedicines = itemRepo.findItemsByStatusAndOhcType(prescriptionMasterDto.ohcType());
Item medicineNames = availableMedicines.stream()
.filter(item -> item.getId().equals(medicineDto.medicineName()))
.findFirst()
.orElseThrow(() -> new RuntimeException("Item with status 1 not found for medicine Id " + medicineDto.medicineName()));
Patient empId = patientRepo.findById(prescriptionMasterDto.empId())
.orElseThrow(() -> new RuntimeException("EmpId not Found for Id " + prescriptionMasterDto.empId()));
prescriptionMaster.setDiagnosis(ailment);
prescriptionMaster.setChronicIllness(abnormality);
prescriptionMaster.setAdminRoute(routeOfAdministration);
prescriptionMaster.setMedicineFrequency(medicineFrequency);
prescriptionMaster.setMedicineTiming(medicineTiming);
if (medicineDto.healthAdvices() != null) {
String advices = medicineDto.healthAdvices().stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
prescriptionMaster.setHealthAdvices(advices);
} else {
prescriptionMaster.setHealthAdvices("");
}
prescriptionMaster.setEmpId(empId);
prescriptionMaster.setMedicineName(medicineNames);
prescriptionMaster.setDuration(medicineDto.duration());
prescriptionMaster.setDoseQty(medicineDto.doseQty());
prescriptionMaster.setDiagnosisDate(prescriptionMasterDto.diagnosisDate());
prescriptionMaster.setRemissionDate(prescriptionMasterDto.remissionDate());
prescriptionMaster.setRemark(medicineDto.remark());
prescriptionMaster.setOhcType(ohcType);
return prescriptionMaster;
}
}