<?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.MkmjGateMapper">

    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.MkmjGateDO">
        SELECT
            g.id,
            g.mkmj_id,
            g.passage_id,
            g.gate_number,
            g.gate_name,
            g.gate_type,
            g.gate_type_name,
            g.gate_status,
            g.gate_category,
            g.gate_category_name,
            g.gate_model,
            g.gate_position,
            g.longitude,
            g.latitude,
            g.remarks,
            g.create_name,
            g.create_time,
            m.mkmj_name AS mkmjName,
            COUNT(DISTINCT v.id) video_count,
            p.passage_name AS passageName
        FROM
            mkmj_gate g
        LEFT JOIN mkmj m ON g.mkmj_id = m.id AND m.delete_enum = 'false'
        LEFT JOIN mkmj_passage p ON g.passage_id = p.id AND p.delete_enum = 'false'
        LEFT JOIN video v ON m.id = v.foreign_id AND v.device_type = 2 AND v.delete_enum = 'false'

        WHERE
            g.delete_enum = 'false'
        <if test="params.mkmjId != null and params.mkmjId != ''">
            AND g.mkmj_id = #{params.mkmjId}
        </if>
        <if test="params.passageId != null and params.passageId != ''">
            AND g.passage_id = #{params.passageId}
        </if>
        <if test="params.gateName != null and params.gateName != ''">
            AND g.gate_name LIKE CONCAT('%', #{params.gateName}, '%')
        </if>
        <if test="params.gateType != null and params.gateType != ''">
            AND g.gate_type = #{params.gateType}
        </if>
        <if test="params.gateCategory != null and params.gateCategory != ''">
            AND g.gate_category = #{params.gateCategory}
        </if>

        <if test="params.gateStatus != null">
            AND g.gate_status = #{params.gateStatus}
        </if>
        ORDER BY g.id DESC
    </select>
</mapper>

