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

    <select id="getCountByHgAuthArea" resultType="com.zcloud.primeport.persistence.dataobject.ClosedAreaDO">
        SELECT
            dic.dict_label hg_auth_area_name,
            dic.dict_value hg_auth_area,
            IFNULL(tmp.totalChildAreasCount,0) total_area_count,
            IFNULL(tmp.totalCorpCount,0) total_corp_count
        FROM
            `jjb-saas-config`.`conf_dict_tree` dic
                LEFT JOIN (
                SELECT
                    ca.hg_auth_area,
                    COUNT( ca.id ) totalChildAreasCount,
                    COUNT( DISTINCT ca.jurisdictional_corp_id ) totalCorpCount
                FROM
                    `closed_area` ca
                WHERE
                    ca.delete_enum = 'FALSE'
                GROUP BY
                    ca.hg_auth_area) tmp on CONVERT(tmp.hg_auth_area USING  utf8mb4) = CONVERT( dic.dict_value USING  utf8mb4)
        WHERE
            dic.parent_id = ( SELECT id FROM `jjb-saas-config`.`conf_dict_tree` dicp WHERE dicp.dict_value = 'HG_AUTH_AREA' AND dicp.delete_enum = 'FALSE' )
          AND dic.delete_enum = 'FALSE'
        ORDER BY dic.id
    </select>
    <select id="listPage" resultType="com.zcloud.primeport.persistence.dataobject.ClosedAreaDO">
        SELECT
            c.*,
            CASE
                WHEN COUNT( child.id ) > 0 THEN
                    1 ELSE 2
                END AS has_child
        FROM
            closed_area c
                LEFT JOIN closed_area child ON c.id = child.parent_id
                AND child.delete_enum = 'FALSE'
        WHERE
            c.delete_enum = 'FALSE'
        <if test="params.parentId != null">
            AND c.parent_id = #{params.parentId}
        </if>
        <if test="params.hgAuthArea != null and params.hgAuthArea != ''">
            AND C.hg_auth_area = #{params.hgAuthArea}
        </if>
        <if test="params.closedAreaName != null and params.closedAreaName != ''">
            AND C.closed_area_name LIKE CONCAT('%', #{params.closedAreaName}, '%')
        </if>
        GROUP BY
            c.id
        ORDER BY
            c.id
    </select>
</mapper>

