<?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.GridVirtualCustomerRelateMapper">
    
    <resultMap type="GridVirtualCustomerRelate" id="GridVirtualCustomerRelateResult">
        <result property="id"    column="id"    />
        <result property="gridId"    column="grid_id"    />
        <result property="custType"    column="cust_type"    />
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <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>

    <sql id="selectGridVirtualCustomerRelateVo">
        select id, grid_id, cust_type, cust_id, cust_name, dept_id, create_by, create_time, update_by, update_time, delete_flag from grid_virtual_customer_relate
    </sql>

    <select id="selectGridVirtualCustomerRelateList" parameterType="GridVirtualCustomerRelate" resultMap="GridVirtualCustomerRelateResult">
        <include refid="selectGridVirtualCustomerRelateVo"/>
        <where>  
            <if test="gridId != null "> and grid_id = #{gridId}</if>
            <if test="custType != null  and custType != ''"> and cust_type = #{custType}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</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="selectGridVirtualCustomerRelateById" parameterType="Long" resultMap="GridVirtualCustomerRelateResult">
        <include refid="selectGridVirtualCustomerRelateVo"/>
        where id = #{id}
    </select>

    <insert id="insertGridVirtualCustomerRelate" parameterType="GridVirtualCustomerRelate" useGeneratedKeys="true" keyProperty="id">
        insert into grid_virtual_customer_relate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gridId != null">grid_id,</if>
            <if test="custType != null">cust_type,</if>
            <if test="custId != null">cust_id,</if>
            <if test="custName != null">cust_name,</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="gridId != null">#{gridId},</if>
            <if test="custType != null">#{custType},</if>
            <if test="custId != null">#{custId},</if>
            <if test="custName != null">#{custName},</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="updateGridVirtualCustomerRelate" parameterType="GridVirtualCustomerRelate">
        update grid_virtual_customer_relate
        <trim prefix="SET" suffixOverrides=",">
            <if test="gridId != null">grid_id = #{gridId},</if>
            <if test="custType != null">cust_type = #{custType},</if>
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custName != null">cust_name = #{custName},</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 id = #{id}
    </update>

    <delete id="deleteGridVirtualCustomerRelateById" parameterType="Long">
        delete from grid_virtual_customer_relate where id = #{id}
    </delete>

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


    <select id="getRetailNumByGridId" parameterType="Long">
        select count(*) from grid_virtual_customer_relate a,cust_info_retail b
        where a.grid_id = #{gridId} and a.cust_type = '个人' and a.cust_id = b.cust_id
    </select>

    <select id="getMerchantNumByGridId" parameterType="Long">
        select count(*) from grid_virtual_customer_relate a,cust_info_merchant b
        where a.grid_id = #{gridId} and a.cust_type = '商户' and a.cust_id = b.cust_id
    </select>

    <select id="getBusiNumByGridId" parameterType="Long">
        select count(*) from grid_virtual_customer_relate a,cust_info_business b
        where a.grid_id = #{gridId} and a.cust_type = '企业' and a.cust_id = b.cust_id
    </select>

    <select id="selectunDisCustByVirtualList"  parameterType="CustManageInfo">
        select b.* from cust_info_retail a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <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>
        group by a.cust_id
        union all
        select b.* from cust_info_merchant a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <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>
        group by a.cust_id
        union all
        select b.* from cust_info_business a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.grid_id = #{gridId} and a.belong_user_name = ''
        <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>
        group by a.cust_id
    </select>

    <select id="selectCustByVirtualList"  parameterType="CustManageInfo">
        select b.* from cust_info_retail a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.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>
        group by a.cust_id
        union all
        select b.* from cust_info_merchant a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.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>
        group by a.cust_id
        union all
        select b.* from cust_info_business a inner join grid_virtual_customer_relate b on b.cust_id =a.cust_id
        where b.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>
        group by a.cust_id
    </select>

</mapper>