package com.its.op.service.its.wcam;

import com.its.op.dao.repository.its.wcam.TbWcamCtlrSttsHsRepository;
import com.its.op.dto.its.wcam.TbWcamCtlrSttsHsDto;
import com.its.op.entity.its.wcam.TbWcamCtlrSttsHs;
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 TbWcamCtlrSttsHsService {

    private final TbWcamCtlrSttsHsRepository repo;

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

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

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

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