<?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.srs.system.mapper.SysDeptMapMapper">
    
    <resultMap type="SysDeptMap" id="SysDeptMapResult">
        <result property="id"    column="id"    />
        <result property="oldDeptId"    column="old_dept_id"    />
        <result property="newDeptId"    column="new_dept_id"    />
        <result property="oldDeptName"    column="old_dept_name"    />
        <result property="newDeptName"    column="new_dept_name"    />
    </resultMap>

    <sql id="selectSysDeptMapVo">
        select a.id, old_dept_id, new_dept_id, b.dept_name as old_dept_name, c.dept_name as new_dept_name,
               b.dept_code as old_dept_code,c.dept_code as new_dept_code
        from sys_dept_map as a
        left join sys_dept as b on a.old_dept_id = b.dept_id
        left join sys_dept as c on a.new_dept_id = c.dept_id
    </sql>

    <select id="selectSysDeptMapList" parameterType="SysDeptMap" resultMap="SysDeptMapResult">
        <include refid="selectSysDeptMapVo"/>
        <where>  
            <if test="oldDeptId != null "> and old_dept_id = #{oldDeptId}</if>
            <if test="newDeptId != null "> and new_dept_id = #{newDeptId}</if>
        </where>
    </select>
    
    <select id="selectSysDeptMapById" parameterType="Long" resultMap="SysDeptMapResult">
        <include refid="selectSysDeptMapVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSysDeptMap" parameterType="SysDeptMap" useGeneratedKeys="true" keyProperty="id">
        insert into sys_dept_map
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="oldDeptId != null">old_dept_id,</if>
            <if test="newDeptId != null">new_dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="oldDeptId != null">#{oldDeptId},</if>
            <if test="newDeptId != null">#{newDeptId},</if>
         </trim>
    </insert>

    <update id="updateSysDeptMap" parameterType="SysDeptMap">
        update sys_dept_map
        <trim prefix="SET" suffixOverrides=",">
            <if test="oldDeptId != null">old_dept_id = #{oldDeptId},</if>
            <if test="newDeptId != null">new_dept_id = #{newDeptId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysDeptMapById" parameterType="Long">
        delete from sys_dept_map where id = #{id}
    </delete>

    <delete id="deleteSysDeptMapByIds" parameterType="String">
        delete from sys_dept_map where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>