<?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.yunzhi.marketing.xlk.mapper.ChannelCustomerMapper">

    <select id="getProfileList" resultType="com.yunzhi.marketing.xlk.entity.ChannelCustomer">
        SELECT
            t.*,
            s.building_name,
            m.nickname AS recommend_person_name,
            m.phone AS recommend_person_phone
        FROM
            xlk_channel_customer t
                LEFT JOIN ta_building s ON t.building_id = s.building_id
                INNER JOIN ta_person m ON t.recommend_person = m.person_id
        WHERE
            t.org_id = #{orgId}
        <if test="institutionIds != null and institutionIds.size > 0">
            AND t.institution_id in
            <foreach collection="institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
          <if test="buildingId != null and buildingId != ''">
              AND t.building_id = #{buildingId}
          </if>
          <if test="name != null and name != ''">
              AND t.`name` LIKE CONCAT( '%', #{name}, '%' )
          </if>
          <if test="phone != null and phone != ''">
            AND t.phone LIKE CONCAT( '%', #{phone}, '%' )
          </if>
          <if test="recommendPersonName != null and recommendPersonName != ''">
            AND m.nickname LIKE CONCAT( '%', #{recommendPersonName}, '%' )
          </if>
          <if test="recommendPhone != null and recommendPhone != ''">
            AND m.phone LIKE CONCAT( '%', #{recommendPhone}, '%' )
          </if>
          <if test="status != null and status != ''">
            AND t.`status` = #{status}
          </if>
          <if test="status == null or status == ''">
            AND t.`status` &gt; -1
          </if>
            <if test="type == 'customer'">
                AND t.channel_id is null
            </if>
            <if test="type == 'channel'">
                AND t.channel_id is not null
            </if>
            ORDER BY
                t.create_date DESC
    </select>
    <select id="getRanklist" resultType="com.yunzhi.marketing.po.CustomerRankListVO">
        SELECT
            COUNT(1) as number,
            t.recommend_person,
            p.`name`,
            p.avatarurl
        FROM
            xlk_channel_customer t
        INNER JOIN xlk_customer_visit v on t.customer_id = v.customer_id
        LEFT JOIN ta_person p on t.recommend_person = p.person_id
        WHERE t.channel_id  = #{params.channelId}
        <if test="params.startDate != null or params.endDate != null">
            AND t.create_date BETWEEN #{params.startDate} and #{params.endDate}
        </if>
        <if test="params.startDate == null or params.endDate == null">
            AND t.create_date BETWEEN DATE_SUB(now(),INTERVAL 7 DAY) and now()
        </if>
        GROUP BY t.recommend_person
        ORDER BY number desc
        LIMIT 3
    </select>

    <select id="getCustomersIRecommended" resultType="com.yunzhi.marketing.po.MineRecommendCustomerPO">
        SELECT
            a.*,
            b.avatarurl ,
            ifnull(c.`status`, 1) as customer_status,
            if(c.customer_id is null, "report", "customer") as type,
            d.expiration_date
        FROM
            xlk_channel_customer a
        LEFT JOIN ta_person b ON a.person_id = b.person_id
        left join ta_recommend_customer c on a.customer_id = c.customer_id
        LEFT JOIN ta_building d on a.building_id = d.building_id
        WHERE
        a.recommend_person = #{params.personId}
        and a.`status` != 3
        and a.org_id = #{params.orgId}
        <if test="params.status != null and params.status !=''">
            and c.status = #{params.status}
        </if>
        <if test="params.name != null and params.name !=''">
            and a.name like CONCAT('%',#{params.name}, '%')
        </if>
        <if test="params.startDate != null and params.startDate != null">
            AND a.create_date >= #{params.startDate}
        </if>
        <if test="params.endDate != null and params.endDate != null">
            AND (a.visit_date &lt;= #{params.endDate} or a.preparatory_date &lt;= #{params.endDate} or a.signed_date &lt;= #{params.endDate} or a.commission_date &lt;= #{params.endDate})
        </if>
        ORDER BY
        a.create_date desc
    </select>
    <select id="getNewCustomer" resultType="java.lang.Integer">
        SELECT
         COUNT(1)
        FROM
        xlk_channel_customer t
        WHERE t.channel_id  = #{params.channelId}
        and t.`status` = 1
        <if test="params.startDate != null or params.endDate != null">
            AND t.create_date BETWEEN #{params.startDate} and #{params.endDate}
        </if>
        <if test="params.startDate == null or params.endDate == null">
            AND t.create_date BETWEEN DATE_SUB(now(),INTERVAL 7 DAY) and now()
        </if>
    </select>
    <select id="getSuccessCustomer" resultType="java.lang.Integer">
        SELECT
        COUNT(1)
        FROM
        xlk_channel_customer t
        LEFT join ta_recommend_customer r on t.customer_id = r.customer_id
        WHERE t.channel_id  = #{params.channelId}
        and r.`status` in (4,5)
        <if test="params.startDate != null or params.endDate != null">
            AND t.create_date BETWEEN #{params.startDate} and #{params.endDate}
        </if>
        <if test="params.startDate == null or params.endDate == null">
            AND t.create_date BETWEEN DATE_SUB(now(),INTERVAL 7 DAY) and now()
        </if>
    </select>
    <select id="getNewCustomerDetail" resultType="com.yunzhi.marketing.xlk.vo.CustomerDetailVO">
        SELECT
            <if test="params.type == 'day'">
                date_format( t.create_date, '%Y-%m-%d' ) as coordinate,
            </if>
            <if test="params.type == 'month'">
                date_format( t.create_date, '%Y-%m' ) as coordinate,
            </if>
            COUNT(1) as customerNum
        FROM
        xlk_channel_customer t
        WHERE t.channel_id  = #{params.channelId}
        and t.`status` = 1
        <if test="params.startDate != null or params.endDate != null">
            AND t.create_date BETWEEN #{params.startDate} and #{params.endDate}
        </if>
        <if test="params.startDate == null or params.endDate == null">
            AND t.create_date BETWEEN DATE_SUB(now(),INTERVAL 7 DAY) and now()
        </if>
        <if test="params.type == 'day'">
            group by date_format( t.create_date, '%Y-%m-%d' )
        </if>
        <if test="params.type == 'month'">
            group by date_format( t.create_date, '%Y-%m' )
        </if>
    </select>
    <select id="getSuccessCustomerDetail" resultType="com.yunzhi.marketing.xlk.vo.CustomerDetailVO">
        SELECT
            <if test="params.type == 'day'">
                date_format( t.create_date, '%Y-%m-%d' ) as coordinate,
            </if>
            <if test="params.type == 'month'">
                date_format( t.create_date, '%Y-%m' ) as coordinate,
            </if>
            COUNT(1) as customerNum
        FROM
        xlk_channel_customer t
        LEFT join ta_recommend_customer r on t.customer_id = r.customer_id
        WHERE t.channel_id  = #{params.channelId}
        and r.`status` in (4,5)
        <if test="params.startDate != null or params.endDate != null">
            AND t.create_date BETWEEN #{params.startDate} and #{params.endDate}
        </if>
        <if test="params.startDate == null or params.endDate == null">
            AND t.create_date BETWEEN DATE_SUB(now(),INTERVAL 7 DAY) and now()
        </if>
        <if test="params.type == 'day'">
            group by date_format( t.create_date, '%Y-%m-%d' )
        </if>
        <if test="params.type == 'month'">
            group by date_format( t.create_date, '%Y-%m' )
        </if>
    </select>
    <select id="getMoreInfoById" resultType="com.yunzhi.marketing.xlk.entity.ChannelCustomer">
        SELECT
            t.*,
            s.channel_name
        FROM
            xlk_channel_customer t
                LEFT JOIN ta_channel s ON t.channel_id = s.channel_id
        WHERE
            t.channel_customer_id = #{id}
    </select>
</mapper>
