package com.healthcare.ohctech.service.impl; import com.healthcare.ohctech.dto.AbnormalityDto; import com.healthcare.ohctech.entity.Abnormality; import com.healthcare.ohctech.repository.AbnormalityRepo; import com.healthcare.ohctech.repository.TrainingMasterRepo; import com.healthcare.ohctech.service.AbnormalityService; 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.stream.Collectors; @Service public class AbnormalityServiceImpl implements AbnormalityService { private final AbnormalityRepo abnormalityRepo; private final TrainingMasterRepo trainingMasterRepo; public AbnormalityServiceImpl(AbnormalityRepo abnormalityRepo, TrainingMasterRepo trainingMasterRepo) { this.abnormalityRepo = abnormalityRepo; this.trainingMasterRepo = trainingMasterRepo; } @Override public Abnormality getAbnormalityById(Long abnormalityId) { return abnormalityRepo.findById(abnormalityId) .orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId)); } @Override public Page getAllAbnormalities(Pageable pageable) { return abnormalityRepo.findAll(pageable); } @Override public void addAbnormality(List abnormalityDtos, Long userId) { List abnormalities = new ArrayList<>(); for (AbnormalityDto dto : abnormalityDtos) { validateWellnessProgramIds(dto.wellnessProgramIds()); Abnormality abnormality = convertToEntity(new Abnormality(), dto); abnormality.setModifiedBy(userId); abnormalities.add(abnormality); } abnormalityRepo.saveAll(abnormalities); } @Override public void updateAbnormality(AbnormalityDto abnormalityDto,Long userId) { validateWellnessProgramIds(abnormalityDto.wellnessProgramIds()); Long abnormalityId = abnormalityDto.id(); Abnormality abnormality = abnormalityRepo.findById(abnormalityId) .orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId)); convertToEntity(abnormality, abnormalityDto); abnormality.setModifiedBy(userId); abnormalityRepo.save(abnormality); } @Override public void deleteAbnormality(Long abnormalityId) { Abnormality abnormality = abnormalityRepo.findById(abnormalityId) .orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId)); abnormalityRepo.delete(abnormality); } private void validateWellnessProgramIds(List wellnessProgramIds) { if (wellnessProgramIds != null) { for (Long programId : wellnessProgramIds) { if (!trainingMasterRepo.existsById(programId)) { throw new RuntimeException("Wellness program not found for ID: " + programId); } } } } private Abnormality convertToEntity(Abnormality abnormality, AbnormalityDto abnormalityDto) { abnormality.setAbnormalityName(abnormalityDto.abnormalityName()); if (abnormalityDto.wellnessProgramIds() != null && !abnormalityDto.wellnessProgramIds().isEmpty()) { String wellnessProgramIds = abnormalityDto.wellnessProgramIds().stream() .map(String::valueOf) .collect(Collectors.joining(",")); abnormality.setWellnessProgramIds(wellnessProgramIds); } else { abnormality.setWellnessProgramIds(""); } return abnormality; } }