package com.its.op.service.its.weather;

import com.its.op.dao.repository.its.weather.TbAtmpPltnPnstRepository;
import com.its.op.dao.repository.its.weather.TbAtmpPltnSttnRepository;
import com.its.op.dto.its.weather.TbAtmpPltnPnstDto;
import com.its.op.dto.its.weather.TbAtmpPltnSttnDto;
import com.its.op.entity.its.weather.TbAtmpPltnPnst;
import com.its.op.entity.its.weather.TbAtmpPltnSttn;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbAtmpPltnSttnService {

    private final TbAtmpPltnSttnRepository repo;
    private final TbAtmpPltnPnstRepository pnstRepo;

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

    /**
     * 대기오염측정소 전체 조회
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbAtmpPltnSttnDto> findAllList() {
        List<TbAtmpPltnSttnDto> result = new ArrayList<>();
        List<TbAtmpPltnSttn> data = this.repo.findAll();
        data.forEach(obj -> {
            if (StringUtils.equalsIgnoreCase("N", obj.getDelYn())) {
                result.add(obj.toDto());
            }
        });
        return result;
    }

    /**
     * 대기오염 실시간 현황
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbAtmpPltnPnstDto> findAllRealtimeStatus() {
        List<TbAtmpPltnPnst> data = this.pnstRepo.findAllList();
        List<TbAtmpPltnPnstDto> result = new ArrayList<>();
        data.forEach(obj -> {
            result.add(obj.toDto());
        });
        return result;
    }
}
