<?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.kiisoo.ic.customer.mapper.OpSellerCustomerRelationDOMapper">
    <select id="selectFriendListBySeller" resultType="com.kiisoo.ic.customer.bean.FriendDTO">
        SELECT
        DISTINCT
        t2.NAME AS customerName,
        t2.id AS customerId,
        IFNULL(t3.phone,'') AS mobile,
        t5.NAME AS inviteSellerName,
        t4.id AS inviteSellerId,
        t2.create_time AS addTime,
        t2.avatar_url as avatar,
        t3.level as vipFlag
        FROM
        op_seller_customer_relation t1,
        op_customer t2 left join op_vip t3 on t2.member_id = t3.id and t3.status = 1,
        poi_store_staff t4,
        privilage_user t5
        WHERE
        t1.customer_id = t2.id
        AND t1.user_id = t4.id
        AND t4.user_id = t5.id
        AND t1.store_id = t4.store_id
        <if test="type == 1">
            and t3.id is not null
        </if>
        <if test="type == 2">
            and t3.id is null
        </if>
        <if test="userId != null">
            and t5.id = #{userId}
        </if>
        <if test="search != null and search !=''">
            and
            (t2.name LIKE CONCAT('%',#{search},'%') OR t3.phone LIKE
            CONCAT('%',#{search},'%'))
        </if>
        <if test="startDate != null">
            and date(t1.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t1.create_time) &lt;= #{endDate}
        </if>
        order by t1.create_time desc
    </select>
    <select id="selectFriendListByShopId" resultType="com.kiisoo.ic.customer.bean.FriendDTO">

        SELECT
        t2. NAME AS customerName ,
        t2.id AS customerId ,
        IFNULL(t3.phone , '') AS mobile ,
        t1.user_id AS inviteSellerId ,
        t2.create_time AS addTime ,
        t2.avatar_url AS avatar ,
        t3. LEVEL AS vipFlag
        FROM
        op_seller_customer_relation t1
        join op_customer t2 on t1.customer_id = t2.id
        LEFT JOIN op_vip t3 ON t2.member_id = t3.id
        AND t3. STATUS = 1
        WHERE
         1 = 1
        <if test="type == 1">
            and t3.id is not null
        </if>
        <if test="type == 2">
            and t3.id is null
        </if>
        <if test="shopId != null">
            and t1.store_id = #{shopId}
        </if>
        <if test="sellerId != null and sellerId != 0">
            and t1.user_id = #{sellerId}
        </if>
        <if test="search != null and search !=''">
            and
            (t2.name LIKE CONCAT('%',#{search},'%') OR t3.phone LIKE
            CONCAT('%',#{search},'%'))
        </if>
        <if test="startDate != null">
            and date(t1.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t1.create_time) &lt;= #{endDate}
        </if>
        order by t1.create_time desc
    </select>
    <!--根据导购id查询导购名-->
    <select id="selectStaffName" resultType="java.lang.String">
        SELECT t1.NAME from privilage_user t1, poi_store_staff t2 where t1.id = t2.user_id AND t2.id = #{id} AND t2.type = 4
    </select>
    <select id="selectAllCustomer" resultType="java.util.Map">
        select user_id,count(0) as count from op_seller_customer_relation
        where 1 = 1
        and user_id in
        <foreach collection="ids" open="(" index="index" close=")" item="item" separator=",">
            #{item}
        </foreach>
        group by user_id
    </select>
    <select id="selectAllCustomerShop" resultType="java.util.Map">
        select store_id,count(0) as count from op_seller_customer_relation
        where 1 = 1
        and store_id in
        <foreach collection="ids" open="(" index="index" close=")" item="item" separator=",">
            #{item}
        </foreach>
        group by store_id
    </select>
    <select id="selectAllCustomerShop1" resultType="java.util.Map">
        select store_id,count(0) as count from op_seller_customer_relation
        where 1 = 1
        and type = 1
        and store_id in
        <foreach collection="ids" open="(" index="index" close=")" item="item" separator=",">
            #{item}
        </foreach>
        group by store_id
    </select>
    <select id="selectAllCustomer1" resultType="java.util.Map">
        select user_id,count(0) as count from op_seller_customer_relation
        where 1 = 1
        and type = 1
        and user_id in
        <foreach collection="ids" open="(" index="index" close=")" item="item" separator=",">
            #{item}
        </foreach>
        group by user_id
    </select>

    <select id="selectFriendListByShopIdAndDate" resultType="com.kiisoo.ic.customer.bean.FriendDTO">
        SELECT
        t2. NAME AS customerName ,
        t2.id AS customerId ,
        IFNULL(t3.phone , '') AS mobile ,
        t1.user_id AS inviteSellerId ,
        t2.create_time AS addTime ,
        t2.avatar_url AS avatar ,
        t3. LEVEL AS vipFlag
        FROM
        op_seller_customer_relation t1, op_customer t2
        LEFT JOIN op_vip t3 ON t2.member_id = t3.id
        AND t3. STATUS = 1
        WHERE
        t1.customer_id = t2.id
        <if test="type == 1">
            and t3.id is not null
        </if>
        <if test="type == 2">
            and t3.id is null
        </if>
        <if test="shopId != null">
            and t1.store_id = #{shopId}
        </if>
        <if test="sellerId != null and sellerId != 0">
            and t1.user_id = #{sellerId}
        </if>
        <if test="search != null and search !=''">
            and
            (t2.name LIKE CONCAT('%',#{search},'%') OR t3.phone LIKE
            CONCAT('%',#{search},'%'))
        </if>
        order by t1.create_time desc
    </select>

    <select id="selectFriendListBySellerAndDate" resultType="com.kiisoo.ic.customer.bean.FriendDTO">
        SELECT
        DISTINCT
        t2.NAME AS customerName,
        t2.id AS customerId,
        IFNULL(t3.phone,'') AS mobile,
        t5.NAME AS inviteSellerName,
        t4.id AS inviteSellerId,
        t2.create_time AS addTime,
        t2.avatar_url as avatar,
        t3.level as vipFlag
        FROM
        op_seller_customer_relation t1,
        op_customer t2 left join op_vip t3 on t2.member_id = t3.id and t3.status = 1,
        poi_store_staff t4,
        privilage_user t5
        WHERE
        t1.customer_id = t2.id
        AND t1.user_id = t4.id
        AND t4.user_id = t5.id
        AND t1.store_id = t4.store_id
        <if test="type == 1">
            and t3.id is not null
        </if>
        <if test="type == 2">
            and t3.id is null
        </if>
        <if test="userId != null">
            and t5.id = #{userId}
        </if>
        <if test="search != null and search !=''">
            and
            (t2.name LIKE CONCAT('%',#{search},'%') OR t3.phone LIKE
            CONCAT('%',#{search},'%'))
        </if>
        order by t1.create_time desc
    </select>


    <select id="selectSellerCustomerRelation" resultType="java.lang.Long">
        select t1.id from op_seller_customer_relation t1
        where t1.customer_id = #{customerId}
        <choose>
            <when test="storeId != null">
                and t1.store_id = #{storeId}
            </when>
            <otherwise>
                and t1.user_id = #{userId} and t1.type = 3 and t1.store_id = 0
            </otherwise>
        </choose>
        limit 1
    </select>

    <select id="selectCustomersByCpUserId" resultType="java.util.Map">

        SELECT
            t.cp_user_id as cpUserId,
            count(1) as sumCustomer
        FROM
            op_seller_customer_relation t ,
            privilage_cp_user t3,
            privilage_cp_user_store t4
        WHERE
            t4.store_id = t.store_id
          and t3.id = t4.cp_user_id
        GROUP BY
            t.cp_user_id;
    </select>

    <select id="selectCustomersByCpUserIdByList" resultType="java.util.Map">
        SELECT
        t.cp_user_id as cpUserId,
        count(1) as sumCustomer
        FROM
        op_seller_customer_relation t ,
        privilage_cp_user t3,
        privilage_cp_user_store t4
        WHERE
        t4.store_id = t.store_id
        and t3.id = t4.cp_user_id
        and t3.cp_user_id in
        <foreach collection="cpUserIds" separator="," item="item" close=")" index="index" open="(">
            #{item}
        </foreach>
        GROUP BY
            t.cp_user_id

    </select>

    <select id="selectCustomerStoreRelation" resultType="com.kiisoo.ic.customer.entity.OpSellerCustomerRelation">
        select t1.id
        from op_seller_customer_relation t1,poi_store t2,op_customer t3
        where t1.store_id = t2.id
        and t1.customer_id = t3.id
        and t3.wechat_uni_id = #{unionId}
        and t2.code = #{storeCode}
        limit 1
    </select>
</mapper>
