package com.its.op.service.its.scrs;

import com.its.op.dao.repository.its.scrs.TbScIxrCmraSttsRepository;
import com.its.op.dto.its.scrs.TbScIxrCmraSttsDto;
import com.its.op.entity.its.scrs.TbScIxrCmraMngmKey;
import com.its.op.entity.its.scrs.TbScIxrCmraStts;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbScIxrCmraSttsService {

    private final TbScIxrCmraSttsRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbScIxrCmraStts requireOne(TbScIxrCmraMngmKey ixrId) throws NoSuchElementException {
        Optional<TbScIxrCmraStts> info = this.repo.findById(ixrId);
        if (info.isPresent()) {
            return info.get();
        }
        else {
            throw new NoSuchElementException("데이터가 존재하지 않습니다: " + ixrId);
        }
    }

    // 전체 데이터 조회
    @Transactional(readOnly = true)
    public List<TbScIxrCmraSttsDto> findAll() {
        List<TbScIxrCmraSttsDto> result = new ArrayList<>();
        List<TbScIxrCmraStts> data = this.repo.findAll();
        for (TbScIxrCmraStts entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

    // 전체 데이터 조회
    @Transactional(readOnly = true)
    public List<TbScIxrCmraSttsDto> findAllList() {
        List<TbScIxrCmraSttsDto> result = new ArrayList<>();
        List<TbScIxrCmraStts> data = this.repo.findAllList();
        for (TbScIxrCmraStts entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

    // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
    @Transactional(readOnly = true)
    public TbScIxrCmraSttsDto findById(TbScIxrCmraMngmKey ixrId) {
        TbScIxrCmraStts entity = requireOne(ixrId);
        return entity.toDto();
    }

}
