package com.its.op.service.its.vms;

import com.its.op.dao.repository.its.vms.TbVmsCtlrSttsHsRepository;
import com.its.op.dto.its.vms.TbVmsCtlrSttsHsDto;
import com.its.op.entity.its.vms.TbVmsCtlrSttsHs;
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 TbVmsCtlrSttsHsService {

    private final TbVmsCtlrSttsHsRepository repo;

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

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

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

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

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