<?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.zcloud.primeport.persistence.mapper.MkmjPassageMapper">

    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.MkmjPassageDO">
        SELECT
            p.id,
            p.passage_name,
            p.passage_type,
            p.passage_type_name,
            p.passage_status,
            p.mkmj_id,
            p.longitude,
            p.latitude,
            p.remarks,
            m.mkmj_name AS mkmjName,
            COUNT(DISTINCT v.id) video_count,
            COUNT(DISTINCT g.id) AS gate_count
        FROM
            mkmj_passage p
        LEFT JOIN mkmj m ON p.mkmj_id = m.id AND m.delete_enum = 'false'
        LEFT JOIN mkmj_gate g ON p.id = g.passage_id AND g.delete_enum = 'false'
        LEFT JOIN video v ON m.id = v.foreign_id AND v.device_type = 2 AND v.delete_enum = 'false'
        WHERE
            p.delete_enum = 'false'
        <if test="params.eqMkmjId != null">
            AND p.mkmj_id = #{params.eqMkmjId}
        </if>
        <if test="params.likePassageName != null and params.likePassageName != ''">
            AND p.passage_name LIKE CONCAT('%', #{params.likePassageName}, '%')
        </if>
        <if test="params.eqPassageType != null and params.eqPassageType != ''">
            AND p.passage_type = #{params.eqPassageType}
        </if>
        <if test="params.eqPassageStatus != null">
            AND p.passage_status = #{params.eqPassageStatus}
        </if>

        GROUP BY p.id
        ORDER BY p.create_time DESC
    </select>
</mapper>

