<?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.draw.mapper.DrawGridCustUserUnbindMapper">
    <resultMap id="DrawGridCustVO" type="DrawGridCustVO">
        <result property="gridId" column="grid_id"/>
        <result property="gridName" column="grid_name"/>
        <result property="gridDutyType" column="grid_duty_type"/>
        <result property="layerId" column="layer_id"/>
        <result property="shapeId" column="shape_id"/>
        <result property="custId" column="cust_id"/>
        <result property="custName" column="cust_name"/>
        <result property="custType" column="cust_type"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="branchId" column="branch_id"/>
        <result property="branchName" column="branch_name"/>
        <result property="outletId" column="outlet_id"/>
        <result property="outletName" column="outlet_id"/>
        <result property="unbindFlag" column="unbind_flag"/>
    </resultMap>

    <select id="getCustList" parameterType="DrawGridCustListDTO" resultMap="DrawGridCustVO">
        select e.grid_id, e.grid_name, e.grid_duty_type, b.layer_id , b.shape_id , b.cust_id , b.cust_name , b.cust_type
        , a.user_name, a.nick_name,
        e.dept_id as branch_id, e.dept_name as branch_name, sd.dept_id as outlet_id, sd.dept_name as outlet_name,
        (select count(0) from grid_draw_cust_user_unbind d where d.grid_id = #{gridId} and d.cust_id = b.cust_id and
        d.user_name = a.user_name and d.delete_flag = '0') as unbind_flag
        from (select * from grid_draw_user_relate where delete_flag = '0') a
        left join (select * from grid_draw_shape_relate where delete_flag = '0') c on c.grid_id = a.grid_id
        left join draw_shape_cust b on b.shape_id = c.shape_id
        left join (select * from grid_draw_grid where delete_flag = '0') e on a.grid_id = e.grid_id
        left join sys_dept sd on sd.dept_id = a.relate_dept_id and sd.dept_type = "outlet"
        where a.grid_id = #{gridId}
        <if test="custId != null  and custId != ''">AND b.cust_id like concat('%', #{custId}, '%')</if>
        <if test="custName != null  and custName != ''">AND b.cust_name like concat('%', #{custName}, '%')</if>
        <if test="custType != null  and custType != ''">AND b.cust_type = #{custType}</if>
    </select>

    <select id="getCustListByManager" parameterType="DrawGridCustListDTO" resultMap="DrawGridCustVO">
        select e.grid_id, e.grid_name, e.grid_duty_type, b.layer_id , b.shape_id , b.cust_id , b.cust_name , b.cust_type
        , a.user_name, a.nick_name
        from (select * from grid_draw_user_relate where delete_flag = '0') a
        left join (select * from grid_draw_shape_relate where delete_flag = '0') c on c.grid_id = a.grid_id
        left join draw_shape_cust b on b.shape_id = c.shape_id
        left join (select * from grid_draw_grid where delete_flag = '0') e on a.grid_id = e.grid_id
        left join sys_dept sd on sd.dept_id = a.relate_dept_id
        where a.grid_id = #{gridId} and a.user_name = #{userName}
        and not exists
        (select 1 from grid_draw_cust_user_unbind d where d.grid_id = #{gridId} and d.cust_id = b.cust_id and
        d.user_name =
        a.user_name and d.delete_flag = '0')
        <if test="custId != null  and custId != ''">AND b.cust_id like concat('%', #{custId}, '%')</if>
        <if test="custName != null  and custName != ''">AND b.cust_name like concat('%', #{custName}, '%')</if>
        <if test="custType != null  and custType != ''">AND b.cust_type = #{custType}</if>
    </select>

    <!-- 根据绘制网格ID查询所有客户（用于客群导入，直接拼接headId绕过拦截器） -->
    <select id="selectCustByDrawGridId" resultType="com.ruoyi.ibs.grid.domain.entity.RegionCustUser">
        SELECT DISTINCT
            sc.cust_id,
            sc.cust_name,
            sc.cust_type
        FROM grid_draw_shape_relate sr
        INNER JOIN draw_shape_cust_${headId} sc ON sc.shape_id = sr.shape_id
        WHERE sr.grid_id = #{gridId}
          AND sr.delete_flag = '0'
    </select>

</mapper>