package com.its.op.service.its.rse;

import com.its.op.dao.repository.its.rse.TbRseOffrSectTrafHsRepository;
import com.its.op.dto.its.rse.TbRseOffrSectTrafHsDto;
import com.its.op.entity.its.rse.TbRseOffrSectTrafHs;
import com.its.op.entity.its.rse.TbRseOffrSectTrafHsKey;
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 TbRseOffrSectTrafHsService {

    private final TbRseOffrSectTrafHsRepository repo;

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

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

}
