ohctechv3/.svn/pristine/42/42f698b7a86785eae6c4e845785b1f07fa8b37ae.svn-base

158 lines
6.8 KiB
Plaintext
Raw Normal View History

2024-10-28 15:03:36 +05:30
package com.healthcare.ohctech.service.impl;
import com.healthcare.ohctech.dto.TreatmentDto;
import com.healthcare.ohctech.dto.TreatmentExternalDto;
import com.healthcare.ohctech.dto.TreatmentInternalExternalDto;
import com.healthcare.ohctech.entity.*;
import com.healthcare.ohctech.repository.*;
import com.healthcare.ohctech.service.TreatmentService;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Service
public class TreatmentServiceImpl implements TreatmentService {
@Autowired
private TreatmentRepo treatmentRepo;
@Autowired
private TreatmentExternalRepo treatmentExternalRepo;
@Autowired
private EmployeeAppointmentRepo employeeAppointmentRepo;
@Autowired
private ItemRepo itemRepo;
@Autowired
private MedicineFrequencyRepo medicineFrequencyRepo;
@Autowired
private MedicineTimingRepo medicineTimingRepo;
@Autowired
private RouteOfAdministrationRepo routeOfAdministrationRepo;
@Override
public List<Treatment> getTreatmentByAppointmentId(Long appointmentId) {
return treatmentRepo.findByEmployeeAppointmentId(appointmentId);
}
@Override
public List<TreatmentExternal> getTreatmentExternalsByAppointmentId(Long appointmentId) {
return treatmentExternalRepo.findByEmployeeAppointmentId(appointmentId);
}
@Override
public Page<Treatment> getAllTreatments(Pageable pageable) {
return treatmentRepo.findAll(pageable);
}
@Override
public Page<TreatmentExternal> getAllTreatmentExternals(Pageable pageable) {
return treatmentExternalRepo.findAll(pageable);
}
@Override
public void addTreatmentOrExternal(TreatmentInternalExternalDto treatmentInternalExternalDto, Long userId) {
List<Treatment> treatments = new ArrayList<>();
List<TreatmentExternal> treatmentExternals = new ArrayList<>();
for (TreatmentDto dto : treatmentInternalExternalDto.treatmentDtos()) {
if (dto.appointmentId() != null || dto.itemId() != null) {
Treatment treatment = convertToInternalEntity(new Treatment(), dto);
treatment.setModifiedBy(userId);
treatments.add(treatment);
}
}
for (TreatmentExternalDto dto : treatmentInternalExternalDto.treatmentExternalDtos()) {
TreatmentExternal treatmentExternal = convertToExternalEntity(new TreatmentExternal(), dto);
treatmentExternal.setModifiedBy(userId);
treatmentExternals.add(treatmentExternal);
}
treatmentRepo.saveAll(treatments);
treatmentExternalRepo.saveAll(treatmentExternals);
}
@Override
public void updateTreatmentOrExternal(TreatmentInternalExternalDto treatmentInternalExternalDto, Long userId) {
Long appointmentId = treatmentInternalExternalDto.treatmentDtos().get(0).appointmentId();
treatmentRepo.deleteByEmployeeAppointmentId(appointmentId);
treatmentExternalRepo.deleteByEmployeeAppointmentId(appointmentId);
addTreatmentOrExternal(treatmentInternalExternalDto, userId);
}
private Treatment convertToInternalEntity(Treatment treatment, TreatmentDto dto) {
if (dto.appointmentId() != null) {
EmployeeAppointment employeeAppointment = employeeAppointmentRepo.findById(dto.appointmentId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.appointmentId()));
treatment.setEmployeeAppointment(employeeAppointment);
}
if (dto.itemId() != null) {
Item item = itemRepo.findByIdWithMedicineForm(dto.itemId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.itemId()));
treatment.setItem(item);
}
if (dto.frequencyId() != null) {
MedicineFrequency medicineFrequency = medicineFrequencyRepo.findById(dto.frequencyId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.frequencyId()));
treatment.setMedicineFrequency(medicineFrequency);
}
if (dto.timingId() != null) {
MedicineTiming medicineTiming = medicineTimingRepo.findById(dto.timingId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.timingId()));
treatment.setMedicineTiming(medicineTiming);
}
if (dto.dosageCategoryId() != null) {
RouteOfAdministration routeOfAdministration = routeOfAdministrationRepo.findById(dto.dosageCategoryId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.dosageCategoryId()));
treatment.setRouteOfAdministration(routeOfAdministration);
}
treatment.setForDays(dto.forDays());
treatment.setDosage(dto.dosage());
treatment.setItemQty(dto.itemQty());
treatment.setItemBatchNo(dto.itemBatchNo());
treatment.setIssuedQty(dto.issuedQty());
treatment.setIssueBy(dto.issueBy());
treatment.setIsDisplay(dto.isDisplay());
treatment.setGroupItemId(dto.groupItemId());
treatment.setFollowupId(dto.followupId());
return treatment;
}
private TreatmentExternal convertToExternalEntity(TreatmentExternal treatmentExternal, TreatmentExternalDto dto) {
if (dto.appointmentId() != null) {
EmployeeAppointment employeeAppointment = employeeAppointmentRepo.findById(dto.appointmentId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.appointmentId()));
treatmentExternal.setEmployeeAppointment(employeeAppointment);
}
treatmentExternal.setFollowupId(dto.followupId());
treatmentExternal.setItemName(dto.itemName());
treatmentExternal.setFrequency(dto.frequency());
treatmentExternal.setForDays(dto.forDays());
treatmentExternal.setItemQty(dto.itemQty());
treatmentExternal.setDosage(dto.dosage());
if (dto.timing() != null) {
MedicineTiming medicineTiming = medicineTimingRepo.findById(dto.timing())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.timing()));
treatmentExternal.setMedicineTiming(medicineTiming);
}
if (dto.dosageCategoryId() != null) {
RouteOfAdministration routeOfAdministration = routeOfAdministrationRepo.findById(dto.dosageCategoryId())
.orElseThrow(() -> new RuntimeException("Ohc not found for ID: " + dto.dosageCategoryId()));
treatmentExternal.setRouteOfAdministration(routeOfAdministration);
}
return treatmentExternal;
}
}