package com.org.web.data.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.org.web.data.domain.DataStatisticsItem;
import com.org.web.person.domain.BcPersonSupport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface DataMapper extends BaseMapper<BcPersonSupport> {

    // 是否精准扶贫
    @Select("SELECT " +
            "    '是' AS name, " +
            "    1 as id, " +
            "    SUM(CASE WHEN if_help_poor = 1 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 " +

            "UNION " +

            "SELECT " +
            "    '否' AS name, " +
            "    0 as id, " +
            "    SUM(CASE WHEN if_help_poor = 0 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 ")
    List<DataStatisticsItem> listIfHelpPoor();

    // 性别
    @Select("SELECT " +
            "    '男' AS name, " +
            "    1 as id, " +
            "    SUM(CASE WHEN sex = 1 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 " +
            "UNION " +
            "SELECT " +
            "    '女' AS name, " +
            "    0 as id, " +
            "    SUM(CASE WHEN sex = 0 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 ")
    List<DataStatisticsItem> listSex();

    // 护理等级
    @Select("select c.name as name, c.value as id,  " +
            "                   (select count(1)   " +
            "                       from bc_person_support a " +
            "                       where a.is_delete = 0  " +
            "                       and a.type = 1 " +
            "                       and a.support_type = 1 " +
            "                       and IFNULL(a.nursing_level, '') != '' " +
            "                       and a.nursing_level = c.value) as value " +
            "            from sys_data_dictionary_item c  " +
            "            where c.is_delete = 0  " +
            "              and c.dictionary_code = '0021' " +
            "              order by c.value ")
    List<DataStatisticsItem> listNursingLevel();

    // 年龄区间
    @Select("SELECT " +
            "    age_ranges.age_group as name, id, " +
            "    COALESCE(COUNT(age_table.id_card_no), 0) AS value " +
            "FROM ( " +
            "    SELECT '50岁以下' AS age_group, 0 AS min_age, 49 AS max_age, 1 AS id " +
            "    UNION SELECT '50-59岁', 50, 59, 2 " +
            "    UNION SELECT '60-69岁', 60, 69, 3 " +
            "    UNION SELECT '70-79岁 ', 70, 79, 4 " +
            "    UNION SELECT '80-89岁', 80, 89, 5 " +
            "    UNION SELECT '90岁以上', 90, 999, 6 " +
            ") AS age_ranges " +
            "LEFT JOIN ( " +
            "    SELECT " +
            "        id_card_no, " +
            "        TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(id_card_no, 7, 8), '%Y%m%d'), CURDATE()) AS age " +
            "    FROM bc_person_support " +
            "   where is_delete = 0 " +
            "   and type = 1  " +
            "   and support_type = 1 " +
            ") AS age_table ON age_table.age BETWEEN age_ranges.min_age AND age_ranges.max_age " +
            "GROUP BY age_ranges.age_group, id ")
    List<DataStatisticsItem> listAgeCount();


    // 是否在院
    @Select("SELECT " +
            "    '是' AS name, " +
            "    1 as id, " +
            "    SUM(CASE WHEN is_in_hospital = 1 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 " +

            "UNION " +
            "SELECT " +
            "    '否' AS name, " +
            "    0 as id, " +
            "    SUM(CASE WHEN is_in_hospital = 0 THEN 1 ELSE 0 END) AS value " +
            "FROM bc_person_support " +
            "where is_delete = 0 " +
            "and type = 1 " +
            "and support_type = 1 ")
    List<DataStatisticsItem> listInHospital();


    /** ----------------------------------------- center ------------------------------ */
    // 在院人数村籍榜
    @Select("select c.name as name, c.value as id,  " +
            "                   (select count(1)   " +
            "                       from bc_person_support a " +
            "                       where a.is_delete = 0  " +
            "                       and a.type = 1 " +
            "                       and a.support_type = 1 " +
            "                       and IFNULL(a.village, '') != '' " +
            "                       and a.village = c.value) as value " +
            "            from sys_data_dictionary_item c  " +
            "            where c.is_delete = 0  " +
            "              and c.dictionary_code = '0040' " +
            "              order by value desc ")
    List<DataStatisticsItem> listVillageRank();

    // 累计入住数
    @Select("select count(1) " +
            "from bc_hospital_record " +
            "where is_delete = 0")
    int getLjrzCount();

    // 今年入住数
    @Select("select count(1) " +
            "from bc_hospital_record " +
            "where is_delete = 0 " +
            "and year(check_in_time) = year(curdate()) ")
    int getJnrzCount();

    // 本月新增数
    @Select("select count(1) " +
            "from bc_hospital_record " +
            "where is_delete = 0 " +
            "and year(check_in_time) = year(curdate()) " +
            "and month(check_in_time) = month(curdate()) ")
    int getByxzCount();

    // 目前在院数
    @Select("select count(1) " +
            "from bc_person_support " +
            "where is_delete = 0 " +
            "  and type = 1 " +
            "  and support_type = 1 " +
            "  and is_in_hospital = 1")
    int getMqzyCount();

    // 外出就医数
    @Select("select count(1) " +
            "from bc_person_support " +
            "where is_delete = 0 " +
            "  and type = 1 " +
            "  and support_type = 1 " +
            "  and is_in_hospital = 0")
    int getWcjyCount();

    // 今年减员数
    @Select("select count(1) " +
            "from bc_hospital_record " +
            "where is_delete = 0 " +
            "  and year(check_out_time) = year(curdate())")
    int getJnjyCount();

    // 集中供养率
    @Select("select ROUND((select count(1) " +
            "              from bc_person_support a " +
            "              where a.is_delete = 0 " +
            "                and a.type = 1 " +
            "                and a.support_type = 1) / " +
            "             (select count(1) " +
            "              from bc_person_support a " +
            "              where a.is_delete = 0 " +
            "                and a.type = 1) * 100)")
    BigDecimal getJzgyl();

    // 分散供养率
    @Select("select ROUND((select count(1) " +
            "              from bc_person_support a " +
            "              where a.is_delete = 0 " +
            "                and a.type = 1 " +
            "                and a.support_type = 2) / " +
            "             (select count(1) " +
            "              from bc_person_support a " +
            "              where a.is_delete = 0 " +
            "                and a.type = 1) * 100)")
    BigDecimal getFsgyl();
    /** ----------------------------------------- center ------------------------------ */


    /** ----------------------------------------- 入住情况统计 ------------------------------ */

    @Select("select c.name                            as name, " +
            "       c.value                           as id, " +
            "       (select count(1) " +
            "        from bc_room a " +
            "        where a.is_delete = 0 " +
            "          and a.room_type = c.value) as value " +
            "from sys_data_dictionary_item c " +
            "where c.is_delete = 0 " +
            "  and c.dictionary_code = '0010' " +
            "order by c.value")
    List<DataStatisticsItem> listRoomType();


    // 房源入住情况
    @Select("select '供养床位'                             as name, " +
            "       (select count(1) " +
            "        from bc_room_bed a " +
            "                 left join bc_room b on a.room_id = b.id " +
            "        where a.is_delete = 0 " +
            "          and b.is_delete = 0 " +
            "          and b.room_type = 1)          as value, " +
            "       (select count(1) " +
            "        from bc_room_bed a " +
            "                 left join bc_hospital_record b on a.id = b.bed_id " +
            "        where a.is_delete = 0 " +
            "          and b.is_delete = 0 " +
            "          and b.check_out_time is null) as value2 "  +

            "union " +

            "select '供养房间'                             as name, " +
            "       (select count(1) " +
            "        from bc_room " +
            "        where is_delete = 0 " +
            "          and room_type = 1)            as value, " +
            "       (select count(distinct a.id) " +
            "        from bc_room a " +
            "                 left join bc_hospital_record b on a.id = b.room_id " +
            "        where a.is_delete = 0 " +
            "          and b.is_delete = 0 " +
            "          and b.check_out_time is null) as value2 ")
    List<DataStatisticsItem> listRoomCount();

    /** ----------------------------------------- 入住情况统计 ------------------------------ */


    // 各村供养人数
    @Select("select c.name as name, c.value as id,  " +
            "                   (select count(1)   " +
            "                       from bc_person_support a " +
            "                       where a.is_delete = 0  " +
            "                       and a.type = 1 " +
            "                       and IFNULL(a.village, '') != '' " +
            "                       and a.village = c.value) as value, " +
            "                   (select count(1)   " +
            "                       from bc_person_support a " +
            "                       where a.is_delete = 0  " +
            "                       and a.type = 1 " +
            "                       and a.support_type = 1 " +
            "                       and IFNULL(a.village, '') != '' " +
            "                       and a.village = c.value) as value2 " +
            "            from sys_data_dictionary_item c  " +
            "            where c.is_delete = 0  " +
            "              and c.dictionary_code = '0040' " +
            "              order by c.value ")
    List<DataStatisticsItem> listVillageCount();

}
