<?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.GridVirtualGridMapper">
    
    <resultMap type="GridVirtualGrid" id="GridVirtualGridResult">
        <result property="gridId"    column="grid_id"    />
        <result property="gridName"    column="grid_name"    />
        <result property="gridType"    column="grid_type"    />
        <result property="hasRetail"    column="has_retail"    />
        <result property="hasBusiness"    column="has_business"    />
        <result property="hasRetailBusiness"    column="has_retail_business"    />
        <result property="gridAssignType"    column="grid_assign_type"    />
        <result property="gridDutyType"    column="grid_duty_type"    />
        <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="VirtualGridSelectInfo" id="VirtualGridSelectInfoResult">
        <result property="gridId"    column="grid_id"    />
        <result property="virtualGridName"    column="grid_name"    />
        <result property="gridType"    column="grid_type"    />
        <result property="gridDutyType"    column="grid_duty_type"    />
        <result property="createRole"    column="dept_type"    />
        <result property="custNum"    column="cust_num"    />
        <result property="posNum"    column="pos_num"    />
        <result property="busiNum"    column="busi_num"    />
    </resultMap>

    <sql id="selectGridVirtualGridVo">
        select grid_id, grid_name, grid_type, has_retail, has_business, has_retail_business, grid_assign_type, grid_duty_type, dept_id, create_by, create_time, update_by, update_time, delete_flag from grid_virtual_grid
    </sql>

    <select id="selectGridVirtualGridList" parameterType="GridVirtualGrid" resultMap="GridVirtualGridResult">
        <include refid="selectGridVirtualGridVo"/>
        <where>  
            <if test="gridName != null  and gridName != ''"> and grid_name like concat('%', #{gridName}, '%')</if>
            <if test="gridType != null  and gridType != ''"> and grid_type = #{gridType}</if>
            <if test="hasRetail != null  and hasRetail != ''"> and has_retail = #{hasRetail}</if>
            <if test="hasBusiness != null  and hasBusiness != ''"> and has_business = #{hasBusiness}</if>
            <if test="hasRetailBusiness != null  and hasRetailBusiness != ''"> and has_retail_business = #{hasRetailBusiness}</if>
            <if test="gridAssignType != null  and gridAssignType != ''"> and grid_assign_type = #{gridAssignType}</if>
            <if test="gridDutyType != null  and gridDutyType != ''"> and grid_duty_type = #{gridDutyType}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deleteFlag != null  and deleteFlag != ''"> and delete_flag = #{deleteFlag}</if>
        </where>
    </select>
    
    <select id="selectGridVirtualGridByGridId" parameterType="Long" resultMap="GridVirtualGridResult">
        <include refid="selectGridVirtualGridVo"/>
        where grid_id = #{gridId}
    </select>

    <insert id="insertGridVirtualGrid" parameterType="GridVirtualGrid" useGeneratedKeys="true" keyProperty="gridId">
        insert into grid_virtual_grid
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gridName != null">grid_name,</if>
            <if test="gridType != null">grid_type,</if>
            <if test="hasRetail != null">has_retail,</if>
            <if test="hasBusiness != null">has_business,</if>
            <if test="hasRetailBusiness != null">has_retail_business,</if>
            <if test="gridAssignType != null">grid_assign_type,</if>
            <if test="gridDutyType != null">grid_duty_type,</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="deleteFlag != null and deleteFlag != ''">delete_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="gridName != null">#{gridName},</if>
            <if test="gridType != null">#{gridType},</if>
            <if test="hasRetail != null">#{hasRetail},</if>
            <if test="hasBusiness != null">#{hasBusiness},</if>
            <if test="hasRetailBusiness != null">#{hasRetailBusiness},</if>
            <if test="gridAssignType != null">#{gridAssignType},</if>
            <if test="gridDutyType != null">#{gridDutyType},</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="deleteFlag != null and deleteFlag != ''">#{deleteFlag},</if>
         </trim>
    </insert>

    <update id="updateGridVirtualGrid" parameterType="GridVirtualGrid">
        update grid_virtual_grid
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridName != null">grid_name = #{gridName},</if>
            <if test="gridType != null">grid_type = #{gridType},</if>
            <if test="hasRetail != null">has_retail = #{hasRetail},</if>
            <if test="hasBusiness != null">has_business = #{hasBusiness},</if>
            <if test="hasRetailBusiness != null">has_retail_business = #{hasRetailBusiness},</if>
            <if test="gridAssignType != null">grid_assign_type = #{gridAssignType},</if>
            <if test="gridDutyType != null">grid_duty_type = #{gridDutyType},</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="deleteFlag != null and deleteFlag != ''">delete_flag = #{deleteFlag},</if>
        </trim>
        where grid_id = #{gridId}
    </update>

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

    <select id="selectVirtualGridList1" parameterType="GridVirtualGrid" resultMap="GridVirtualGridResult">
        select a.* from grid_virtual_grid a,sys_dept b,grid_virtual_user d,sys_user e
        <where>
            a.dept_id =b.dept_id and a.grid_id =d.grid_id and d.user_name = e.user_name
            <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="createRole != null  and createRole != ''">
                and case when #{createRole} = '0' then b.dept_type ='head' else b.dept_type ='branch' end
            </if>
            <if test="custType != null  and custType != ''">
                and case when #{custType} ='1' then a.has_retail = '1'
                when #{custType} ='2' then a.has_business = '1'
                when #{custType} ='3' then a.has_retail_business = '1' else 1 = 1 end
            </if>
            <if test="virtualGridName != null and virtualGridName != ''">
                and a.grid_name like concat('%', #{virtualGridName}, '%')
            </if>
            <if test="relationDept != null  and relationDept != ''">
                and d.relate_dept_name like concat('%', #{relationDept}, '%') and d.relate_dept_type = 'branch'
            </if>
            <if test="relationOutlet != null  and relationOutlet != ''">
                and d.relate_dept_name like concat('%', #{relationOutlet}, '%') and d.relate_dept_type = 'outlet'
            </if>
            <if test="custManager != null  and custManager != ''"> and e.nick_name like concat('%', #{custManager}, '%')</if>
            <if test="userDept != null and userDept != ''">
                and (cast(a.dept_id as char) like concat('%', #{userDept}, '%') or d.user_name = #{userDept})
            </if>
            and a.delete_flag = '0'
        </where>
        group by a.grid_id
    </select>

    <select id="selectVirtualGridList" parameterType="GridVirtualGrid" resultMap="VirtualGridSelectInfoResult">
        select distinct a.grid_id,a.grid_name,a.grid_type,a.grid_duty_type,a.dept_type,
        count(distinct f.cust_id) as cust_num,count(distinct g.cust_id) as pos_num,count(distinct h.cust_id) as busi_num
        from grid_virtual_grid a
        left join grid_virtual_user d on a.grid_id =d.grid_id
        left join (select distinct grid_id,cust_type,cust_id from grid_virtual_cust) f on a.grid_id = f.grid_id and f.cust_type ='0'
        left join (select distinct grid_id,cust_type,cust_id from grid_virtual_cust) g on a.grid_id = g.grid_id and g.cust_type ='1'
        left join (select distinct grid_id,cust_type,cust_id from grid_virtual_cust) h on a.grid_id = h.grid_id and h.cust_type ='2'
        <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="createRole != null  and createRole != ''">
                and case when #{createRole} = '0' then a.dept_type ='head' else a.dept_type ='branch' end
            </if>
            <if test="custType != null  and custType != ''">
                and case when #{custType} ='1' then a.has_retail = '1'
                when #{custType} ='2' then a.has_business = '1'
                when #{custType} ='3' then a.has_retail_business = '1' else 1 = 1 end
            </if>
            <if test="virtualGridName != null and virtualGridName != ''">
                and a.grid_name like concat('%', #{virtualGridName}, '%')
            </if>
            <if test="relationDept != null  and relationDept != ''">
                and d.relate_dept_name like concat('%', #{relationDept}, '%') and d.relate_dept_type = 'branch'
            </if>
            <if test="relationOutlet != null  and relationOutlet != ''">
                and d.relate_dept_name like concat('%', #{relationOutlet}, '%') and d.relate_dept_type = 'outlet'
            </if>
            <if test="custManager != null  and custManager != ''"> and d.nick_name like concat('%', #{custManager}, '%')</if>
            <if test="userDept != null and userDept != ''">
                and (d.relate_dept_id in (select dept_id from sys_dept where dept_id = #{userDept} or find_in_set(#{userDept},ancestors)) or d.user_name = #{userDept} or a.dept_id = #{userDept})
            </if>
            and a.delete_flag = '0'
        </where>
        group by a.grid_id,a.grid_name,a.grid_type,a.grid_duty_type,a.dept_type
    </select>

    <select id="selectVirtualNameByGridId" parameterType="Long" >
        select grid_name from grid_virtual_grid
        where grid_id = #{gridId}
    </select>

    <select id="selectVirtualGridDutyType" parameterType="Long" >
        select grid_duty_type from grid_virtual_grid
        where grid_id = #{gridId}
    </select>

    <select id="getGridCreateRole" parameterType="Long" >
        select b.dept_type from grid_virtual_grid a,sys_dept b
        where a.grid_id = #{gridId} and a.dept_id = b.dept_id
    </select>

    <select id="getVirtualGridNumByDeptId" parameterType="Long" resultType="java.lang.Long">
        select count(*)
        from (select grid_id
              from grid_virtual_grid
              where delete_flag = '0'
                and dept_id in
                    (select dept_id from sys_dept where dept_id = #{deptId} or find_in_set(#{deptId}, ancestors))
              union
              select grid_id
              from grid_virtual_cust_user
              where relate_dept_id in (select dept_id
                                       from sys_dept
                                       where dept_id = #{deptId} or find_in_set(#{deptId}, ancestors))) as total
    </select>

    <select id="getVirtualGridNumByUserId" parameterType="String" resultType="java.lang.Long">
        SELECT count(distinct a.grid_id)
        FROM grid_virtual_grid a
        WHERE a.delete_flag = '0'
          AND exists(SELECT 1
                     from grid_virtual_cust_user b
                     where b.grid_id = a.grid_id
                       and b.user_name = #{userName}
                       AND not exists(select 1
                                      from grid_virtual_cust_user_unbind c
                                      where c.grid_id = a.grid_id
                                        and c.cust_id = b.cust_id
                                        and c.user_name = #{userName}
                                        and c.delete_flag = '0'))
    </select>

    <select id="getVirtualType" resultType="java.lang.String">
        select grid_name from grid_virtual_grid where grid_id = #{gridId} and delete_flag = '0'
        and case when #{custType} = '1' then has_retail ='1'
            when #{custType} = '2' then has_retail_business ='1'
            when #{custType} = '3' then has_business ='1'
            else 1=1 end
    </select>


</mapper>