<?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="cn.iocoder.yudao.module.staff.mapper.CompanyStaffChangeMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.staff.vo.CompanyStaffChangeRespVO">
        <id property="id" column="csc_id" jdbcType="VARCHAR"/>
        <result property="oldUserId" column="csc_old_user_id" jdbcType="BIGINT"/>
        <result property="newUserId" column="csc_new_user_id" jdbcType="BIGINT"/>
        <result property="changeTime" column="csc_change_time" jdbcType="TIMESTAMP"/>
        <result property="fileUrls" column="csc_file_urls" jdbcType="VARCHAR"/>
        <result property="remark" column="csc_remark" jdbcType="VARCHAR"/>

        <association property="oldStaff" javaType="cn.iocoder.yudao.module.staff.entity.CompanyStaff">
            <id property="id" column="cs_old_id" jdbcType="VARCHAR"/>
            <result property="workNo" column="cs_old_work_no" jdbcType="VARCHAR"/>
            <result property="name" column="cs_old_name" jdbcType="VARCHAR"/>
            <result property="tel" column="cs_old_tel" jdbcType="VARCHAR"/>
            <result property="sex" column="cs_old_sex" jdbcType="VARCHAR"/>
            <result property="address" column="cs_old_address" jdbcType="VARCHAR"/>
            <result property="workDate" column="cs_old_work_date" jdbcType="DATE"/>
            <result property="workYear" column="cs_old_work_year" jdbcType="DECIMAL"/>
            <result property="joinedDate" column="cs_old_joined_date" jdbcType="DATE"/>
            <result property="joinedYear" column="cs_old_joined_year" jdbcType="DECIMAL"/>
            <result property="education" column="cs_old_education" jdbcType="VARCHAR"/>
            <result property="content" column="cs_old_content" jdbcType="VARCHAR"/>
            <result property="uniqueCode" column="cs_old_unique_code" jdbcType="VARCHAR"/>
            <result property="fileUrls" column="cs_old_file_urls" jdbcType="VARCHAR"/>
        </association>

        <association property="newStaff" javaType="cn.iocoder.yudao.module.staff.entity.CompanyStaff">
            <id property="id" column="cs_new_id" jdbcType="VARCHAR"/>
            <result property="workNo" column="cs_new_work_no" jdbcType="VARCHAR"/>
            <result property="name" column="cs_new_name" jdbcType="VARCHAR"/>
            <result property="tel" column="cs_new_tel" jdbcType="VARCHAR"/>
            <result property="sex" column="cs_new_sex" jdbcType="VARCHAR"/>
            <result property="address" column="cs_new_address" jdbcType="VARCHAR"/>
            <result property="workDate" column="cs_new_work_date" jdbcType="DATE"/>
            <result property="workYear" column="cs_new_work_year" jdbcType="DECIMAL"/>
            <result property="joinedDate" column="cs_new_joined_date" jdbcType="DATE"/>
            <result property="joinedYear" column="cs_new_joined_year" jdbcType="DECIMAL"/>
            <result property="education" column="cs_new_education" jdbcType="VARCHAR"/>
            <result property="content" column="cs_new_content" jdbcType="VARCHAR"/>
            <result property="uniqueCode" column="cs_new_unique_code" jdbcType="VARCHAR"/>
            <result property="fileUrls" column="cs_new_file_urls" jdbcType="VARCHAR"/>
        </association>
    </resultMap>

    <select id="getStaffChangePage" resultMap="BaseResultMap">
        SELECT
        csc.id as csc_id,
        csc.old_user_id as csc_old_user_id,
        csc.new_user_id as csc_new_user_id,
        csc.change_time as csc_change_time,
        csc.file_urls as csc_file_urls,
        csc.remark as csc_remark,
        cs_old.id as cs_old_id,
        cs_old.work_no as cs_old_work_no,
        cs_old.name as cs_old_name,
        cs_old.tel as cs_old_tel,
        cs_old.sex as cs_old_sex,
        cs_old.address as cs_old_address,
        cs_old.work_date as cs_old_work_date,
        cs_old.work_year as cs_old_work_year,
        cs_old.joined_date as cs_old_joined_date,
        cs_old.joined_year as cs_old_joined_year,
        cs_old.education as cs_old_education,
        cs_old.content as cs_old_content,
        cs_old.unique_code as cs_old_unique_code,
        cs_old.file_urls as cs_old_file_urls,
        cs_new.id as cs_new_id,
        cs_new.work_no as cs_new_work_no,
        cs_new.name as cs_new_name,
        cs_new.tel as cs_new_tel,
        cs_new.sex as cs_new_sex,
        cs_new.address as cs_new_address,
        cs_new.work_date as cs_new_work_date,
        cs_new.work_year as cs_new_work_year,
        cs_new.joined_date as cs_new_joined_date,
        cs_new.joined_year as cs_new_joined_year,
        cs_new.education as cs_new_education,
        cs_new.content as cs_new_content,
        cs_new.unique_code as cs_new_unique_code,
        cs_new.file_urls as cs_new_file_urls
        FROM company_staff_change csc
        INNER JOIN company_staff cs_old ON csc.old_user_id = cs_old.user_id
        INNER JOIN company_staff cs_new ON csc.new_user_id = cs_new.user_id
        where csc.deleted = '0'
        <if test="map.name != null and map.name != ''">
            and (cs_old.name like concat('%', #{map.name}, '%') or cs_new.name like concat('%', #{map.name}, '%'))
        </if>
        <if test="map.workNo != null and map.workNo != ''">
            and (cs_old.work_no like concat('%', #{map.workNo}, '%') or cs_new.work_no like concat('%', #{map.workNo},
            '%'))
        </if>
        <if test="map.tel != null and map.tel != ''">
            and (cs_old.tel like concat('%', #{map.tel}, '%') or cs_new.tel like concat('%', #{map.tel}, '%'))
        </if>
        <if test="map.changeTimeArray != null and map.changeTimeArray.length > 0">
            and (csc.change_time between #{map.changeTimeArray[0]} and #{map.changeTimeArray[1]})
        </if>
        <if test="map.oldUserIds != null and map.oldUserIds.size > 0">
            and csc.old_user_id in
            <foreach collection="map.oldUserIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="map.newUserIds != null and map.newUserIds.size > 0">
            and csc.new_user_id in
            <foreach collection="map.newUserIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        order by csc.create_time desc
    </select>

</mapper>