<?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.mdjfxj.djXj.mapper.DjXjMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="djXjResultMap" type="org.springblade.modules.mdjfxj.djXj.entity.DjXjEntity">
        <result column="id" property="id"/>
        <result column="jfmc" property="jfmc"/>
        <result column="jflx" property="jflx"/>
        <result column="jfyy" property="jfyy"/>
        <result column="jffssj" property="jffssj"/>
        <result column="jffsdd" property="jffsdd"/>
        <result column="jyaq" property="jyaq"/>
        <result column="jfdjsj" property="jfdjsj"/>
        <result column="jfsssq" property="jfsssq"/>
        <result column="jfsspcs" property="jfsspcs"/>
        <result column="jfssfj" property="jfssfj"/>
        <result column="sbzt" property="sbzt"/>
        <result column="sbsj" property="sbsj"/>
        <result column="sbdw" property="sbdw"/>
        <result column="yjzt" property="yjzt"/>
        <result column="yjsj" property="yjsj"/>
        <result column="yjdw" property="yjdw"/>
        <result column="fksj" property="fksj"/>
        <result column="fknr" property="fknr"/>
        <result column="fkr" property="fkr"/>
        <result column="fkdw" property="fkdw"/>
        <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="djr" property="djr"/>
        <result column="mdjb" property="mdjb"/>
        <result column="zp" property="zp"/>
        <result column="jfly" property="jfly"/>
        <result column="hfr" property="hfr"/>
        <result column="hfnr" property="hfnr"/>
        <result column="hfsj" property="hfsj"/>
        <result column="sfsc" property="sfsc"/>
        <result column="sfmy" property="sfmy"/>
        <result column="sfjj" property="sfjj"/>
        <result column="wtjy" property="wtjy"/>
        <result column="thjl" property="thjl"/>
        <result column="pj" property="pj"/>
        <result column="pjly" property="pjly"/>
        <result column="pjsj" property="pjsj"/>
        <result column="pjr" property="pjr"/>
        <result column="jfssjg" property="jfssjg"/>
        <result column="bz" property="bz"/>
        <result column="zzdw" property="zzdw"/>
        <result column="mdjb_v" property="mdjbV"/>
        <result column="xhfcs" property="xhfcs"/>
        <result column="yhfcs" property="yhfcs"/>
        <result column="hfzt_new" property="hfztNew"/>
        <result column="cszt" property="cszt"/>
        <result column="hfjzsj" property="hfjzsj"/>
        <result column="cscs" property="cscs"/>
        <result column="hfjzsj2" property="hfjzsj2"/>
        <result column="hfjzsj3" property="hfjzsj3"/>
        <result column="csnr" property="csnr"/>
        <result column="dychf" property="dychf"/>
        <result column="dechf" property="dechf"/>
        <result column="dschf" property="dschf"/>
        <result column="bjrdh" property="bjrdh"/>
    </resultMap>


    <select id="selectDjXjPage" resultMap="djXjResultMap">
        select
            *
        from jdrh.mdjf_dj_xj mdx
        left join jdrh.blade_dept bd on mdx.create = bd.id
        where mdx.is_deleted = 0
        and ((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR sq.ancestors LIKE '%' || #{params.deptId} || '%')
    </select>


    <select id="exportDjXj" resultType="org.springblade.modules.mdjfxj.djXj.excel.DjXjExcel">
        SELECT * FROM mdjf_dj_xj ${ew.customSqlSegment}
    </select>

    <select id="pageToApp" resultType="org.springblade.modules.mdjfxj.djXj.vo.DjXjVO">
        SELECT
        *
        FROM
        (
        SELECT
        ROW_NUMBER () OVER ( PARTITION BY md.id ORDER BY mt.create_time DESC ) maxRow,
        md.*,
        mt.tjzt,
        (SELECT COUNT(1) FROM mdjf_hf_xj hf WHERE hf.jf_id = md.id) AS hfzt
        FROM
        jdrh.mdjf_dj_xj md
        LEFT JOIN jdrh.mdjf_tj_xj mt ON mt.jf_id = md.id
        LEFT JOIN jdrh.blade_dept sq ON sq.id = md.create_dept
        WHERE
        md.is_deleted = 0
        AND ((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        ) temp
        WHERE
        temp.maxRow = 1
        <if test="params.jfmc != null and params.jfmc != ''">
            AND temp.jfmc like '%' || #{params.jfmc} || '%'
        </if>
        <if test="params.jflx != null and params.jflx != ''">
            AND temp.jflx like '%' || #{params.jflx} || '%'
        </if>
        <if test="params.tjzt == 1">
            AND (temp.tjzt IS NULL OR temp.tjzt = '调解未果')
        </if>
        <if test="params.tjzt == 2">
            AND temp.tjzt IN ('已调解') or temp.tjzt IN ('已流转')
        </if>
        <if test="params.tjzt == 3">
            AND temp.tjzt = '已调解' AND temp.hfzt_new in ('0','1')
        </if>
        <if test="null != params.sfsc and 1 == params.sfsc">
            AND temp.sfsc = 1
        </if>
        <if test="null != params.mdId and '' != params.mdId">
            AND temp.id :: text in
            <foreach collection="mdIds" item="mdId" separator="," open="(" close=")">
                #{mdId}
            </foreach>
        </if>
        ORDER BY temp.create_time DESC
    </select>

    <select id="getMdByPcs" resultType="org.springblade.modules.mdjfxj.djXj.vo.DjXjVO">
        SELECT
        *
        FROM
        (
        SELECT
        ROW_NUMBER () OVER ( PARTITION BY md.id ORDER BY mt.create_time DESC ) maxRow,
        md.*,
        mt.tjzt,
        (SELECT COUNT(1) FROM mdjf_hf_xj hf WHERE hf.jf_id = md.id) AS hfzt
        FROM
        jdrh.mdjf_dj_xj md
        LEFT JOIN jdrh.mdjf_tj_xj mt ON mt.jf_id = md.id
        LEFT JOIN jdrh.blade_dept sq ON sq.id = md.create_dept
        WHERE
        md.is_deleted = 0
        and md.jfsspcs = #{params.deptId} :: int8
        and md.jfsssq is null
        ) temp
        WHERE
        temp.maxRow = 1
        <if test="params.jfmc != null and params.jfmc != ''">
            AND temp.jfmc like '%' || #{params.jfmc} || '%'
        </if>
        <if test="params.jflx != null and params.jflx != ''">
            AND temp.jflx like '%' || #{params.jflx} || '%'
        </if>
        <if test="null != params.sfsc and 1 == params.sfsc">
            AND temp.sfsc = 1
        </if>
        <if test="null != params.mdId and '' != params.mdId">
            AND temp.id :: text in
            <foreach collection="mdIds" item="mdId" separator="," open="(" close=")">
                #{mdId}
            </foreach>
        </if>
        ORDER BY temp.create_time DESC
    </select>

    <select id="getMdyjByPcs" resultType="org.springblade.modules.mdjfxj.djXj.vo.DjXjVO">
        SELECT
        *
        FROM
        (
        SELECT
        ROW_NUMBER () OVER ( PARTITION BY md.id ORDER BY mt.create_time DESC ) maxRow,
        md.*,
        mt.tjzt,
        (SELECT COUNT(1) FROM mdjf_hf_xj hf WHERE hf.jf_id = md.id) AS hfzt
        FROM
        jdrh.mdjf_dj_xj md
        LEFT JOIN jdrh.mdjf_tj_xj mt ON mt.jf_id = md.id
        LEFT JOIN jdrh.blade_dept sq ON sq.id = md.create_dept
        WHERE
        md.is_deleted = 0
        and md.jfsspcs = #{params.deptId} :: int8
        and md.jfsssq is not null
        ) temp
        WHERE
        temp.maxRow = 1
        <if test="params.jfmc != null and params.jfmc != ''">
            AND temp.jfmc like '%' || #{params.jfmc} || '%'
        </if>
        <if test="params.jflx != null and params.jflx != ''">
            AND temp.jflx like '%' || #{params.jflx} || '%'
        </if>
        <if test="null != params.sfsc and 1 == params.sfsc">
            AND temp.sfsc = 1
        </if>
        <if test="null != params.mdId and '' != params.mdId">
            AND temp.id :: text in
            <foreach collection="mdIds" item="mdId" separator="," open="(" close=")">
                #{mdId}
            </foreach>
        </if>
        ORDER BY temp.create_time DESC
    </select>
</mapper>
