<?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.multictrl.modules.business.dao.MediaFileDao">

    <select id="pageList" resultType="com.multictrl.modules.business.entity.MediaFileEntity">
        select m.*
        from bus_media_file m
                 left join bus_flight_task t on m.task_id = t.task_id
            ${ew.customSqlSegment}
    </select>

    <select id="getAlbum" resultType="com.multictrl.modules.business.entity.MediaFileEntity">
        SELECT a.* FROM(
        SELECT DISTINCT
        ON (h.dock_name)
        h.dock_name,
        t.dock_sn,
        m.task_id,
        m.name,
        m.object_key,
        total_count AS media_num,h.create_date
        FROM bus_flight_task t
        LEFT JOIN bus_dock h ON t.dock_sn = h.dock_sn
        JOIN bus_media_file m ON t.task_id = m.task_id
        LEFT JOIN LATERAL (
        SELECT COUNT(1) AS total_count
        FROM bus_flight_task t2
        JOIN bus_media_file m2 ON t2.task_id = m2.task_id
        WHERE t2.dock_sn = h.dock_sn
        <if test="deptId != null">
            and t2.dept_id = #{deptId}
        </if>
        ) AS subquery ON true
        ORDER BY h.dock_name, t.dock_sn, m.created_time DESC
        )a
        <where>
            <if test="dockSn != null and dockSn != ''">
                a.dock_sn = #{dockSn}
            </if>
        </where>
        ORDER BY a.create_date
    </select>

    <select id="list" resultType="com.multictrl.modules.business.entity.MediaFileEntity">
        select m.*
        from bus_media_file m
                 left join bus_flight_task t on m.task_id = t.task_id
            ${ew.customSqlSegment}
    </select>
</mapper>