package com.its.api.its.service.vds;

import com.its.api.its.model.dto.vds.TbVdsDtctRawClctDto;
import com.its.api.its.model.entity.vds.TbVdsDtctRawClct;
import com.its.api.its.model.entity.vds.TbVdsDtctRawClctKey;
import com.its.api.its.repository.vds.TbVdsDtctRawClctRepository;
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;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbVdsDtctRawClctService {

    private final TbVdsDtctRawClctRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbVdsDtctRawClct requireOne(TbVdsDtctRawClctKey id) {
        return this.repo.findById(id)
                .orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
    }

    // 이력 데이터 조회
    @Transactional(readOnly = true)
    public List<TbVdsDtctRawClctDto> findAllByDateRange(String fromDt, String toDt, List<String> ids) {
        List<TbVdsDtctRawClctDto> result = new ArrayList<>();
        List<TbVdsDtctRawClct>  data = this.repo.findAllByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                result.add(obj.toDto());
            });
        }
        return result;
    }
}
