<?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.GridRegionUserRelateMapper">
    
    <resultMap type="GridRegionUserRelate" id="GridRegionUserRelateResult">
        <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>

    <resultMap type="CustManageInfo" id="CustManageInfoResult">
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="custNo"    column="cust_idc"    />
        <result property="belongUserId"    column="user_name"    />
        <result property="belongUserName"    column="nick_name"    />
        <result property="custPattern"    column="cust_type"    />
        <result property="regionCode"    column="region_code"    />
    </resultMap>

    <sql id="selectGridRegionUserRelateVo">
        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_region_user_relate
    </sql>

    <select id="selectGridRegionUserRelateList" parameterType="GridRegionUserRelate" resultMap="GridRegionUserRelateResult">
        <include refid="selectGridRegionUserRelateVo"/>
        <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="selectGridRegionUserRelateById" parameterType="Long" resultMap="GridRegionUserRelateResult">
        <include refid="selectGridRegionUserRelateVo"/>
        where id = #{id}
    </select>

    <insert id="insertGridRegionUserRelate" parameterType="GridRegionUserRelate" useGeneratedKeys="true" keyProperty="id">
        insert into grid_region_user_relate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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="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="updateGridRegionUserRelate" parameterType="GridRegionUserRelate">
        update grid_region_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="deleteGridRegionUserRelateById" parameterType="Long">
        delete from grid_region_user_relate where id = #{id}
    </delete>

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


    <select id="selectGridRegionUserByGridId" parameterType="Long" resultType="java.lang.String" >
        select b.nick_name from grid_region_user_relate a,sys_user b
        where a.grid_id = #{gridId}
          and a.delete_flag = '0'
          and (a.relate_dept_id in
               (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept}, ancestors)) or
               a.user_name = #{userDept})
          and a.user_name = b.user_name
    </select>

    <select id="selectGridRegionDeptByGridId" parameterType="Long" resultType="java.lang.String">
        select distinct relate_dept_name from grid_region_user_relate
        where grid_id = #{gridId}
          and relate_dept_type = 'branch'
          and delete_flag = '0'
    </select>

    <select id="selectGridRegionDeptTypeByGridId" parameterType="Long" resultType="java.lang.String">
        select distinct relate_dept_name from grid_region_user_relate
        where grid_id = #{gridId} and (relate_dept_id  in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name = #{userDept}) and relate_dept_type ='outlet'
    </select>

    <select id="selectHeadDeptExist" parameterType="Long"  resultType="java.lang.String">
        select distinct relate_dept_name from grid_region_user_relate
        where grid_id = #{gridId}
          and relate_dept_type = 'head'
          and delete_flag = '0'
    </select>

    <select id="selectGridRegionDeptByUserId" resultType="java.lang.String">
        select distinct relate_dept_name from grid_region_user_relate
        where user_name = #{userId} and (parent_grid_id = #{gridId} or grid_id = #{gridId})and relate_dept_type = #{deptType} limit 1
    </select>

    <select id="selectCustInfoByManage" parameterType="CustManageInfo"  resultMap="CustManageInfoResult">
        select distinct a.cust_id,a.cust_name,a.cust_idc,a.region_code,c.user_name,c.nick_name,'0' as cust_type from cust_info_retail a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate) b on b.code =a.region_code
        inner join (select distinct grid_id,relate_dept_id,user_name,nick_name from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name =#{userDept})) c
        on b.grid_id = c.grid_id
        where  c.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        <if test="custNo != null  and custNo != ''"> and a.cust_idc like concat('%', #{custNo}, '%') </if>
        <if test="distributeTag != null  and distributeTag != ''">
            and case when #{distributeTag} = '0' then exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            when #{distributeTag} = '1' then not exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            else 1=1 end
        </if>
        union all
        select distinct a.cust_id,a.cust_name,a.social_credit_code,a.region_code,c.user_name,c.nick_name,'1' as cust_type from cust_info_merchant a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate) b on b.code =a.region_code
        inner join (select distinct grid_id,relate_dept_id,user_name,nick_name from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name =#{userDept})) c
        on b.grid_id = c.grid_id
        where  c.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        <if test="custNo != null  and custNo != ''"> and a.social_credit_code like concat('%', #{custNo}, '%') </if>
        <if test="distributeTag != null  and distributeTag != ''">
            and case when #{distributeTag} = '0' then exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            when #{distributeTag} = '1' then not exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            else 1=1 end
        </if>
        union all
        select distinct a.cust_id,a.cust_name,a.social_credit_code,a.region_code,c.user_name,c.nick_name,'2' as cust_type from cust_info_business a
        inner join (select distinct code,grid_id from grid_region_admin_division_relate) b on b.code =a.region_code
        inner join (select distinct grid_id,relate_dept_id,user_name,nick_name from grid_region_user_relate where delete_flag ='0'
        and (relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or user_name =#{userDept})) c
        on b.grid_id = c.grid_id
        where  c.grid_id = #{gridId}
        <if test="custName != null  and custName != ''"> and a.cust_name like concat('%', #{custName}, '%') </if>
        <if test="custId != null  and custId != ''"> and a.cust_id like concat('%', #{custId}, '%') </if>
        <if test="custNo != null  and custNo != ''"> and a.social_credit_code like concat('%', #{custNo}, '%') </if>
        <if test="distributeTag != null  and distributeTag != ''">
            and case when #{distributeTag} = '0' then exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            when #{distributeTag} = '1' then not exists (select * from sys_grid_user_cust d where d.grid_id = #{gridId} and a.cust_id =d.cust_id and c.user_name = d.user_name)
            else 1=1 end
        </if>
    </select>

</mapper>