<?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="vip.xiaonuo.modular.ai.followEvents.mapper.FollowEventsMapper">

    <select id="page" resultType="vip.xiaonuo.modular.ai.followEvents.entity.FollowEventsVo">
        SELECT * FROM (
            SELECT p.id,p.cap_time,p.name,p.person_type AS TYPE,p.car_img_url AS 'imgUrl',p.dev_name AS devName,p.cap_result AS capResult,u.name AS 'notifierName',e.create_time, 1 AS eventType  FROM
             `follow_events` e
             LEFT JOIN `person_capture` p ON e.event_id = p.id
             LEFT JOIN `SYS_USER` u ON p.notifier_id = u.id
             WHERE e.event_type = 1
             AND e.user_id = #{id}
             AND e.delete_flag = 'NOT_DELETE'

             UNION ALL

             SELECT c.id,c.cap_time,c.car_num AS NAME,c.car_type AS TYPE,c.car_img_url AS 'imgUrl',c.dev_name AS devName,c.cap_result AS capResult,u.name AS notifierName,e.create_time, 2 AS eventType FROM
             `follow_events` e
             LEFT JOIN `car_capture` c ON e.event_id = c.id
             LEFT JOIN `SYS_USER` u ON c.notifier_id = u.id
             WHERE e.event_type = 2
             AND e.user_id = #{id}
             AND e.delete_flag = 'NOT_DELETE'

	     UNION ALL

             SELECT s.id,s.cap_time,NULL AS NAME,NULL AS TYPE,s.img_url AS 'imgUrl',s.dev_name AS devName,NULL AS capResult,u.name AS notifierName,e.create_time, 3 AS eventType FROM
             `follow_events` e
             LEFT JOIN `species_capture` s ON e.event_id = s.id
             LEFT JOIN `SYS_USER` u ON s.notifier_id = u.id
             WHERE e.event_type = 3
             AND e.user_id = #{id}
             AND e.delete_flag = 'NOT_DELETE'

             UNION ALL

             SELECT d.id,d.cap_time,NULL AS NAME,NULL AS TYPE,d.img_url AS 'imgUrl',d.dev_name AS devName,NULL AS capResult,u.name AS notifierName,e.create_time, 4 AS eventType FROM
             `follow_events` e
             LEFT JOIN `dirty_capture` d ON e.event_id = d.id
             LEFT JOIN `SYS_USER` u ON d.notifier_id = u.id
             WHERE e.event_type = 4
             AND e.user_id = #{id}
             AND e.delete_flag = 'NOT_DELETE'
             ) a
             <if test="eventType != null and eventType != ''">
                 WHERE a.eventType = #{eventType}
             </if>
             ORDER BY create_time DESC

    </select>
</mapper>