package com.nokia.tsl_data.dao;

import org.apache.ibatis.annotations.*;

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

@Mapper
public interface HighQualityDataMapper {

    /**
     * 区县投诉问题解决率
     */
    @Select("select checked_region as region, count(cp_is_ok = '解决' or null)::float / count(1) as rate " +
            "from tsl_data.high_quality_data hqd " +
            "where day_id = #{day_id} and (cp_is_ok  in ('解决', '未解决') or no_visit_tag = '未回访') " +
            "group by checked_region ")
    List<Map<String, Object>> selectRateOfCpIsOkOfRegion(@Param("day_id") String day);

    /**
     * 区县投诉问题本月累计未解决工单
     */
    @Select("select checked_region as region, count(1) as count " +
            "from tsl_data.high_quality_data hqd " +
            "where day_id = #{day_id} and (cp_is_ok  = '未解决' or no_visit_tag = '未回访') " +
            "group by checked_region ")
    List<Map<String, Object>> selectCountOfCpNotOkOfRegion(@Param("day_id") String day);

    /**
     * 区县投诉问题满意度 满意 包括 满意 不满意包括 一般和不满意
     */
    @Select("select checked_region as region, count(cp_satisfaction = '满意' or null)::float / count(1) as rate " +
            "from tsl_data.high_quality_data hqd " +
            "where day_id = #{day_id} and (cp_satisfaction  in ('满意', '一般', '不满意') or no_visit_tag = '未回访') " +
            "group by checked_region ")
    List<Map<String, Object>> selectRateOfCpSatisfactionOfRegion(@Param("day_id") String day);

    /**
     * 区县投诉问题本月累计不满意工单 不满意包括 一般和不满意
     */
    @Select("select checked_region as region, count(1) as count " +
            "from tsl_data.high_quality_data hqd " +
            "where day_id = #{day_id} and (cp_satisfaction in ('不满意', '一般') or no_visit_tag = '未回访') " +
            "group by checked_region ")
    List<Map<String, Object>> selectCountOfCpNotSatisfactionOfRegion(@Param("day_id") String day);

    /**
     * 区县投诉问题响应率
     */
    @Select("select checked_region as region, count(cp_timely_contact = '是' or null)::float / count(1) as rate " +
            "from tsl_data.high_quality_data hqd where day_id = #{day_id} " +
            "and (cp_timely_contact in ('是', '否') or no_visit_tag = '未回访') group by checked_region ")
    List<Map<String, Object>> selectRateOfCpTimelyContactOfRegion(@Param("day_id") String day);

    /**
     * 获取本月累计未响应工单量，这里不会每个区县都有
     */
    @Select("select checked_region as region, count(1) as count from tsl_data.high_quality_data hqd " +
            "where day_id = #{day_id} and (cp_timely_contact = '否' or no_visit_tag = '未回访') group by checked_region")
    List<Map<String, Object>> selectCountOfCpNotTimelyContactOfRegion(@Param("day_id") String day);

    /**
     * 依据工单流转的信息更新HighQualityData的地市信息
     */
    @Update("update tsl_data.high_quality_data t1\n" +
            "set checked_city = t2.city, checked_city_reason = 'work_flow', last_update_date = now() \n" +
            "from (select w.kfsn as kfsn, s.real_name as city from tsl_data.work_flow_basic_data w, \n" +
            " tsl_data.sys_data_dictionary s where s.type = 'work_flow_city' and s.nick_code = w.city_id) t2\n" +
            "where t2.kfsn = t1.sheet_no and t1.day_id = #{day}")
    int updateHighQualityDataCityFromWorkFlowBasicData(String day);

    /**
     * 依据工单流转的信息更新HighQualityData的区县信息
     */
    @Update("update tsl_data.high_quality_data t1\n" +
            "set checked_region  = t2.region, checked_region_reason = 'work_flow', last_update_date = now()\n" +
            "from (select w.kfsn as kfsn, s.real_name as region from tsl_data.work_flow_basic_data w,\n" +
            "tsl_data.sys_data_dictionary s where s.type = 'work_flow_region' and s.nick_code = w.region_id) t2\n" +
            "where t2.kfsn = t1.sheet_no and t1.day_id = #{day}")
    int updateHighQualityDataRegionFromWorkFlowBasicData(String day);

