<?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.branch.mapper.TBranchMemTransferMapper">
    
    <resultMap type="TBranchMemTransfer" id="TBranchMemTransferResult">
        <result property="transferId"    column="transfer_id"    />
        <result property="userId"    column="user_id"    />
        <result property="oldDeptId"    column="old_dept_id"    />
        <result property="newDeptId"    column="new_dept_id"    />
        <result property="transferTime"    column="transfer_time"    />
        <result property="duesPaidTo"    column="dues_paid_to"    />
        <result property="remarks"    column="remarks"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectTBranchMemTransferVo">
        select u.transfer_id, u.user_id, u.old_dept_id, u.new_dept_id, u.transfer_time, u.dues_paid_to, u.remarks, u.del_flag, u.create_by, u.create_time, u.update_by, u.update_time, u.dept_id,
        us.NICK_NAME
        from t_branch_mem_transfer u
          left join sys_dept d on u.dept_id = d.dept_id
          left join SYS_USER us on us.user_id = u.user_id
    </sql>

    <select id="selectTBranchMemTransferList" parameterType="TBranchMemTransfer" resultMap="TBranchMemTransferResult">
        <include refid="selectTBranchMemTransferVo"/>
        <where>  
            <if test="transferId != null "> and u.transfer_id = #{transferId}</if>
            <if test="userId != null "> and u.user_id = #{userId}</if>
            <if test="oldDeptId != null "> and u.old_dept_id = #{oldDeptId}</if>
            <if test="newDeptId != null "> and u.new_dept_id = #{newDeptId}</if>
            <if test="transferTime != null "> and u.transfer_time = #{transferTime}</if>
            <if test="duesPaidTo != null "> and u.dues_paid_to = #{duesPaidTo}</if>
            <if test="remarks != null  and remarks != ''"> and u.remarks like concat(concat('%', #{remarks}), '%')</if>
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            <if test="nickName != null  and nickName != ''"> and us.nick_name like concat(concat('%', #{nickName}), '%')</if>
            and u.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTBranchMemTransferByTransferId" parameterType="Long" resultMap="TBranchMemTransferResult">
        <include refid="selectTBranchMemTransferVo"/>
        where u.transfer_id = #{transferId}
        and u.del_flag = 0
    </select>
        
    <insert id="insertTBranchMemTransfer" parameterType="TBranchMemTransfer">
        <selectKey keyProperty="transferId" resultType="long" order="BEFORE">
            SELECT seq_t_branch_mem_transfer.NEXTVAL as transferId FROM DUAL
        </selectKey>
        insert into t_branch_mem_transfer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="transferId != null">transfer_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="oldDeptId != null">old_dept_id,</if>
            <if test="newDeptId != null">new_dept_id,</if>
            <if test="transferTime != null">transfer_time,</if>
            <if test="duesPaidTo != null">dues_paid_to,</if>
            <if test="remarks != null">remarks,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="transferId != null">#{transferId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="oldDeptId != null">#{oldDeptId},</if>
            <if test="newDeptId != null">#{newDeptId},</if>
            <if test="transferTime != null">#{transferTime},</if>
            <if test="duesPaidTo != null">#{duesPaidTo},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTBranchMemTransfer" parameterType="TBranchMemTransfer">
        update t_branch_mem_transfer
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="oldDeptId != null">old_dept_id = #{oldDeptId},</if>
            <if test="newDeptId != null">new_dept_id = #{newDeptId},</if>
            <if test="transferTime != null">transfer_time = #{transferTime},</if>
            <if test="transferTime == null">transfer_time = null,</if>
            <if test="duesPaidTo != null">dues_paid_to = #{duesPaidTo},</if>
            <if test="duesPaidTo == null">dues_paid_to = null,</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where transfer_id = #{transferId}
    </update>

    <update id="deleteTBranchMemTransferByTransferId" parameterType="Long">
        update t_branch_mem_transfer set del_flag = 2 where transfer_id = #{transferId}
    </update>

    <update id="deleteTBranchMemTransferByTransferIds" parameterType="String">
        update t_branch_mem_transfer set del_flag = 2 where transfer_id in
        <foreach item="transferId" collection="array" open="(" separator="," close=")">
            #{transferId}
        </foreach>
    </update>
</mapper>