<?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.ruoyi.info.collection.mapper.CcdiStaffRecruitmentMapper">

    <!-- 招聘信息ResultMap -->
    <resultMap type="com.ruoyi.info.collection.domain.vo.CcdiStaffRecruitmentVO" id="CcdiStaffRecruitmentVOResult">
        <id property="id" column="id"/>
        <result property="recruitId" column="recruit_id"/>
        <result property="recruitName" column="recruit_name"/>
        <result property="posName" column="pos_name"/>
        <result property="posCategory" column="pos_category"/>
        <result property="posDesc" column="pos_desc"/>
        <result property="candName" column="cand_name"/>
        <result property="recruitType" column="recruit_type"/>
        <result property="candEdu" column="cand_edu"/>
        <result property="candId" column="cand_id"/>
        <result property="candSchool" column="cand_school"/>
        <result property="candMajor" column="cand_major"/>
        <result property="candGrad" column="cand_grad"/>
        <result property="admitStatus" column="admit_status"/>
        <result property="workExperienceCount" column="work_experience_count"/>
        <result property="interviewerName1" column="interviewer_name1"/>
        <result property="interviewerId1" column="interviewer_id1"/>
        <result property="interviewerName2" column="interviewer_name2"/>
        <result property="interviewerId2" column="interviewer_id2"/>
        <result property="createdBy" column="created_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <!-- 分页查询招聘信息列表 -->
    <select id="selectRecruitmentPage" resultMap="CcdiStaffRecruitmentVOResult">
        SELECT
            r.id, r.recruit_id, r.recruit_name, r.pos_name, r.pos_category, r.pos_desc,
            r.cand_name, r.recruit_type, r.cand_edu, r.cand_id, r.cand_school, r.cand_major, r.cand_grad,
            r.admit_status, COALESCE(w.work_experience_count, 0) AS work_experience_count,
            r.interviewer_name1, r.interviewer_id1, r.interviewer_name2, r.interviewer_id2,
            r.created_by, r.create_time, r.updated_by, r.update_time
        FROM ccdi_staff_recruitment r
        LEFT JOIN (
            SELECT recruitment_id, COUNT(1) AS work_experience_count
            FROM ccdi_staff_recruitment_work
            GROUP BY recruitment_id
        ) w ON w.recruitment_id = r.id
        <where>
            <if test="query.recruitName != null and query.recruitName != ''">
                AND r.recruit_name LIKE CONCAT('%', #{query.recruitName}, '%')
            </if>
            <if test="query.posName != null and query.posName != ''">
                AND r.pos_name LIKE CONCAT('%', #{query.posName}, '%')
            </if>
            <if test="query.candName != null and query.candName != ''">
                AND r.cand_name LIKE CONCAT('%', #{query.candName}, '%')
            </if>
            <if test="query.recruitType != null and query.recruitType != ''">
                AND r.recruit_type = #{query.recruitType}
            </if>
            <if test="query.candId != null and query.candId != ''">
                AND r.cand_id = #{query.candId}
            </if>
            <if test="query.admitStatus != null and query.admitStatus != ''">
                AND r.admit_status = #{query.admitStatus}
            </if>
            <if test="query.interviewerName != null and query.interviewerName != ''">
                AND (r.interviewer_name1 LIKE CONCAT('%', #{query.interviewerName}, '%')
                     OR r.interviewer_name2 LIKE CONCAT('%', #{query.interviewerName}, '%'))
            </if>
            <if test="query.interviewerId != null and query.interviewerId != ''">
                AND (r.interviewer_id1 = #{query.interviewerId}
                     OR r.interviewer_id2 = #{query.interviewerId})
            </if>
        </where>
        ORDER BY r.create_time DESC
    </select>

    <!-- 查询招聘信息详情 -->
    <select id="selectRecruitmentById" resultMap="CcdiStaffRecruitmentVOResult">
        SELECT
            id, recruit_id, recruit_name, pos_name, pos_category, pos_desc,
            cand_name, recruit_type, cand_edu, cand_id, cand_school, cand_major, cand_grad,
            admit_status, interviewer_name1, interviewer_id1, interviewer_name2, interviewer_id2,
            created_by, create_time, updated_by, update_time
        FROM ccdi_staff_recruitment
        WHERE id = #{id}
    </select>

    <!-- 批量插入招聘信息数据 -->
    <insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ccdi_staff_recruitment
        (recruit_id, recruit_name, pos_name, pos_category, pos_desc,
         cand_name, recruit_type, cand_edu, cand_id, cand_school, cand_major, cand_grad,
         admit_status, interviewer_name1, interviewer_id1, interviewer_name2, interviewer_id2,
         created_by, create_time, updated_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.recruitId}, #{item.recruitName}, #{item.posName}, #{item.posCategory}, #{item.posDesc},
            #{item.candName}, #{item.recruitType}, #{item.candEdu}, #{item.candId}, #{item.candSchool}, #{item.candMajor}, #{item.candGrad},
            #{item.admitStatus}, #{item.interviewerName1}, #{item.interviewerId1}, #{item.interviewerName2}, #{item.interviewerId2},
            #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </insert>

    <!-- 批量更新招聘信息数据 -->
    <update id="updateBatch">
        <foreach collection="list" item="item" separator=";">
            UPDATE ccdi_staff_recruitment
            SET recruit_name = #{item.recruitName},
                pos_name = #{item.posName},
                pos_category = #{item.posCategory},
                pos_desc = #{item.posDesc},
                cand_name = #{item.candName},
                recruit_type = #{item.recruitType},
                cand_edu = #{item.candEdu},
                cand_id = #{item.candId},
                cand_school = #{item.candSchool},
                cand_major = #{item.candMajor},
                cand_grad = #{item.candGrad},
                admit_status = #{item.admitStatus},
                interviewer_name1 = #{item.interviewerName1},
                interviewer_id1 = #{item.interviewerId1},
                interviewer_name2 = #{item.interviewerName2},
                interviewer_id2 = #{item.interviewerId2},
                updated_by = #{item.updatedBy},
                update_time = NOW()
            WHERE id = #{item.id}
        </foreach>
    </update>

</mapper>
