<?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.sqjw.pcsInfo.mapper.PcsInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="pcsInfoResultMap" type="org.springblade.modules.sqjw.pcsInfo.entity.PcsInfoEntity">
        <result column="id" property="id"/>
        <result column="gajgbm" property="gajgbm"/>
        <result column="gajgmc" property="gajgmc"/>
        <result column="jgjc" property="jgjc"/>
        <result column="gajglb" property="gajglb"/>
        <result column="gajgxz" property="gajgxz"/>
        <result column="gajgxzqh" property="gajgxzqh"/>
        <result column="rkywxzqh" property="rkywxzqh"/>
        <result column="ssdw" property="ssdw"/>
        <result column="gajgjb" property="gajgjb"/>
        <result column="bzgg" property="bzgg"/>
        <result column="sjtjjg" property="sjtjjg"/>
        <result column="dwlxdh" property="dwlxdh"/>
        <result column="pcsm" property="pcsm"/>
        <result column="pcsgllb" property="pcsgllb"/>
        <result column="pcslb" property="pcslb"/>
        <result column="pcsdj" property="pcsdj"/>
        <result column="ldys" property="ldys"/>
        <result column="fqsqk" property="fqsqk"/>
        <result column="xqmj" property="xqmj"/>
        <result column="sqsl" property="sqsl"/>
        <result column="xzcsl" property="xzcsl"/>
        <result column="zrcsl" property="zrcsl"/>
        <result column="wgsl" property="wgsl"/>
        <result column="jgsl" property="jgsl"/>
        <result column="jwssl" property="jwssl"/>
        <result column="jwzsl" property="jwzsl"/>
        <result column="hjrksl" property="hjrksl"/>
        <result column="syrks" property="syrks"/>
        <result column="syhs" property="syhs"/>
        <result column="fzr" property="fzr"/>
        <result column="dbmjrs" property="dbmjrs"/>
        <result column="symjrs" property="symjrs"/>
        <result column="xpmjrs" property="xpmjrs"/>
        <result column="sqmjrs" property="sqmjrs"/>
        <result column="zzfjrs" property="zzfjrs"/>
        <result column="jwzlrs" property="jwzlrs"/>
        <result column="bgdz" property="bgdz"/>
        <result column="dwdzjd" property="dwdzjd"/>
        <result column="dwdzwd" property="dwdzwd"/>
        <result column="pcszp" property="pcszp"/>
        <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="selectPcsInfoPage" resultType="org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO">
        SELECT
            bd.id AS deptId,
            bd.full_name AS gajgmc,
            pcs.id,
            pcs.gajglb,
            pcs.pcslb,
            pcs.fzr,
            pcs.dwlxdh,
            pcs.create_time
        FROM
            (select * from blade_dept bd
                where is_deleted = 0 and bd.dept_category = 6 and ((bd.id :: text = #{params.deptId}) OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
                    <if test="null != params.gajgmc and '' != params.gajgmc">
                         AND bd.full_name LIKE '%' || #{params.gajgmc} || '%'
                    </if>
            ) bd
        LEFT JOIN blade_pcs_info pcs ON pcs.gajgbm = bd.id :: text
        WHERE 1 = 1
        <if test="null != params.gajglb and '' != params.gajglb">
          AND pcs.gajglb = #{params.gajglb}
        </if>
        <if test="null != params.fzr and '' != params.fzr">
          AND pcs.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        ORDER BY
            bd.id
    </select>

    <select id="exportPcsInfo" resultType="org.springblade.modules.sqjw.pcsInfo.excel.PcsInfoExcel">
        SELECT
        bd.id AS deptId,
        bd.full_name AS gajgmc,
        pcs.id,
        pcs.gajglb,
        pcs.fzr,
        pcs.dwlxdh,
        pcs.create_time
        FROM
        blade_dept bd
        LEFT JOIN blade_pcs_info pcs ON pcs.gajgbm = bd.id :: text
        WHERE
        bd.is_deleted = 0
        AND bd.dept_category = 6
        AND ((bd.id :: text = #{params.deptId}) OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.gajgmc and '' != params.gajgmc">
            AND bd.full_name LIKE '%' || #{params.gajgmc} || '%'
        </if>
        <if test="null != params.gajglb and '' != params.gajglb">
            AND #{params.gajglb} LIKE '%' || pcs.gajglb || '%'
        </if>
        <if test="null != params.fzr and '' != params.fzr">
            AND pcs.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        ORDER BY
        bd.id
    </select>

    <select id="getDetail" resultType="org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO">
        SELECT
            bd.id AS gajgbm,
            bd.dept_name AS jgjc,
            bd.full_name AS gajgmc,
            fj.id AS ssdwbm,
            fj.full_name AS ssdw,
            pcs.id,
            pcs.gajglb,
            pcs.gajgxz,
            pcs.gajgxzqh,
            pcs.rkywxzqh,
            pcs.gajgjb,
            pcs.bzgg,
            pcs.sjtjjgbm,
            pcs.sjtjjg,
            pcs.dwlxdh,
            pcs.pcsm,
            pcs.pcsgllb,
            pcs.pcslb,
            pcs.pcsdj,
            pcs.ldys,
            pcs.fqsqk,
            pcs.xqmj,
            pcs.sqsl,
            pcs.xzcsl,
            pcs.zrcsl,
            pcs.wgsl,
            pcs.jgsl,
            pcs.jwssl,
            pcs.jwzsl,
            pcs.hjrksl,
            pcs.syrks,
            pcs.syhs,
            ( SELECT bui.xm FROM jdrh.blade_user_info bui WHERE bui.zwlb = '1760218852527259650' AND bui.gzdwmc :: int8 = bd.ID LIMIT 1) AS fzr,
            pcs.dbmjrs,
            pcs.symjrs,
            pcs.xpmjrs,
            pcs.sqmjrs,
            pcs.zzfjrs,
            pcs.jwzlrs,
            pcs.bgdz,
            pcs.dwdzjd,
            pcs.dwdzwd,
            pcs.pcszp,
            pcs.pc
        FROM
            jdrh.blade_dept bd
        LEFT JOIN jdrh.blade_dept fj ON fj.id = bd.parent_id
        LEFT JOIN jdrh.blade_pcs_info pcs ON pcs.gajgbm = bd.id :: text
        WHERE
            bd.id = #{deptId}
    </select>

    <select id="getDictList" resultType="org.springblade.modules.system.vo.ZdVO">
        SELECT
            id AS value,
            dict_value AS label
        FROM
            blade_dict_biz
        WHERE
            code = #{code}
          AND parent_id > 0
          AND is_sealed = 0
          AND is_deleted = 0
        ORDER BY sort
    </select>

    <select id="getFzr" resultType="org.springblade.modules.system.entity.User">
        SELECT
            bu.id,
            bu.account,
            bu.NAME
        FROM
            blade_user bu
        WHERE
            bu.dept_id = #{params.gajgbm}
          AND is_deleted = 0
    </select>

    <select id="getFjSj" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            id,
            dept_name
        FROM
            blade_dept
        WHERE
            dept_category = 5
          AND is_deleted = 0
        ORDER BY
            id
    </select>

    <select id="getAllPcs" resultType="org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO">
        SELECT
            pcs.id,
            pcs.dept_name AS fullName,
            fj.dept_name || '——' || pcs.dept_name AS deptName,
        CASE WHEN bpi.ldys IS NULL THEN '0' ELSE bpi.ldys
        END
        FROM
            jdrh.blade_dept pcs
        LEFT JOIN jdrh.blade_dept fj ON fj.ID = pcs.parent_id AND fj.is_deleted = 0
        LEFT JOIN jdrh.blade_pcs_info bpi ON bpi.gajgbm = pcs.id :: text
        WHERE
            pcs.dept_category = 6
        AND pcs.is_deleted = 0
        AND pcs.dept_name IS NOT NULL
        AND fj.dept_name IS NOT NULL
        <if test="null != params.deptName and '' != params.deptName">
            AND pcs.dept_name LIKE '%' || #{params.deptName} || '%'
        </if>
        <if test="null != params.parentId and '' != params.parentId">
            AND pcs.parent_id = #{params.parentId} :: int8
        </if>
        ORDER BY
            pcs.ID
    </select>

    <select id="getSqByPcsid" resultType="org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO">
        SELECT
        sq.ID,
        sq.dept_name
        FROM
        jdrh.blade_dept sq
        WHERE
        sq.dept_category = 7
        AND sq.is_deleted = 0
        <if test="null != params.id and '' != params.id">
            AND sq.parent_id :: text = #{params.id}
        </if>
        ORDER BY
        sq.ID
    </select>

    <select id="getJgBySqId" resultType="org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO">
        SELECT
            id,
            jg_mc AS deptName
        FROM
            jdrh.jg_info
        WHERE
            is_deleted = 0
          AND sq_id :: text = #{params.id}
    </select>

    <select id="getDzxx" resultType="org.springblade.modules.sqjw.pcsInfo.vo.DzxxVO">
        SELECT
            dzbm,
            pcsdm,
            pcsjgmc,
            xzmc,
            ctmc,
            jlxmc,
            mlph,
            mlxz,
            jwqmc
        FROM
            sspt.rk_dzxx
        <where>
            <if test="null != params.xjgajgjgdm and '' != params.xjgajgjgdm">
                AND xjgajgjgdm = #{params.xjgajgjgdm}
            </if>
            <if test="null != params.pcsjgdm and '' != params.pcsjgdm">
                AND pcsjgdm = #{params.pcsjgdm}
            </if>
            <if test="null != params.mlph and '' != params.mlph">
                AND mlph LIKE '%' || #{params.mlph} || '%'
            </if>
            <if test="null != params.mlxz and '' != params.mlxz">
                AND mlxz LIKE '%' || #{params.mlxz} || '%'
            </if>
        </where>
    </select>

    <select id="getChildDept" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            *
        FROM
            jdrh.blade_dept
        WHERE
            is_deleted = 0
        <if test="null == params.parentId or '' == params.parentId">
            AND dept_category = 4
        </if>
        <if test="null != params.parentId and '' != params.parentId">
            AND parent_id = #{params.parentId} :: int8
        </if>
    </select>
</mapper>
