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

    <sql id="selectPostCandidateColumns">
        SELECT
            c.id,
            c.post_id AS postId,
            c.student_id AS studentId,
            s.name AS name,
            CASE s.gender
                WHEN '0' THEN '男'
                WHEN '1' THEN '女'
                ELSE '--'
            END AS gender,
            s.mobile AS phone,
            CASE
                WHEN COALESCE(NULLIF(c.student_status, ''), 'pending') = 'rejected' THEN 'closed'
                ELSE COALESCE(NULLIF(c.enterprise_status, ''), 'pending')
            END AS status,
            CASE
                WHEN COALESCE(NULLIF(c.student_status, ''), 'pending') = 'rejected' THEN '学员已拒绝'
                WHEN COALESCE(NULLIF(c.enterprise_status, ''), 'pending') = 'adopted' THEN '录用'
                WHEN COALESCE(NULLIF(c.enterprise_status, ''), 'pending') = 'rejected' THEN '不录用'
                ELSE '待回复'
            END AS statusText,
            CASE
                WHEN COALESCE(NULLIF(c.student_status, ''), 'pending') = 'rejected' THEN 'danger'
                WHEN COALESCE(NULLIF(c.enterprise_status, ''), 'pending') = 'adopted' THEN 'success'
                WHEN COALESCE(NULLIF(c.enterprise_status, ''), 'pending') = 'rejected' THEN 'danger'
                ELSE 'warning'
            END AS statusType,
            DATE_FORMAT(COALESCE(c.update_time, c.create_time), '%Y-%m-%d %H:%i:%s') AS evaluateTime,
            s.resume_file AS resumeFile,
            c.remark
    </sql>

    <sql id="selectPostCandidateFromWhere">
        FROM main_back_candidate c
        LEFT JOIN main_student s ON c.student_id = s.id AND IFNULL(s.del_flag, '0') = '0'
        WHERE IFNULL(c.del_flag, '0') = '0'
          AND c.post_id = #{bo.postId}
        <if test="bo.keyword != null and bo.keyword != ''">
            AND (
                s.name LIKE CONCAT('%', #{bo.keyword}, '%')
                OR s.mobile LIKE CONCAT('%', #{bo.keyword}, '%')
            )
        </if>
        <if test="bo.status != null and bo.status != ''">
            AND CASE
                    WHEN COALESCE(NULLIF(c.student_status, ''), 'pending') = 'rejected' THEN 'closed'
                    ELSE COALESCE(NULLIF(c.enterprise_status, ''), 'pending')
                END = #{bo.status}
        </if>
    </sql>

    <select id="selectPostCandidatePage" resultType="org.dromara.main.domain.vo.MainPostCandidateVo">
        <include refid="selectPostCandidateColumns" />
        <include refid="selectPostCandidateFromWhere" />
        ORDER BY c.create_time DESC, c.id DESC
    </select>

    <select id="selectPostCandidateList" resultType="org.dromara.main.domain.vo.MainPostCandidateVo">
        <include refid="selectPostCandidateColumns" />
        <include refid="selectPostCandidateFromWhere" />
        ORDER BY c.create_time DESC, c.id DESC
    </select>

</mapper>
