109 lines
4.3 KiB
Plaintext
109 lines
4.3 KiB
Plaintext
package com.healthcare.ohctech.service.impl;
|
|
|
|
import com.healthcare.ohctech.dto.CheckupFormSectionDto;
|
|
import com.healthcare.ohctech.entity.*;
|
|
import com.healthcare.ohctech.repository.*;
|
|
import com.healthcare.ohctech.service.CheckupFormSectionService;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.*;
|
|
|
|
@Service
|
|
public class CheckupFormSectionServiceImpl implements CheckupFormSectionService {
|
|
|
|
private final CheckupFormSectionRepo checkupFormSectionRepo;
|
|
// private final ProductRepo productRepo;
|
|
private final RuleEquationRepo ruleEquationRepo;
|
|
private final InterpretationMasterRepo interpretationMasterRepo;
|
|
|
|
public CheckupFormSectionServiceImpl(CheckupFormSectionRepo checkupFormSectionRepo,
|
|
// ProductRepo productRepo,
|
|
RuleEquationRepo ruleEquationRepo, InterpretationMasterRepo interpretationMasterRepo) {
|
|
this.checkupFormSectionRepo = checkupFormSectionRepo;
|
|
// this.productRepo = productRepo;
|
|
this.ruleEquationRepo = ruleEquationRepo;
|
|
this.interpretationMasterRepo = interpretationMasterRepo;
|
|
}
|
|
|
|
@Override
|
|
public CheckupFormSection getCheckupFormSectionById(Long id) {
|
|
return checkupFormSectionRepo.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("Checkup Form Section not found for ID: " + id));
|
|
}
|
|
|
|
@Override
|
|
public Page<CheckupFormSection> getAllCheckupFormSections(Pageable pageable) {
|
|
return checkupFormSectionRepo.findAll(pageable);
|
|
}
|
|
|
|
@Override
|
|
public void addCheckupFormSection(CheckupFormSectionDto dto, Long userId) {
|
|
validateRuleIds(dto.ruleIds());
|
|
CheckupFormSection section = convertToEntity(new CheckupFormSection(), dto);
|
|
section.setModifiedBy(userId);
|
|
checkupFormSectionRepo.save(section);
|
|
}
|
|
|
|
@Override
|
|
public void updateCheckupFormSection(CheckupFormSectionDto dto, Long userId) {
|
|
validateRuleIds(dto.ruleIds());
|
|
Long sectionId = dto.id();
|
|
CheckupFormSection section = checkupFormSectionRepo.findById(sectionId)
|
|
.orElseThrow(() -> new RuntimeException("Checkup Form Section not found for ID: " + sectionId));
|
|
|
|
convertToEntity(section, dto);
|
|
section.setModifiedBy(userId);
|
|
checkupFormSectionRepo.save(section);
|
|
}
|
|
|
|
@Override
|
|
public void deleteCheckupFormSection(Long id) {
|
|
CheckupFormSection section = checkupFormSectionRepo.findById(id)
|
|
.orElseThrow(() -> new RuntimeException("Checkup Form Section not found for ID: " + id));
|
|
checkupFormSectionRepo.delete(section);
|
|
}
|
|
|
|
private void validateRuleIds(List<Long> ruleIds) {
|
|
if (ruleIds != null) {
|
|
for (Long ruleId : ruleIds) {
|
|
if (!ruleEquationRepo.existsById(ruleId)) {
|
|
throw new RuntimeException("Rule Equation not found for ID: " + ruleId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private CheckupFormSection convertToEntity(CheckupFormSection section, CheckupFormSectionDto dto) {
|
|
section.setSectionName(dto.sectionName());
|
|
section.setSectionDesc(dto.sectionDesc());
|
|
section.setStatus(dto.status());
|
|
section.setNotes(dto.notes());
|
|
section.setComments(dto.comments());
|
|
section.setDisplayOrder(dto.displayOrder());
|
|
|
|
// Product product = productRepo.findById(dto.productId())
|
|
// .orElseThrow(() -> new RuntimeException("Product not found for ID: " + dto.productId()));
|
|
// section.setProduct(product);
|
|
|
|
if (dto.ruleIds() != null) {
|
|
String ruleIdsString = dto.ruleIds().stream()
|
|
.map(String::valueOf)
|
|
.collect(Collectors.joining(","));
|
|
section.setRuleIds(ruleIdsString);
|
|
} else {
|
|
section.setRuleIds("");
|
|
}
|
|
|
|
if (dto.interpretationId() != null) {
|
|
InterpretationMaster interpretationMaster = interpretationMasterRepo.findById(dto.interpretationId())
|
|
.orElseThrow(() -> new RuntimeException("Interpretation Master not found for ID: " + dto.interpretationId()));
|
|
section.setInterpretationMaster(interpretationMaster);
|
|
}
|
|
|
|
return section;
|
|
}
|
|
}
|