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

import com.its.op.dao.repository.its.cctv.TbCctvCtlrSttsHsRepository;
import com.its.op.dto.its.cctv.TbCctvCtlrSttsHsDto;
import com.its.op.entity.its.cctv.TbCctvCtlrSttsHs;
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;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbCctvCtlrSttsHsService {

    private final TbCctvCtlrSttsHsRepository repo;

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

    /**
     * 상태 이력 조회
     * @param fromDt
     * @param toDt
     * @param ids
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbCctvCtlrSttsHsDto> findAllByDateRange(String fromDt, String toDt, List<Long> ids) {

        List<TbCctvCtlrSttsHsDto> result = new ArrayList<>();
        List<TbCctvCtlrSttsHs> data = this.repo.findAllByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                if (obj.getCctv() != null && ("N").equals(obj.getCctv().getDelYn())) {
                    result.add(obj.toDto());
                }
            });
        }
        return result;
    }

    /**
     * 장애 이력 조회
     * @param fromDt
     * @param toDt
     * @param ids
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbCctvCtlrSttsHsDto> findAllFailByDateRange(String fromDt, String toDt, List<Long> ids) {

        List<TbCctvCtlrSttsHsDto> result = new ArrayList<>();
        List<TbCctvCtlrSttsHs> data = this.repo.findAllFailByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                if (obj.getCctv() != null && ("N").equals(obj.getCctv().getDelYn())) {
                    result.add(obj.toDto());
                }
            });
        }
        return result;
    }
}
