<?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.grid.mapper.GridVirtualUserRelateMapper">
    
    <resultMap type="GridVirtualUserRelate" id="GridVirtualUserRelateResult">
        <result property="id"    column="id"    />
        <result property="gridId"    column="grid_id"    />
        <result property="relateType"    column="relate_type"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="deptType"    column="dept_type"    />
        <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="deleteFlag"    column="delete_flag"    />
    </resultMap>

    <sql id="selectGridVirtualUserRelateVo">
        select id, grid_id, relate_type, user_id, user_name, dept_id, dept_name, dept_type, create_by, create_time, update_by, update_time, delete_flag from grid_virtual_user_relate
    </sql>

    <select id="selectGridVirtualUserRelateList" parameterType="GridVirtualUserRelate" resultMap="GridVirtualUserRelateResult">
        <include refid="selectGridVirtualUserRelateVo"/>
        <where>  
            <if test="gridId != null "> and grid_id = #{gridId}</if>
            <if test="relateType != null  and relateType != ''"> and relate_type = #{relateType}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="deptType != null  and deptType != ''"> and dept_type = #{deptType}</if>
            <if test="deleteFlag != null  and deleteFlag != ''"> and delete_flag = #{deleteFlag}</if>
        </where>
    </select>
    
    <select id="selectGridVirtualUserRelateById" parameterType="Long" resultMap="GridVirtualUserRelateResult">
        <include refid="selectGridVirtualUserRelateVo"/>
        where id = #{id}
    </select>

    <insert id="insertGridVirtualUserRelate" parameterType="GridVirtualUserRelate">
        insert into grid_virtual_user_relate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="gridId != null">grid_id,</if>
            <if test="relateType != null">relate_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="deptType != null">dept_type,</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="deleteFlag != null and deleteFlag != ''">delete_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="gridId != null">#{gridId},</if>
            <if test="relateType != null">#{relateType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="deptType != null">#{deptType},</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="deleteFlag != null and deleteFlag != ''">#{deleteFlag},</if>
         </trim>
    </insert>

    <update id="updateGridVirtualUserRelate" parameterType="GridVirtualUserRelate">
        update grid_virtual_user_relate
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="relateType != null">relate_type = #{relateType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="deptType != null">dept_type = #{deptType},</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="deleteFlag != null and deleteFlag != ''">delete_flag = #{deleteFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGridVirtualUserRelateById" parameterType="Long">
        delete from grid_virtual_user_relate where id = #{id}
    </delete>

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

    <select id="selectDeptNameByGridId" parameterType="Long" >
        select distinct relate_dept_name, user_id
        from grid_virtual_user_relate
        where grid_id = #{gridId} and relate_dept_type ='branch' order by user_id
    </select>

    <select id="selectDeptTypeByGridId" parameterType="Long" >
        select distinct relate_dept_name, user_id
        from grid_virtual_user_relate
        where grid_id = #{gridId} and relate_dept_type ='outlet' order by user_id
    </select>

    <select id="selectCustManagerByGridId" parameterType="Long" >
        select distinct b.nick_name from grid_virtual_user_relate a ,sys_user b
        where a.user_name = b.user_name and grid_id = #{gridId}
    </select>


</mapper>