package com.healthcare.ohctech.service.impl; import com.healthcare.ohctech.dto.PatientDietPlanDto; import com.healthcare.ohctech.entity.NutrientMaster; import com.healthcare.ohctech.entity.PatientConsultation; import com.healthcare.ohctech.entity.PatientDietPlan; import com.healthcare.ohctech.repository.NutrientMasterRepo; import com.healthcare.ohctech.repository.PatientConsultationRepo; import com.healthcare.ohctech.repository.PatientDietPlanRepo; import com.healthcare.ohctech.service.PatientDietPlanService; 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; import java.util.Map; import java.util.stream.Collectors; @Service public class PatientDietPlanServiceImpl implements PatientDietPlanService { @Autowired private PatientDietPlanRepo patientDietPlanRepo; @Autowired private NutrientMasterRepo nutrientMasterRepo; @Autowired private PatientConsultationRepo patientConsultationRepo; @Override public List getPatientDietPlansByConsultationId(Long consultationId) { List plans = patientDietPlanRepo.findByConsultationId(consultationId); Map dtoMap = plans.stream().collect( Collectors.groupingBy( plan -> plan.getConsultation().getId(), Collectors.collectingAndThen( Collectors.toList(), planList -> { Map dietPlanValues = planList.stream() .collect(Collectors.toMap( plan -> plan.getNutrientMaster().getId(), plan -> new PatientDietPlanDto.DietPlanValue(plan.getValue(), plan.getRemark(), plan.getNutrientMaster().getUnitMaster().getId()) )); return new PatientDietPlanDto(planList.get(0).getId(), planList.get(0).getConsultation().getId(), dietPlanValues); } ) ) ); return new ArrayList<>(dtoMap.values()); } @Override public List getAllPatientDietPlans() { List plans = patientDietPlanRepo.findAll(); Map dtoMap = plans.stream().collect( Collectors.groupingBy( plan -> plan.getConsultation().getId(), Collectors.collectingAndThen( Collectors.toList(), planList -> { Map dietPlanValues = planList.stream() .collect(Collectors.toMap( plan -> plan.getNutrientMaster().getId(), plan -> new PatientDietPlanDto.DietPlanValue(plan.getValue(), plan.getRemark(),plan.getNutrientMaster().getUnitMaster().getId()) )); return new PatientDietPlanDto(planList.get(0).getId(),planList.get(0).getConsultation().getId(), dietPlanValues); } ) ) ); return new ArrayList<>(dtoMap.values()); } @Override public void addPatientDietPlan(PatientDietPlanDto planDto, Long userId) { List plans = convertToEntity(planDto); patientDietPlanRepo.saveAll(plans); } @Override public void updatePatientDietPlan(Long consultationId, PatientDietPlanDto planDto, Long userId) { List existingPlans = patientDietPlanRepo.findByConsultationId(consultationId); for (Map.Entry entry : planDto.dietPlanValues().entrySet()) { Long nutrientId = entry.getKey(); PatientDietPlanDto.DietPlanValue planValue = entry.getValue(); PatientDietPlan plan = existingPlans.stream() .filter(p -> p.getNutrientMaster().getId().equals(nutrientId)) .findFirst() .orElse(new PatientDietPlan()); NutrientMaster nutrientMaster = nutrientMasterRepo.findById(nutrientId) .orElseThrow(() -> new RuntimeException("Nutrient not found for ID: " + nutrientId)); plan.setConsultation(patientConsultationRepo.findById(consultationId) .orElseThrow(() -> new RuntimeException("Consultation not found for ID: " + consultationId))); plan.setNutrientMaster(nutrientMaster); plan.setValue(planValue.value()); plan.setRemark(planValue.remark()); patientDietPlanRepo.save(plan); } } @Override public void deletePatientDietPlan(Long id) { PatientDietPlan patientDietPlan = patientDietPlanRepo.findById(id) .orElseThrow(() -> new RuntimeException("Patient Diet Plan not found for ID: " + id)); patientDietPlanRepo.delete(patientDietPlan); } private List convertToEntity(PatientDietPlanDto planDto) { PatientConsultation consultation = patientConsultationRepo.findById(planDto.consultationId()) .orElseThrow(() -> new RuntimeException("Consultation not found for ID: " + planDto.consultationId())); return planDto.dietPlanValues().entrySet().stream().map(entry -> { Long nutrientId = entry.getKey(); PatientDietPlanDto.DietPlanValue planValue = entry.getValue(); NutrientMaster nutrientMaster = nutrientMasterRepo.findById(nutrientId) .orElseThrow(() -> new RuntimeException("Nutrient not found for ID: " + nutrientId)); PatientDietPlan plan = new PatientDietPlan(); plan.setConsultation(consultation); plan.setNutrientMaster(nutrientMaster); plan.setValue(planValue.value()); plan.setRemark(planValue.remark()); return plan; }).collect(Collectors.toList()); } }