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

import com.its.op.dao.repository.its.rse.TbRseOffrSectTrafRepository;
import com.its.op.dto.its.rse.TbRseOffrSectTrafDto;
import com.its.op.entity.its.rse.TbRseOffrSectTraf;
import com.its.op.entity.its.rse.TbRseOffrSectTrafKey;
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;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbRseOffrSectTrafService {

    private final TbRseOffrSectTrafRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbRseOffrSectTraf requireOne(TbRseOffrSectTrafKey id) throws NoSuchElementException {
        Optional<TbRseOffrSectTraf> info = this.repo.findById(id);
        if (info.isPresent()) {
            return info.get();
        }
        else {
            throw new NoSuchElementException("데이터가 존재하지 않습니다: " + id);
        }
//        return this.repo.findById(id)
//                .orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
    }

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

}
