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

    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.MkmjDO">
        SELECT
            m.id,
            m.hg_auth_area,
            m.hg_auth_area_name,
            m.mkmj_name,
            m.mkmj_level,
            m.mkmj_type,
            m.mkmj_type_name,
            m.mkmj_status,
            m.in_direction_area,
            m.in_direction_area_name,
            m.out_direction_area,
            m.out_direction_area_name,
            m.longitude,
            m.latitude,
            m.closed_area_id,
            m.out_direction_type,
            ca.jurisdictional_corp_id,
            ca.jurisdictional_corp_name,
            COUNT(DISTINCT v.id) video_count,
            COUNT(DISTINCT p.id) passage_count
        FROM
            mkmj m
        LEFT JOIN video v ON m.id = v.foreign_id AND v.device_type = 1 AND v.delete_enum = 'false'
        LEFT JOIN mkmj_passage p ON m.id = p.mkmj_id AND p.delete_enum = 'false'
        LEFT JOIN closed_area ca ON m.closed_area_id = ca.id AND ca.delete_enum = 'false'
        WHERE
            m.delete_enum = 'false'
        <if test="params.hgAuthArea != null and params.hgAuthArea != ''">
            AND m.hg_auth_area = #{params.hgAuthArea}
        </if>
        <if test="params.likeMkmjName != null and params.likeMkmjName != ''">
            AND m.mkmj_name LIKE CONCAT('%', #{params.likeMkmjName}, '%')
        </if>
        <if test="params.mkmjLevel != null">
            AND m.mkmj_level = #{params.mkmjLevel}
        </if>
        <if test="params.mkmjType != null and params.mkmjType != ''">
            AND m.mkmj_type = #{params.mkmjType}
        </if>
        <if test="params.mkmjStatus != null">
            AND m.mkmj_status = #{params.mkmjStatus}
        </if>
        GROUP BY m.id
        ORDER BY m.mkmj_status asc,m.id DESC
    </select>
</mapper>

