<?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.hotwj.platform.securityManagement.securityMeetingAttendee.mapper.HotSecurityMeetingAttendeeMapper">

    <select id="selectMyMeetingList"
            resultType="com.hotwj.platform.securityManagement.securityMeetingAttendee.domain.vo.HotSecurityMeetingMyVo">
        SELECT
        m.id,
        m.company_id,
        m.meeting_topic,
        m.meeting_category,
        m.meeting_type,
        m.start_time,
        m.host_name,
        m.location,
        m.recorder_id,
        m.recorder_name,
        m.file_attachments,
        m.video_attachments,
        m.online_meeting_url,
        m.meeting_content,
        m.meeting_status,
        m.participant_ids,
        a.id AS attendee_id,
        a.attendee_photo_oss_id,
        a.attendee_sign_oss_id,
        a.finish_time,
        mt.task_id AS taskId
        FROM hot_security_meeting_attendee a
        LEFT JOIN hot_security_meeting m ON a.meeting_id = m.id
        LEFT JOIN (
            SELECT business_id, company_id, approver_id, task_id
            FROM (
                SELECT
                    inst.business_id,
                    inst.company_id,
                    task.approver_id,
                    task.task_id,
                    ROW_NUMBER() OVER (
                        PARTITION BY inst.business_id, inst.company_id, task.approver_id
                        ORDER BY task.create_time DESC
                    ) AS rn
                FROM sys_flow_instance inst
                INNER JOIN sys_flow_task task ON task.instance_id = inst.instance_id
                WHERE inst.flow_code = 'MEETING_SIGN'
                  AND inst.status = 0
                  AND task.approver_id = #{personId}
                  AND task.company_id = inst.company_id
            ) t
            WHERE t.rn = 1
        ) mt ON mt.business_id = m.id
            AND mt.company_id = m.company_id
            AND mt.approver_id = #{personId}
        WHERE a.person_id = #{personId}
        AND a.is_deleted = 0
        AND m.is_deleted = 0
        AND (m.meeting_type IS NULL OR m.meeting_type != 'offline')
        <if test="status != null and status == 0">
            AND a.finish_time IS NULL
        </if>
        <if test="status != null and status == 1">
            AND a.finish_time IS NOT NULL
        </if>
        ORDER BY m.start_time DESC
    </select>

</mapper>
