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

    <resultMap type="com.sundot.airport.system.domain.portrait.PositionInfo" id="PositionInfoResult">
        <result property="postId" column="post_id"/>
        <result property="postName" column="post_name"/>
        <result property="postCode" column="post_code"/>
    </resultMap>

    <resultMap type="com.sundot.airport.system.domain.portrait.PositionCompetencyStats"
               id="PositionCompetencyStatsResult">
        <result property="postName" column="post_name"/>
        <result property="competentCount" column="competent_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="queryPersonalAvailablePositions" parameterType="Long" resultMap="PositionInfoResult">
        select p.post_name, p.post_id, p.post_code
        from sys_post p
                 left join sys_user_post up on up.post_id = p.post_id
                 left join sys_user u on u.user_id = up.user_id
        where u.user_id = #{userId}
          and u.del_flag = '0'
          and u.status = '0'
    </select>

    <!-- 查询组织岗位胜任统计 -->
    <select id="queryOrgPositionCompetencyStats" resultMap="PositionCompetencyStatsResult">
        SELECT
        p.post_id,
        p.post_name,
        COUNT(u.user_id) as competent_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(u.user_id) * 100.0 / (SELECT COUNT(*) FROM sys_user WHERE user_id in
        <include refid="userIdsCondition"/>
        and del_flag='0' and status='0'), 2) as percentage
        from sys_post p
        left join sys_user_post up on up.post_id = p.post_id
        left join sys_user u on u.user_id = up.user_id AND u.del_flag='0' and u.status='0'
        WHERE u.user_id in
        <include refid="userIdsCondition"/>
        and u.del_flag='0' and u.status='0'
        GROUP BY p.post_id, p.post_name
    </select>

</mapper>