<?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="org.springblade.modules.bzdz.mapper.BzdzMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="bzdzResultMap" type="org.springblade.modules.bzdz.entity.BzdzEntity">
        <result column="id" property="id"/>
        <result column="dzbm" property="dzbm"/>
        <result column="dzmc" property="dzmc"/>
        <result column="dzmc_dt" property="dzmcDt"/>
        <result column="sssfdm" property="sssfdm"/>
        <result column="sssfmc" property="sssfmc"/>
        <result column="ssdsdm" property="ssdsdm"/>
        <result column="ssdsmc" property="ssdsmc"/>
        <result column="ssxqdm" property="ssxqdm"/>
        <result column="ssxqmc" property="ssxqmc"/>
        <result column="xzjddm" property="xzjddm"/>
        <result column="xzjdmc" property="xzjdmc"/>
        <result column="sqjcwhdm" property="sqjcwhdm"/>
        <result column="sqjcwhmc" property="sqjcwhmc"/>
        <result column="dzjd" property="dzjd"/>
        <result column="dzwd" property="dzwd"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="ld_type" property="ldType"/>
    </resultMap>


    <select id="selectBzdzPage" resultType="org.springblade.modules.bzdz.vo.BzdzVO">
        SELECT
            ld.id,
            ld.dzbm,
            ld.dzmc,
            ld.dzmc_dt,
            ld.xqmc || ld.ldph || ld.ldph_suffix AS ldMc,
            ld.dzjd,
            ld.dzwd,
            ld.ld_type
        FROM
            circle( point( #{params.dzjd} :: float8, #{params.dzwd} :: float8 ), 0.003423 ) AS jwd,
            UNNEST ( string_to_array( #{params.deptId}, ',' ) ) AS sqbm,
            ld_bzdz ld
        LEFT JOIN blade_dept sq ON sq.id = ld.sqjcwhdm
        WHERE
            ld.is_deleted = 0
          AND ld.sqjcwhdm :: TEXT IN ( sqbm )
	      AND circle_contain_pt ( jwd, point( ld.dzjd :: float8, ld.dzwd :: float8 ) )
    </select>


    <select id="exportBzdz" resultType="org.springblade.modules.bzdz.excel.BzdzExcel">
        SELECT * FROM ld_bzdz ${ew.customSqlSegment}
    </select>

    <select id="getXzqh" resultType="org.springblade.modules.bzdz.vo.XzqhVO">
        SELECT
            zddm,
            zdz
        FROM
            PUBLIC.zdb
        <where>
            <if test="null != params.lb and '' != params.lb">
                AND lb = #{params.lb}
            </if>
            <if test="null != params.parentId and '' != params.parentId">
                AND parent_id = #{params.parentId}
            </if>
        </where>
        ORDER BY
            zddm
    </select>

    <!--<select id="getXzqhPtTree" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
            id,
            zddm,
            zdz,
            parent_id
        FROM
            PUBLIC.zdb
        where lb = '1'
        <if test="params.type == '1'">
            and length(zddm) = 13
        </if>
        <if test="params.type != '1'">
            and length(zddm) != 13
        </if>
        ORDER BY
            zddm
    </select>-->

    <select id="getXzqhPtTree" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
        id,
        zddm,
        zdz,
        parent_id,
        lb,
        (
        SELECT
        CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
        FROM
        PUBLIC.zdb
        WHERE
        parent_id = #{params.parentId}
        ) AS "has_children"
        FROM
        PUBLIC.zdb
        where lb = #{params.lb}
        and (#{params.parentId} like '%' || zdb.zddm || '%' or zdb.parents like '%' || #{params.parentId} || '%')
        <if test="params.type == 1">
            and length(zddm) = 13
        </if>
        <if test="params.type != 1">
            and length(zddm) != 13
        </if>
        <if test="params.lb == 2 and params.type != 1">
            and zddm = #{params.shi} :: text
        </if>
        ORDER BY
        zddm
    </select>

    <select id="getShi" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
        id,
        zddm,
        zdz,
        parent_id
        FROM
        PUBLIC.zdb
        where lb = '2'
        <if test="params.type == '1'">
            and length(zddm) = 13
        </if>
        <if test="params.type != '1'">
            and length(zddm) != 13
        </if>
        <if test="params.shi != null and params.shi != ''">
            and shi = #{params.shi} :: text
        </if>
        ORDER BY
        zddm
    </select>

    <select id="getQv" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
        id,
        zddm,
        zdz,
        parent_id
        FROM
        PUBLIC.zdb
        where lb = '3'
        <if test="params.type == '1'">
            and length(zddm) = 13
        </if>
        <if test="params.type != '1'">
            and length(zddm) != 13
        </if>
        <if test="params.shi != null and params.shi != ''">
            and shi = #{params.shi} :: text
        </if>
        <if test="params.qv != null and params.qv != ''">
            and qv = #{params.qv} :: text
        </if>
        ORDER BY
        zddm
    </select>

    <select id="getJd" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
        id,
        zddm,
        zdz,
        parent_id
        FROM
        PUBLIC.zdb
        where lb = '4'
        <if test="params.type == '1'">
            and length(zddm) = 13
        </if>
        <if test="params.type != '1'">
            and length(zddm) != 13
        </if>
        <if test="params.shi != null and params.shi != ''">
            and shi = #{params.shi} :: text
        </if>
        <if test="params.qv != null and params.qv != ''">
            and parent_id = #{params.qv} :: text
        </if>
        ORDER BY
        zddm
    </select>

    <select id="getSq" resultType="org.springblade.modules.bzdz.vo.RlsqVO">
        SELECT
        id,
        zddm,
        zdz,
        parent_id
        FROM
        PUBLIC.zdb
        where lb = '5'
        <if test="params.type == '1'">
            and length(zddm) = 13
        </if>
        <if test="params.type != '1'">
            and length(zddm) != 13
        </if>
        <if test="params.shi != null and params.shi != ''">
            and shi = #{params.shi} :: text
        </if>
        <if test="jdid != null and jdid != ''">
            and parents like '%' || #{jdid} :: text || '%'
        </if>
        ORDER BY
        zddm
    </select>
</mapper>
