package com.its.op.service.its.link;

import com.its.op.dao.repository.its.link.TbClctSystSttsHsRepository;
import com.its.op.dto.its.analysis.TbClctSystSttsHsClctDto;
import com.its.op.dto.its.analysis.TbClctSystSttsHsClctInf;
import com.its.op.dto.its.analysis.TbClctSystSttsHsPrcnDto;
import com.its.op.dto.its.analysis.TbClctSystSttsHsPrcnInf;
import com.its.op.dto.its.link.TbClctSystSttsHsDto;
import com.its.op.entity.its.link.TbClctSystSttsHs;
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 TbClctSystSttsHsService {

    private final TbClctSystSttsHsRepository repo;

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

    /**
     * 04.분석-1.교통정보수집결과분석
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbClctSystSttsHsClctDto> findAllClctSystByDateRange(String fromDt, String toDt) {
        List<TbClctSystSttsHsClctDto> result = new ArrayList<>();
        List<TbClctSystSttsHsClctInf> data = this.repo.findAllClctSystByDateRange(fromDt, toDt);
        if (data != null) {
            data.forEach(obj -> {
                TbClctSystSttsHsClctDto dto = TbClctSystSttsHsClctDto.builder()
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .dsrc01(obj.getDsrc01().intValue())
                        .gits01(obj.getGits01().intValue())
                        .build();
                dto.makeClctTot();
                result.add(dto);
            });
        }
        return result;
    }


    /**
     * 04.분석-2.교통정보가공결과분석
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbClctSystSttsHsPrcnDto> findAllPrcsSystByDateRange(String fromDt, String toDt) {
        List<TbClctSystSttsHsPrcnDto> result = new ArrayList<>();
        List<TbClctSystSttsHsPrcnInf> data = this.repo.findAllPrcnSystByDateRange(fromDt, toDt);
        if (data != null) {
            data.forEach(obj -> {
                TbClctSystSttsHsPrcnDto dto = TbClctSystSttsHsPrcnDto.builder()
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .dsrc01(obj.getDsrc01().intValue())
                        .gits01(obj.getGits01().intValue())
                        .missNum(obj.getMissNum().intValue())
                        .adjsNum(obj.getAdjsNum().intValue())
                        .smthNum(obj.getSmthNum().intValue())
                        .build();
                dto.makeClctTot();
                result.add(dto);
            });
        }
        return result;
    }
}
