<?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.ExamUserEvaluationDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.artfess.examine.model.ExamUserEvaluation">
        <id column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="user_name_" property="userName" />
        <result column="evaluation_time_" property="evaluationTime" />
        <result column="in_count_" property="inCount" />
        <result column="not_count_" property="notCount" />
        <result column="total_count" property="totalCount" />
        <result column="level_" property="level" />
        <result column="position_id_" property="positionId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, user_id_, user_name_, evaluation_time_, in_count_, not_count_, total_count, level_, position_id_
    </sql>
    <select id="findByPage" resultType="com.artfess.examine.model.ExamUserEvaluation">
        SELECT e.* from
        exam_user_evaluation e
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="myYearPage" resultType="com.artfess.examine.model.ExamUserEvaluation">
        SELECT
            e.id_,
            e.subject_name_,
            o.user_id_,
            u.fullname_ AS userName,
            y.year_,
            y.totalCount,
            COALESCE ( e.in_count_, 0 ) AS inCount,
            (y.totalCount - COALESCE ( e.in_count_, 0 )) as notCount,
            y.position_id_,
            e.evaluation_time_,
            e.level_
        FROM
            uc_user u
        LEFT JOIN uc_org_user o on u.id_ = o.user_id_
        LEFT JOIN (
        SELECT
            e.position_id_,e.year_,count(e.*) as totalCount
        FROM
            exam_year_assessment_sub e
            GROUP BY e.position_id_,e.year_) y on o.pos_id_ = y.position_id_
        LEFT JOIN exam_user_evaluation e on e.user_id_ = u.id_ and y.position_id_ = e.position_id_
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
        <if test="@com.artfess.base.ognl.Ognl@isEmpty(ew.customSqlSegment)"> WHERE </if>
        <if test="@com.artfess.base.ognl.Ognl@isNotEmpty(ew.customSqlSegment)"> AND </if>
        y.position_id_ is not NULL
    </select>
    <select id="getSubjectInfo" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            subject_ids_ AS subjectId,
            subject_name_ AS subjectName
        FROM
            exam_year_assessment_sub
        WHERE
            position_id_ = #{vo.positionId}
            AND year_ = #{vo.year}
    </select>
    <select id="getPositionPaper" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            b.id_ as paperId,b.name_ as name
        FROM
            exam_paper_base b
            LEFT JOIN exam_paper_setting s on s.paper_id_ = b.id_
        WHERE
            b.is_dele_ = '0'
            AND b.paper_type_ = '2'
            AND b.create_time_ > date_trunc( 'year', now( ) )
            AND date_trunc( 'year', now( ) ) + INTERVAL '1 year' > b.create_time_
            and s.status_ not in('1')
            AND b.subject_id_ = #{ vo.subjectId}
    </select>
    <select id="findByPositionId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            s.name_ as subjectName,
            s.id_ as subjectId
        FROM
            exam_subject_info s
            LEFT JOIN exam_subject_pos p ON s.id_ = p.subject_id_
        WHERE
            p.position_id_ = #{positionId}
            AND s.train_type_ = #{type}
    </select>
    <select id="offineInfo" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            d.subject_name_ AS subjectname
        FROM
            exam_user_evaluation_detail d
            LEFT JOIN uc_org_user u ON u.user_id_ = d.user_id_
        WHERE
            d.type_ = '2'
        <if test="orgId != null and orgId != ''">
            AND u.org_id_ = #{orgId}
        </if>
            AND d.year_ = #{year}
        GROUP BY
            d.subject_name_


    </select>
    <select id="gwxlSubjectNames" resultType="java.lang.String">

        SELECT DISTINCT s.name_ FROM
        exam_subject_info s
        LEFT JOIN exam_subject_pos p ON s.id_ = p.subject_id_
        LEFT JOIN
        (SELECT s.* FROM (SELECT
        count(*) as count,p.position_id_
        FROM
        exam_subject_info s
        LEFT JOIN exam_subject_pos p ON s.id_ = p.subject_id_
        WHERE
        s.train_type_ = '4'
        and s.is_dele_ ='0'
        <if test="orgId != null and orgId != ''">
            AND p.org_id_ = #{orgId}
        </if>

        GROUP BY s.train_type_,p.position_id_) s
        ORDER BY s.count desc
        LIMIT 1) a on a.position_id_ = p.position_id_
        WHERE   s.train_type_ = '4'
        and s.is_dele_ ='0'
        <if test="orgId != null and orgId != ''">
            AND p.org_id_ = #{orgId}
        </if>

    </select>
    <select id="zygtxlSubjectNames" resultType="java.lang.String">

        SELECT DISTINCT s.name_ FROM
        exam_subject_info s
        LEFT JOIN exam_subject_pos p ON s.id_ = p.subject_id_
        LEFT JOIN
        (SELECT s.* FROM (SELECT
        count(*) as count,p.org_id_
        FROM
        exam_subject_info s
        LEFT JOIN exam_subject_pos p ON s.id_ = p.subject_id_
        WHERE
        s.train_type_ = '4'
        and s.is_dele_ ='0'
        <if test="orgId != null and orgId != ''">
            AND p.org_id_ = #{orgId}
        </if>

        GROUP BY s.train_type_,p.org_id_) s
        ORDER BY s.count desc
        LIMIT 1) a on a.org_id_ = p.org_id_
        WHERE   s.train_type_ = '4'
        and s.is_dele_ ='0'
        <if test="orgId != null and orgId != ''">
            AND p.org_id_ = #{orgId}
        </if>
    </select>

    <select id="positionArchives" resultType="com.artfess.data.vo.ArchivesManagerVo">
        SELECT s.*,p.pos_name_ as posName FROM
        (SELECT
            e.position_id_ as positionId,
            e.year_,
            COUNT ( 1 ) AS shouldCount,
            COUNT ( CASE WHEN e.in_count_ = 0 THEN '1' END ) AS lackCount,
            COUNT ( CASE WHEN e.in_count_ > 0 THEN '1' END ) AS actualCount,
            COUNT ( CASE WHEN e.level_ = '4' or e.level_ is null THEN '1' END ) AS noPassCount,
            COUNT ( CASE WHEN e.level_ != '4' THEN '1' END ) AS passCount
        FROM
            exam_user_evaluation e
        GROUP BY
            e.position_id_,
            e.year_) s
        LEFT JOIN uc_org_post p on p.id_ = s.positionId
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="orgArchives" resultType="com.artfess.data.vo.ArchivesManagerVo">
        SELECT * FROM exam_org_evaluation
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="subjectArchives" resultType="com.artfess.data.vo.ArchivesManagerVo">
        SELECT
            p.pos_name_ as posName,s.subject_ids_ as subjectId,
            s.*
        FROM
            exam_year_assessment_sub s
            LEFT JOIN uc_org_post P ON P.id_ = s.position_id_
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
    <select id="getShouldCountMap" resultType="com.artfess.data.vo.ArchivesManagerVo">
        SELECT
            pos_id_ as positionId,
            COUNT ( * ) as shouldCount
        FROM
            uc_org_user
        WHERE is_dele_='0' and pos_id_ in
        <foreach collection="postionIds" index="index" item="posId" open="(" separator="," close=")">
            #{posId}
        </foreach>
        GROUP BY pos_id_
    </select>
    <select id="getEvaluationList" resultType="com.artfess.data.vo.ArchivesManagerVo">
        SELECT
            e.position_id_ AS positionId,
            e.year_,
            e.subject_id_ as subjectId,
            COUNT ( 1 ) AS actualCount,
            COUNT ( CASE WHEN e.level_ = '4' OR e.level_ IS NULL THEN '1' END ) AS noPassCount,
            COUNT ( CASE WHEN e.level_ != '4' THEN '1' END ) AS passCount
        FROM
            exam_user_evaluation_detail e

            WHERE e.year_=#{year}
        GROUP BY
            e.position_id_,
            e.year_,
            e.subject_id_
    </select>
    <select id="userQualityAnalyse" resultType="com.artfess.data.vo.UserExamVo">
        SELECT e.* from
        exam_user_evaluation e
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

</mapper>
