<?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.gsdrsjb.mapper.GsdrsjbMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="gsdrsjbResultMap" type="org.springblade.modules.gsdrsjb.entity.GsdrsjbEntity">
        <id column="shty_bm" property="shtyBm"/>
        <result column="zj_sj" property="zjSj"/>
        <result column="xg_sj" property="xgSj"/>
        <result column="bgjg_mc" property="bgjgMc"/>
        <result column="bg_rq" property="bgRq"/>
        <result column="bgxx_ms" property="bgxxMs"/>
        <result column="qy_dzmc" property="qyDzmc"/>
        <result column="qy_mc" property="qyMc"/>
        <result column="dwmc" property="dwmc"/>
        <result column="dwlx_ms" property="dwlxMs"/>
        <result column="fr_lxdh" property="frLxdh"/>
        <result column="fr_xm" property="frXm"/>
        <result column="fe_zjhm" property="feZjhm"/>
        <result column="frzjlx_ms" property="frzjlxMs"/>
        <result column="gd_xm" property="gdXm"/>
        <result column="gdxx_ms" property="gdxxMs"/>
        <result column="gs_czbs" property="gsCzbs"/>
        <result column="gsdjjg_ms" property="gsdjjgMs"/>
        <result column="gszchm" property="gszchm"/>
        <result column="jyfwzy" property="jyfwzy"/>
        <result column="jy_zt" property="jyZt"/>
        <result column="khmc" property="khmc"/>
        <result column="yhzh" property="yhzh"/>
        <result column="rz_bh" property="rzBh"/>
        <result column="rz_lx" property="rzLx"/>
        <result column="qyhy_ms" property="qyhyMs"/>
        <result column="ss_xzqhmc" property="ssXzqhmc"/>
        <result column="zcrq" property="zcrq"/>
        <result column="zcxs_ms" property="zcxsMs"/>
        <result column="zczj_sz" property="zczjSz"/>
        <result column="zg_rs" property="zgRs"/>
        <result column="zjbz_ms" property="zjbzMs"/>
        <result column="gssc_bm" property="gsscBm"/>
        <result column="rgxksj" property="rgxksj"/>
        <result column="tsqy_mc" property="tsqyMc"/>
    </resultMap>

    <select id="selectGsdrsjbPage" resultType="org.springblade.modules.gsdrsjb.vo.GsdrsjbVO">
        select * from (
            select *,ROW_NUMBER() OVER (PARTITION BY shty_bm ORDER BY inspection_batch desc, create_time desc) AS row_num from (
                select
                distinct
                g.zj_sj,
                g.xg_sj,
                g.bgjg_mc,
                g.bg_rq,
                g.bgxx_ms,
                g.qy_dzmc,
                g.qy_mc,
                g.dwmc,
                g.dwlx_ms,
                g.fr_lxdh,
                g.fr_xm,
                g.fe_zjhm,
                g.frzjlx_ms,
                g.gd_xm,
                g.gdxx_ms,
                g.gs_czbs,
                g.gsdjjg_ms,
                g.gszchm,
                g.jyfwzy,
                g.jy_zt,
                g.khmc,
                g.yhzh,
                g.rz_bh,
                g.rz_lx,
                g.shty_bm,
                g.qyhy_ms,
                g.ss_xzqhmc,
                g.zcrq,
                g.zcxs_ms,
                g.zczj_sz,
                g.zg_rs,
                g.zjbz_ms,
                g.gssc_bm,
                g.rgxksj,
                g.tsqy_mc,
                g.create_user,
                h.execution_unit create_dept,
                g.create_time,
                g.update_user,
                g.update_time,
                g.status,
                g.is_deleted,
                h.inspection_batch,
                h.inspection_status,
                h.company_status
                from public.gsdrsjb g
                left join public.hcrwb h
                on g.shty_bm = h.sid
                where g.is_deleted = 0
                <if test="gsdrsjb.isApp != null and gsdrsjb.isApp != ''">
                    AND inspection_status is not null
                </if>
                <if test="gsdrsjb.shtyBm != null and gsdrsjb.shtyBm != ''">
                    AND shty_bm LIKE '%' || #{gsdrsjb.shtyBm} || '%'
                </if>
                <if test="gsdrsjb.qyMc != null and gsdrsjb.qyMc != ''">
                    AND qy_mc LIKE '%' || #{gsdrsjb.qyMc} || '%'
                </if>
                <if test="gsdrsjb.dwlxMs != null and gsdrsjb.dwlxMs != ''">
                    AND dwlx_ms LIKE '%' || #{gsdrsjb.dwlxMs} || '%'
                </if>
                <if test="gsdrsjb.frXm != null and gsdrsjb.frXm != ''">
                    AND fr_xm LIKE '%' || #{gsdrsjb.frXm} || '%'
                </if>
                <if test="gsdrsjb.qyDzmc != null and gsdrsjb.qyDzmc != ''">
                    AND qy_dzmc LIKE '%' || #{gsdrsjb.qyDzmc} || '%'
                </if>
                <if test="gsdrsjb.inspectionStatus != null and gsdrsjb.inspectionStatus != ''">
                    AND inspection_status LIKE '%' || #{gsdrsjb.inspectionStatus} || '%'
                </if>
                <if test="gsdrsjb.isDistribution == null and gsdrsjb.inspectionBatch != null and gsdrsjb.inspectionBatch != ''">
                    AND inspection_batch LIKE '%' || #{gsdrsjb.inspectionBatch} || '%'
                </if>
                <if test="gsdrsjb.ssXzqhmc != null and gsdrsjb.ssXzqhmc != ''">
                    AND ss_xzqhmc LIKE '%' || #{gsdrsjb.ssXzqhmc} || '%'
                </if>
                <if test="gsdrsjb.zcrq != null and gsdrsjb.zcrq != ''">
                    AND zcrq LIKE '%' || #{gsdrsjb.zcrq} || '%'
                </if>
                <if test="gsdrsjb.isDistribution == 0 and gsdrsjb.inspectionBatch != null and gsdrsjb.inspectionBatch != ''">
                    AND NOT EXISTS(
                    select 1 from public.hcrwb h2 where inspection_batch LIKE '%' || #{gsdrsjb.inspectionBatch} || '%'
                    and g.shty_bm = h2.sid
                    )
                </if>
                <if test="gsdrsjb.isDistribution == 1 and gsdrsjb.inspectionBatch != null and gsdrsjb.inspectionBatch != ''">
                    AND EXISTS(
                    select 1 from public.hcrwb h2 where inspection_batch LIKE '%' || #{gsdrsjb.inspectionBatch} || '%'
                    and g.shty_bm = h2.sid
                    )
                </if>
            ) t1
        ) t2
        where row_num = 1
        order by create_time

    </select>

    <select id="exportGsdrsjb" resultType="org.springblade.modules.gsdrsjb.excel.GsdrsjbExcel">
        SELECT * FROM gsdrsjb ${ew.customSqlSegment}
    </select>

</mapper>
