<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.nokia.tsl_data.dao.MobileCompDao">

    <select id="selectCompCountForDay" resultType="int"> select count(1) from
        report_auto.he_d_mobile_comp_new where month_id = substring(#{day} from 1 for 6) and day_id
        = substring(#{day} from 7 for 2) </select>

    <!-- 从现有数据获取上月处理时长（按上月最后一天处理）并插入数据表 -->
    <insert id="insertOldTsDurationForMonth">with t1 as (select compl_area_local, case when
        proce_time != '' then (extract('epoch' from to_timestamp(proce_time, 'YYYY-MM-DD
        HH24:MI:SS')) - extract('epoch' from to_timestamp(accept_time, 'YYYY-MM-DD
        HH24:MI:SS')))/3600 when is_online_complete = '是' then 0 else (extract('epoch' from
        to_timestamp(end_time, 'YYYY-MM-DD HH24:MI:SS')) - extract('epoch' from
        to_timestamp(accept_time, 'YYYY-MM-DD HH24:MI:SS')))/3600 end as duration from
        report_auto.he_d_mobile_comp_new hdmc where month_id = #{month_id} and day_id::float8 =
        extract('day' from to_timestamp(#{month_id}, 'YYYYMM') + interval '1 month' - interval '1
        day')) insert into report_auto.avg_duration (month_id, city_name, avg_duration) select
        #{month_id} as month_id, compl_area_local as city_name, avg(duration) as avg_duration from
        t1 group by compl_area_local </insert>

    <select id="selectTsDurationForDay" resultType="Map"> with t1 as (select compl_area_local, case
        when proce_time != '' then (extract('epoch' from to_timestamp(proce_time, 'YYYY-MM-DD
        HH24:MI:SS')) - extract('epoch' from to_timestamp(accept_time, 'YYYY-MM-DD
        HH24:MI:SS')))/3600 when is_online_complete = '是' then 0 else (extract('epoch' from
        to_timestamp(end_time, 'YYYY-MM-DD HH24:MI:SS')) - extract('epoch' from
        to_timestamp(accept_time, 'YYYY-MM-DD HH24:MI:SS')))/3600 end as duration from
        report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1 for 6) and
        day_id = substring(#{day} from 7 for 2)) select compl_area_local, avg(duration) as
        avg_duration from t1 group by compl_area_local </select>

    <select id="selectTimeoutTsCountForDay" resultType="Map">with t1 as (select compl_area_local,
        is_timeout from report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from
        1 for 6) and day_id = substring(#{day} from 7 for 2)), t2 as (select '全省' as
        compl_area_local, count(1) as total_num from t1), t3 as (select compl_area_local, count(1)
        as total_num from t1 group by compl_area_local), t4 as (select * from t2 union select * from
        t3), t5 as (select compl_area_local from t1 where is_timeout = '是'), t7 as (select '全省' as
        compl_area_local, count(1) as timeout_num from t5), t8 as (select compl_area_local, count(1)
        as timeout_num from t5 group by compl_area_local), t9 as (select * from t7 union select *
        from t8) select t4.compl_area_local, t4.total_num, t9.timeout_num, t9.timeout_num /
        t4.total_num::float8 as timeout_ratio from t4,t9 where t4.compl_area_local =
        t9.compl_area_local</select>

    <select id="selectRepeatTsCountForDay" resultType="Map"> with t1 as (select compl_area_local,
        busi_no from report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1
        for 6) and day_id = substring(#{day} from 7 for 2)), t2 as (select distinct * from t1), t3
        as (select compl_area_local, count(1) as total_num from t1 group by compl_area_local), t4 as
        (select compl_area_local, count(1) as distinct_num from t2 group by compl_area_local), t5 as
        (select t3.compl_area_local, t3.total_num, t3.total_num - t4.distinct_num::float8 as
        repeat_num, (t3.total_num - t4.distinct_num)/t3.total_num::float8 as repeat_ratio from T3,
        t4 where t3.compl_area_local = t4.compl_area_local) select '全省' as compl_area_local,
        sum(total_num) as total_num, sum(repeat_num) as repeat_num, sum(repeat_num)
        /sum(total_num)::float8 as repeat_ratio from t5 union select * from t5 </select>

    <select id="selectCityTslForMonth" resultType="Map"> select compl_area_local,
        substring(acct_date from 7 for 2) as day_id, count(1) as num from
        report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1 for 6) and
        day_id = substring(#{day} from 7 for 2) group by compl_area_local, substring(acct_date from
        7 for 2) order by compl_area_local, substring(acct_date from 7 for 2) </select>

    <select id="selectAllTslForMonth" resultType="Map"> select substring(acct_date from 7 for 2) as
        day_id, count(1) as num from report_auto.he_d_mobile_comp_new hdmc where month_id =
        substring(#{day} from 1 for 6) and day_id = substring(#{day} from 7 for 2) group by
        substring(acct_date from 7 for 2) order by substring(acct_date from 7 for 2) </select>

    <select id="selectCityAllForMonth" resultType="Map">select compl_area_local, count(1) as num
        from report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1 for 6)
        and day_id = substring(#{day} from 7 for 2) group by compl_area_local order by
        compl_area_local </select>

    <select id="selectAllForMonth" resultType="int">select count(1) as num from
        report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1 for 6) and
        day_id = substring(#{day} from 7 for 2)</select>

    <delete id="deleteMobileCompForDay" parameterType="string">delete from
        report_auto.he_d_mobile_comp_new hdmc where month_id = substring(#{day} from 1 for 6) and
        day_id = substring(#{day} from 7 for 2)</delete>
</mapper>