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

    <!-- 员工调动记录ResultMap -->
    <resultMap type="com.ruoyi.info.collection.domain.vo.CcdiStaffTransferVO" id="CcdiStaffTransferVOResult">
        <id property="id" column="id"/>
        <result property="staffId" column="staff_id"/>
        <result property="staffName" column="staff_name"/>
        <result property="transferType" column="transfer_type"/>
        <result property="transferSubType" column="transfer_sub_type"/>
        <result property="deptIdBefore" column="dept_id_before"/>
        <result property="deptNameBefore" column="dept_name_before"/>
        <result property="gradeBefore" column="grade_before"/>
        <result property="positionBefore" column="position_before"/>
        <result property="salaryLevelBefore" column="salary_level_before"/>
        <result property="deptIdAfter" column="dept_id_after"/>
        <result property="deptNameAfter" column="dept_name_after"/>
        <result property="gradeAfter" column="grade_after"/>
        <result property="positionAfter" column="position_after"/>
        <result property="salaryLevelAfter" column="salary_level_after"/>
        <result property="transferDate" column="transfer_date"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createdBy" column="created_by"/>
        <result property="updatedBy" column="updated_by"/>
    </resultMap>

    <!-- 分页查询员工调动记录列表 -->
    <select id="selectTransferPage" resultMap="CcdiStaffTransferVOResult">
        SELECT
            t.id, t.staff_id, s.name as staff_name, t.transfer_type, t.transfer_sub_type,
            t.dept_id_before, t.dept_name_before, t.grade_before, t.position_before, t.salary_level_before,
            t.dept_id_after, t.dept_name_after, t.grade_after, t.position_after, t.salary_level_after,
            t.transfer_date, t.created_by, t.create_time, t.updated_by, t.update_time
        FROM ccdi_staff_transfer t
        LEFT JOIN ccdi_base_staff s ON t.staff_id = s.staff_id
        <where>
            <if test="query.staffId != null">
                AND t.staff_id = #{query.staffId}
            </if>
            <if test="query.staffName != null and query.staffName != ''">
                AND s.name LIKE CONCAT('%', #{query.staffName}, '%')
            </if>
            <if test="query.transferType != null and query.transferType != ''">
                AND t.transfer_type = #{query.transferType}
            </if>
            <if test="query.transferSubType != null and query.transferSubType != ''">
                AND t.transfer_sub_type = #{query.transferSubType}
            </if>
            <if test="query.deptIdBefore != null">
                AND t.dept_id_before = #{query.deptIdBefore}
            </if>
            <if test="query.deptIdAfter != null">
                AND t.dept_id_after = #{query.deptIdAfter}
            </if>
            <if test="query.transferDateStart != null">
                AND t.transfer_date &gt;= #{query.transferDateStart}
            </if>
            <if test="query.transferDateEnd != null">
                AND t.transfer_date &lt;= #{query.transferDateEnd}
            </if>
        </where>
        ORDER BY t.transfer_date DESC, t.create_time DESC
    </select>

    <!-- 查询员工调动记录详情 -->
    <select id="selectTransferById" resultMap="CcdiStaffTransferVOResult">
        SELECT
            t.id, t.staff_id, s.name as staff_name, t.transfer_type, t.transfer_sub_type,
            t.dept_id_before, t.dept_name_before, t.grade_before, t.position_before, t.salary_level_before,
            t.dept_id_after, t.dept_name_after, t.grade_after, t.position_after, t.salary_level_after,
            t.transfer_date, t.created_by, t.create_time, t.updated_by, t.update_time
        FROM ccdi_staff_transfer t
        LEFT JOIN ccdi_base_staff s ON t.staff_id = s.staff_id
        WHERE t.id = #{id}
    </select>

    <!-- 查询员工调动记录列表（用于导出） -->
    <select id="selectTransferListForExport" resultMap="CcdiStaffTransferVOResult">
        SELECT
            t.id, t.staff_id, s.name as staff_name, t.transfer_type, t.transfer_sub_type,
            t.dept_id_before, t.dept_name_before, t.grade_before, t.position_before, t.salary_level_before,
            t.dept_id_after, t.dept_name_after, t.grade_after, t.position_after, t.salary_level_after,
            t.transfer_date, t.created_by, t.create_time, t.updated_by, t.update_time
        FROM ccdi_staff_transfer t
        LEFT JOIN ccdi_base_staff s ON t.staff_id = s.staff_id
        <where>
            <if test="query.staffId != null">
                AND t.staff_id = #{query.staffId}
            </if>
            <if test="query.staffName != null and query.staffName != ''">
                AND s.name LIKE CONCAT('%', #{query.staffName}, '%')
            </if>
            <if test="query.transferType != null and query.transferType != ''">
                AND t.transfer_type = #{query.transferType}
            </if>
            <if test="query.transferSubType != null and query.transferSubType != ''">
                AND t.transfer_sub_type = #{query.transferSubType}
            </if>
            <if test="query.deptIdBefore != null">
                AND t.dept_id_before = #{query.deptIdBefore}
            </if>
            <if test="query.deptIdAfter != null">
                AND t.dept_id_after = #{query.deptIdAfter}
            </if>
            <if test="query.transferDateStart != null">
                AND t.transfer_date &gt;= #{query.transferDateStart}
            </if>
            <if test="query.transferDateEnd != null">
                AND t.transfer_date &lt;= #{query.transferDateEnd}
            </if>
        </where>
        ORDER BY t.transfer_date DESC, t.create_time DESC
    </select>

    <!-- 批量插入员工调动记录数据 -->
    <insert id="insertBatch">
        INSERT INTO ccdi_staff_transfer
        (staff_id, transfer_type, transfer_sub_type, dept_id_before, dept_name_before, grade_before,
         position_before, salary_level_before, dept_id_after, dept_name_after, grade_after,
         position_after, salary_level_after, transfer_date, created_by, create_time, updated_by, update_time)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.staffId}, #{item.transferType}, #{item.transferSubType}, #{item.deptIdBefore},
            #{item.deptNameBefore}, #{item.gradeBefore}, #{item.positionBefore}, #{item.salaryLevelBefore},
            #{item.deptIdAfter}, #{item.deptNameAfter}, #{item.gradeAfter}, #{item.positionAfter},
            #{item.salaryLevelAfter}, #{item.transferDate}, #{item.createdBy}, NOW(), #{item.updatedBy}, NOW())
        </foreach>
    </insert>

    <!-- 查询单条记录是否存在（根据唯一键） -->
    <select id="checkExists" resultType="com.ruoyi.info.collection.domain.CcdiStaffTransfer">
        SELECT
            id, staff_id, dept_id_before, dept_id_after, transfer_date
        FROM ccdi_staff_transfer
        WHERE staff_id = #{key.staffId}
          AND dept_id_before = #{key.deptIdBefore}
          AND dept_id_after = #{key.deptIdAfter}
          AND transfer_date = #{key.transferDate}
        LIMIT 1
    </select>

    <!-- 查询单条记录是否存在（排除指定ID） -->
    <select id="checkExistsExcludeId" resultType="com.ruoyi.info.collection.domain.CcdiStaffTransfer">
        SELECT
            id, staff_id, dept_id_before, dept_id_after, transfer_date
        FROM ccdi_staff_transfer
        WHERE staff_id = #{key.staffId}
          AND dept_id_before = #{key.deptIdBefore}
          AND dept_id_after = #{key.deptIdAfter}
          AND transfer_date = #{key.transferDate}
          AND id != #{excludeId}
        LIMIT 1
    </select>

</mapper>
