package com.its.op.service.its.vds;

import com.its.op.dao.repository.its.vds.TbVdsCtlrSttsHsRepository;
import com.its.op.dto.its.vds.TbVdsCtlrSttsHsDto;
import com.its.op.entity.its.vds.TbVdsCtlrSttsHs;
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 TbVdsCtlrSttsHsService {

    private final TbVdsCtlrSttsHsRepository repo;

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

    /**
     * 상태 이력 조회
     * @param fromDt
     * @param toDt
     * @param ids
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbVdsCtlrSttsHsDto> findAllByDateRange(String fromDt, String toDt, List<Long> ids) {
        List<TbVdsCtlrSttsHsDto> result = new ArrayList<>();
        List<TbVdsCtlrSttsHs> data = this.repo.findAllByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                if (obj.getVds() != null && ("N").equals(obj.getVds().getDelYn())) {
                    result.add(obj.toDto());
                }
            });
        }
        return result;
    }

    /**
     * 장애 이력 조회
     * @param fromDt
     * @param toDt
     * @param ids
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbVdsCtlrSttsHsDto> findAllFailByDateRange(String fromDt, String toDt, List<Long> ids) {
        List<TbVdsCtlrSttsHsDto> result = new ArrayList<>();
        List<TbVdsCtlrSttsHs> data = this.repo.findAllFailByDateRange(fromDt, toDt, ids);
        if (data != null) {
            data.forEach(obj -> {
                if (obj.getVds() != null && ("N").equals(obj.getVds().getDelYn())) {
                    result.add(obj.toDto());
                }
            });
        }
        return result;
    }
}
