package com.its.api.its.service.link;

import com.its.api.its.model.dto.link.TbLinkTrafClctHsAnalDto;
import com.its.api.its.model.dto.link.TbLinkTrafClctHsAnalInf;
import com.its.api.its.model.dto.link.TbLinkTrafClctHsHistDto;
import com.its.api.its.model.dto.link.TbLinkTrafClctHsHistInf;
import com.its.api.its.repository.link.TbLinkTrafClctHsRepository;
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 TbLinkTrafClctHsService {

    private final TbLinkTrafClctHsRepository repo;
    //private final EntityManager entityManager;

    /**
     * 이력조회 - 수집데이터 이력
     * @param linkId
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbLinkTrafClctHsHistDto> findAllByDateRange(Long linkId, String fromDt, String toDt) {

        List<TbLinkTrafClctHsHistDto> result = new ArrayList<>();
        List<TbLinkTrafClctHsHistInf> data = repo.findAllHistByDateRange(linkId, fromDt, toDt);
        if (data != null) {
            data.forEach(obj -> {
                result.add(TbLinkTrafClctHsHistDto.builder()
                        .linkId(obj.getLinkId().longValue())
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .build());
            });
        }
        return result;
        /*Map<String, String> param = new HashMap<>();
        param.put("LINK_ID", String.valueOf(link_id));
        param.put("FROM_DT", from_dt);
        param.put("TO_DT", to_dt);
        return mapper.findAllByDateRange(param);*/
    }

    /**
     * 이력조회 - 수집데이터 분석
     * @param fromDt
     * @param toDt
     * @param ids
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbLinkTrafClctHsAnalDto> findAllAnalByDateRange(String fromDt, String toDt, List<Long> ids) {

        List<TbLinkTrafClctHsAnalDto> result = new ArrayList<>();
        List<TbLinkTrafClctHsAnalInf> data;
        if (ids != null && ids.size() > 0) {
            data = this.repo.findAllAnalByDateRangeByIds(fromDt, toDt, ids);
        }
        else {
            data = this.repo.findAllAnalByDateRange(fromDt, toDt);
        }

        if (data != null) {
            data.forEach(obj -> {
                result.add(TbLinkTrafClctHsAnalDto.builder()
                        .roadName(obj.getRoadName())
                        .fromNodeInfo(obj.getFromNodeInfo())
                        .toNodeInfo(obj.getToNodeInfo())
                        .linkId(obj.getLinkId().longValue())
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .build());
            });
        }
        return result;
    }
/*
    @Transactional(readOnly = true)
    public List<TbLinkTrafClctHsHistDto> findAllByDateRange2(Long linkId, String fromDt, String toDt) {
        Map<String, String> param = new HashMap<>();
        param.put("LINK_ID", String.valueOf(linkId));
        param.put("FROM_DT", fromDt);
        param.put("TO_DT", toDt);

        StringBuffer sb = new StringBuffer();
        sb.append("SELECT A.LINK_ID as linkId, A.PRCN_DT AS prcnDt ");
        sb.append("     , MAX(DECODE(A.CLCT_SYST_CD, 'OPER01', A.SPED, 0)) AS OPER01 ");
        sb.append("     , MAX(DECODE(A.CLCT_SYST_CD, 'UTIS01', A.SPED, 0)) AS UTIS01 ");
        sb.append("     , MAX(DECODE(A.CLCT_SYST_CD, 'UTIS02', A.SPED, 0)) AS UTIS02 ");
        sb.append("     , MAX(DECODE(A.CLCT_SYST_CD, 'VDS01',  A.SPED, 0)) AS VDS01 ");
        sb.append("     , MAX(DECODE(A.CLCT_SYST_CD, 'MOCT01', A.SPED, 0)) AS MOCT01 ");
        sb.append("  FROM TB_LINK_TRAF_CLCT_HS A ");
        sb.append(" WHERE A.PRCN_DT BETWEEN :fromDt AND :toDt ");
        sb.append("   AND A.LINK_ID = :linkId ");
        sb.append(" GROUP BY A.LINK_ID, A.PRCN_DT ");

        Query nativeQuery = this.entityManager.createNativeQuery(sb.toString())
                .setParameter("fromDt", fromDt)
                .setParameter("toDt", toDt)
                .setParameter("linkId", linkId);

        //List<TbLinkTrafClctHsDto> result = nativeQuery.getResultList();
        List<TbLinkTrafClctHsHistDto> result = new JpaResultMapper().list(nativeQuery, TbLinkTrafClctHsHistDto.class);
        this.entityManager.close();
        return result;
    }*/

}
