<?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.OpCustomerDOMapper">

    <select id="selectCustomerInfoByShopIds" resultType="com.kiisoo.ic.customer.bean.OpCustomerDTO">
        select
        t2.id,t2.`name`,
        t2.member_id as idCard,t0.name as shopName,t0.code as shopCode,
        t4.name as sellerName, t3.birthday, t2.phone,
        t3.name as vipName,t3.phone as vipPhone, t5.staff_code as sellerCode, t1.create_time as createTime
        from
        poi_store t0,
        op_seller_customer_relation t1 LEFT JOIN privilage_user t4 on t1.user_id = t4.id left join poi_store_staff t5 on t4.id = t5.user_id,
        op_customer t2 LEFT JOIN op_vip t3 on t2.member_id = t3.id
        where t0.id = t1.store_id and t1.customer_id = t2.id
        <choose>
            <when test="shopId != null and shopId.size > 0 ">
                and t1.store_id in
                <foreach collection="shopId" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and  1 = 0
            </otherwise>
        </choose>
        <if test="startDate != null">
            and t1.create_time >=  #{startDate}
        </if>
        <if test="endDate != null">
            and t1.create_time <![CDATA[ < ]]> #{endDate}
        </if>
        order by t2.id,t1.user_id asc
    </select>
    <select id="selectCustomerInfoBySellerId" resultType="com.kiisoo.ic.customer.bean.OpCustomerDTO">
        select
        t2.id,t2.`name`,t2.member_id as idCard,GROUP_CONCAT(DISTINCT t0.`name`) as shopName, GROUP_CONCAT(DISTINCT t4.`name`) as sellerName, t3.birthday, t2.phone
        ,t3.name as vipName,t3.phone as vipPhone
        from
        poi_store t0,
        op_seller_customer_relation t1 LEFT JOIN privilage_user t4 on t1.user_id = t4.id,
        op_customer t2 LEFT JOIN op_vip t3 on t2.member_id = t3.id
        where t0.id = t1.store_id and t1.customer_id = t2.id
        and t1.user_id = #{sellerId}
        <if test="startDate != null">
            and t1.create_time >=  #{startDate}
        </if>
        <if test="endDate != null">
            and t1.create_time <![CDATA[ < ]]> #{endDate}
        </if>
        GROUP BY t2.id
        order by t2.id asc
    </select>

    <!--客户list-->
    <select id="selectCustomerList" resultType="com.kiisoo.ic.customer.entity.OpCustomer">
        select t1.id as id, t1.`name` as name, t1.member_id as memberId, t1.phone as phone,
         t2.create_time as registerTime, t2.type as validType, t2.store_id as shopId
        from op_customer t1, op_seller_customer_relation t2
        where t1.id=t2.customer_id
        <if test="memberId != null">
            and t1.member_id = #{memberId}
        </if>
        <if test="shopIds != null and shopIds.size > 0">
            and t2.store_id in
            <foreach collection="shopIds" separator="," item="item" index="index" close=")" open="(">
                #{item}
            </foreach>
        </if>
        <if test="sellerId != null">
            and t2.user_id=#{sellerId}
        </if>
        <if test="startDate != null and startDate != ''">
            and t2.create_time <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t2.create_time <![CDATA[ <= ]]> #{endDate}
        </if>
    </select>

    <!--客户总数-->
    <select id="selectCustomerCount" resultType="Long">
        select count(t1.id)
        from op_customer t1, op_seller_customer_relation t2
        where t1.id=t2.customer_id
        <if test="shopIds != null and shopIds.size > 0">
            and t2.store_id in
            <foreach collection="shopIds" separator="," item="item" index="index" close=")" open="(">
                #{item}
            </foreach>
        </if>
        <if test="type != null">
            and t2.type = #{type}
        </if>
        <if test="startDate != null and startDate != ''">
            and t2.create_time <![CDATA[ >= ]]> #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and t2.create_time <![CDATA[ <= ]]> #{endDate}
        </if>
    </select>

    <!--店铺ids→客户组织-->
    <select id="selectCustomerOrgByShopIds" resultType="com.kiisoo.ic.customer.entity.ShopOrgInfoVO">
        select t1.id as customerId,t1.`name` as customerName, t1.parent_id as companyId, t3.entity_id as shopId, t4.`name` as shopName
        from privilage_organizational t1, privilage_organizational_domain t2, privilage_domain_entity t3, poi_store t4
        where t1.id = t2.org_id
        and t2.domain_id = t3.domain_id
        and t3.type = 3
        and t3.entity_id = t4.id
        and t3.entity_id in
        <foreach collection="shopIds" separator="," item="item" index="index" close=")" open="(">
            #{item}
        </foreach>
        <if test="level != null">
            and t1.`level`=#{level}
        </if>
    </select>

    <!--店铺ids→零售公司信息-->
    <select id="selectOrgInfoByShopIds" resultType="com.kiisoo.ic.customer.entity.CustomerViewZeroExtendVO">
        SELECT region.id as regionId,region.name as region,company.id as companyId,company.name as company,
        customer.id as customerId,customer.name as customer ,shop.id as shopId, shop.`code` as shopCode, shop.name as shopName
        FROM `privilage_organizational` region,`privilage_organizational` company,`privilage_organizational` customer,
        privilage_organizational_domain t4, privilage_domain_entity t5,poi_store shop
        where company.parent_id=region.id
        and customer.parent_id = company.id
        and region.`level`=1
        and company.`level`=2
        and customer.`level`=3
        and customer.id = t4.org_id
        and t4.domain_id=t5.domain_id
        and t5.type=3
        and t5.entity_id=shop.id
        <choose>
            <when test="shopIds != null and shopIds.size > 0 ">
                and shop.id in
                <foreach collection="shopIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and  1 = 0
            </otherwise>
        </choose>
        <if test="regionId != null and regionId != 0">
            and region.id = #{regionId}
        </if>
        <if test="companyId != null and companyId != 0">
            and company.id = #{companyId}
        </if>
        <if test="customerId != null and customerId != 0">
            and customer.id = #{customerId}
        </if>
    </select>
</mapper>
