<?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.zdb.mapper.ZdbMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="zdbResultMap" type="org.springblade.modules.zdb.entity.ZdbEntity">
        <result column="id" property="id"/>
        <result column="zddm" property="zddm"/>
        <result column="zdz" property="zdz"/>
        <result column="shi" property="shi"/>
        <result column="qv" property="qv"/>
        <result column="jiedao" property="jiedao"/>
        <result column="parent_id" property="parentId"/>
        <result column="parents" property="parents"/>
        <result column="lb" property="lb"/>
    </resultMap>


    <select id="selectZdbPage" resultMap="zdbResultMap">
        select * from public.zdb
    </select>


    <select id="exportZdb" resultType="org.springblade.modules.zdb.excel.ZdbExcel">
        SELECT * FROM public.zdb ${ew.customSqlSegment}
    </select>
<!--    根据社区编码 查询其父类-->
    <select id="getParentsBySqbm" resultType="org.springblade.modules.zdb.entity.ZdbEntity">
        SELECT
            zddm,
            zdz,
            lb
        FROM
            public.zdb
        WHERE
                zddm IN ( SELECT UNNEST ( string_to_array( parents, ',' ) ) par FROM public.zdb WHERE zddm = #{zddm} )
          AND lb != '1'
        ORDER BY
            lb
    </select>

    <!--    根据zddm  查询子类-->
     <select id="getJdByZddm" resultType="org.springblade.modules.zdb.vo.JdVo">
        select zddm,zdz from public.zdb where parent_id = #{zddm}
    </select>

    <select id="getSqByZddm" resultType="org.springblade.modules.zdb.vo.SqVo">
        select zddm,zdz from public.zdb where parent_id = #{zddm}
    </select>

    <select id="getDeptByZddm" resultType="org.springblade.modules.zdb.entity.ZdbEntity">
        select zddm,zdz,parents,lb from public.zdb where zddm = #{zddm}
    </select>

    <select id="getDetailByZddm" resultType="org.springblade.modules.zdb.entity.ZdbEntity">
        select shi,qv from public.zdb where zddm = #{zddm}
    </select>

    <insert id="saveZdb" parameterType="org.springblade.modules.zdb.entity.ZdbEntity">
        INSERT INTO public.zdb (id, zddm,zdz,shi,qv,jiedao,parent_id, parents,lb)
        VALUES (#{id}, #{zddm}, #{zdz}, #{shi}, #{qv}, #{jiedao}, #{parentId}, #{parents}, #{lb})
    </insert>

    <select id="selectZddm" resultMap="zdbResultMap">
        select zddm from public.zdb where parent_id = #{jdid} order by zddm desc limit 1
    </select>

    <select id="selectParent" resultMap="zdbResultMap">
        select shi,parents from public.zdb where zddm = #{jdid}
    </select>
</mapper>