    /**
     * 依据高品质工单详表信息更新HighQualityData的地市信息
     */
    @Update("update tsl_data.high_quality_data t1\n" +
            "set checked_city = t2.real_name, checked_city_reason = 'high_quality_list', last_update_date = now()\n" +
            "from (select nick_code, real_name from tsl_data.sys_data_dictionary where type = 'high_quality_list_city') t2\n" +
            "where t2.nick_code = t1.area_id and t1.checked_city is null and t1.day_id = #{day}")
    int updateHighQualityDataCityFromHighQualityListDay(String day);

    /**
     * 依据高品质工单详表信息更新HighQualityData的区县信息
     */
    @Update("update tsl_data.high_quality_data t1\n" +
            "set checked_region = t2.real_name, checked_region_reason = 'high_quality_list', last_update_date = now()\n" +
            "from (select nick_code, real_name from tsl_data.sys_data_dictionary where type = 'high_quality_list_region') t2\n" +
            "where t2.nick_code = t1.city_id and t1.checked_region is null and t1.day_id = #{day}")
    int updateHighQualityDataRegionFromHighQualityListDay(String day);

    /**
     * 根据默认规则更新HighQualityData的区县信息
     */
    @Update("update tsl_data.high_quality_data t1\n" +
            "set checked_region = concat(checked_city, '市区'), checked_region_reason = 'default', last_update_date = now()\n" +
            "where t1.checked_region is null and t1.day_id = #{day}")
    int updateHighQualityDataRegionFromDefault(String day);

    /**
     * 按照日期查询区县的响应率
     */
    @Select("select checked_city, checked_region, count(cp_timely_contact = '是' or null)::float / count(1) as rate, count(cp_timely_contact = '否' or null) as not_timely_contact_count \n" +
            "from tsl_data.high_quality_data hqd \n" +
            "where day_id = #{day} and (cp_timely_contact in ('是', '否') or no_visit_tag = '未回访')\n" +
            "group by checked_city, checked_region \n" +
            "order by count(cp_timely_contact = '是' or null)::float / count(1)")
    List<Map<String, Object>> selectRegionTimelyContactRate(String day);

    /**
     * 查询某日期月累计未响应工单数量
     */
    @Select("select checked_city, checked_region, count(cp_timely_contact = '否' or null) as not_timely_contact_count \n" +
            "from tsl_data.high_quality_data hqd \n" +
            "where day_id = #{day} and (cp_timely_contact in ('是', '否') or no_visit_tag = '未回访')\n" +
            "group by checked_city, checked_region \n" +
            "order by count(cp_timely_contact = '是' or null)::float / count(1)")
    List<Map<String, Object>> selectRegionNotTimelyContactCount(String day);

    /**
     * 按照日期查询区县的满意率
     */
    @Select("select checked_city, checked_region, count(1) as total, count(cp_satisfaction in ('满意', '一般') or null) as satisfaction, count(cp_satisfaction in ('满意', '一般') or null)::float / count(1) as rate \n" +
            "from tsl_data.high_quality_data hqd \n" +
            "where day_id = #{day} and (cp_satisfaction in ('满意', '一般', '不满意') or no_visit_tag = '未回访')\n" +
            "group by checked_city, checked_region")
    List<Map<String, Object>> selectRegionSatisfactionRate(String day);

    /**
     * 按照日期查询解决率
     */
    @Select("select checked_city, checked_region, count(1) as total, count(cp_is_ok = '解决' or null) as is_ok, count(cp_is_ok = '解决' or null)::float / count(1) as rate \n" +
            "from tsl_data.high_quality_data hqd \n" +
            "where day_id = #{day} and (cp_is_ok in ('解决', '未解决') or no_visit_tag = '未回访') \n" +
            "group by checked_city, checked_region")
    List<Map<String, Object>> selectRegionIsOkRate(String day);

    @Delete("delete from tsl_data.high_quality_data where day_id = #{day} ")
    int deleteForDay(String day);
}
