<?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.QualificationLevelIndicatorMapper">

    <resultMap type="com.sundot.airport.system.domain.portrait.QualificationStats" id="QualificationStatsResult">
        <result property="levelName" column="level_name"/>
        <result property="count" column="count"/>
        <result property="totalCount" column="total_count"/>
        <result property="percentage" column="percentage"/>
    </resultMap>

    <!-- 定义用户ID列表的SQL片段 -->
    <sql id="userIdsCondition">
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询个人资质等级 -->
    <select id="queryPersonalQualificationLevel" parameterType="Long" resultType="String">
        SELECT qualification_level 
        FROM sys_user 
        WHERE user_id = #{userId}
    </select>

    <!-- 查询组织资质等级统计 -->
    <select id="queryOrgQualificationLevelStats" resultMap="QualificationStatsResult">
        SELECT 
            qualification_level as level_name,
            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 qualification_level
    </select>

</mapper>