package tkio.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import tkio.service.FlowService;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    @Qualifier("tikoTemplate")
    JdbcTemplate etlJdbcTemplate;

    @Override
    public BigInteger getFlowByAccount(String startDate, String pastDate, List<String> appkeys) {
        NamedParameterJdbcTemplate nameJdbc = new NamedParameterJdbcTemplate(etlJdbcTemplate);
        String sql = " SELECT sum(click_num+ IFNULL(num_impression,0)) as click_sum FROM tkio.measures_trackingio_new " +
                " WHERE appid in  (:appids ) AND ds>= :startdate and ds<= :enddate ";
        Map param = new HashMap();
        param.put("appids", appkeys);
        param.put("startdate", startDate);
        param.put("enddate", pastDate);
        return nameJdbc.queryForObject(sql, param, BigInteger.class);
    }
}

