<?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.sundot.airport.system.mapper.portrait.PoliticalStatusIndicatorMapper">

    <resultMap type="com.sundot.airport.system.domain.portrait.PoliticalStatusStats" id="PoliticalStatusStatsResult">
        <result property="politicalStatus" column="political_status"/>
        <result property="count" column="count"/>
        <result property="totalCount" column="total_count"/>
        <result property="percentage" column="percentage"/>
    </resultMap>

    <sql id="userIdsCondition">
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询个人政治面貌 -->
    <select id="queryPersonalPoliticalStatus" parameterType="Long" resultType="String">
        SELECT political_status 
        FROM sys_user 
        WHERE user_id = #{userId}
    </select>

    <!-- 查询组织政治面貌统计 -->
    <select id="queryOrgPoliticalStatusStats" resultMap="PoliticalStatusStatsResult">
        SELECT 
            political_status,
            COUNT(*) as count,
            (SELECT COUNT(*) FROM sys_user WHERE user_id in <include refid="userIdsCondition" />  and del_flag='0' and status='0') as total_count,
            ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM sys_user WHERE user_id in <include refid="userIdsCondition" />), 2) as percentage
        FROM sys_user 
        WHERE user_id in <include refid="userIdsCondition" />
        and del_flag='0' and status='0'
        GROUP BY political_status
    </select>

</mapper>