<?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.mapper.TaShareMapper">

    <select id="selectHouseShareList" resultType="com.yunzhi.marketing.entity.TaShare">
      select a.avatarurl as photoOravatar, a.nickname as nameOrnick, a.name, a.phone, a.person_type, t.tagert_type, t.create_date From ta_share_count t
      left join ta_person a on t.person_id = a.person_id
      left join ta_sales_batch b on t.be_share = b.sales_batch_id
      where a.org_id = #{orgId}
      and t.tagert_type in ('housePost','houseApp')
      and b.sales_batch_id = #{saleBatchId}
      <if test="personType != null and personType != ''">
          and a.person_Type = #{personType}
      </if>
      <if test="shareType != null and shareType != ''">
          and t.tagert_type = #{shareType}
      </if>
      <if test="phone != null and phone != ''">
          and a.phone = #{phone}
      </if>
        order by t.create_date desc
    </select>

    <select id="listLiveShare" resultType="com.yunzhi.marketing.entity.TaShare">
        SELECT
            a.avatarurl AS photoOravatar,
            a.nickname AS nameOrnick,
            a.NAME,
            a.phone,
            a.person_type,
            t.tagert_type,
            t.create_date
        FROM
          ta_share_count t
          LEFT JOIN ta_person a ON t.person_id = a.person_id
          LEFT JOIN ta_live_activity b ON t.be_share = b.live_activity_id
        WHERE
          a.org_id = #{orgId}
        AND t.tagert_type IN ( 'livePost', 'liveApp' )
        AND b.live_activity_id = #{liveActivityId}
        <if test = "personType != null and personType != ''" >
            AND a.person_Type = #{personType}
        </if>
        <if test = "shareType != null and shareType != ''" >
            AND t.tagert_type = #{shareType}
        </if>
        <if test = "phone != null and phone != ''" >
            AND a.phone = #{phone}
        </if>
        ORDER BY
        t.create_date DESC
    </select>

    <select id="statTopShare" resultType="java.util.Map">
        SELECT
            a.top,
            s.person_id AS sharePerson,
            s.nickname,
            s.avatarurl,
            IFNULL( s.tel, s.phone ) AS phone
        FROM
        (
            SELECT
                t.share_person,
                COUNT( 1 ) AS top
            FROM
                ta_share_person_from t
                INNER JOIN ta_person s on t.person_id = s.person_id
            WHERE
                t.org_id = #{orgId}
                AND t.target_type = #{targetType}
                AND t.target_id = #{targetId}
                AND t.is_first_time = 1
                AND (IFNULL( s.tel, s.phone ) IS NOT NULL or s.remark like 'p-1%')
                <if test="beginDate != null and beginDate != ''">
                    and  date_format(t.create_date,'%Y-%m-%d %H:%i:%S') >= #{beginDate}
                </if>
                <if test="endDate != null and endDate != ''">
                    and  date_format(t.create_date,'%Y-%m-%d %H:%i:%S') &lt;= #{endDate}
                </if>
            GROUP BY
                t.share_person
            ORDER BY
                COUNT( 1 ) DESC
            LIMIT #{top}
        ) a
        LEFT JOIN ta_person s ON s.org_id = #{orgId}
            AND ( a.share_person = s.person_id OR a.share_person = s.user_id )
    </select>
</mapper>
