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

142 lines
6.4 KiB
Plaintext

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<PatientDietPlanDto> getPatientDietPlansByConsultationId(Long consultationId) {
List<PatientDietPlan> plans = patientDietPlanRepo.findByConsultationId(consultationId);
Map<Long, PatientDietPlanDto> dtoMap = plans.stream().collect(
Collectors.groupingBy(
plan -> plan.getConsultation().getId(),
Collectors.collectingAndThen(
Collectors.toList(),
planList -> {
Map<Long, PatientDietPlanDto.DietPlanValue> 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<PatientDietPlanDto> getAllPatientDietPlans() {
List<PatientDietPlan> plans = patientDietPlanRepo.findAll();
Map<Long, PatientDietPlanDto> dtoMap = plans.stream().collect(
Collectors.groupingBy(
plan -> plan.getConsultation().getId(),
Collectors.collectingAndThen(
Collectors.toList(),
planList -> {
Map<Long, PatientDietPlanDto.DietPlanValue> 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<PatientDietPlan> plans = convertToEntity(planDto);
patientDietPlanRepo.saveAll(plans);
}
@Override
public void updatePatientDietPlan(Long consultationId, PatientDietPlanDto planDto, Long userId) {
List<PatientDietPlan> existingPlans = patientDietPlanRepo.findByConsultationId(consultationId);
for (Map.Entry<Long, PatientDietPlanDto.DietPlanValue> 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<PatientDietPlan> 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());
}
}