<?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.rk.mapper.RkMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="rkResultMap" type="org.springblade.modules.rk.entity.RkEntity">
        <result column="id" property="id"/>
        <result column="md_id" property="mdId"/>
        <result column="xm" property="xm"/>
        <result column="yhzgx" property="yhzgx"/>
        <result column="lxdh" property="lxdh"/>
        <result column="xb" property="xb"/>
        <result column="sfzh" property="sfzh"/>
        <result column="gzdw" property="gzdw"/>
        <result column="tc" property="tc"/>
        <result column="zzmm" property="zzmm"/>
        <result column="hyzk" property="hyzk"/>
        <result column="sg" property="sg"/>
        <result column="xx" property="xx"/>
        <result column="jycs" property="jycs"/>
        <result column="jyqk" property="jyqk"/>
        <result column="sfcjsb" property="sfcjsb"/>
        <result column="sfcjyb" property="sfcjyb"/>
        <result column="stjkqk" property="stjkqk"/>
        <result column="sfyfdx" property="sfyfdx"/>
        <result column="sfyfx" property="sfyfx"/>
        <result column="sfysf" property="sfysf"/>
        <result column="yhzjxy" property="yhzjxy"/>
        <result column="jjlxr_xm" property="jjlxrXm"/>
        <result column="jjlxr_dh" property="jjlxrDh"/>
        <result column="lset" property="lset"/>
        <result column="gglr" property="gglr"/>
        <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="xl" property="xl"/>
    </resultMap>


    <select id="selectRkPage" resultType="org.springblade.modules.rk.vo.RkVO">
        SELECT
            *
        FROM
            fw_rk fr
        WHERE
            fr.is_deleted = 0
        AND md_id = #{params.mdId}
    </select>


    <select id="exportRk" resultType="org.springblade.modules.rk.excel.RkExcel">
        SELECT * FROM fw_rk ${ew.customSqlSegment}
    </select>

    <select id="getXqList" resultType="org.springblade.modules.rk.vo.XqVO">
        SELECT
            li.xq AS xqmc,
            li.sqmc AS sssq,
            li.create_time
        FROM
            ld_info li
        LEFT JOIN blade_dept sq ON sq.ID = li.sq_id
        WHERE ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        OR li.wgz_id LIKE '%' || #{params.userId} || '%'
        GROUP BY
            li.xq,
            li.sqmc,
            li.create_time
        ORDER BY
            li.xq
    </select>

    <select id="getLdList" resultType="org.springblade.modules.rk.vo.LdVO">
        SELECT
            li.b_name AS ldNname
        FROM
            ld_info li
        LEFT JOIN blade_dept sq ON sq.id = li.sq_id
        WHERE
            li.xq = #{params.xqmc}
          AND (((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) OR li.wgz_id = #{params.userId} :: text)
        GROUP BY
            li.b_name
        ORDER BY
            li.b_name
    </select>

    <select id="getDyList" resultType="org.springblade.modules.rk.vo.DyVO">
        SELECT
            li.unit
        FROM
            ld_info li
        LEFT JOIN blade_dept sq ON sq.id = li.sq_id
        WHERE
            li.b_name = #{params.ldName}
        AND li.unit IS NOT NULL
        AND (((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) OR li.wgz_id = #{params.userId} :: text)
        ORDER BY
            li.unit
    </select>

    <select id="getDyWebList" resultType="org.springblade.modules.rk.vo.DyWebVO">
        SELECT
            li.unit
        FROM
            ld_info li
        LEFT JOIN blade_dept sq ON sq.id = li.sq_id
        WHERE
            li.b_name = #{params.ldName}
        AND li.unit IS NOT NULL
        AND (((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) OR li.wgz_id = #{params.userId} :: text)
        ORDER BY
            li.unit
    </select>

    <select id="getLcList" resultType="org.springblade.modules.rk.vo.LcWebVO">
        SELECT
            fb.lch
        FROM
            fw_bzdz fb
        WHERE
            fb.b_name = #{params.ldName}
          AND fb.dyh = #{params.unit}
        GROUP BY
            fb.lch
        ORDER BY
            fb.lch :: int8 desc
    </select>

    <select id="getFjList" resultType="org.springblade.modules.rk.vo.FjVO">
        SELECT
            fb.md_id,
            fb.lcnpmh,
            ( SELECT COUNT ( fr.id ) FROM fw_rk fr WHERE fr.md_id = fb.md_id ) AS personNum
        FROM
            fw_bzdz fb
        WHERE
            fb.b_name = #{params.ldName}
          AND fb.dyh = #{params.unit}
        ORDER BY
            fb.lcnpmh desc
    </select>

    <select id="getFjWebList" resultType="org.springblade.modules.rk.vo.FjVO">
        SELECT
            fb.md_id,
            fb.lcnpmh,
            ( SELECT COUNT ( fr.id ) FROM fw_rk fr WHERE fr.md_id = fb.md_id AND fr.is_deleted = 0 ) AS personNum
        FROM
            fw_bzdz fb
        WHERE
            fb.b_name = #{params.ldName}
        AND fb.dyh = #{params.unit}
        AND fb.lch = #{params.lch}
        ORDER BY
            fb.lcnpmh
    </select>

    <select id="selectXq" resultType="org.springblade.modules.rk.vo.LbVOS">
        select distinct
	        xq as value,
	        xq as label
        from
	        ld_info
        where
	        wgz_id = #{value}
    </select>

    <select id="selectLd" resultType="org.springblade.modules.rk.vo.LbVOSS">
        select distinct
	        b_name as value,
	        b_name as label
        from
	        ld_info
        where
	        xq = #{label} and unit is not null
    </select>

    <select id="selectxqmj" resultType="org.springblade.modules.rk.vo.LbVOS">
        select distinct
	        xq as value,
	        xq as label
        from
	        ld_info
        where
            #{deptId} LIKE '%' || sq_id || '%'
    </select>
</mapper>
