<?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.GridAdminDivisionRelateMapper">
    
    <resultMap type="GridRegionAdminDivisionRelate" id="GridRegionAdminDivisionRelateResult">
        <result property="id"    column="id"    />
        <result property="gridId"    column="grid_id"    />
        <result property="parentGridId"    column="parent_grid_id"    />
        <result property="code"    column="code"    />
        <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="selectGridRegionAdminDivisionRelateVo">
        select id, grid_id, code, parent_grid_id, create_by, create_time, update_by, update_time, delete_flag from grid_region_admin_division_relate
    </sql>

    <select id="selectGridRegionAdminDivisionRelateList" parameterType="GridRegionAdminDivisionRelate" resultMap="GridRegionAdminDivisionRelateResult">
        <include refid="selectGridRegionAdminDivisionRelateVo"/>
        <where>  
            <if test="gridId != null "> and grid_id = #{gridId}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="parentGridId != null  and parentGridId != ''"> and parent_grid_id = #{parentGridId}</if>
            <if test="deleteFlag != null  and deleteFlag != ''"> and delete_flag = #{deleteFlag}</if>
        </where>
    </select>
    
    <select id="selectGridRegionAdminDivisionRelateById" parameterType="Long" resultMap="GridRegionAdminDivisionRelateResult">
        <include refid="selectGridRegionAdminDivisionRelateVo"/>
        where id = #{id}
    </select>

    <insert id="insertGridRegionAdminDivisionRelate" parameterType="GridRegionAdminDivisionRelate" useGeneratedKeys="true" keyProperty="id">
        insert into grid_region_admin_division_relate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gridId != null">grid_id,</if>
            <if test="code != null">code,</if>
            <if test="parentGridId != null">parent_grid_id,</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="code != null">#{code},</if>
            <if test="parentGridId != null">#{parentGridId},</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="updateGridRegionAdminDivisionRelate" parameterType="GridRegionAdminDivisionRelate">
        update grid_region_admin_division_relate
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="code != null">code = #{code},</if>
            <if test="parentGridId != null">parent_grid_id = #{parentGridId},</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="deleteGridRegionAdminDivisionRelateById" parameterType="Long">
        delete from grid_region_admin_division_relate where id = #{id}
    </delete>

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

    <select id="selectGridAdminByGridId" parameterType="Long">
        select code from grid_region_admin_division_relate
        where grid_id = #{gridId}
    </select>

    <select id="getGridIdByRetailCust">
        select c.* from grid_region_admin_division_relate a , cust_info_retail b ,grid_region_grid c
        where a.code like concat('',b.region_code , '%') and a.grid_id = c.grid_id and c.grid_level = '2' AND b.id = #{Id} AND c.parent_grid_id = #{GridId} order by a.grid_id limit 1
    </select>

    <select id="getGridIdByBusiCust">
        select c.* from grid_region_admin_division_relate a , cust_info_business b,grid_region_grid c
        where a.code like concat('',b.region_code , '%') and a.grid_id = c.grid_id and c.grid_level = '2' AND b.id = #{Id} AND c.parent_grid_id = #{GridId} order by a.grid_id limit 1
    </select>

    <select id="getGridNameByRetailCust">
        select c.grid_name from grid_region_admin_division_relate a , cust_info_retail b ,grid_region_grid c
        where a.code like concat('',b.region_code , '%') and a.grid_id = c.grid_id and c.grid_level = '2' AND b.cust_id = #{CustId} AND c.parent_grid_id = #{GridId} order by a.grid_id limit 1
    </select>

    <select id="getGridNameByBusiCust">
        select c.grid_name from grid_region_admin_division_relate a,cust_info_business b,grid_region_grid c
        where a.code like concat('',b.region_code , '%') and a.grid_id = c.grid_id and c.grid_level = '2' AND b.cust_id = #{CustId} AND c.parent_grid_id = #{GridId} order by a.grid_id limit 1
    </select>

    <select id="getGridUserName" parameterType="String" resultType="java.lang.String">
        select group_concat(c.nick_name) from grid_region_admin_division_relate a,grid_region_user_relate b,sys_user c
        where a.grid_id =b.grid_id and b.user_name =c.user_name
          and a.code = #{regionCode}
          and b.user_name not in (select user_name from sys_grid_user_cust where cust_id = #{custId})
    </select>

</mapper>