<?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="org.dromara.main.mapper.MainExamEvaluationMapper">

    <resultMap type="org.dromara.main.domain.MainExamEvaluation" id="MainExamEvaluationResult">
        <result property="id" column="id"/>
        <result property="evaluationName" column="evaluation_name"/>
        <result property="grade" column="grade"/>
        <result property="positionId" column="position_id"/>
        <result property="positionType" column="position_type"/>
        <result property="tags" column="tags"/>
        <result property="mainImage" column="main_image"/>
        <result property="imageAlbum" column="image_album"/>
        <result property="detail" column="detail"/>
        <result property="price" column="price"/>
        <result property="onTime" column="on_time"/>
        <result property="downTime" column="down_time"/>
        <result property="status" column="status"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="createDept" column="create_dept"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>

    <resultMap type="org.dromara.main.domain.vo.MainExamEvaluationVo" id="MainExamEvaluationVoResult" extends="MainExamEvaluationResult">
        <result property="mainImageUrl" column="main_image_url"/>
        <result property="positionName" column="position_name"/>
        <collection property="abilityConfigs"
                    ofType="org.dromara.main.domain.MainAbilityConfig"
                    select="org.dromara.main.mapper.MainAbilityConfigMapper.selectByEvaluationId"
                    column="id"/>
    </resultMap>

    <sql id="selectMainExamEvaluationVo">
        SELECT
            e.id, e.evaluation_name, e.grade, e.position_id, e.position_type,
            e.tags, e.main_image, e.image_album, e.detail, e.price,
            e.on_time, e.down_time, e.status,
            e.tenant_id, e.create_dept, e.create_by, e.create_time,
            e.update_by, e.update_time, e.remark, e.del_flag,
            oss.url AS main_image_url,
            p.post_name AS position_name
        FROM main_exam_evaluation e
                 LEFT JOIN sys_oss oss ON e.main_image = oss.oss_id
                 LEFT JOIN main_position p ON e.position_id = p.id
    </sql>

    <select id="selectVoByIdWithImages" resultMap="MainExamEvaluationVoResult">
        <include refid="selectMainExamEvaluationVo"/>
        WHERE e.id = #{id} AND e.del_flag = '0'
    </select>

    <select id="selectVoPageWithImages" resultMap="MainExamEvaluationVoResult">
        <include refid="selectMainExamEvaluationVo"/>
        ${ew.customSqlSegment}
    </select>

    <select id="selectParticipantStatsByEvaluationIds" resultType="java.util.HashMap">
        SELECT
            a.evaluation_id AS evaluationId,
            COUNT(DISTINCT a.id) AS totalCount,
            COUNT(DISTINCT CASE
                WHEN a.apply_status = '2' OR a.final_result IN ('1', '2')
                THEN a.id
            END) AS participantCount
        FROM main_exam_apply a
        WHERE IFNULL(a.del_flag, '0') = '0'
          AND a.evaluation_id IN
          <foreach collection="ids" item="id" open="(" separator="," close=")">
              #{id}
          </foreach>
        GROUP BY a.evaluation_id
    </select>

    <select id="selectApplyListByEvaluationId" resultType="org.dromara.main.domain.vo.MainExamApplyListVo">
        SELECT
            a.id,
            a.evaluation_id AS evaluationId,
            a.student_id AS studentId,
            s.name AS name,
            CASE s.gender
                WHEN '0' THEN '男'
                WHEN '1' THEN '女'
                ELSE '--'
            END AS gender,
            '--' AS department,
            s.mobile AS phone,
            CASE
                WHEN a.final_result = '1' THEN '通过'
                WHEN a.final_result = '2' THEN '未通过'
                WHEN a.apply_status = '2' THEN '待评分'
                WHEN a.apply_status = '1' THEN '测评中'
                ELSE '待测评'
            END AS statusText,
            CASE
                WHEN a.final_result = '1' THEN 'success'
                WHEN a.final_result = '2' THEN 'danger'
                WHEN a.apply_status IN ('1', '2') THEN 'warning'
                ELSE 'info'
            END AS statusType,
            DATE_FORMAT(COALESCE(a.finished_time, a.update_time, a.create_time), '%Y-%m-%d %H:%i:%s') AS evaluateTime,
            CASE
                WHEN a.final_result IN ('1', '2') OR a.apply_status IN ('1', '2') THEN 'detail'
                ELSE 'remove'
            END AS actionType
        FROM main_exam_apply a
        LEFT JOIN main_student s ON a.student_id = s.id AND IFNULL(s.del_flag, '0') = '0'
        WHERE IFNULL(a.del_flag, '0') = '0'
          AND a.evaluation_id = #{evaluationId}
          AND a.tenant_id = #{tenantId}
          <if test="keyword != null and keyword != ''">
            AND (
                s.name LIKE CONCAT('%', #{keyword}, '%')
                OR s.mobile LIKE CONCAT('%', #{keyword}, '%')
                OR a.final_result LIKE CONCAT('%', #{keyword}, '%')
            )
          </if>
        ORDER BY a.create_time DESC, a.id DESC
    </select>

</mapper>
