package com.nokia.tsl_data.dao;

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

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

/**
 * 客户端数据表 高质量统计表
 * 对应主表 tsl_data.high_quality_count_day
 */
@Mapper
public interface HighQualityCountMapper {

        /**
         * 查询河北_CEM高品质2日统计数据量
         */
        @Select("select count(1) from tsl_data.high_quality_count_day where acct_date = #{day} ")
        int selectQualityCountForDay(String day);

        /**
         * 查询客户端地市响应率数据 参数day_id 20231105
         */
        @Select("select businoareaname as city,\n" +
                        " complaint_response_list::float8 as numerator,\n" +
                        " complaint_response_count::float8 + complaint::float8 as denominator,\n" +
                        " case when complaint_response_count::float8 + complaint::float8 = 0 then 1 else " +
                        " complaint_response_list::float8 / (complaint_response_count::float8 + complaint::float8) end as rate "
                        + " from tsl_data.high_quality_count_day\n" +
                        " where month_id = substring(#{day_id} from 1 for 6)\n" +
                        "  and day_id = substring(#{day_id} from 7 for 2)\n" +
                        "  and profes_dep = '网络质量'\n" +
                        "  and big_type_name = '移网网络体验'\n" +
                        "  and small_type_name = '--'")
        List<Map<String, Object>> selectResponseOfCity(@Param("day_id") String day);

        /**
         * 查询客户端地市解决率 参数day_id 20231105
         */
        @Select("select businoareaname as city,\n" +
                        " complaint_resolution_list::float8 as numerator,\n" +
                        " complaint_resolution_count::float8 + complaint::float8 as denominator,\n" +
                        " case when complaint_resolution_count::float8 + complaint::float8 = 0 then 1 else " +
                        " complaint_resolution_list::float8 / (complaint_resolution_count::float8 + complaint::float8) end as rate "
                        + "from tsl_data.high_quality_count_day\n" +
                        "where month_id = substring(#{day_id} from 1 for 6)\n" +
                        "  and day_id = substring(#{day_id} from 7 for 2)\n" +
                        "  and profes_dep = '网络质量'\n" +
                        "  and big_type_name = '移网网络体验'\n" +
                        "  and small_type_name = '--'")
        List<Map<String, Object>> selectResolutionOfCity(@Param("day_id") String day);

        /**
         * 查询客户端地市满意度 参数day_id 20231105
         */
        @Select("select businoareaname as city,\n"
                        + " complaint_satisfied_list::float8 as numerator,\n"
                        + " complaint_satisfied_count::float8 + complaint::float8 as denominator,\n"
                        + " case when complaint_satisfied_count::float8 + complaint::float8 = 0 then 1 else" +
                        " complaint_satisfied_list::float8 / (complaint_satisfied_count::float8 + complaint::float8) end as rate "
                        + " from tsl_data.high_quality_count_day\n" +
                        " where month_id = substring(#{day_id} from 1 for 6)\n" +
                        "  and day_id = substring(#{day_id} from 7 for 2)\n" +
                        "  and profes_dep = '网络质量'\n" +
                        "  and big_type_name = '移网网络体验'\n" +
                        "  and small_type_name = '--' ")
        List<Map<String, Object>> selectSatisfiedOfCity(@Param("day_id") String day);

        /**
         * 客户端-投诉问题解决满意度 客户端-投诉问题解决率 客户端-投诉问题响应率
         */
        @Select("with t1 as (select businoareaname, complaint_satisfied_list::float8, complaint_satisfied_count::float8, "
                        + "complaint_resolution_list::float8, complaint_resolution_count::float8, complaint_response_list::float8, "
                        + "complaint_response_count::float8, complaint::float8 " +
                        "from tsl_data.high_quality_count_day hdhq " +
                        "where acct_date = #{day} and profes_dep = '网络质量' and big_type_name = '移网网络体验' and small_type_name = '--') "
                        + "select '全省' as businoareaname, sum(complaint_satisfied_list) / sum(complaint_satisfied_count + complaint) as complaint_satisfied, "
                        + " sum(complaint_resolution_list) / sum(complaint_resolution_count + complaint) as complaint_resolution, "
                        + " sum(complaint_response_list) / sum(complaint_response_count + complaint)  as complaint_response from t1 "
                        + "union select businoareaname, " +
                        "case when (complaint_satisfied_count + complaint) = 0 then 1 " +
                        "  else complaint_satisfied_list / (complaint_satisfied_count + complaint) end as complaint_satisfied, "
                        + "case when (complaint_resolution_count + complaint) = 0 then 1 " +
                        "  else complaint_resolution_list / (complaint_resolution_count + complaint) end as complaint_resolution, "
                        + "case when (complaint_response_count + complaint) = 0 then 1 " +
                        "  else complaint_response_list / (complaint_response_count + complaint) end     as complaint_response from t1")
        List<Map<String, Object>> selectClientRatioForDay(String day);

        /**
         * 查询某个日期各个地市的客户端总数（总投诉量（办结量））（所得结果是当月0日到当天的一个累加量）
         */
        default List<Map<String, Object>> selectTotalComplaintsForDay(String day) {
                return selectTotalComplaintsForMonthIdAndDay(day.substring(0, 6), day.substring(6));
        }

        /**
         * 查询某个日期各个地市的客户端总数（总投诉量（办结量））（所得结果是当月0日到当天的一个累加量）
         */
        @Select("select businoareaname, total_complaints from tsl_data.high_quality_count_day " +
                        "where month_id = #{month_id} and day_id = #{day_id} and profes_dep = '网络质量' " +
                        "and big_type_name = '移网网络体验' and small_type_name = '--'")
        List<Map<String, Object>> selectTotalComplaintsForMonthIdAndDay(@Param("month_id") String monthId,
                        @Param("day_id") String day);

        @Delete("delete from tsl_data.high_quality_count_day where month_id = #{month_id} and day_id = #{day_id}")
        int deleteHighQualityCountForMonthIdAndDayId(@Param("month_id") String monthId, @Param("day_id") String dayId);

        default int deleteHighQualityCountForDay(String day) {
                return deleteHighQualityCountForMonthIdAndDayId(day.substring(0, 6), day.substring(6));
        }

        @Select("select count(1) from tsl_data.high_quality_count_day where month_id = #{month_id} and day_id = #{day_id}")
        int countForDay(@Param("month_id") String monthId, @Param("day_id") String dayId);

        default int countForDay(String day) {
                return countForDay(day.substring(0, 6), day.substring(6));
        }

        /**
         * 按照账期查询数据
         */
        default int selectCountOfStatDay(String day) {
                return countForDay(day.substring(0, 6), day.substring(6));
        }
}
