<?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.system.mapper.DeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="deptResultMap" type="org.springblade.modules.system.entity.Dept">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="dept_name" property="deptName"/>
        <result column="full_name" property="fullName"/>
        <result column="ancestors" property="ancestors"/>
        <result column="dept_category" property="deptCategory"/>
        <result column="sort" property="sort"/>
        <result column="remark" property="remark"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <resultMap id="deptVOResultMap" type="org.springblade.modules.system.vo.DeptVO">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="dept_name" property="deptName"/>
        <result column="full_name" property="fullName"/>
        <result column="ancestors" property="ancestors"/>
        <result column="dept_category" property="deptCategory"/>
        <result column="sort" property="sort"/>
        <result column="remark" property="remark"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="has_children" property="hasChildren"/>
    </resultMap>

    <resultMap id="treeNodeResultMap" type="org.springblade.core.tool.node.TreeNode">
        <id column="id" property="id"/>
        <result column="parent_id" property="parentId"/>
        <result column="title" property="title"/>
        <result column="value" property="value"/>
        <result column="key" property="key"/>
        <result column="has_children" property="hasChildren"/>
    </resultMap>

    <select id="lazyList" resultMap="deptVOResultMap">
        SELECT
            dept.* ,
            (
                SELECT
                    CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
                FROM
                    blade_dept
                WHERE
                    parent_id = dept.id and is_deleted = 0
            ) AS "has_children"
        FROM
            blade_dept dept
        WHERE dept.is_deleted = 0
            and (#{param3.deptId} like '%' || dept.id || '%' or dept.ancestors like '%' || #{param3.deptId} || '%')
        <if test="param1!=null and param1!=''">
            and dept.tenant_id = #{param1}
        </if>
        <if test="param2!=null">
            and dept.parent_id = #{param2}
        </if>
        <if test="param3.deptName!=null and param3.deptName!=''">
            and dept.dept_name like concat(concat('%', #{param3.deptName}),'%')
        </if>
        <if test="param3.fullName!=null and param3.fullName!=''">
            and dept.full_name like concat(concat('%', #{param3.fullName}),'%')
        </if>
        ORDER BY dept.sort
    </select>

    <select id="tree" resultType="org.springblade.modules.system.vo.DeptVO">
        select id, parent_id, dept_name as title, id as "value", id as "key", dept_category from blade_dept where is_deleted = 0
        <if test="_parameter!=null and _parameter!=''">
            and tenant_id = #{tenantId}
        </if>
        and (id :: text = #{deptId} or ancestors like '%' || #{deptId} || '%')
        order by sort
    </select>

    <select id="lazyTree" resultMap="treeNodeResultMap" >
        SELECT
            dept.id,
            dept.parent_id,
            dept.dept_name AS title,
            dept.id AS "value",
            dept.id AS "key",
            (
                SELECT
                    CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
                FROM
                    blade_dept
                WHERE
                    parent_id = dept.id and is_deleted = 0
            ) AS "has_children"
        FROM
            blade_dept dept
        WHERE
            dept.parent_id = #{param2} AND dept.is_deleted = 0
        and (#{param3} like '%' || dept.id || '%' or dept.ancestors like '%' || #{param3} || '%')
        <if test="param1!=null and param1!=''">
            and dept.tenant_id = #{param1}
        </if>
        ORDER BY dept.sort
    </select>

    <select id="getDeptNames" resultType="java.lang.String">
        SELECT
        dept_name
        FROM
        blade_dept
        WHERE
        id IN
        <foreach collection="array" item="ids" index="index" open="(" close=")" separator=",">
            #{ids}
        </foreach>
        and is_deleted = 0
    </select>


    <select id="getDeptIdByDeptname" resultType="java.lang.Long">
        select id from blade_dept where dept_name = #{deptName} and is_deleted = 0
    </select>

    <select id="getXfbm" resultType="org.springblade.modules.system.vo.DeptVO">
        SELECT
            *
        FROM
            blade_dept
        WHERE
            is_xf = 1
    </select>

    <select id="getDeptList" resultType="org.springblade.modules.system.vo.DeptListVo">
        SELECT
            bd.id,
            bd.dept_name
        FROM
            blade_user bu
        LEFT JOIN blade_dept bd ON bu.dept_id LIKE'%' || bd.id :: TEXT || '%'
        WHERE
            bu.id = #{userId}
    </select>

    <select id="getfJList" resultType="org.springblade.modules.system.vo.FjVO">
        SELECT
            fj.id,
            fj.dept_name
        FROM
            blade_dept bd
        LEFT JOIN blade_dept fj ON (
                    ( bd.ancestors LIKE '%' || fj.id || '%' )
                    OR ( fj.ancestors LIKE '%' || bd.id || '%' )
                    OR fj.id = bd.id
                )
        WHERE
            bd.id :: text = #{deptId}
        AND fj.dept_category = 5
    </select>

    <select id="getPcsList" resultType="org.springblade.modules.system.vo.PcsVO">
        SELECT
            pcs.id,
            pcs.dept_name
        FROM
            blade_dept bd
        LEFT JOIN blade_dept pcs ON (
                    ( bd.ancestors LIKE'%' || pcs.id || '%' )
                    OR ( pcs.ancestors LIKE'%' || bd.id || '%' )
                    OR pcs.id = bd.id
                )
        WHERE
            bd.id :: text = #{deptId}
	    AND pcs.parent_id = #{fjId}
	    AND pcs.dept_category = 6
    </select>

    <select id="getSqList" resultType="org.springblade.modules.system.vo.SqVO">
        SELECT
            sq.id,
            sq.dept_name
        FROM
            blade_dept bd
        LEFT JOIN blade_dept sq ON (
                    ( bd.ancestors LIKE'%' || sq.id || '%' )
                    OR ( sq.ancestors LIKE'%' || bd.id || '%' )
                    OR sq.id = bd.id
                )
        WHERE
            bd.id :: text = #{deptId}
	    AND sq.parent_id = #{pcsId}
	    AND sq.dept_category = 7
    </select>

    <select id="selectSq" resultMap="deptResultMap">
        select dept_name from blade_dept where dept_category = '7' and parent_id :: text = #{deptId}
    </select>

    <select id="selectPcs" resultMap="deptResultMap">
        select full_name from blade_dept where dept_category = '6' and id :: text = #{deptId}
    </select>

    <select id="selectSsfjBm" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            id,
            dept_name
        FROM
            blade_dept
        WHERE
            dept_name = #{ssfjMc}
    </select>

    <select id="selectSspcsBm" resultMap="deptResultMap">
        SELECT
            pcs.ID
        FROM
            blade_dept pcs
        WHERE
            pcs.dept_name = #{sspcsMc}
            AND pcs.parent_id = ( SELECT fj.ID FROM blade_dept fj WHERE fj.dept_name = #{ssfjMc} )
    </select>

    <select id="selectSssqBm" resultMap="deptResultMap">
        select id from blade_dept where dept_name = #{sssqMc} and parent_id = #{parentId}
    </select>

    <select id="getJsByFj" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where id :: text = #{deptId}
    </select>

    <select id="getJsByPcs" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where id = #{parentId}
    </select>

    <select id="getJsBySq" resultMap="deptResultMap">
        select b.id as value,b.dept_name as label from blade_dept a
        left join blade_dept b on a.parent_id = b.id
        where a.id = #{parentId}
    </select>

    <select id="getJsByFjDownPcs" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where parent_id :: text = #{value} and dept_category = 6
    </select>

    <select id="getJsByPcsDownPcs" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where id :: text = #{deptId} and dept_category = 6
    </select>

    <select id="getJsBySqDownPcs" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where id = #{parentId} and dept_category = 6
    </select>

    <select id="getJsByFjDownSq" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where parent_id :: text = #{value}
    </select>

    <select id="getJsByPcsDownSq" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where parent_id :: text = #{value}
    </select>

    <select id="getJsBySqDownSq" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where id :: text = #{deptId}
    </select>

    <select id="getJsBySj" resultMap="deptResultMap">
        select id as value,dept_name as label from blade_dept where parent_id :: text = #{deptId}
    </select>

    <select id="getParentId" resultType="java.lang.Long">
        SELECT pcs.id
        FROM
            blade_dept pcs
        WHERE
            pcs.dept_name = #{parentName}
        AND pcs.parent_id =
            (select id from blade_dept where dept_name = #{fjName})
    </select>

    <select id="getQsga" resultMap="deptResultMap">
        select * from blade_dept where
        <if test="deptId == '220000000000'">
            dept_category = 4
        </if>
        <if test="deptId != '220000000000'">
            parent_id :: text = #{deptId}
        </if>
         order by sort asc
    </select>

    <select id="getJxcspc" resultMap="deptResultMap">
        select
            count(0) as jxcs,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小学校' and info.is_deleted = 0) as xxx,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小医院' and info.is_deleted = 0) as xyy,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小商店' and info.is_deleted = 0) as xsd,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小餐饮' and info.is_deleted = 0) as xcy,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小旅馆' and info.is_deleted = 0) as xlg,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小歌舞娱乐' and info.is_deleted = 0) as xgw,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小网吧' and info.is_deleted = 0) as xwb,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小美容洗浴' and info.is_deleted = 0) as xmr,
            (select count(0) from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.dwlb = '小生产加工企业' and info.is_deleted = 0) as xsc
        from jdrh.cs_info info left join jdrh.blade_dept dept on info.sssq_bm = dept.id where ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and info.is_deleted = 0
    </select>

    <select id="getJxcsjc" resultMap="deptResultMap">
        SELECT
	        COUNT(0) as jcsl,
	        (select count(0) from csjc_info csjc LEFT JOIN cs_info info ON info.ID = csjc.cs_id LEFT JOIN jdrh.blade_dept dept ON info.sssq_bm = dept.ID WHERE ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and csjc.zgzt IN (1, 2, 3, 4, 5) and csjc.is_deleted = 0) as pcfxyh,
	        (select count(0) from csjc_info csjc LEFT JOIN cs_info info ON info.ID = csjc.cs_id LEFT JOIN jdrh.blade_dept dept ON info.sssq_bm = dept.ID WHERE ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and (csjc.zgzt = '1' or csjc.zgzt = '2') and csjc.is_deleted = 0) as zg,
	        (select count(0) from csjc_info csjc LEFT JOIN cs_info info ON info.ID = csjc.cs_id LEFT JOIN jdrh.blade_dept dept ON info.sssq_bm = dept.ID WHERE ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and csjc.zgtsdzp is not null and csjc.is_deleted = 0) as zgtsd,
	        (select count(0) from csjc_yjjl yjjl left join csjc_info csjc on csjc.id = yjjl.csjc_id LEFT JOIN cs_info info ON info.ID = csjc.cs_id LEFT JOIN jdrh.blade_dept dept ON info.sssq_bm = dept.ID WHERE ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and yjjl.is_deleted = 0) as yjjl
        FROM
	        csjc_info csjc
	    LEFT JOIN cs_info info ON info.ID = csjc.cs_id
	    LEFT JOIN jdrh.blade_dept dept ON info.sssq_bm = dept.ID
        WHERE ((#{id} :: text LIKE '%' || dept.id || '%') OR (dept.ancestors LIKE '%' || #{id} || '%')) and csjc.is_deleted = 0
    </select>

    <select id="getQsyhcsjc" resultType="org.springblade.modules.system.vo.DeptVOS">
        with dept as (
        select
        dept_name市局名,
        id市局,
        id社区
        from v_dept_all
        where dept_name分局名 IS NOT NULL
        GROUP by dept_name市局名, id社区, id市局
        ), yhcount as (
        select
        dept_name市局名,
        count(1) as yhcount,
        count(1) filter(where bu.last_login_time :: date = now() :: date) as jryhsl
        from blade_user bu, unnest(string_to_array(bu.dept_id, ',')) as deptid, dept
        where bu.code is not null and dept.id社区 :: text = deptid
        group by dept_name市局名
        ), cscount as (
        select
        dept_name市局名,
        count(1) as cscount
        from cs_info info, dept
        where dept.id社区 = info.sssq_bm and info.is_deleted = 0
        group by dept_name市局名
        ), jccount as (
        select
        dept_name市局名,
        count(1) as jccount
        from csjc_info jc, cs_info info, dept
        where info.id = jc.cs_id
        and dept.id社区 = info.sssq_bm
        and jc.create_time > '2023-11-21 19:00:00'
        and info.is_deleted = 0
        <if test="str != null and str != '' and strEnd != null and strEnd != ''">
            and jc.create_time :: text between #{str} and #{strEnd}
        </if>
        group by dept_name市局名
        )
        select
        distinct
        dept.dept_name市局名 as sjmc,
        dept.id市局 as id,
        COALESCE(yh.yhcount,0) as yhsl,
        COALESCE(info.cscount,0) as cssl,
        COALESCE(jc.jccount,0) as jcsl,
        COALESCE(yh.jryhsl,0) as jryhsl
        from dept
        left join yhcount as yh on yh.dept_name市局名 = dept.dept_name市局名
        left join cscount as info on info.dept_name市局名 = dept.dept_name市局名
        left join jccount as jc on jc.dept_name市局名 = dept.dept_name市局名
    </select>

    <select id="getQsyhcsjcfj" resultType="org.springblade.modules.system.vo.DeptVOS">
        with dept as (
        select
        dept_name分局名,
        id分局,
        id社区
        from v_dept_all
        where dept_name分局名 IS NOT NULL
        and "ancestors分局祖级" like concat('%',#{id},'%')
        GROUP by dept_name市局名, id社区, dept_name分局名, id分局
        ), yhcount as (
        select
        dept_name分局名,
        count(1) as yhcount,
        count(1) filter(where bu.last_login_time :: date = now() :: date) as jryhsl
        from blade_user bu, unnest(string_to_array(bu.dept_id, ',')) as deptid, dept
        where bu.code is not null and dept.id社区 :: text = deptid
        group by dept_name分局名
        ), cscount as (
        select
        dept_name分局名,
        count(1) as cscount
        from cs_info info, dept
        where dept.id社区 = info.sssq_bm and info.is_deleted = 0
        group by dept_name分局名
        ), jccount as (
        select
        dept_name分局名,
        count(1) as jccount
        from csjc_info jc, cs_info info, dept
        where info.id = jc.cs_id
        and dept.id社区 = info.sssq_bm
        and jc.create_time > '2023-11-21 19:00:00'
        and info.is_deleted = 0
        <if test="str != null and str != '' and strEnd != null and strEnd != ''">
            and jc.create_time between #{str} and #{strEnd}
        </if>
        group by dept_name分局名
        )
        select
        distinct
        dept.dept_name分局名 as sjmc,
        dept.id分局 as id,
        COALESCE(yh.yhcount,0) as yhsl,
        COALESCE(info.cscount,0) as cssl,
        COALESCE(jc.jccount,0) as jcsl,
        COALESCE(yh.jryhsl,0) as jryhsl
        from dept
        left join yhcount as yh on yh.dept_name分局名 = dept.dept_name分局名
        left join cscount as info on info.dept_name分局名 = dept.dept_name分局名
        left join jccount as jc on jc.dept_name分局名 = dept.dept_name分局名
    </select>

    <select id="getDeptname" resultMap="deptResultMap">
        select replace(substring(dept_name,4,length(dept_name)),'区','') as dept_name,dept_category from jdrh.blade_dept where id :: text = #{id}
    </select>

    <select id="getName" resultMap="deptResultMap">
        select dept_name,dept_category from jdrh.blade_dept where id :: text = #{id}
    </select>

    <select id="getPublicName" resultMap="deptResultMap">
        select id社区 as idsq,id派出所 as idpcs,id分局 as idfj,id市局 as idsj from public.v_dept_all where dept_name社区名 = #{deptName} or dept_name派出所名 = #{deptName} or dept_name分局名 = #{deptName} or dept_name市局名 = #{deptName} limit 1
    </select>

    <select id="getJl" resultMap="deptResultMap">
        select * from (
                          select distinct id, dept_name, ancestors, dept_category from jdrh.blade_dept bd where dept_category = '4'
                          union all
                          select id, dept_name, ancestors, dept_category from jdrh.blade_dept bd where dept_category = '5'
                          union all
                          select id, dept_name, ancestors, dept_category from jdrh.blade_dept bd where dept_category = '6'
                          union all
                          select id, dept_name, ancestors, dept_category from jdrh.blade_dept bd where dept_category = '7'
                      )as a where (ancestors like '%220200000000%' or id = '220200000000') and dept_category != '7'
    </select>

    <select id="getLdByPcsbm" resultType="org.springblade.modules.system.entity.Bzdz">
        SELECT xxzjbh, wzxxdz FROM PUBLIC.fw_bzdz
        WHERE pcsjgdm = #{ deptId }
          AND sssq_bm IS NULL
          AND dyh = ''
          AND lch = ''
          AND lcnpmh = ''
        <if test="wzxxdz != '' and wzxxdz !=null">
           and wzxxdz LIKE '%' || #{wzxxdz} || '%'
        </if>
        order by wzxxdz
    </select>
    <select id="getLdInfoByLdbh" resultType="org.springblade.modules.system.entity.Bzdz">
        SELECT wzxxdz,dzbm FROM PUBLIC.fw_bzdz
        WHERE xxzjbh = #{xxzjbh} and pcsjgdm = #{ deptId }
    </select>

    <update id="updateSSSqDmByxxzjbh">
        update PUBLIC.fw_bzdz set sssq_bm = #{sqId}::bigint where xxzjbh = #{xxzjbh} and pcsjgdm = #{ deptId }
    </update>

    <update id="updateSSSqDmIsNUll">
        update PUBLIC.fw_bzdz set sssq_bm = null where dzbm = #{xxzjbh}  and pcsjgdm = #{ deptId }
    </update>

    <update id="updateSSSqDmIsNUllnew">
        update PUBLIC.fw_bzdz set sssq_bm = null where dzbm = #{dzbm}  and sssq_bm::text = #{sssqBm}
    </update>

    <delete id="deleteByLdId">
        delete from blade_dept where id::text =#{id}
    </delete>

    <update id="updateId">
        update jdrh.blade_dept set id = #{id} where parent_id = #{parentId} and dept_name = #{deptName}
    </update>

    <select id="getDeptCategory" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            sq.dept_category,
            pcs.id
        FROM
            blade_dept sq
        LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        WHERE
            sq.is_deleted = 0
          AND sq.id = #{deptId} :: int8
    </select>

    <select id="getDeptDetail" resultType="org.springblade.modules.system.vo.MjDeptVO">
        SELECT
            id,
            dept_name,
            dept_category
        FROM
            blade_dept
        WHERE
            is_deleted = 0
          AND id = #{deptId} :: int8
    </select>

    <select id="getSqByPcs" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            *
        FROM
            blade_dept
        WHERE
            is_deleted = 0
          AND parent_id = #{id}
    </select>

    <select id="countDeptById" resultType="int">
        SELECT
            count(*)
        FROM
            blade_dept
        WHERE
            is_deleted = 0
          AND id = #{deptId}
    </select>

    <select id="getShi" resultType="org.springblade.modules.system.vo.ZdbVo">
        SELECT id,zddm,zdz FROM  public.zdb where zddm like '%00000000'
    </select>

    <select id="getXqShiZddm" resultType="org.springblade.modules.system.vo.ZdbVo">
        SELECT id,zddm,zdz,parent_id,parents FROM  public.zdb where shi = #{shiZddm} and zddm != #{shiZddm}  AND zddm like '%000000'
    </select>

    <select id="getJdByXqZddm" resultType="org.springblade.modules.system.vo.ZdbVo">
        select id,zddm,zdz,parent_id,parents from public.zdb where  qv= #{xqZddm} and  zddm like '%000' and zddm  != #{xqZddm}
    </select>

    <select id="getSqByJdZddm" resultType="org.springblade.modules.system.vo.ZdbVo">
        select id,zddm,zdz,parent_id,parents from public.zdb where  qv= #{jdZddm} and  zddm like '%000' and zddm  != #{jdZddm}
    </select>
<!--    修改县区父类id和parents-->
    <update id="updateXqParent">
        update public.zdb set parent_id = #{shiZddm}, parents = #{shiZddm} where id in (SELECT id FROM  public.zdb where shi = #{shiZddm} and zddm != #{shiZddm}  AND zddm like '%000000' )
    </update>

    <update id="updateJdParent">
        update public.zdb set parent_id = #{xqzddm},parents = #{parents} where id in (select id from public.zdb where  qv= #{xqzddm} and  zddm like '%000' and zddm  !=#{xqzddm})
    </update>

    <update id="updateSqParent">
        update public.zdb set parent_id = #{jdzddm},parents = #{parents} where id in (select id from public.zdb where  jiedao = #{jdzddm}  and zddm  != #{jdzddm} )
    </update>


    <select id="getDeptByQhAndCategory" resultType="java.lang.Long">
        SELECT
            id
        FROM
            blade_dept
        WHERE
            ID :: TEXT LIKE concat(#{xzqh},'%')
	    AND dept_category = '7'
        ORDER BY ID DESC LIMIT 1;
    </select>

    <select id="getDept" resultType="org.springblade.modules.system.vo.ZdVOSS">
        select
            dept.id as sqid,
            dept.dept_name as sqname,
            jiedao.zddm as jdid,
            jiedao.zdz as jdname,
            qu.zddm as quid,
            qu.zdz as quname,
            shi.zddm as shiid,
            shi.zdz as shiname,
            sheng.zddm as shengid,
            sheng.zdz as shengname
        from unnest(string_to_array(#{deptId}, ',')) as deptId,jdrh.blade_dept dept
        left join public.zdb on dept.id :: text = zdb.zddm
        left join public.zdb as jiedao on zdb.jiedao = jiedao.zddm
        left join public.zdb as qu on zdb.qv = qu.zddm
        left join public.zdb as shi on zdb.shi = shi.zddm
        left join public.zdb as sheng on shi.parent_id = sheng.zddm
        where dept.id :: text in (deptId)
    </select>


    <select id="getZdLdBySssqbm" resultType="org.springblade.modules.system.vo.ZdldVo">
        SELECT xxzjbh,dzbm,wzxxdz FROM public.fw_bzdz where sssq_bm::text = #{sssqBm}
    </select>

    <select id="getBmByDept" resultType="org.springblade.modules.system.vo.ZdVO">
        select
            id as value,
            dept_name as label
        from jdrh.blade_dept bd
        where bd.ancestors LIKE '%' || #{deptId} || '%'
    </select>


    <select id="getSssqBmBydzbm" resultType="org.springblade.modules.system.entity.Bzdz">
        select sssq_bm from public.fw_bzdz where dzbm = #{dzbm}
    </select>

    <update id="updatePlsc">
        update PUBLIC.fw_bzdz set
            sssq_bm = null
        where dzbm in
        <foreach collection="dzbms" item="dzbm" separator="," open="(" close=")">
            #{dzbm}
        </foreach>
        and sssq_bm::text = #{sssqBm}
    </update>

    <select id="getDeptNameById" resultType="org.springblade.modules.system.vo.ZdVO">
        select id as value,dept_name as label from jdrh.blade_dept, string_to_array( #{deptId} , ',') as deptid
        <where>
            <if test="byId.deptCategory != 7 or byId.deptCategory == 14">
                parent_id :: text = #{deptId}
            </if>
            <if test="byId.deptCategory == 7">
                id :: text = ANY(deptid)
            </if>
        </where>
    </select>

    <select id="selectSjByFj" resultMap="deptResultMap">
        select sj.* from jdrh.blade_dept dept left join jdrh.blade_dept sj on dept.parent_id = sj.id where dept.id :: text = #{deptId}
    </select>

    <select id="selectSjByPcs" resultMap="deptResultMap">
        select sj.* from jdrh.blade_dept dept left join jdrh.blade_dept fj on dept.parent_id = fj.id left join jdrh.blade_dept sj on fj.parent_id = sj.id where dept.id::text = #{deptId}
    </select>

    <select id="selectSjBySqmj" resultMap="deptResultMap">
        select sj.* from jdrh.blade_dept dept left join jdrh.blade_dept pcs on dept.parent_id = pcs.id left join jdrh.blade_dept fj on pcs.parent_id = fj.id left join jdrh.blade_dept sj on fj.parent_id = sj.id where dept.id::text = #{deptId}
    </select>

    <select id="getParentDept" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            parent.id,
            parent.dept_name
        FROM
            jdrh.blade_dept bd
        LEFT JOIN jdrh.blade_dept parent ON parent.id = bd.parent_id
        WHERE
            bd.id :: TEXT = #{deptId}
	    AND bd.is_deleted = 0
        AND parent.is_deleted = 0
    </select>

    <update id="getBmxg">
        update jdrh.blade_dept set parent_id = #{pcsid},ancestors = #{ancestors} where id = #{id}
    </update>

    <select id="getFjPcsBySq" resultMap="deptResultMap">
        select
            fj.id as fjid,
            fj.dept_name as fjmc,
            pcs.id as pcsid,
            pcs.dept_name as pcsmc
        from jdrh.blade_dept sq
        left join jdrh.blade_dept pcs on sq.parent_id = pcs.id
        left join jdrh.blade_dept fj on pcs.parent_id = fj.id
        where sq.id = #{sssqBm}
    </select>

    <select id="getfJLists" resultType="org.springblade.modules.system.vo.FjVOS">
        SELECT
            fj.id as value,
            fj.dept_name as label
        FROM
            blade_dept bd
        LEFT JOIN blade_dept fj ON (
                    ( bd.ancestors LIKE '%' || fj.id || '%' )
                    OR ( fj.ancestors LIKE '%' || bd.id || '%' )
                    OR fj.id = bd.id
                )
        WHERE
            bd.id :: text = #{deptId}
        AND fj.dept_category = 5
    </select>

    <select id="getPcsLists" resultType="org.springblade.modules.system.vo.PcsVOS">
        SELECT
            pcs.id as value,
            pcs.dept_name as label
        FROM
            blade_dept bd
        LEFT JOIN blade_dept pcs ON (
                    ( bd.ancestors LIKE'%' || pcs.id || '%' )
                    OR ( pcs.ancestors LIKE'%' || bd.id || '%' )
                    OR pcs.id = bd.id
                )
        WHERE
            bd.id :: text = #{deptId}
	    AND pcs.parent_id :: text = #{fjId}
	    AND pcs.dept_category = 6
    </select>

    <select id="getSfjZdb" resultType="org.springblade.modules.system.vo.FjVOS">
        select zddm as value,zdz as label from public.zdb
        where lb = '3'
        <if test="deptId != '2200000000007'">
            and parent_id = #{deptId}
        </if>
        <if test="deptId == '2200000000007'">
            and parents like '%' || #{deptId} || '%'
        </if>
    </select>

    <select id="getPcsZdb" resultType="org.springblade.modules.system.vo.PcsVOS">
        select zddm as value,zdz as label from public.zdb where parent_id = #{value}
    </select>

    <select id="getDeptById" resultType="int">
        SELECT
            count(*)
        FROM
            blade_dept
        WHERE
            is_deleted = 0
          AND id = #{id}
    </select>

    <select id="getPcsByFjId" resultType="org.springblade.modules.system.vo.ZdVO">
        select id as value,dept_name as label from jdrh.blade_dept where is_deleted = 0 and (id = #{deptId} :: int8 or ancestors like '%' || #{deptId} || '%') and dept_category = 6
    </select>

    <update id="updateAncestors">
        update jdrh.blade_dept set parent_id = #{id},ancestors = #{ancestors} where parent_id :: text = #{str}
    </update>

    <update id="deleteByPcsId">
        update jdrh.blade_dept set is_deleted = 1 where id :: text = #{str}
    </update>

    <select id="getDeptCategoryByAccount" resultMap="deptResultMap">
        SELECT
            dept_category,
            ancestors
        FROM
            jdrh.blade_dept bd
        WHERE
            bd.is_deleted = 0
          AND bd.id = (
            SELECT
                split_part(bu.dept_id, ',', '1') :: int8
            FROM
                jdrh.blade_user bu
            WHERE
                bu.account = #{username}
            AND bu.is_deleted = 0
          )
    </select>

    <select id="getSjByAccount" resultMap="deptResultMap">
        SELECT ( string_to_array( ancestors, ',' ) ) [ 3 ] as id FROM blade_dept where id = #{deptId} :: int8
    </select>
</mapper>
