<?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.ibs.cmpm.mapper.GridCmpmTransferMapper">

    <select id="getGridCmpmTransferList" resultType="GridCmpmTransferVO">
        select gct.* , concat(prev.nick_name , gct.prev_user_name)as prev_user,
               concat(next.nick_name, gct.next_user_name) as next_user
        from grid_cmpm_transfer gct
        left join sys_user prev on gct.prev_user_name = prev.user_name
        left join sys_user next on gct.next_user_name = next.user_name
        where (gct.transfer_status != '3' and gct.transfer_status != '-1') and head_id = #{headId}
        <if test="gridType != null and gridType != ''">
            and gct.grid_type = #{gridType}
        </if>
        <if test="keyword != null and keyword != ''">
            and (gct.cust_name like concat('%',#{keyword},'%') or gct.cust_id like concat('%',#{keyword},'%') or gct.account_no like concat('%',#{keyword},'%'))
        </if>
        <if test="userRole != null and userRole == 'outlet'">
            and gct.transfer_status = '0' and gct.prev_outlet_id = #{deptId}
        </if>
        <if test="userRole != null and userRole == 'branch'">
            and gct.transfer_status = '1' and gct.prev_branch_id = #{deptId}
        </if>
        <if test="userRole != null and userRole == 'private' ">
            and gct.transfer_status = '2' and gct.grid_type = 'retail'
        </if>
        <if test="userRole != null and userRole == 'public'">
            and gct.transfer_status = '2' and gct.grid_type like 'corporate%'
        </if>
        <if test="userRole != null and userRole == 'head'">
            and gct.transfer_status = '2'
        </if>
        <if test="userRole != null and userRole == 'manager'">
            and gct.prev_user_name = #{userName}
        </if>
        order by gct.update_time desc
    </select>

    <select id="getGridCmpmTransferHistoryList" resultType="GridCmpmTransferVO">
        select gct.* , concat(prev.nick_name , gct.prev_user_name)as prev_user,
        concat(next.nick_name, gct.next_user_name) as next_user
        from grid_cmpm_transfer gct
        left join sys_user prev on gct.prev_user_name = prev.user_name
        left join sys_user next on gct.next_user_name = next.user_name
        where head_id = #{headId}
        <if test="gridType != null and gridType != ''">
            and gct.grid_type = #{gridType}
        </if>
        <if test="keyword != null and keyword != ''">
            and (gct.cust_name like concat('%',#{keyword},'%') or gct.cust_id like concat('%',#{keyword},'%') or gct.account_no like concat('%',#{keyword},'%'))
        </if>
        <if test="userRole != null and userRole == 'outlet'">
            and (gct.prev_outlet_id = #{deptId} or gct.next_outlet_id = #{deptId})
        </if>
        <if test="userRole != null and userRole == 'branch'">
            and (gct.prev_branch_id = #{deptId} or gct.next_branch_id = #{deptId})
        </if>
        <if test="userRole != null and userRole == 'private'">
            and gct.grid_type = 'retail'
        </if>
        <if test="userRole != null and userRole == 'public'">
            and gct.grid_type like 'corporate%'
        </if>
        <if test="userRole != null and userRole == 'manager'">
            and (gct.prev_user_name = #{userName} or gct.next_user_name = #{userName})
        </if>
        order by gct.update_time desc
    </select>

    <select id="checkExistTransferCust" resultType="Long">
        select count(*) from grid_cmpm_transfer
        where transfer_status != '3' and transfer_status != '-1'
        and (cust_id = #{custKey} or account_no = #{custKey})
        <if test="userName != null and userName != ''">
            and prev_user_name = #{userName}
        </if>
        <if test="userName == null">
            and prev_user_name is null
        </if>
    </select>

    <insert id="batchInsert" parameterType="List">
        INSERT INTO grid_cmpm_transfer (transfer_label, transfer_type, transfer_status, grid_type, head_id, cust_type, cust_name, cust_id, account_no, prev_user_name, prev_outlet_id, prev_branch_id,
                                            next_user_name, next_outlet_id, next_branch_id, create_time, update_time)
        VALUES
        <foreach collection="gridCmpmTransferList" item="transfer" separator=",">
            (#{transfer.transferLabel}, #{transfer.transferType}, #{transfer.transferStatus}, #{transfer.gridType}, #{transfer.headId}, #{transfer.custName}, #{transfer.custId}, #{transfer.accountNo},
             #{transfer.prevUserName}, #{transfer.prevOutletId}, #{transfer.prevBranchId}, #{transfer.nextUserName}, #{transfer.nextOutletId}, #{transfer.nextBranchId}, current_timestamp(), current_timestamp())
        </foreach>

    </insert>


</mapper>