package com.its.op.service.its.cctv;

import com.its.op.dao.repository.its.cctv.TbCctvCtrlHsRepository;
import com.its.op.dto.its.cctv.TbCctvCtrlHsDto;
import com.its.op.entity.its.cctv.TbCctvCtrlHs;
import com.its.op.entity.its.cctv.TbCctvCtrlHsKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@RequiredArgsConstructor
@Service
public class TbCctvCtrlHsService {

    private final TbCctvCtrlHsRepository repo;

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

    public List<TbCctvCtrlHsDto> findAllByDateRange(String fromDt, String toDt, List<Long> ids) {

        List<TbCctvCtrlHsDto> result = new ArrayList<>();
        List<TbCctvCtrlHs> data = this.repo.findAllByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                result.add(obj.toDto());
            });
        }
        return result;
    }
}
