package com.nokia.tsl_data.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface WorkFlowBasicDataMapper {

        /**
         * 超时工单统计
         */
        @Select("with t1 as (select sdd.real_name as city,kfsn, to_timestamp(kf_file_time, 'yyyy-mm-dd hh24:mi:ss')" +
                        " - to_timestamp(kd_accept_time, 'yyyy-mm-dd hh24:mi:ss') > make_interval(hours => #{target}) as is_timeout\n"
                        + "from tsl_data.work_flow_basic_data wfbd, tsl_data.sys_data_dictionary sdd \n" + //
                        "where kf_file_time is not null and re_is_status_id != '06678b79185349b5bf0c24490a978fbb'\n" + //
                        "and wfbd.city_id = sdd.nick_code and is_effctive = '1' " +
                        " and to_timestamp(kf_file_time, 'yyyy-mm-dd hh24:mi:ss') >= #{start}"
                        + " and to_timestamp(kf_file_time, 'yyyy-mm-dd hh24:mi:ss') <= #{end})," +
                        "t2 as (select '全省' as city, count(1) as total_num, count(is_timeout or null) as timeout_num, "
                        + " count(is_timeout or null)::float8 / count(1) as timeout_ratio from t1),"
                        + " t3 as (select city, count(1) as total_num, count(is_timeout or null) as timeout_num, " +
                        " count(is_timeout or null)::float8 / count(1) as timeout_ratio from t1 group by city) " +
                        " select * from t2 union select * from t3 ")
        List<Map<String, Object>> selectTimeoutTsCountForDay(@Param("start") Date start, @Param("end") Date end, @Param("target") int target);

        /**
         * 处理时长统计
         */
        @Select("with t1 as (select sdd.real_name,kfsn, (extract(epoch from to_timestamp(kf_file_time, 'YYYY-MM-DD HH24:MI:SS'))"
                        + " - extract(epoch from to_timestamp(kd_accept_time, 'YYYY-MM-DD HH24:MI:SS'))) / 3600 as cost_time\n"
                        + "from tsl_data.work_flow_basic_data wfbd, tsl_data.sys_data_dictionary sdd \n" + //
                        " where wfbd.city_id = sdd.nick_code and kf_file_time is not null" +
                        " and re_is_status_id != '06678b79185349b5bf0c24490a978fbb' and is_effctive = '1' "
                        + "and to_timestamp(wfbd.kf_file_time, 'yyyy-mm-dd hh24:mi:ss') >= #{start}" +
                        " and to_timestamp(wfbd.kf_file_time, 'yyyy-mm-dd hh24:mi:ss') <= #{end})," +
                        " t2 as (select real_name as city, avg(cost_time) as avg_duration\n" + //
                        " from t1 group by real_name order by real_name), " +
                        " t3 as (select '全省' as city, avg(cost_time) as avg_duration from t1)\n" + //
                        " select * from t2 union select * from t3 ")
        List<Map<String, Object>> selectTsDurationForDay(@Param("start") Date start, @Param("end") Date end);
}
