<?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.GridRegionGridMapper">
    
    <resultMap type="GridRegionGrid" id="GridRegionGridResult">
        <result property="gridId"    column="grid_id"    />
        <result property="gridName"    column="grid_name"    />
        <result property="gridLevel"    column="grid_level"    />
        <result property="gridType"    column="grid_type"    />
        <result property="gridDutyType"    column="grid_duty_type"    />
        <result property="parentGridId"    column="parent_grid_id"    />
        <result property="deptId"    column="dept_id"    />
        <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="GridSelectInfo" id="GridSelectInfoResult">
        <result property="topGridId"    column="parent_grid_id"    />
        <result property="gridId"    column="grid_id"    />
        <result property="secGridName"    column="grid_name"    />
        <result property="gridDutyType"    column="grid_duty_type"    />
        <result property="gridType"    column="grid_type"    />
        <result property="gridLevel"    column="grid_level"    />
        <result property="custNum"    column="cust_num"    />
        <result property="posNum"    column="pos_num"    />
        <result property="busiNum"    column="busi_num"    />
        <result property="secGridNum"    column="sec_grid_num"    />
        <result property="createDept" column="create_dept"/>
    </resultMap>

    <sql id="selectGridRegionGridVo">
        select grid_id, grid_name, grid_level, grid_type, grid_duty_type, parent_grid_id, dept_id, create_by, create_time, update_by, update_time, delete_flag from grid_region_grid
    </sql>

    <select id="selectGridRegionGridList" parameterType="GridRegionGrid" resultMap="GridRegionGridResult">
        <include refid="selectGridRegionGridVo"/>
        <where>
            delete_flag = 0
            <if test="gridName != null  and gridName != ''"> and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="gridLevel != null "> and grid_level = #{gridLevel}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and grid_duty_type = #{gridDutyType}</if>
            <if test="parentGridId != null "> and parent_grid_id = #{parentGridId}</if>
            <if test="deptId != null  and deptId != ''"> and dept_id = #{deptId}</if>
        </where>
    </select>
    
    <select id="selectGridRegionGridByGridId" parameterType="Long" resultMap="GridRegionGridResult">
        <include refid="selectGridRegionGridVo"/>
        where delete_flag = 0 and  grid_id = #{gridId}
    </select>

    <insert id="insertGridRegionGrid" parameterType="GridRegionGrid" useGeneratedKeys="true" keyProperty="gridId">
        insert into grid_region_grid
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gridName != null">grid_name,</if>
            <if test="gridLevel != null">grid_level,</if>
            <if test="gridDutyType != null">grid_duty_type,</if>
            <if test="parentGridId != null">parent_grid_id,</if>
            <if test="deptId != null">dept_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="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="gridName != null">#{gridName},</if>
            <if test="gridLevel != null">#{gridLevel},</if>
            <if test="gridDutyType != null">#{gridDutyType},</if>
            <if test="parentGridId != null">#{parentGridId},</if>
            <if test="deptId != null">#{deptId},</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="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateGridRegionGrid" parameterType="GridRegionGrid">
        update grid_region_grid
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="gridLevel != null">grid_level = #{gridLevel},</if>
            <if test="gridDutyType != null">grid_duty_type = #{gridDutyType},</if>
            <if test="parentGridId != null">parent_grid_id = #{parentGridId},</if>
            <if test="deptId != null">dept_id = #{deptId},</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="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where grid_id = #{gridId}
    </update>

    <select id="selectTopGrid" parameterType="gridSelectInfo" resultMap="GridSelectInfoResult">
        select distinct a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level, d.dept_name as create_dept
        from grid_region_grid a
        left join sys_dept d on a.dept_id = d.dept_id
        inner join (select distinct grid_id ,relate_dept_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})) b on a.grid_id = b.grid_id
        <where>
            <if test="gridType != null  and gridType != ''"> and a.grid_type = #{gridType}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and a.grid_duty_type = #{gridDutyType}</if>
            <if test="belongDept != null  and belongDept != ''"> and b.relate_dept_name like concat('%',#{belongDept}, '%')  </if>
            <if test="topGridName != null  and topGridName != ''"> and a.grid_name like concat('%',#{topGridName}, '%')</if>
            and a.grid_level = '1' and a.delete_flag = '0'
        </where>
    </select>

    <select id="selectSecGrid" parameterType="gridSelectInfo" resultMap="GridSelectInfoResult">
        select distinct a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level,a.parent_grid_id, d.dept_name as
        create_dept
        from grid_region_grid a
        left join sys_dept d on a.dept_id = d.dept_id
        inner join (select distinct grid_id,grid_name,parent_grid_id from grid_region_grid) c on a.parent_grid_id = c.grid_id
        inner join (select distinct grid_id ,relate_dept_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})) b on a.grid_id = b.grid_id
        <where>
            <if test="gridType != null  and gridType != ''"> and a.grid_type = #{gridType}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and a.grid_duty_type = #{gridDutyType}</if>
            <if test="belongDept != null  and belongDept != ''"> and b.relate_dept_name like concat('%',#{belongDept}, '%')  </if>
            <if test="relationOutletList != null  and relationOutletList != ''"> and b.relate_dept_name like concat('%',#{relationOutletList}, '%')  </if>
            <if test="relationManagerList != null  and relationManagerList != ''"> and b.nick_name like concat('%',#{relationManagerList}, '%')  </if>
            <if test="secGridName != null  and secGridName != ''"> and a.grid_name like concat('%',#{secGridName}, '%')</if>
            <if test="topGridName != null  and topGridName != ''"> and c.grid_name like concat('%',#{topGridName}, '%')</if>
            and a.grid_level = '2' and a.delete_flag = '0'
        </where>
    </select>

    <select id="selectTopGridList" parameterType="gridSelectInfo" resultMap="GridSelectInfoResult">
        select distinct a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level,
        sum(e.cust_num) as cust_num,sum(f.pos_num) as pos_num,sum(g.busi_num) as busi_num,b.sec_grid_num
        from grid_region_grid a
        inner join (select count(distinct grid_id) as sec_grid_num,any_value(relate_dept_name) as relate_dept_name,parent_grid_id 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}) group by parent_grid_id) b on a.grid_id = b.parent_grid_id
        inner JOIN (SELECT distinct code, grid_id FROM grid_region_admin_division_relate) h ON a.grid_id = h.grid_id
        LEFT JOIN (SELECT region_code, count(distinct cust_id) AS cust_num FROM cust_info_retail GROUP BY region_code) e ON h.code = e.region_code
        LEFT JOIN (SELECT region_code, count(distinct cust_id) AS pos_num FROM cust_info_merchant GROUP BY region_code) f ON h.code = f.region_code
        LEFT JOIN (SELECT region_code, count(distinct cust_id) AS busi_num FROM cust_info_business GROUP BY region_code) g ON h.code = g.region_code
        <where>
            <if test="gridType != null  and gridType != ''"> and a.grid_type = #{gridType}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and a.grid_duty_type = #{gridDutyType}</if>
            <if test="belongDept != null  and belongDept != ''"> and b.relate_dept_name like concat('%',#{belongDept}, '%')  </if>
            <if test="topGridName != null  and topGridName != ''"> and a.grid_name like concat('%',#{topGridName}, '%')</if>
            and a.grid_level = '1' and a.delete_flag = '0'
        </where>
        group by a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level
    </select>

    <select id="selectSecGridList" parameterType="gridSelectInfo" resultMap="GridSelectInfoResult">
        select distinct a.parent_grid_id,a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level,
        sum(e.cust_num) as cust_num,sum(f.pos_num) as pos_num,sum(g.busi_num) as busi_num
        from grid_region_grid a
        inner join (select distinct grid_id,relate_dept_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})) b on a.grid_id = b.grid_id
        inner join (select distinct grid_id,grid_name,parent_grid_id from grid_region_grid) d on a.parent_grid_id = d.grid_id
        inner join (select code,grid_id from grid_region_admin_division_relate) h on a.grid_id = h.grid_id
        left join (select region_code,count(distinct cust_id) as cust_num from cust_info_retail group by region_code) e on h.code = e.region_code
        left join (select region_code,count(distinct cust_id) as pos_num from cust_info_merchant group by region_code) f on h.code = f.region_code
        left join (select region_code,count(distinct cust_id) as busi_num from cust_info_business group by region_code) g on h.code = g.region_code
        <where>
            <if test="gridType != null  and gridType != ''"> and a.grid_type = #{gridType}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and a.grid_duty_type = #{gridDutyType}</if>
            <if test="belongDept != null  and belongDept != ''"> and b.relate_dept_name like concat('%',#{belongDept}, '%')</if>
            <if test="topGridName != null  and topGridName != ''"> and d.grid_name like concat('%',#{topGridName}, '%')</if>
            <if test="secGridName != null  and secGridName != ''"> and a.grid_name like concat('%',#{secGridName}, '%')</if>
            and a.grid_level = '2' and a.delete_flag = '0'
        </where>
        group by a.parent_grid_id,a.grid_id,a.grid_name,a.grid_duty_type,a.grid_type,a.grid_level
    </select>

    <select id="selectGridName" parameterType="Long" resultType="java.lang.String" >
        select grid_name from grid_region_grid
        where grid_id = #{gridId}
    </select>

    <select id="getTopGridNumByDeptId" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.grid_id) from grid_region_grid a,grid_region_user_relate b
        where b.relate_dept_id in
            (select dept_id from sys_dept where dept_id = #{headDeptId} or find_in_set(#{headDeptId},ancestors))
          and a.grid_level = '1'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
        and (a.grid_id = b.grid_id or a.grid_id =b.parent_grid_id );
    </select>

    <select id="getSecGridNumByDeptId" parameterType="Long" resultType="java.lang.Long">
        select count(distinct a.grid_id) from grid_region_grid a,grid_region_user_relate b
        where b.relate_dept_id in
              (select dept_id from sys_dept where dept_id = #{headDeptId} or find_in_set(#{headDeptId},ancestors))
          and a.grid_level = '2'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
        and a.grid_id = b.grid_id ;
    </select>

    <select id="getTopGridNumByUserId" parameterType="String" resultType="java.lang.Long">
        select count(distinct a.grid_id) from grid_region_grid a,grid_region_user_relate b
        where b.user_name = #{UserId}
          and a.grid_level = '1'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
          and (a.grid_id = b.grid_id or a.grid_id =b.parent_grid_id );
    </select>

    <select id="getSecGridNumByUserId" parameterType="String" resultType="java.lang.Long">
        select count(distinct a.grid_id) from grid_region_grid a,grid_region_user_relate b
        where b.user_name = #{UserId}
          and a.grid_level = '2'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
          and a.grid_id = b.grid_id ;
    </select>

    <select id="getParentGridName" parameterType="Long" resultType="java.lang.String" >
        select a.grid_name from grid_region_grid a , grid_region_grid b
        where a.grid_id  = b.parent_grid_id and b.grid_id = #{gridId}
    </select>

    <select id="getSecGridName" parameterType="Long"  resultType="java.lang.String">
        select group_concat(grid_name) from grid_region_grid
        where parent_grid_id = #{gridId}
          and delete_flag = '0'
          and dept_id in
              (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept}, ancestors))
    </select>

    <select id="getParentGridDutyType" parameterType="Long" resultType="java.lang.String" >
        select a.grid_duty_type from grid_region_grid a , grid_region_grid b
        where a.grid_id  = b.parent_grid_id and b.grid_id = #{gridId}
    </select>
    <select id="selectGridDutyType" parameterType="Long" resultType="java.lang.String" >
        select grid_duty_type from grid_region_grid
        where grid_id = #{gridId}
    </select>

    <select id="getSecGridNameByRegionCode" parameterType="String" resultType="java.lang.String" >
        select distinct a.grid_name from grid_region_grid a,grid_region_admin_division_relate b
        where b.code = #{regionCode} and a.grid_level ='2'
          and a.grid_id = b.grid_id
          and a.delete_flag = '0'
          and b.delete_flag = '0'
    </select>

    <select id="getSecGridDutyTypeByRegionCode" parameterType="String" resultType="java.lang.String" >
        select distinct a.grid_duty_type from grid_region_grid a,grid_region_admin_division_relate b
        where b.code = #{regionCode} and a.grid_level ='2'
          and a.grid_id = b.grid_id
          and a.delete_flag = '0'
          and b.delete_flag = '0'
    </select>

    <select id="getDeptListByRegionCode" resultType="Long">
        select distinct a.relate_dept_id
        from grid_region_user_relate a
                 left join grid_region_admin_division_relate b on a.grid_id = b.grid_id
        where a.relate_type = '1'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
          and b.code = #{regionCode}
    </select>

    <select id="getUserListByRegionCode" resultType="String">
        select distinct a.nick_name
        from grid_region_user_relate a
                 left join grid_region_admin_division_relate b on a.grid_id = b.grid_id
        where a.relate_type = '1'
          and a.delete_flag = '0'
          and b.delete_flag = '0'
          and b.code = #{regionCode}
    </select>

</mapper>