<?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.list.mapper.CustDeptUserGridMapper">

    <resultMap type="CustDeptUserGrid" id="CustDeptUserGridResult">
        <result property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="gridId" column="grid_id"/>
        <result property="gridName" column="grid_name"/>
        <result property="custId" column="cust_id"/>
        <result property="custIdc" column="cust_idc"/>
        <result property="custName" column="cust_name"/>
        <result property="custPhone" column="cust_phone"/>
        <result property="deptName" column="dept_name"/>
        <result property="gridName2" column="grid_name2"/>
        <result property="outlets" column="outlets"/>
        <result property="outletsId" column="outlets_id"/>
        <result property="tellerId" column="teller_id"/>
        <result property="opsDept" column="ops_dept"/>
    </resultMap>

    <sql id="selectCustDeptUserGridVo">
        select id,
               dept_id,
               user_id,
               user_name,
               grid_id,
               grid_name,
               cust_id,
               cust_idc,
               cust_name,
               cust_phone,
               dept_name,
               grid_name2,
               outlets,
               outlets_id,
               teller_id,
               ops_dept
        from cust_dept_user_grid
    </sql>

    <select id="selectCustDeptUserGridList" parameterType="CustDeptUserGrid" resultMap="CustDeptUserGridResult">
        <include refid="selectCustDeptUserGridVo"/>
        <where>
            <if test="deptId != null ">and dept_id in
                (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId},ancestors))</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="gridId != null ">and grid_id = #{gridId}</if>
            <if test="gridName != null  and gridName != ''">and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="custId != null  and custId != ''">and cust_id = #{custId}</if>
            <if test="custIdc != null  and custIdc != ''">and cust_idc = #{custIdc}</if>
            <if test="custName != null  and custName != ''">and cust_name like concat('%', #{custName}, '%')</if>
            <if test="custPhone != null  and custPhone != ''">and cust_phone = #{custPhone}</if>
            <if test="deptName != null  and deptName != ''">and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="gridName2 != null  and gridName2 != ''">and grid_name2 like concat('%', #{gridName2}, '%')</if>
            <if test="outlets != null  and outlets != ''">and outlets like concat('%', #{outlets}, '%')</if>
            <if test="tellerId != null  and tellerId != ''">and teller_id = #{tellerId}</if>
            <if test="opsDept != null  and opsDept != ''">and ops_dept = #{opsDept}</if>
        </where>
    </select>

    <select id="selectCustDeptUserGridById" parameterType="Long" resultMap="CustDeptUserGridResult">
        <include refid="selectCustDeptUserGridVo"/>
        where id = #{id}
    </select>

    <insert id="insertCustDeptUserGrid" parameterType="CustDeptUserGrid" useGeneratedKeys="true" keyProperty="id">
        insert into cust_dept_user_grid
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="gridId != null">grid_id,</if>
            <if test="gridName != null">grid_name,</if>
            <if test="custId != null">cust_id,</if>
            <if test="custIdc != null">cust_idc,</if>
            <if test="custName != null">cust_name,</if>
            <if test="custPhone != null">cust_phone,</if>
            <if test="deptName != null">dept_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="gridId != null">#{gridId},</if>
            <if test="gridName != null">#{gridName},</if>
            <if test="custId != null">#{custId},</if>
            <if test="custIdc != null">#{custIdc},</if>
            <if test="custName != null">#{custName},</if>
            <if test="custPhone != null">#{custPhone},</if>
            <if test="deptName != null">#{deptName},</if>
        </trim>
    </insert>

    <update id="updateCustDeptUserGrid" parameterType="CustDeptUserGrid">
        update cust_dept_user_grid
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custIdc != null">cust_idc = #{custIdc},</if>
            <if test="custName != null">cust_name = #{custName},</if>
            <if test="custPhone != null">cust_phone = #{custPhone},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustDeptUserGridById" parameterType="Long">
        delete
        from cust_dept_user_grid
        where id = #{id}
    </delete>

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


    <select id="selectCustDeptUserGridListByCustIds" parameterType="String" resultMap="CustDeptUserGridResult">
        <include refid="selectCustDeptUserGridVo"/>
         where cust_id in
            <foreach item="id" collection="custIds" open="(" separator="," close=")">
                #{id}
            </foreach>

    </select>
</mapper>