<?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.artfess.examine.dao.ExamUserEvaluationDetailDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.artfess.examine.model.ExamUserEvaluationDetail">
        <id column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="user_name_" property="userName" />
        <result column="record_id_" property="recordId" />
        <result column="subject_id_" property="subjectId" />
        <result column="level_" property="level" />
        <result column="position_id_" property="positionId" />
        <result column="year_" property="year" />
        <result column="status_" property="status" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, user_id_, user_name_, record_id_, subject_id_, level_, position_id_, year_, status_
    </sql>
    <select id="getUserList" resultType="com.artfess.examine.vo.UserInfoVo">
         SELECT
            DISTINCT p.user_id_,u.fullname_ as userName,u.sex_,o.name_ as orgName
        FROM
		uc_org_user p
		LEFT JOIN uc_user u on u.id_ = p.user_id_
		LEFT JOIN uc_org o on o.id_ = p.org_id_
        <where>
            <if test="positionId != null and positionId != ''">
                AND p.pos_id_ = #{positionId}
            </if>
        </where>

    </select>
    <select id="getYearUserList" resultType="com.artfess.examine.vo.UserInfoVo">
          SELECT
            DISTINCT p.user_id_,u.fullname_ as userName,u.sex_,o.name_ as orgName,e.level_ as level
        FROM
		uc_org_user p
		LEFT JOIN uc_user u on u.id_ = p.user_id_
		LEFT JOIN uc_org o on o.id_ = p.org_id_
		LEFT JOIN exam_user_evaluation e on e.position_id_ = p.pos_id_ and e.user_id_  = u.id_
		<where>
            <if test="orgId != null and orgId != ''">
                AND p.org_id_ = #{orgId}
            </if>
        </where>

    </select>
    <select id="findByYearParams" resultType="com.artfess.examine.model.ExamUserEvaluationDetail">
        SELECT DISTINCT
            d.*
        FROM
            exam_user_evaluation_detail d
            LEFT JOIN uc_org_user u ON u.user_id_ = d.user_id_
        LEFT JOIN exam_subject_info s on s.id_ = d.subject_id_  and s.is_dele_ ='0'
        WHERE
            d.year_ =#{year}
        and (s.train_type_ != '3' OR d.subject_id_ is NULL)
        <if test="orgId != null and orgId != ''">
            and u.org_id_ = #{orgId}
        </if>

    </select>
    <select id="getPositionList" resultType="com.artfess.examine.vo.PositionVo">
        SELECT
            id_ AS positionId,
            pos_name_ AS positionName
        FROM
            uc_org_post
        WHERE
            is_dele_ = '0'
    </select>
    <select id="userStudyAnalyse" resultType="com.artfess.data.vo.UserExamVo">
        SELECT
            e.*,a.*
        FROM
            exam_user_evaluation e
            LEFT JOIN
        (SELECT
            s.year_,
            s.position_id_,
            s.user_id_,
            COUNT ( 1 ) AS shouldCount,
            COUNT ( CASE WHEN s.level_ is not null THEN '1' END ) AS actualCount,
            COUNT ( CASE WHEN s.level_ = '1' THEN '1' END ) AS excellentCount,
            COUNT ( CASE WHEN s.level_ = '2' THEN '1' END ) AS goodCount,
            COUNT ( CASE WHEN s.level_ = '3' THEN '1' END ) AS passCount,
            COUNT ( CASE WHEN s.level_ = '4' THEN '1' END ) AS noPassCount
        FROM
            exam_user_evaluation_detail s
        GROUP BY
            s.year_,
            s.position_id_,
            s.user_id_) a on e.year_ = a.year_ and a.user_id_ = e.user_id_ and a.position_id_ = e.position_id_
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="getSubjectNames" resultType="java.lang.String">
        SELECT
            string_agg ( a.subject_name_, ',' ) AS subjectName
        FROM
            ( SELECT regexp_split_to_table( subject_name_, ',' ) AS subject_name_ FROM exam_user_evaluation_detail a
            where a.position_id_ =#{positionId} and a.year_ =#{year} and a.user_id_ =#{userId}
            ORDER BY a.score_ DESC LIMIT 4 ) a
    </select>
    <select id="findByPage" resultType="com.artfess.examine.model.ExamUserEvaluationDetail">
            SELECT s.* from (SELECT
            d.*,
            u.mobile_,
            u.education_,
            u.birthday_,
            u.id_card_,
            u.entry_date_,
            u.sex_
        FROM
            exam_user_evaluation_detail d
            LEFT JOIN uc_user u ON u.id_ = d.user_id_) s
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

</mapper>
