<?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.huiju.estateagents.mapper.TaPersonVisitRecordMapper">

    <sql id="columnSql">
        <trim suffixOverrides=",">
            t.record_id,
            t.person_id,
            c.person_type,
            t.visit_time,
            t.leave_time,
            t.visit_duration,
            t.event,
            t.data,
            t.activity,
            t.org_id,
            t.building_id,
            t.event_type,
            t.target_id,
            t.consultant_id,
            t.share_person_id,
        </trim>
    </sql>

    <select id="visitRecordByPersonId" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        select
            t.* ,
            b.parent_type_id from ta_person_visit_record t
        left join td_biz_event_type b on t.event_type = b.type_id
        where t.person_id = #{personId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND (b.parent_type_id = 'public' or t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
            )
        </if>
        <if test="buildingId != null and buildingId != ''">
            AND (b.parent_type_id = 'public' or t.building_id = #{buildingId} )
        </if>
        order by t.visit_time desc
    </select>

    <select id="selectAll" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        SELECT
            tpvr.record_id as recordId,
            tpvr.person_id as personId,
            tpvr.person_type as personType,
            tpvr.building_id as buildingId,
            tpvr.activity as activity,
            tpvr.event as event,
            tpvr.event_type as eventType,
            tpvr.visit_duration as visitDuration,
            (SELECT MIN(t.visit_time) FROM ta_person_visit_record t WHERE t.person_id = tpvr.person_id and t.event = tpvr.event and t.org_id = #{orgId} ) as visitTime,
            (SELECT MAX(t.leave_time) FROM ta_person_visit_record t WHERE t.person_id = tpvr.person_id and t.event = tpvr.event and t.org_id = #{orgId} ) as leaveTime,
            ifnull(tp.name, tp.nickname) as userName,
            tbe.event_name as eventName,
            COUNT(1) as accessCount
        FROM
            ta_person_visit_record tpvr
            LEFT JOIN ta_person tp ON tpvr.person_id = tp.person_id
            LEFT JOIN td_biz_event tbe on tpvr.event = tbe.event_code
            <trim prefix="where" prefixOverrides="and | or">
                tpvr.org_id = #{orgId}
                and tp.org_id = #{orgId}
                <if test="startDate != null and endDate != null">
                    and tpvr.visit_time between #{startDate} and #{endDate}
                </if>
                <if test="buildingId != null and buildingId != ''">
                    and tpvr.building_id = #{buildingId}
                </if>
                <if test="eventType != null and eventType != ''">
                    and tpvr.event_type = #{eventType}
                </if>
                <if test="event != null and event != ''">
                    and tpvr.event = #{event}
                </if>
                <if test="activity != null and activity != ''">
                    and tpvr.activity = #{activity}
                </if>
            </trim>

        GROUP BY tpvr.person_id, tpvr.event
        ORDER BY 	accessCount DESC,tpvr.visit_time DESC
    </select>
    <select id="getPersonVisitRecordList" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        SELECT
            t.*,
            b.building_name
        FROM
            ta_person_visit_record t
            LEFT JOIN ta_building b ON t.building_id = b.building_id
        WHERE
            t.person_id = #{personId}
        ORDER BY
            t.visit_time DESC
    </select>

    <select id="getDurationByPersonId" resultType="java.lang.Integer">
        select SUM(visit_duration) from ta_person_visit_record where person_id = #{personId}
    </select>

    <select id="getFirstVisitTimeByPersonId" resultType="java.time.LocalDateTime">
        select visit_time from ta_person_visit_record where person_id = #{personId} order by visit_time asc limit 1
    </select>
    <select id="getWxVisitRecordList" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        SELECT
            t.* ,
	        p.`name` as user_name,
	        p.`nickname` as nickname,
	        p.avatarurl,
	        d.name as activity_name,
	        d.create_date
        FROM
            ( SELECT * FROM ta_person_visit_record WHERE org_id = #{orgId} and event_type = #{eventType} AND consultant_id = #{userId} and person_id != #{personId} ORDER BY visit_time DESC LIMIT 999) t
            left JOIN ta_person p on t.person_id = p.person_id
            LEFT join ta_drainage d on t.target_id = d.drainage_id
        GROUP BY
            t.person_id
        ORDER BY
	        t.visit_time DESC
    </select>

    <select id="getWxActivityVisitRecordList" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        SELECT
            t.share_from_id, t.share_person as consultant_id, t.share_person_type, t.person_id, t.target_type, t.target_id, t.create_date as visit_time, t.org_id ,t.building_id, t.status, t.is_first_time,
	        p.`name` as user_name,
	        p.`nickname` as nickname,
	        p.avatarurl
        FROM
            ( SELECT * FROM ta_share_person_from WHERE org_id = #{orgId} and target_id = #{targetId} AND (share_person = #{userId} or share_person = #{personId}) and target_type = #{eventType} ORDER BY create_date DESC LIMIT 999) t
            left JOIN ta_person p on t.person_id = p.person_id
            where t.person_id != #{personId}
        GROUP BY
            t.person_id
        ORDER BY
	        t.create_date DESC
    </select>


    <select id="getWxVisitRecordActivityList" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
            SELECT
                t.*,
                d.NAME AS activity_name,
                d.create_date
            FROM
                ( SELECT * FROM ta_person_visit_record WHERE event_type = #{eventType} AND consultant_id = #{userId} AND person_id = #{personId} ORDER BY visit_time DESC ) t
                LEFT JOIN ta_drainage d ON t.target_id = d.drainage_id
            GROUP BY
                t.target_id
            ORDER BY
	            t.visit_time DESC
    </select>

    <select id="getDrainageVisitRecord" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        select * from (
            select
            t.share_person as share_person_id,
            t.person_id,
            c.person_type,
            t.create_date as visit_time,
            t.target_type as event_type,
            t.target_id as target_id,
            b.name as drainageName,
            d.building_name as buildingName,
            tn.news_name as newsName,
            tha.title as helpActivityName,
            tsa.activity_name as groupActivityName,
            tbd.title as activityName,
            b.drainage_id
            from ta_share_person_from t
            left join ta_drainage b on t.target_id = b.drainage_id
            left join ta_building d on t.target_id = d.building_id
            left join ta_news tn on t.target_id = tn.news_id
            left join ta_help_activity tha on t.target_id = tha.help_activity_id
            left join ta_share_activity tsa on t.target_id = tsa.group_activity_id
            left join ta_building_dynamic tbd on t.target_id = tbd.dynamic_id
            left join ta_person c on t.org_id = c.org_id and (t.share_person = c.person_id or t.share_person = c.user_id)
            where t.org_id = #{orgId}
            and t.target_type in ('h5_share','group_share','help_share','news_share','dynamic_share','building_share')
            <if test="eventType !=null and eventType != ''">
                and t.target_type = #{eventType}
            </if>
            <if test="activityName !=null and activityName != ''">
                and (
                (b.name like concat('%',#{activityName},'%') and t.target_type = 'h5_share')or (d.name like concat('%',#{activityName},'%') and t.target_type = 'building_share') or (tn.news_name like concat('%',#{activityName},'%') and t.target_type = 'news_share')
                or (tha.title like concat( '%', #{activityName}, '%' ) and t.target_type = 'help_share')  or (tsa.activity_name like concat('%',#{activityName},'%') and t.target_type = 'group_share') or (tbd.title like concat('%',#{activityName},'%') and t.target_type = 'dynamic_share')
                )
            </if>
            <if test="shareName !=null and shareName != ''">
                and c.nickname like concat('%',#{shareName},'%')
            </if>
            <if test="shareTel !=null and shareTel != ''">
                and c.phone = #{shareTel}
            </if>
            <if test="personType == 'Realty Consultant'">
                and c.person_type = 'Realty Consultant'
            </if>
            <if test="personType == 'customer'">
                and c.person_type != 'Realty Consultant'
            </if>
            <if test="buildingId !=null and buildingId != ''">
                and d.building_id = #{buildingId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND d.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            order by t.create_date desc
        ) t
        group by t.person_id, t.target_id,t.event_type, t.share_person_id
        order by t.visit_time desc
    </select>

    <select id="getConsultantShareInfoList" resultType="com.huiju.estateagents.entity.TaConsultantInfo">
        select * From (
            select * from (
                select
                t.be_share as target_id,
                a.url as activity_img,
                a.building_name as activity_name,
                a.building_name,
                a.address,
                a.price,
                a.building_type_id,
                t.tagert_type as eventType,
				t.create_date as visit_time
                from ta_share t
                left join (select b.url, a.building_name,a.address,a.price,a.building_id,a.building_type_id from ta_building a left join ta_building_img b on a.building_id = b.building_id where b.img_type = 'list') a on t.be_share = a.building_id
                where t.tagert_type = 'project'
                and t.person_id = #{personId} order by t.create_date desc limit 9999
            ) t
            group by t.target_id

        union all

        select * from (
        select
        t.be_share as target_id,
        b.list_img_url as activity_img,
        b.title as activity_name,
        '1' as price,
        '2' as building_name,
        '3' as address,
        1 as building_type_id,
        t.tagert_type as eventType,
		t.create_date as visit_time
        from ta_share t
        left join ta_building_dynamic b on t.be_share = b.dynamic_id
        where t.tagert_type = 'activity'
        and t.person_id = #{personId} order by t.create_date desc limit 9999
        ) t
        group by t.target_id

        union all

        select * from (
        select
        t.be_share as target_id,
        c.list_img as activity_img,
        c.title as activity_name,
        '1' as price,
        '2' as building_name,
        '3' as address,
        1 as building_type_id,
        t.tagert_type as eventType,
		t.create_date as visit_time
        from ta_share t
        left join ta_help_activity c on t.be_share = c.help_activity_id
        where t.tagert_type = 'help'
        and t.person_id = #{personId} order by t.create_date desc limit 9999
        ) t
        group by t.target_id

        union all

        select * from (
        select
        t.be_share as target_id,
        d.list_img as activity_img,
        d.activity_name as activity_name,
        '1' as price,
        '2' as building_name,
        '3' as address,
        1 as building_type_id,
        t.tagert_type as eventType,
		t.create_date as visit_time
        from ta_share t
        left join ta_share_activity d on t.be_share = d.group_activity_id
        where t.tagert_type = 'group'
        and t.person_id = #{personId} order by t.create_date desc limit 9999
        ) t
		group by t.target_id

        union all

        select * from (
        select
        t.be_share as target_id,
        e.news_img as activity_img,
        e.news_name as activity_name,
        '1' as price,
        '2' as building_name,
        '3' as address,
        1 as building_type_id,
        t.tagert_type as eventType,
		t.create_date as visit_time
        from ta_share t
        left join ta_news e on t.be_share = e.news_id
        where t.tagert_type = 'news'
        and t.person_id = #{personId} order by t.create_date desc limit 9999
        ) t
        group by t.target_id

        union all

        select * from (
            select
            t.be_share as target_id,
            f.share_img as activity_img,
            f.`name` as activity_name,
            '1' as price,
            '2' as building_name,
            '3' as address,
            1 as building_type_id,
			t.tagert_type as eventType,
			t.create_date as visit_time
            from ta_share t
            left join ta_drainage f on t.be_share = f.drainage_id
            where t.tagert_type = 'h5'
            and t.person_id = #{personId} order by t.create_date desc limit 9999
            ) t
            group by t.target_id
         union all

        select * from (
            select
            t.be_share as target_id,
            g.aerial_view_img as activity_img,
            g.sales_batch_name as activity_name,
            '1' as price,
            '2' as building_name,
            '3' as address,
            1 as building_type_id,
			left(t.tagert_type,5) as eventType,
			t.create_date as visit_time
            from ta_share t
            left join ta_sales_batch g on t.be_share = g.sales_batch_id
            where t.tagert_type like CONCAT('house' , '%')
            and t.person_id = #{personId} order by t.create_date desc limit 9999
            ) t
            group by t.target_id

			union all
				select * from (
					SELECT
						t.be_share AS target_id,
						g.list_img AS activity_img,
						g.live_activity_title AS activity_name,
						'1' AS price,
						'2' AS building_name,
						'3' AS address,
						1 AS building_type_id,
						LEFT ( t.tagert_type, 4 ) AS eventType,
						t.create_date AS visit_time
					FROM
						ta_share t
						LEFT JOIN ta_live_activity g ON t.be_share = g.live_activity_id
					WHERE
						t.tagert_type LIKE CONCAT( 'live', '%' )
							AND t.person_id = #{personId}
						ORDER BY
							t.create_date DESC
							LIMIT 9999
					) t
				group by t.target_id
        ) t
        order by t.visit_time desc
    </select>

    <select id="countShareNumByEventType" resultType="java.lang.Integer">
        select count(DISTINCT t.person_id)  from ta_share_person_from t
        left join ta_person a on t.person_id = a.person_id
        where
        (t.share_person = #{personId}
        <if test="userId != null and userId != ''">
            or t.share_person = #{userId}
        </if>
        )
        and t.org_id = #{orgId}
        and t.target_type = #{eventType}
        and t.target_id = #{targetId}
        and t.status = 1
        and t.person_id != #{personId}
        group by t.target_type
    </select>

    <select id="selectData" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
		select t.share_person_id, t.target_id,
		if(t.target_type = 'help' and t.event_type = 'activity', 'help', if(t.target_type = 'group' and t.event_type = 'activity' , 'group', t.event_type)) as target_type,
		a.nickname, a.avatarurl FROM
        ta_person_visit_record t
        LEFT JOIN ta_person a ON t.share_person_id = a.person_id
        LEFT JOIN ta_share_person_from b ON t.target_id = b.target_id  and t.event_type = b.target_type + '_share'
        WHERE
        (
            ( t.consultant_id IS NOT NULL AND t.consultant_id != '' )
            OR ( t.share_person_id IS NOT NULL AND t.share_person_id != '' )
        )
        AND t.person_id != t.share_person_id
        AND t.`event` = 'detail'
        AND ( t.share_person_id != b.share_person OR t.consultant_id != b.share_person )
        GROUP BY
        t.target_id, t.event_type;
    </select>

    <select id="selectTapersonFromShare" resultType="com.huiju.estateagents.entity.TaPersonVisitRecord">
        select t.share_person_id, if (t.consultant_id != '' and t.consultant_id is not null, 'Realty Consultant', 'customer') as person_type, t.person_id, b.target_type, t.target_id,t.org_id, t.building_id  From ta_person_visit_record t
        left join ta_person a on t.share_person_id = a.person_id
        LEFT JOIN ta_share_person_from b ON t.target_id = b.target_id  and t.event_type = b.target_type + '_share'
        where
        (
            ( t.consultant_id IS NOT NULL AND t.consultant_id != '' )
            OR ( t.share_person_id IS NOT NULL AND t.share_person_id != '' )
            )
            AND t.person_id != t.share_person_id
            AND t.`event` = 'detail'
            AND ( t.share_person_id != b.share_person OR t.consultant_id != b.share_person )
        GROUP BY
            t.target_id, t.event_type;
    </select>


</mapper>
