package com.its.op.service.its.rse;

import com.its.op.dao.repository.its.rse.TbRseCtlrSttsHsRepository;
import com.its.op.dto.its.rse.TbRseCtlrSttsHsDto;
import com.its.op.entity.its.rse.TbRseCtlrSttsHs;
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 TbRseCtlrSttsHsService {

    private final TbRseCtlrSttsHsRepository repo;

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

    @Transactional(readOnly = true)
    public List<TbRseCtlrSttsHsDto> findAllByDateRange(String fromDt, String toDt, List<Long> ids) {

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

    @Transactional(readOnly = true)
    public List<TbRseCtlrSttsHsDto> findAllFailByDateRange(String fromDt, String toDt, List<Long> ids) {

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