<?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.jgInfo.mapper.JgInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="jgInfoResultMap" type="org.springblade.modules.jgInfo.entity.JgInfoEntity">
        <result column="id" property="id"/>
        <result column="jg_mc" property="jgMc"/>
        <result column="sq_id" property="sqId"/>
        <result column="sq_mc" property="sqMc"/>
        <result column="pcs_id" property="pcsId"/>
        <result column="pcs_mc" property="pcsMc"/>
        <result column="fj_id" property="fjId"/>
        <result column="fj_mc" property="fjMc"/>
        <result column="jy_id" property="jyId"/>
        <result column="jy_mc" property="jyMc"/>
        <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"/>
    </resultMap>

    <select id="exportjgInfo" resultType="org.springblade.modules.jgInfo.excel.JgInfoExcel">
        SELECT * FROM jg_info ${ew.customSqlSegment}
    </select>

    <select id="getFjList" resultType="org.springblade.modules.jgInfo.vo.FjVO">
        SELECT fj.id,
               fj.dept_name
        FROM blade_dept fj
        WHERE
            (#{params.deptId} LIKE ('%' || fj.id :: TEXT || '%')
            OR (SELECT sq.ancestors FROM blade_dept sq WHERE sq.id :: TEXT = (string_to_array(#{params.deptId} , ',' )) [1]) LIKE ('%' || fj.id :: TEXT || '%')
            OR (fj.ancestors like ('%' || (string_to_array(#{params.deptId}, ',')) [1] || '%')) )
            AND fj.dept_category = 5
            AND fj.is_deleted = 0
        ORDER BY
            fj.dept_name
    </select>

    <select id="getPcsList" resultType="org.springblade.modules.jgInfo.vo.PcsVO">
        SELECT pcs.id,
               pcs.dept_name
        FROM blade_dept pcs
        WHERE
            (#{params.deptId} LIKE ('%' || pcs.id :: TEXT || '%')
            OR (SELECT sq.ancestors FROM blade_dept sq WHERE sq.id :: TEXT = (string_to_array(#{params.deptId} , ',' )) [1]) LIKE ('%' || pcs.id :: TEXT || '%')
            OR (pcs.ancestors like ('%' || (string_to_array(#{params.deptId}, ',')) [1] || '%')) )
            AND pcs.parent_id = #{params.parentId}
            AND pcs.dept_category = 6
            AND pcs.is_deleted = 0
        ORDER BY
            pcs.dept_name
    </select>

    <select id="getSqList" resultType="org.springblade.modules.jgInfo.vo.SqVO">
        SELECT sq.id,
               sq.dept_name
        FROM blade_dept sq
        WHERE
            (#{params.deptId} LIKE ('%' || sq.id :: TEXT || '%')
            OR (SELECT sq.ancestors FROM blade_dept sq WHERE sq.id :: TEXT = (string_to_array(#{params.deptId} , ',' )) [1]) LIKE ('%' || sq.id :: TEXT || '%')
            OR (sq.ancestors like ('%' || (string_to_array(#{params.deptId}, ',')) [1] || '%')) )
            AND sq.parent_id = #{params.parentId}
            AND sq.dept_category = 7
            AND sq.is_deleted = 0
        ORDER BY
            sq.dept_name
    </select>

    <select id="getJgList" resultType="org.springblade.modules.jgInfo.vo.JgVO">
        SELECT
            id,
            jg_mc AS deptName,
            jy_id,
            jy_mc,
            jy_role_name
        FROM
            jg_info
        WHERE
            sq_id = #{params.parentId}
          AND is_deleted = 0
    </select>

    <select id="getJyList" resultType="org.springblade.modules.jgInfo.vo.JyVO">
        SELECT
            bu.id AS jyId,
            bu.real_name AS jyMc,
            br.role_name
        FROM
            blade_user bu
        LEFT JOIN blade_role br ON br.ID :: TEXT = bu.role_id
        WHERE
            bu.dept_id LIKE '%' || #{params.sqId} || '%'
        AND bu.role_id IN ('1677215233615192065', '1677215613229064194')
        AND bu.is_deleted = 0
        ORDER BY
            br.role_name,
            bu.real_name
    </select>

    <select id="getJgByJh" resultType="java.lang.String">
        SELECT
            jg.id
        FROM
            jdrh.jg_info jg
        LEFT JOIN jdrh.blade_user bu ON bu.ID = jg.jy_id
        WHERE
            jg.is_deleted = 0
          AND bu.jh = #{jh}
    </select>
</mapper>
