<?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.swzdr.mapper.RyDjclMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ryDjclResultMap" type="org.springblade.swzdr.pojo.entity.RyDjclEntity">
        <result column="id" property="id"/>
        <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="ry_id" property="ryId"/>
        <result column="clqk" property="clqk"/>
        <result column="fssj" property="fssj"/>
        <result column="sfdd" property="sfdd"/>
        <result column="djjg" property="djjg"/>
        <result column="xwms" property="xwms"/>
        <result column="fj" property="fj"/>
    </resultMap>

    <select id="selectRyDjclPage" resultType="org.springblade.swzdr.pojo.vo.RyDjclVO">
        SELECT
            dj.id,
            ry.id AS ryId,
            ry.xm,
            ry.level,
            ry.xb,
            ry.gmsfhm,
            dj.sfdd,
            dj.fssj,
            dj.clqk,
            dj.djjg
        FROM
            swzdr_ry_djcl dj
        LEFT JOIN swzdr_ry_info ry ON ry.id = dj.ry_id
        WHERE
            (ry.mj_id = #{params.userId} :: int8 OR ry.pcs_id = #{params.deptId} :: int8 OR ry.wkdw = #{params.deptId} :: int8)
        <if test="null != params.gmsfhm and '' != params.gmsfhm">
            AND ry.gmsfhm ILIKE '%' || #{params.gmsfhm} || '%'
        </if>
        <if test="null != params.xm and '' != params.xm">
            AND ry.xm ILIKE '%' || #{params.xm} || '%'
        </if>
        <if test="null != params.xb">
            AND ry.xb = #{params.xb} :: int
        </if>
        <if test="null != params.sfdd and '' != params.sfdd">
            AND dj.sfdd ILIKE '%' || #{params.sfdd} || '%'
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND dj.fssj :: TIMESTAMP BETWEEN #{params.timeBegin} :: TIMESTAMP AND #{params.timeEnd} :: TIMESTAMP
        </if>
        <if test="null != params.clqk and '' != params.clqk">
            AND dj.clqk = #{params.clqk}
        </if>
        <if test="null != params.djjg and '' != params.djjg">
            AND dj.djjg = #{params.djjg}
        </if>
    </select>

    <select id="exportRyDjcl" resultType="org.springblade.swzdr.excel.RyDjclExcel">
        SELECT * FROM swzdr_ry_djcl ${ew.customSqlSegment}
    </select>

    <select id="getListByRyId" resultType="org.springblade.swzdr.pojo.vo.RyDjclVO">
        SELECT
            dj.*
        FROM
            swzdr_ry_djcl dj
        WHERE
            dj.is_deleted = 0
          AND dj.ry_id = #{ryId}
        ORDER BY
            dj.create_time DESC
    </select>

</mapper>
