package egovframework.service.impl;

import egovframework.rte.fdl.cmmn.EgovAbstractServiceImpl;
import egovframework.service.TrafficStatsService;
import egovframework.service.mapper.TrafficStatsMapper;
import egovframework.vo.VdsStatsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class TrafficStatsServiceImpl extends EgovAbstractServiceImpl implements TrafficStatsService {

    private final TrafficStatsMapper trafficStatsMapper;

    @Override
    public List<Map<String, Object>> getVertexHistoryByRoadText(Map<String, Object> paramMap) {
        return this.trafficStatsMapper.getVertexHistoryByRoadText(paramMap);
    }

    @Override
    public List<Map<String, Object>> getAtrdNm() {
        return this.trafficStatsMapper.getAtrdNm();
    }

    @Override
    public List<Map<String, Object>> getVdsCtlrList() {
        return this.trafficStatsMapper.getVdsCtlrList();
    }

    @Override
    public List<VdsStatsVO> getVdsStatsInfo(HashMap<String, String> paramMap) {
        List<VdsStatsVO> result = this.trafficStatsMapper.getVdsStatsInfo(paramMap);

        for (VdsStatsVO vdsVo : result) {
            String[] statDay = vdsVo.getStat_day().split(",");
            String[] tfvl = vdsVo.getTfvl().split(",");

            int[] tfvlArr = new int[24];
            //0으로 초기화..
            Arrays.fill(tfvlArr, 0);

            int tfvlSum = 0;
            for (int ii = 0; ii < statDay.length; ii++) {
                int dyNum = Integer.parseInt(statDay[ii]);
                tfvlArr[dyNum] = Integer.parseInt(tfvl[ii]);
                tfvlSum += Integer.parseInt(tfvl[ii]);
            }

            vdsVo.setTfvlArr(tfvlArr);
            vdsVo.setTfvlSum(tfvlSum);
        }

        return result;
    }

}
