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

    <select id = "getStatisticDataByType" resultType="com.yunzhi.marketing.bo.request.ActivityStatisticResponseBO">
      select
        -- 活动分享人数
        (SELECT count(DISTINCT b.person_id) FROM ta_share_count b
        WHERE b.org_id = a.org_id
            <choose>
                <when test=" type == 'live'">
                    AND b.tagert_type like concat('%',a.target_type,'%')
                </when>
                <otherwise>
                    AND b.tagert_type = a.target_type
                </otherwise>
            </choose>
            AND b.be_share = a.target_id
        ) as share_persons,

        -- 活动分享次数
        (SELECT count(*) FROM ta_share_count c
        WHERE c.org_id = a.org_id
            <choose>
                <when test=" type == 'live'">
                    AND c.tagert_type like concat('%',a.target_type,'%')
                </when>
                <otherwise>
                    AND c.tagert_type = a.target_type
                </otherwise>
            </choose>
            AND c.be_share = a.target_id
        ) as share_num,

        -- 活动访问人数
        (select count( DISTINCT d.person_id ) from ta_person_visit_record d
        WHERE d.org_id = a.org_id
            AND if(d.target_type is null ,d.event_type = a.target_type, d.target_type = REPLACE(a.target_type, 'activity', 'dynamic'))
            AND d.target_id = a.target_id
            AND d.`event` = 'detail'
        ) as visit_persons,

        -- 活动访问次数
        (select count(*) from ta_person_visit_record e
        WHERE e.org_id = a.org_id
            AND if(e.target_type is null ,e.event_type = a.target_type, e.target_type = REPLACE(a.target_type, 'activity', 'dynamic'))
            AND e.target_id = a.target_id
            AND e.`event` = 'detail'
        ) as visit_num,

        -- 分享新增用户
        (select count(DISTINCT f.person_id) from ta_share_person_from f
        where	f.org_id = a.org_id
        <choose>
            <when test=" type == 'live'">
                AND f.target_type = concat('%',REPLACE(a.target_type, 'activity', 'dynamic'), '_share%')
            </when>
            <otherwise>
                AND f.target_type = concat(REPLACE(a.target_type, 'activity', 'dynamic'), '_share')
            </otherwise>
        </choose>
        AND f.target_id = a.target_id
        AND f.`status` > 0
        AND f.is_first_time = 1
        ) as new_persons,

        -- 分享新增客户
        (select count(DISTINCT g.person_id) from ta_share_person_from g
        inner join ta_person h on h.person_id = g.person_id
        where	g.org_id = a.org_id
        <choose>
            <when test=" type == 'live'">
                AND g.target_type = concat('%',REPLACE(a.target_type, 'activity', 'dynamic'), '_share%')
            </when>
            <otherwise>
                AND g.target_type = concat(REPLACE(a.target_type, 'activity', 'dynamic'), '_share')
            </otherwise>
        </choose>
        AND g.target_id = a.target_id
        AND g.`status` > 0
        AND g.is_first_time = 1
        AND h.phone is not null
        ) as new_customers,
        a.activityName

        from (
            <if test=" type == 'activity' ">
                SELECT
                    t.org_id,
                    t.building_id,
                    s.building_name,
                    t.dynamic_id AS target_id,
                    t.title AS target_name,
                    'activity' AS target_type,
                    t.title as activityName
                FROM
                    ta_building_dynamic t
                LEFT JOIN ta_building s ON s.building_id = t.building_id
                WHERE
                   t.dynamic_id = #{id}
                  AND t.org_id = #{orgId}
            </if>

            <if test=" type == 'h5' " >
                    SELECT
                    t.org_id,
                    t.building_id,
                    s.building_name,
                    t.drainage_id AS target_id,
                    t.`name` AS target_name,
                    'h5' AS target_type,
                    t.name as activityName
                FROM
                  ta_drainage t
                LEFT JOIN ta_building s ON s.building_id = t.building_id
                WHERE
                  t.drainage_id = #{id}
                  AND t.org_id = #{orgId}
            </if>

            <if test=" type == 'help' ">
                SELECT
                    t.org_id,
                    t.building_id,
                    s.building_name,
                    t.help_activity_id AS target_id,
                    t.title AS target_name,
                    'help' AS target_type,
                    t.title as activityName
                FROM
                    ta_help_activity t
                LEFT JOIN ta_building s ON s.building_id = t.building_id
                WHERE
                    t.help_activity_id = #{id}
                    AND t.org_id = #{orgId}
            </if>

            <if test=" type == 'group' ">
                SELECT
                    t.org_id,
                    t.building_id,
                    s.building_name,
                    t.group_activity_id AS target_id,
                    t.activity_name AS target_name,
                    'group' AS target_type,
                    t.activity_name as activityName
                FROM
                    ta_share_activity t
                LEFT JOIN ta_building s ON s.building_id = t.building_id
                WHERE
                    t.group_activity_id = #{id}
                    AND t.org_id = #{orgId}
            </if>

            <if test=" type == 'live' ">
                SELECT
                    t.org_id,
                    t.building_id,
                    s.building_name,
                    t.live_activity_id AS target_id,
                    t.live_activity_title AS target_name,
                    'live' AS target_type,
                    t.live_activity_title as activityName
                FROM
                    ta_live_activity t
                LEFT JOIN ta_building s ON s.building_id = t.building_id
                WHERE
                    t.live_activity_id = #{id}
                    AND t.org_id = #{orgId}
            </if>
        ) a
    </select>

</mapper>