<?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="com.brilliance.isc.mda.dao.GcdMapper">

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Gcd">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="ownref" column="ownref" jdbcType="VARCHAR"/>
        <result property="pntinr" column="pntinr" jdbcType="VARCHAR"/>
        <result property="pnttyp" column="pnttyp" jdbcType="VARCHAR"/>
        <result property="nam" column="nam" jdbcType="VARCHAR"/>
        <result property="credat" column="credat" jdbcType="DATE"/>
        <result property="clsdat" column="clsdat" jdbcType="DATE"/>
        <result property="opndat" column="opndat" jdbcType="DATE"/>
        <result property="newexpdat" column="newexpdat" jdbcType="DATE"/>
        <result property="ownusr" column="ownusr" jdbcType="VARCHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="clmtyp" column="clmtyp" jdbcType="VARCHAR"/>
        <result property="clmctl" column="clmctl" jdbcType="VARCHAR"/>
        <result property="clmdat" column="clmdat" jdbcType="DATE"/>
        <result property="cannowflg" column="cannowflg" jdbcType="VARCHAR"/>
        <result property="msgdat" column="msgdat" jdbcType="DATE"/>
        <result property="payrol" column="payrol" jdbcType="VARCHAR"/>
        <result property="docprbrol" column="docprbrol" jdbcType="VARCHAR"/>
        <result property="demstat" column="demstat" jdbcType="VARCHAR"/>
        <result property="dipreiday" column="dipreiday" jdbcType="INTEGER"/>
        <result property="dipreiflg1" column="dipreiflg1" jdbcType="VARCHAR"/>
        <result property="dipreiflg3" column="dipreiflg3" jdbcType="VARCHAR"/>
        <result property="paydat" column="paydat" jdbcType="DATE"/>
        <result property="totamt" column="totamt" jdbcType="DECIMAL"/>
        <result property="totcur" column="totcur" jdbcType="VARCHAR"/>
        <result property="etyextkey" column="etyextkey" jdbcType="VARCHAR"/>
        <result property="frepayflg" column="frepayflg" jdbcType="VARCHAR"/>
        <result property="bchkeyinr" column="bchkeyinr" jdbcType="VARCHAR"/>
        <result property="branchinr" column="branchinr" jdbcType="VARCHAR"/>
        <result property="prechkdat" column="prechkdat" jdbcType="DATE"/>
        <result property="ischktyp" column="ischktyp" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,ownref,pntinr,pnttyp,nam,credat,clsdat,opndat,newexpdat,ownusr,ver,clmtyp,clmctl,clmdat,cannowflg,msgdat,payrol,docprbrol,demstat,dipreiday,dipreiflg1,dipreiflg3,paydat,totamt,totcur,etyextkey,frepayflg,bchkeyinr,branchinr,prechkdat,ischktyp
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List" />
        from gcd
        where inr = #{inr,jdbcType=VARCHAR}
    </select>

    <delete id="deleteByPrimaryKey">
        delete from gcd
        where inr = #{inr,jdbcType=VARCHAR}
    </delete>

    <insert id="insert">
        insert into gcd (<include refid="Base_Column_List" />)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=VARCHAR},
            #{ownref,jdbcType=VARCHAR},
            #{pntinr,jdbcType=VARCHAR},
            #{pnttyp,jdbcType=VARCHAR},
            #{nam,jdbcType=VARCHAR},
            #{credat,jdbcType=DATE},
            #{clsdat,jdbcType=DATE},
            #{opndat,jdbcType=DATE},
            #{newexpdat,jdbcType=DATE},
            #{ownusr,jdbcType=VARCHAR},
            #{ver,jdbcType=VARCHAR},
            #{clmtyp,jdbcType=VARCHAR},
            #{clmctl,jdbcType=VARCHAR},
            #{clmdat,jdbcType=DATE},
            #{cannowflg,jdbcType=VARCHAR},
            #{msgdat,jdbcType=DATE},
            #{payrol,jdbcType=VARCHAR},
            #{docprbrol,jdbcType=VARCHAR},
            #{demstat,jdbcType=VARCHAR},
            #{dipreiday,jdbcType=INTEGER},
            #{dipreiflg1,jdbcType=VARCHAR},
            #{dipreiflg3,jdbcType=VARCHAR},
            #{paydat,jdbcType=DATE},
            #{totamt,jdbcType=DECIMAL},
            #{totcur,jdbcType=VARCHAR},
            #{etyextkey,jdbcType=VARCHAR},
            #{frepayflg,jdbcType=VARCHAR},
            #{bchkeyinr,jdbcType=VARCHAR},
            #{branchinr,jdbcType=VARCHAR},
            #{prechkdat,jdbcType=DATE},
            #{ischktyp,jdbcType=VARCHAR},
        </trim>
    </insert>

    <insert id="insertSelective">
        insert into gcd
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inr != null">inr,</if>
            <if test="ownref != null">ownref,</if>
            <if test="pntinr != null">pntinr,</if>
            <if test="pnttyp != null">pnttyp,</if>
            <if test="nam != null">nam,</if>
            <if test="credat != null">credat,</if>
            <if test="clsdat != null">clsdat,</if>
            <if test="opndat != null">opndat,</if>
            <if test="newexpdat != null">newexpdat,</if>
            <if test="ownusr != null">ownusr,</if>
            <if test="ver != null">ver,</if>
            <if test="clmtyp != null">clmtyp,</if>
            <if test="clmctl != null">clmctl,</if>
            <if test="clmdat != null">clmdat,</if>
            <if test="cannowflg != null">cannowflg,</if>
            <if test="msgdat != null">msgdat,</if>
            <if test="payrol != null">payrol,</if>
            <if test="docprbrol != null">docprbrol,</if>
            <if test="demstat != null">demstat,</if>
            <if test="dipreiday != null">dipreiday,</if>
            <if test="dipreiflg1 != null">dipreiflg1,</if>
            <if test="dipreiflg3 != null">dipreiflg3,</if>
            <if test="paydat != null">paydat,</if>
            <if test="totamt != null">totamt,</if>
            <if test="totcur != null">totcur,</if>
            <if test="etyextkey != null">etyextkey,</if>
            <if test="frepayflg != null">frepayflg,</if>
            <if test="bchkeyinr != null">bchkeyinr,</if>
            <if test="branchinr != null">branchinr,</if>
            <if test="prechkdat != null">prechkdat,</if>
            <if test="ischktyp != null">ischktyp,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inr != null">#{inr,jdbcType=VARCHAR}, </if>
            <if test="ownref != null">#{ownref,jdbcType=VARCHAR}, </if>
            <if test="pntinr != null">#{pntinr,jdbcType=VARCHAR}, </if>
            <if test="pnttyp != null">#{pnttyp,jdbcType=VARCHAR}, </if>
            <if test="nam != null">#{nam,jdbcType=VARCHAR}, </if>
            <if test="credat != null">#{credat,jdbcType=DATE}, </if>
            <if test="clsdat != null">#{clsdat,jdbcType=DATE}, </if>
            <if test="opndat != null">#{opndat,jdbcType=DATE}, </if>
            <if test="newexpdat != null">#{newexpdat,jdbcType=DATE}, </if>
            <if test="ownusr != null">#{ownusr,jdbcType=VARCHAR}, </if>
            <if test="ver != null">#{ver,jdbcType=VARCHAR}, </if>
            <if test="clmtyp != null">#{clmtyp,jdbcType=VARCHAR}, </if>
            <if test="clmctl != null">#{clmctl,jdbcType=VARCHAR}, </if>
            <if test="clmdat != null">#{clmdat,jdbcType=DATE}, </if>
            <if test="cannowflg != null">#{cannowflg,jdbcType=VARCHAR}, </if>
            <if test="msgdat != null">#{msgdat,jdbcType=DATE}, </if>
            <if test="payrol != null">#{payrol,jdbcType=VARCHAR}, </if>
            <if test="docprbrol != null">#{docprbrol,jdbcType=VARCHAR}, </if>
            <if test="demstat != null">#{demstat,jdbcType=VARCHAR}, </if>
            <if test="dipreiday != null">#{dipreiday,jdbcType=INTEGER}, </if>
            <if test="dipreiflg1 != null">#{dipreiflg1,jdbcType=VARCHAR}, </if>
            <if test="dipreiflg3 != null">#{dipreiflg3,jdbcType=VARCHAR}, </if>
            <if test="paydat != null">#{paydat,jdbcType=DATE}, </if>
            <if test="totamt != null">#{totamt,jdbcType=DECIMAL}, </if>
            <if test="totcur != null">#{totcur,jdbcType=VARCHAR}, </if>
            <if test="etyextkey != null">#{etyextkey,jdbcType=VARCHAR}, </if>
            <if test="frepayflg != null">#{frepayflg,jdbcType=VARCHAR}, </if>
            <if test="bchkeyinr != null">#{bchkeyinr,jdbcType=VARCHAR}, </if>
            <if test="branchinr != null">#{branchinr,jdbcType=VARCHAR}, </if>
            <if test="prechkdat != null">#{prechkdat,jdbcType=DATE}, </if>
            <if test="ischktyp != null">#{ischktyp,jdbcType=VARCHAR}, </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update gcd
        <set>
            <if test="ownref != null">ownref = #{ownref,jdbcType=VARCHAR},</if>
            <if test="pntinr != null">pntinr = #{pntinr,jdbcType=VARCHAR},</if>
            <if test="pnttyp != null">pnttyp = #{pnttyp,jdbcType=VARCHAR},</if>
            <if test="nam != null">nam = #{nam,jdbcType=VARCHAR},</if>
            <if test="credat != null">credat = #{credat,jdbcType=DATE},</if>
            <if test="clsdat != null">clsdat = #{clsdat,jdbcType=DATE},</if>
            <if test="opndat != null">opndat = #{opndat,jdbcType=DATE},</if>
            <if test="newexpdat != null">newexpdat = #{newexpdat,jdbcType=DATE},</if>
            <if test="ownusr != null">ownusr = #{ownusr,jdbcType=VARCHAR},</if>
            <if test="ver != null">ver = #{ver,jdbcType=VARCHAR},</if>
            <if test="clmtyp != null">clmtyp = #{clmtyp,jdbcType=VARCHAR},</if>
            <if test="clmctl != null">clmctl = #{clmctl,jdbcType=VARCHAR},</if>
            <if test="clmdat != null">clmdat = #{clmdat,jdbcType=DATE},</if>
            <if test="cannowflg != null">cannowflg = #{cannowflg,jdbcType=VARCHAR},</if>
            <if test="msgdat != null">msgdat = #{msgdat,jdbcType=DATE},</if>
            <if test="payrol != null">payrol = #{payrol,jdbcType=VARCHAR},</if>
            <if test="docprbrol != null">docprbrol = #{docprbrol,jdbcType=VARCHAR},</if>
            <if test="demstat != null">demstat = #{demstat,jdbcType=VARCHAR},</if>
            <if test="dipreiday != null">dipreiday = #{dipreiday,jdbcType=INTEGER},</if>
            <if test="dipreiflg1 != null">dipreiflg1 = #{dipreiflg1,jdbcType=VARCHAR},</if>
            <if test="dipreiflg3 != null">dipreiflg3 = #{dipreiflg3,jdbcType=VARCHAR},</if>
            <if test="paydat != null">paydat = #{paydat,jdbcType=DATE},</if>
            <if test="totamt != null">totamt = #{totamt,jdbcType=DECIMAL},</if>
            <if test="totcur != null">totcur = #{totcur,jdbcType=VARCHAR},</if>
            <if test="etyextkey != null">etyextkey = #{etyextkey,jdbcType=VARCHAR},</if>
            <if test="frepayflg != null">frepayflg = #{frepayflg,jdbcType=VARCHAR},</if>
            <if test="bchkeyinr != null">bchkeyinr = #{bchkeyinr,jdbcType=VARCHAR},</if>
            <if test="branchinr != null">branchinr = #{branchinr,jdbcType=VARCHAR},</if>
            <if test="prechkdat != null">prechkdat = #{prechkdat,jdbcType=DATE},</if>
            <if test="ischktyp != null">ischktyp = #{ischktyp,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update gcd
        <set>
            ownref = #{ownref,jdbcType=VARCHAR},
            pntinr = #{pntinr,jdbcType=VARCHAR},
            pnttyp = #{pnttyp,jdbcType=VARCHAR},
            nam = #{nam,jdbcType=VARCHAR},
            credat = #{credat,jdbcType=DATE},
            clsdat = #{clsdat,jdbcType=DATE},
            opndat = #{opndat,jdbcType=DATE},
            newexpdat = #{newexpdat,jdbcType=DATE},
            ownusr = #{ownusr,jdbcType=VARCHAR},
            ver = #{ver,jdbcType=VARCHAR},
            clmtyp = #{clmtyp,jdbcType=VARCHAR},
            clmctl = #{clmctl,jdbcType=VARCHAR},
            clmdat = #{clmdat,jdbcType=DATE},
            cannowflg = #{cannowflg,jdbcType=VARCHAR},
            msgdat = #{msgdat,jdbcType=DATE},
            payrol = #{payrol,jdbcType=VARCHAR},
            docprbrol = #{docprbrol,jdbcType=VARCHAR},
            demstat = #{demstat,jdbcType=VARCHAR},
            dipreiday = #{dipreiday,jdbcType=INTEGER},
            dipreiflg1 = #{dipreiflg1,jdbcType=VARCHAR},
            dipreiflg3 = #{dipreiflg3,jdbcType=VARCHAR},
            paydat = #{paydat,jdbcType=DATE},
            totamt = #{totamt,jdbcType=DECIMAL},
            totcur = #{totcur,jdbcType=VARCHAR},
            etyextkey = #{etyextkey,jdbcType=VARCHAR},
            frepayflg = #{frepayflg,jdbcType=VARCHAR},
            bchkeyinr = #{bchkeyinr,jdbcType=VARCHAR},
            branchinr = #{branchinr,jdbcType=VARCHAR},
            prechkdat = #{prechkdat,jdbcType=DATE},
            ischktyp = #{ischktyp,jdbcType=VARCHAR},
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <delete id="deleteByIds">
        delete from act where inr in
        <foreach item="item" index="index" collection="collection" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <update id="dyncUpdate">
        update gcd
        <set>
            <if test="module.ownref != null">ownref = #{module.ownref,jdbcType=VARCHAR},</if>
            <if test="module.pntinr != null">pntinr = #{module.pntinr,jdbcType=VARCHAR},</if>
            <if test="module.pnttyp != null">pnttyp = #{module.pnttyp,jdbcType=VARCHAR},</if>
            <if test="module.nam != null">nam = #{module.nam,jdbcType=VARCHAR},</if>
            <if test="module.credat != null">credat = #{module.credat,jdbcType=DATE},</if>
            <if test="module.clsdat != null">clsdat = #{module.clsdat,jdbcType=DATE},</if>
            <if test="module.opndat != null">opndat = #{module.opndat,jdbcType=DATE},</if>
            <if test="module.newexpdat != null">newexpdat = #{module.newexpdat,jdbcType=DATE},</if>
            <if test="module.ownusr != null">ownusr = #{module.ownusr,jdbcType=VARCHAR},</if>
            <if test="module.ver != null">ver = #{module.ver,jdbcType=VARCHAR},</if>
            <if test="module.clmtyp != null">clmtyp = #{module.clmtyp,jdbcType=VARCHAR},</if>
            <if test="module.clmctl != null">clmctl = #{module.clmctl,jdbcType=VARCHAR},</if>
            <if test="module.clmdat != null">clmdat = #{module.clmdat,jdbcType=DATE},</if>
            <if test="module.cannowflg != null">cannowflg = #{module.cannowflg,jdbcType=VARCHAR},</if>
            <if test="module.msgdat != null">msgdat = #{module.msgdat,jdbcType=DATE},</if>
            <if test="module.payrol != null">payrol = #{module.payrol,jdbcType=VARCHAR},</if>
            <if test="module.docprbrol != null">docprbrol = #{module.docprbrol,jdbcType=VARCHAR},</if>
            <if test="module.demstat != null">demstat = #{module.demstat,jdbcType=VARCHAR},</if>
            <if test="module.dipreiday != null">dipreiday = #{module.dipreiday,jdbcType=INTEGER},</if>
            <if test="module.dipreiflg1 != null">dipreiflg1 = #{module.dipreiflg1,jdbcType=VARCHAR},</if>
            <if test="module.dipreiflg3 != null">dipreiflg3 = #{module.dipreiflg3,jdbcType=VARCHAR},</if>
            <if test="module.paydat != null">paydat = #{module.paydat,jdbcType=DATE},</if>
            <if test="module.totamt != null">totamt = #{module.totamt,jdbcType=DECIMAL},</if>
            <if test="module.totcur != null">totcur = #{module.totcur,jdbcType=VARCHAR},</if>
            <if test="module.etyextkey != null">etyextkey = #{module.etyextkey,jdbcType=VARCHAR},</if>
            <if test="module.frepayflg != null">frepayflg = #{module.frepayflg,jdbcType=VARCHAR},</if>
            <if test="module.bchkeyinr != null">bchkeyinr = #{module.bchkeyinr,jdbcType=VARCHAR},</if>
            <if test="module.branchinr != null">branchinr = #{module.branchinr,jdbcType=VARCHAR},</if>
            <if test="module.prechkdat != null">prechkdat = #{module.prechkdat,jdbcType=DATE},</if>
            <if test="module.ischktyp != null">ischktyp = #{module.ischktyp,jdbcType=VARCHAR},</if>
        </set>
        ${conditions}
    </update>

    <delete id="dyncDelete">
        ${sql}
    </delete>

    <select id="dyncRead" resultMap="BaseResultMap">
        ${sql}
    </select>

    <select id="getGcdCount" resultType="java.lang.Integer">
        select count(*) from GCD WHERE PNTINR = #{inr} AND PNTTYP = 'GID'
    </select>

    <select id="getGcdOwnrefList" resultType="java.lang.String">
        select OWNREF from GCD WHERE PNTINR = #{inr} AND PNTTYP = 'GID'
    </select>

    <select id="getSptCount" resultType="java.lang.Integer">
       SELECT COUNT(*) FROM SPT WHERE OWNREF LIKE concat(#{ownref},'%') AND OBJTYP ='GCD'
            AND OWNREF NOT IN (SELECT OWNREF FROM GCD WHERE PNTINR=#{inr})
    </select>

    <select id="getSptOwnrefList" resultType="java.lang.String">
       SELECT OWNREF FROM SPT WHERE OWNREF LIKE concat(#{ownref},'%') AND OBJTYP ='GCD'
            AND OWNREF NOT IN (SELECT OWNREF FROM GCD WHERE PNTINR=#{inr})
    </select>

    <select id="getActPendingClaimCount" resultType="java.lang.Integer">
         select count(*) from GCD WHERE PNTINR =  #{inr} AND PNTTYP = 'GID' AND CLSDAT IS NULL
    </select>

    <select id="getGcdByGidinr" resultType="com.brilliance.isc.vo.lg.GcdInfoVo">
        select
        <if test=' opentyp=="GJBH" '>
           gcd.id id, gcd.inr ,gcd.ownref, (@i:=@i+1) as gcdnum,gcd.ownusr,#{gidinr} gidinr,gcd.credat , 'GCBH' opentyp  ,'GJBH' gidopentyp,
           (select ownref from gid where gid.inr=gcd.pntinr) gidownref, cbb1.cur cur,
            case when cbb6.amt is not null then (cbb1.amt + cbb6.amt) else cbb1.amt end   gcdamt,
             case when cbb3.amt is not null then (cbb3.amt +cbb2.amt ) else cbb2.amt end  gidamt1,
             case when cbb5.amt is not null then (cbb5.amt +cbb4.amt ) else cbb4.amt end  gcdopnamt,
            case when gcd.clsdat is null then 'N' else 'Y' end  clearsta,(case
            when gcd.clsdat is not  null
            then 'CLS'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITSET', 'GITPSE')
            and relflg = 'R') > 0 then
            'SET'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITCRJ', 'GITPCJ')
            and relflg = 'R') > 0 then
            'CRJ'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITCRJ', 'GITPCJ', 'GITSET', 'GITPSE')
            and relflg = 'R') = 0 then
            'TBC'
            when gcd.clsdat is  not null
            then 'CLS'
            end) claimsta,
            (select count(*) from trn trn  where trn.objinr = gcd.inr
            and trn.objtyp = 'GCD' and inifrm = 'GITPCR' and trn.RELFLG != 'R') clswatcnt
            from gcd gcd
            left join cbb cbb1 on cbb1.objtyp = 'GCD' and cbb1.objinr = gcd.inr and cbb1.cbc = 'MAXSUM' and cbb1.extid = 'AMT1' and cbb1.enddat = '2299-12-31'
            left join cbb cbb2 on cbb2.objtyp = 'GID' and cbb2.objinr = #{gidinr, jdbcType=VARCHAR} and cbb2.cbc = 'OPN' and cbb2.extid = 'AMT1' and cbb2.enddat = '2299-12-31'
            left join cbb cbb3 on cbb3.objtyp = 'GID' and cbb3.objinr = #{gidinr, jdbcType=VARCHAR} and cbb3.cbc = 'OPN' and cbb3.extid = 'AMT2' and cbb3.enddat = '2299-12-31'
            left join cbb cbb4 on cbb4.objtyp = 'GCD' and cbb4.objinr = gcd.inr and cbb4.cbc = 'OPN' and cbb4.extid = 'AMT1' and cbb4.enddat = '2299-12-31'
            left join cbb cbb5 on cbb5.objtyp = 'GCD' and cbb5.objinr = gcd.inr and cbb5.cbc = 'OPN' and cbb5.extid = 'AMT2' and cbb5.enddat = '2299-12-31'
            left join cbb cbb6 on cbb6.objtyp = 'GCD' and cbb6.objinr = gcd.inr and cbb6.cbc = 'MAXSUM' and cbb6.extid = 'AMT2' and cbb6.enddat = '2299-12-31'
            ,(select @i:=0)t
            where pntinr = #{gidinr, jdbcType=VARCHAR}
            and cbb1.amt is not null
            order by claimsta desc,clearsta, gcd.id
        </if>
        <if test=' opentyp!="GJBH" '>
           ncd.id id , ncd.inr ,ncd.ownref,(@i:=@i+1) as gcdnum,ncd.ownusr,#{gidinr} gidinr,ncd.credat ,'NCBH' opentyp ,'GNBH' gidopentyp,(select ownref from nid where nid.inr=ncd.pntinr) "gidownref" ,
            cbb1.cur cur, case when cbb6.amt is not null then (cbb1.amt + cbb6.amt) else cbb1.amt end  gcdamt,
            case when cbb3.amt is not null then (cbb3.amt +cbb2.amt )  else cbb2.amt end   gidamt1,
            case when cbb5.amt is not null then (cbb5.amt +cbb4.amt ) else cbb4.amt end  gcdopnamt,
            case when ncd.clsdat is null then 'N' else 'Y' end  clearsta,
            (case
            when ncd.clsdat is not  null
            then 'CLS'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITSET', 'NITPSE')
            and relflg = 'R') > 0 then
            'SET'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITCRJ', 'NITPCJ')
            and relflg = 'R') > 0 then
            'CRJ'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITCRJ', 'NITPCJ', 'NITSET', 'NITPSE')
            and relflg = 'R') = 0 then
            'TBC'
            end) claimsta,
            (select count(*) from trn trn  where trn.objinr = ncd.inr
            and trn.objtyp = 'NCD' and inifrm = 'NITPCR' and trn.RELFLG != 'R') clswatcnt
            from ncd ncd
            left join cbb cbb1 on cbb1.objtyp = 'NCD' and cbb1.objinr = ncd.inr and cbb1.cbc = 'MAXSUM' and cbb1.extid = 'AMT1' and cbb1.enddat = '2299-12-31'
            left join cbb cbb2 on cbb2.objtyp = 'NID' and cbb2.objinr = #{gidinr, jdbcType=VARCHAR} and cbb2.cbc = 'OPN' and cbb2.extid = 'AMT1' and cbb2.enddat = '2299-12-31'
            left join cbb cbb3 on cbb3.objtyp = 'NID' and cbb3.objinr = #{gidinr, jdbcType=VARCHAR} and cbb3.cbc = 'OPN' and cbb3.extid = 'AMT2' and cbb3.enddat = '2299-12-31'
            left join cbb cbb4 on cbb4.objtyp = 'NCD' and cbb4.objinr = ncd.inr and cbb4.cbc = 'OPN' and cbb4.extid = 'AMT1' and cbb4.enddat = '2299-12-31'
            left join cbb cbb5 on cbb5.objtyp = 'NCD' and cbb5.objinr = ncd.inr and cbb5.cbc = 'OPN' and cbb5.extid = 'AMT2' and cbb5.enddat = '2299-12-31'
            left join cbb cbb6 on cbb6.objtyp = 'NCD' and cbb6.objinr = ncd.inr and cbb6.cbc = 'MAXSUM' and cbb6.extid = 'AMT2' and cbb6.enddat = '2299-12-31'
            ,(select @i:=0)t
            where pntinr = #{gidinr, jdbcType=VARCHAR}
            and cbb1.amt is not null
            order by  claimsta desc,clearsta, ncd.id
        </if>

    </select>
    <select id="querySingleGcdInfo" resultType="java.util.Map">
    select IFNULL(gcd.inr,'') inr, IFNULL(gcd.CLSDAT,'') clsdat, IFNULL(gcd.OPNDAT,'') opndat,  IFNULL(cbb.amt,'') cbbamt,'GJBH' opentyp
	from gcd gcd
	left join cbb on cbb.objtyp = 'GCD' and cbb.objinr = gcd.inr
	and cbb.cbc = 'OPN' and EXTID  = 'AMT1' and ENDDAT = '2299-12-31'
	where gcd.inr = #{inr}
    </select>
    <select id="querySingleNcdInfo" resultType="java.util.Map">
    select  IFNULL(ncd.inr,'') inr, IFNULL(ncd.CLSDAT,'') clsdat, IFNULL(ncd.OPNDAT,'') opndat,  IFNULL(cbb.amt,'') cbbamt,'GNBH' opentyp
	from ncd ncd
	left join cbb on cbb.objtyp = 'NCD' and cbb.objinr = ncd.inr
	and cbb.cbc = 'OPN' and EXTID  = 'AMT1' and ENDDAT = '2299-12-31'
	where ncd.inr = #{inr}
    </select>

    <select id="selectByAnyref" resultType="com.brilliance.isc.bo.Gcd">
        select
        <include refid="Base_Column_List"/>
        from gcd WHERE INR = ( select distinct OBJINR from PTS where OBJTYP = #{objtyp} and REF = #{ref}
        <if test="rol != null and rol != ''">
            and rol = #{rol}
        </if>
        )
    </select>

    <select id="selectByWhere" resultType="com.brilliance.isc.bo.Gcd">
        select
        <include refid="Base_Column_List"/>
        from gcd WHERE 1=1
        <if test="pntinr != null and pntinr != ''"> and pntinr = #{pntinr,jdbcType=VARCHAR}</if>
        <if test="cannowflg != null and cannowflg != ''"> and cannowflg = #{cannowflg,jdbcType=VARCHAR}</if>
        <if test="ownref != null and ownref != ''"> and ownref = #{ownref,jdbcType=VARCHAR}</if>
        limit 1
    </select>

    <select id="queryInfgcd" resultType="com.brilliance.isc.vo.lg.InfgcdDetailVo">
    select * from (
        select
        gcd.INR inr,
        gcd.OWNREF gcdownref,
        gcd.OWNUSR gcdownusr,
        gcd.OPNDAT gcdopndat,
        gcd.CLSDAT gcdclsdat,
        apl.EXTKEY gcdaplextkey,
        apl.NAM gcdaplnam,
        ben.EXTKEY gcdbenextkey,
        ben.NAM gcdbennam,
        maxsum.CUR gcdmaxcur,
        case when maxsum1.amt is not null then (maxsum.amt + maxsum1.amt)
        else  maxsum.AMT end  gcdmaxamt,
        opn.CUR gcdopncur,
        case when opn1.amt is not null then (opn.amt +opn1.amt )
        else opn.AMT  end gcdopnamt,
        'GCBH' opentyp,
        'GJBH' gidopentyp
        from gcd
        left join gid on gid.INR = gcd.PNTINR
        left join pts apl on apl.OBJTYP = 'GCD' and apl.OBJINR = gcd.INR and apl.ROL = 'APL'
        left join pts ben on ben.OBJTYP = 'GCD' and ben.OBJINR = gcd.INR and ben.ROL = 'BEN'
        left join cbb maxsum on maxsum.OBJTYP = 'GCD' and maxsum.OBJINR = gcd.INR and maxsum.CBC = 'MAXSUM' and maxsum.EXTID = 'AMT1' and maxsum.enddat = '2299-12-31'
        left join cbb maxsum1 on maxsum1.OBJTYP = 'GCD' and maxsum1.OBJINR = gcd.INR and maxsum1.CBC = 'MAXSUM' and maxsum1.EXTID = 'AMT2' and maxsum1.enddat = '2299-12-31'
        left join cbb opn on opn.OBJTYP = 'GID' and opn.OBJINR = gid.INR and opn.CBC = 'OPN' and opn.EXTID = 'AMT1' and opn.enddat = '2299-12-31'
        left join cbb opn1 on opn1.OBJTYP = 'GID' and opn1.OBJINR = gid.INR and opn1.CBC = 'OPN' and opn1.EXTID = 'AMT2'  and opn1.enddat = '2299-12-31'
        left join trn on trn.objinr = gcd.inr and trn.objtyp = 'GCD' and trn.inifrm in('GITCRQ','GETCRQ')
        where maxsum.amt is not null
        and trn.relflg = 'R'
        <if test="branch != '1000' and bchLst != null and bchLst.size() >0 ">
            and gcd.bchkeyinr in
            <foreach item="bch" collection="bchLst" open="(" separator="," close=")">
                #{bch.inr}
            </foreach>

        </if> <!--开立/通知-->
        <if test="hndtyps != null and hndtyps.length >0 ">
            and gid.hndtyp in
            <foreach item="hndtypitem" collection="hndtyps" open="(" separator="," close=")">
                #{hndtypitem}
            </foreach>
        </if>
        <if test="nam != null and nam != ''">
            and UPPER(GCD.NAM) LIKE concat(concat('%', UPPER(#{nam})), '%')
        </if>
        <if test="seaownref != null and seaownref != ''">
            AND UPPER(GCD.OWNREF) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
        </if>

        <if test="opndatfrom != null ">
            AND GCD.CREDAT &gt;= #{opndatfrom}
        </if>

        <if test="opndatto != null">
            AND GCD.CREDAT &lt;= #{opndatto}
        </if>

        <if test="seacur != null and seacur != ''">
            AND EXISTS (
            select 1 from CBB INFCONCBS where
            UPPER(INFCONCBS.CUR ) = UPPER(#{seacur})
            <if test="seaamt != null and seaamt != ''">
                AND INFCONCBS.AMT = #{seaamt}
            </if>
                AND (INFCONCBS.OBJTYP = 'GCD'
                AND INFCONCBS.OBJINR = GCD.INR
                AND (INFCONCBS.BEGDAT &lt;= str_to_date('2299-12-30',  '%Y-%m-%d')
                AND INFCONCBS.ENDDAT &gt; str_to_date('2299-12-30',  '%Y-%m-%d'))
                AND INFCONCBS.EXTID IN (' ', 'AMT1')
                AND INFCONCBS.CBC IN ('MAXSUM', 'PRESUM', 'NOMSUM', 'OPN'))
            )
        </if>
        <if test="searol != null and searol != ''">
            AND EXISTS (
            select 1 from PTS where
            GCD.INR = PTS.OBJINR
            AND 'GCD' = PTS.OBJTYP
            AND PTS.ROL = #{searol}
            )
        </if>
        <if test="ptyExtkey != null and ptyExtkey != ''">
            and exists (
            select 1 from PTS, PTA where
            PTS.PTAINR = PTA.INR
            AND GCD.INR = PTS.OBJINR
            AND 'GCD' = PTS.OBJTYP
            <!--     if rol is not empty we use rol,default use 'APL' or 'BEN' -->
            <choose>
                <when test="searol != null and searol != ''" >AND PTS.ROL =  #{searol}</when>
                <otherwise>AND PTS.ROL IN ('BEN','APL') </otherwise>
            </choose>

            AND UPPER(PTA.PTYINR) IN (SELECT INR FROM PTY WHERE EXTKEY = UPPER(#{ptyExtkey}))
            )
        </if>

        <if test="seapty != null and seapty != ''">
            and exists (
            select 1 from PTS, PTA where
            PTS.PTAINR = PTA.INR
            AND GCD.INR = PTS.OBJINR
            AND 'GCD' = PTS.OBJTYP
            AND (
            UPPER(PTA.PTYEXTKEY) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            or UPPER(PTA.BIC) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            or UPPER(PTA.NAM) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            )
            /*if rol is not empty we use rol,default use 'APL'or 'BEN'*/
            <choose>
                <when test="searol != null and searol != ''" >AND PTS.ROL =  #{searol}</when>
                <otherwise>AND PTS.ROL IN ('BEN','APL') </otherwise>
            </choose>
            )
        </if>

        <if test="seagtyp != null and seagtyp != ''">
            AND gid.gartyp = #{seagtyp}
        </if>

        <if test="searef != null and searef != ''">
            and (UPPER(apl.REF) LIKE concat(concat('%', UPPER(#{searef})), '%')
            or UPPER(ben.REF) LIKE concat(concat('%', UPPER(#{searef})), '%'))
        </if>

        <if test="seasta != null and seasta != ''">
            <if test="seasta == 'O'.toString()">
                AND GCD.OPNDAT IS NOT NULL
                AND GCD.CLSDAT IS NULL
            </if>
            <if test="seasta == 'C'.toString()">
                AND GCD.CLSDAT IS NOT NULL
            </if>
        </if>
        <!--是否对外担保-->
        <if test="fingua != null and fingua != ''">
            AND GID.FINGUA = #{fingua}
        </if>

        <!-- 跨境人民币保函 -->
        <if test="cmtflg != null and cmtflg != ''">
            AND GID.CMTFLG = #{cmtflg}
        </if>
        <!-- 分离式保函 -->
        <if test="fenlishi != null ">
            AND GID.FENLISHI = #{fenlishi}
        </if>
        <if test="seaauthorized != null  and seaauthorized != ''">
            <if test="seaauthorized == 'Y'.toString()">
                AND NOT EXISTS (
                SELECT 1 FROM TRN WHERE
                TRN.OBJINR= GCD.INR
                AND TRN.OBJTYP='GCD'
                AND TRN.RELFLG in ('W','C')
                )
            </if>
            <if test="seaauthorized == 'N'.toString()">
                AND EXISTS (
                SELECT 1 FROM TRN WHERE
                TRN.OBJINR= GCD.INR
                AND TRN.OBJTYP='GCD'
                AND TRN.RELFLG in ('W','C')
                )
            </if>
        </if>

        <if test="isGuarantee != null and isGuarantee != ''">
            AND substring(gcd.OWNREF,1,2) = #{isGuarantee}
        </if>
        union all
        select
        ncd.INR inr,
        ncd.OWNREF gcdownref,
        ncd.OWNUSR gcdownusr,
        ncd.OPNDAT gcdopndat,
        ncd.CLSDAT gcdclsdat,
        apl.EXTKEY gcdaplextkey,
        apl.NAM gcdaplnam,
        ben.EXTKEY gcdbenextkey,
        ben.NAM gcdbennam,
        maxsum.CUR gcdmaxcur,
        case when maxsum1.amt is not null then (maxsum.amt + maxsum1.amt)
        else  maxsum.AMT end gcdmaxamt,
        opn.CUR gcdopncur,
        case when opn1.amt is not null then (opn.amt +opn1.amt )
        else opn.AMT  end gcdopnamt,
        'NCBH' opentyp,
        'GNBH' gidopentyp
        from ncd
        left join nid on nid.INR = ncd.PNTINR
        left join pts apl on apl.OBJTYP = 'NCD' and apl.OBJINR = ncd.INR and apl.ROL = 'APL'
        left join pts ben on ben.OBJTYP = 'NCD' and ben.OBJINR = ncd.INR and ben.ROL = 'BEN'
        left join cbb maxsum on maxsum.OBJTYP = 'NCD' and maxsum.OBJINR = ncd.INR and maxsum.CBC = 'MAXSUM' and maxsum.EXTID = 'AMT1' and maxsum.enddat = '2299-12-31'
        left join cbb maxsum1 on maxsum1.OBJTYP = 'NCD' and maxsum1.OBJINR = ncd.INR and maxsum1.CBC = 'MAXSUM' and maxsum1.EXTID = 'AMT2' and maxsum1.enddat = '2299-12-31'
        left join cbb opn on opn.OBJTYP = 'NID' and opn.OBJINR = nid.INR and opn.CBC = 'OPN' and opn.EXTID = 'AMT1'  and opn.enddat = '2299-12-31'
        left join cbb opn1 on opn1.OBJTYP = 'NID' and opn1.OBJINR = nid.INR and opn1.CBC = 'OPN' and opn1.EXTID = 'AMT2'  and opn1.enddat = '2299-12-31'
        left join trn on trn.objinr = ncd.inr and trn.objtyp = 'NCD' and trn.inifrm in('NITCRQ','NETCRQ')
        where maxsum.amt is not null
        and trn.relflg = 'R'
        <if test="branch != '1000' and bchLst != null and bchLst.size() >0 ">
            and ncd.bchkeyinr in
            <foreach item="bch" collection="bchLst" open="(" separator="," close=")">
                #{bch.inr}
            </foreach>
        </if>
        <!--开立/通知-->
        <if test="hndtyps != null and hndtyps.length >0 ">
            and nid.hndtyp in
            <foreach item="hndtypitem" collection="hndtyps" open="(" separator="," close=")">
                #{hndtypitem}
            </foreach>
        </if>
        <if test="nam != null and nam != ''">
            and UPPER(NCD.NAM) LIKE concat(concat('%', UPPER(#{nam})), '%')
        </if>
        <if test="seaownref != null and seaownref != ''">
            AND UPPER(NCD.OWNREF) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
        </if>

        <if test="opndatfrom != null ">
            AND NCD.CREDAT &gt;= #{opndatfrom}

        </if>

        <if test="opndatto != null">
            AND NCD.CREDAT &lt;= #{opndatto}
        </if>

        <if test="seacur != null and seacur != ''">
            AND EXISTS (
            select 1 from CBB INFCONCBS where
            UPPER(INFCONCBS.CUR ) = UPPER(#{seacur})
            <if test="seaamt != null and seaamt != ''">
                AND INFCONCBS.AMT = #{seaamt}
            </if>
            AND (INFCONCBS.OBJTYP = 'NCD'
            AND INFCONCBS.OBJINR = NCD.INR
            AND (INFCONCBS.BEGDAT &lt;= str_to_date('2299-12-30',  '%Y-%m-%d')
            AND INFCONCBS.ENDDAT &gt; str_to_date('2299-12-30',  '%Y-%m-%d'))
            AND INFCONCBS.EXTID IN (' ', 'AMT1')
            AND INFCONCBS.CBC IN ('MAXSUM', 'PRESUM', 'NOMSUM', 'OPN'))

            )
        </if>
        <if test="searol != null and searol != ''">
            AND EXISTS (
            select 1 from PTS where
            NCD.INR = PTS.OBJINR
            AND 'NCD' = PTS.OBJTYP
            AND PTS.ROL = #{searol}
            )
        </if>
        <if test="ptyExtkey != null and ptyExtkey != ''">
            and exists (
            select 1 from PTS, PTA where
            PTS.PTAINR = PTA.INR
            AND NCD.INR = PTS.OBJINR
            AND 'NCD' = PTS.OBJTYP
            <choose>
                <when test="searol != null and searol != ''" >AND PTS.ROL =  #{searol}</when>
                <otherwise>AND PTS.ROL IN ('BEN','APL') </otherwise>
            </choose>
            AND UPPER(PTA.PTYINR) IN (SELECT INR FROM PTY WHERE EXTKEY = UPPER(#{ptyExtkey}))
            )
        </if>

        <if test="seapty != null and seapty != ''">
            and exists (
            select 1 from PTS, PTA where
            PTS.PTAINR = PTA.INR
            AND NCD.INR = PTS.OBJINR
            AND 'NCD' = PTS.OBJTYP
            AND (
            UPPER(PTA.PTYEXTKEY) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            or UPPER(PTA.BIC) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            or UPPER(PTA.NAM) LIKE concat(concat('%', UPPER(#{seapty})), '%')
            )

            <choose>
                <when test="searol != null and searol != ''" >AND PTS.ROL =  #{searol}</when>
                <otherwise>AND PTS.ROL IN ('BEN','APL') </otherwise>
            </choose>
            )
        </if>

        <if test="seagtyp != null and seagtyp != ''">
            AND nid.gartyp = #{seagtyp}
        </if>

        <if test="searef != null and searef != ''">
            and (UPPER(apl.REF) LIKE concat(concat('%', UPPER(#{searef})), '%')
            or UPPER(ben.REF) LIKE concat(concat('%', UPPER(#{searef})), '%'))
        </if>

        <if test="seasta != null and seasta != ''">
            <if test="seasta== 'O'.toString()">
                AND NCD.OPNDAT IS NOT NULL
                AND NCD.CLSDAT IS NULL
            </if>
            <if test="seasta== 'C'.toString()">
                AND NCD.CLSDAT IS NOT NULL
            </if>
        </if>
        <!--是否对外担保-->
        <if test="fingua != null and fingua != ''">
            AND NID.FINGUA = #{fingua}
        </if>

        <!-- 跨境人民币保函 -->
        <if test="cmtflg != null and cmtflg != ''">
            AND NID.CMTFLG = #{cmtflg}
        </if>
        <!-- 分离式保函 -->
        <if test="fenlishi != null ">
            AND NID.FENLISHI = #{fenlishi}
        </if>
        <if test="seaauthorized != null  and seaauthorized != ''">
            <if test="seaauthorized == 'Y'.toString()">
                AND NOT EXISTS (
                SELECT 1 FROM TRN WHERE
                TRN.OBJINR= NCD.INR
                AND TRN.OBJTYP='NCD'
                AND TRN.RELFLG in ('W','C')
                )
            </if>
            <if test="seaauthorized == 'N'.toString()">
                AND EXISTS (
                SELECT 1 FROM TRN WHERE
                TRN.OBJINR= NCD.INR
                AND TRN.OBJTYP='NCD'
                AND TRN.RELFLG in ('W','C')
                )
            </if>
        </if>

        <if test="isGuarantee != null and isGuarantee != ''">
            AND substring(ncd.OWNREF,1,2) = #{isGuarantee}
        </if>
        ) inftmp
        where 1=1
        <if test="gidopentyps != null and gidopentyps.length>0">
            and gidopentyp in
            <foreach item="gidopentypItem" collection="gidopentyps" open="(" separator="," close=")">
                #{gidopentypItem}
            </foreach>
        </if>
        <if test='column != null and column!="" '>
            order by  ${column}  ${order}
        </if>
        <if test='column == null or column=="" '>
            order by GCDOPNDAT DESC ,GCDOWNREF DESC
        </if>
    </select>

    <select id="selectAllGcdByCondition" resultType="com.brilliance.isc.vo.lg.GcdInfoVo">
        select * from (
        <if test="querytyp == 'GID'.toString() or querytyp == 'FIX'.toString() ">
         select
           gcd.id id, gcd.inr ,gcd.ownref,gcd.ownusr,gid.inr gidinr,gid.ownref gidownref,gcd.credat , 'GCBH' opentyp ,'GJBH' gidopentyp,  cbb1.cur cur,
            case when cbb6.amt is not null then (cbb1.amt + cbb6.amt) else cbb1.amt end  gcdamt,
            case when cbb3.amt is not null then (cbb3.amt +cbb2.amt ) else cbb2.amt end  gidamt1,
            case when cbb5.amt is not null then (cbb5.amt +cbb4.amt ) else cbb4.amt end  gcdopnamt,
            case when gcd.clsdat is null then 'N' else 'Y' end  clearsta,
            case when gid.clsdat is null then 'N' else 'Y' end  clssta,
            (case
            when gcd.clsdat is not  null
            then 'CLS'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITSET', 'GITPSE')
            and relflg = 'R') > 0 then
            'SET'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITCRJ', 'GITPCJ')
            and relflg = 'R') > 0 then
            'CRJ'
            when (select count(inr)
            from trn
            where ownref = gcd.ownref
            and inifrm in ('GITCRJ', 'GITPCJ', 'GITSET', 'GITPSE')
            and relflg = 'R') = 0 then
            'TBC'
            end) claimsta,
             (select count(*) from trn trn  where trn.objinr = gcd.inr
              and trn.objtyp = 'GCD' and inifrm = 'GITPCR' and trn.RELFLG != 'R') clswatcnt
            from gcd gcd
            left join gid gid on gid.inr = gcd.PNTINR
            left join cbb cbb1 on cbb1.objtyp = 'GCD' and cbb1.objinr = gcd.inr and cbb1.cbc = 'MAXSUM' and cbb1.extid = 'AMT1' and cbb1.enddat = '2299-12-31'
            left join cbb cbb2 on cbb2.objtyp = 'GID' and cbb2.objinr = gid.inr and cbb2.cbc = 'OPN' and cbb2.extid = 'AMT1' and cbb2.enddat = '2299-12-31'
            left join cbb cbb3 on cbb3.objtyp = 'GID' and cbb3.objinr = gid.inr and cbb3.cbc = 'OPN' and cbb3.extid = 'AMT2' and cbb3.enddat = '2299-12-31'
            left join cbb cbb4 on cbb4.objtyp = 'GCD' and cbb4.objinr = gcd.inr and cbb4.cbc = 'OPN' and cbb4.extid = 'AMT1' and cbb4.enddat = '2299-12-31'
            left join cbb cbb5 on cbb5.objtyp = 'GCD' and cbb5.objinr = gcd.inr and cbb5.cbc = 'OPN' and cbb5.extid = 'AMT2' and cbb5.enddat = '2299-12-31'
            left join cbb cbb6 on cbb6.objtyp = 'GCD' and cbb6.objinr = gcd.inr and cbb6.cbc = 'MAXSUM' and cbb6.extid = 'AMT2' and cbb6.enddat = '2299-12-31'
            where cbb1.amt is not null
            <!--机构隔离-->
            <if test="branch != '1000'">
                and gcd.bchkeyinr = #{bchinr}
            </if>
            <!--开立/通知-->
            <if test="hndtyps != null and hndtyps.length >0 ">
                and gid.hndtyp in
                <foreach item="hndtypitem" collection="hndtyps" open="(" separator="," close=")">
                    #{hndtypitem}
                </foreach>
            </if>
            <!--保函编号-->
            <if test="seaownref != null and seaownref != ''">
                AND (UPPER(gid.OWNREF) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
                <if test="searol == 'BEN'.toString()">
                    or exists (
                    select 1 from pts where pts.objinr = gid.inr
                    and pts.objtyp = 'GID' and pts.rol = 'ISS'
                    and UPPER(pts.ref) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
                    )
                </if>
                )
            </if>
            <!--当事人-->
            <if test="ptyExtkey != null and ptyExtkey != ''">
                and exists (
                    select 1 from PTS
                    where gid.INR = PTS.OBJINR
                    AND PTS.OBJTYP = 'GID'
                    AND PTS.ROL = #{searol}
                    AND (
                    PTS.EXTKEY LIKE concat(concat('%', #{ptyExtkey}), '%')
                    or PTS.NAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                    or PTS.CNNAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                    or PTS.ENNAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                    )
                    )
            </if>
            <!--开立方式-->
            <if test="seapurpos != null and seapurpos != ''">
                AND gid.PURPOS = #{seapurpos}
            </if>
            <!--电子渠道-->
            <if test="fromflg != null and fromflg != ''">
                AND gid.FROMFLG = #{fromflg}
            </if>
            <!--分离式保函-->
            <if test="fenlishi != null and fenlishi != ''">
                AND gid.FENLISHI is not null
                AND gid.FENLISHI != ''
            </if>
            <!--融资类保函-->
            <if test="fingua != null and  fingua != ''">
                AND gid.FENLISHI = '1'
            </if>
            <!--跨境人民币保函-->
            <if test="cmtflg != null and cmtflg != ''">
                AND gid.CMTFLG = #{cmtflg}
            </if>
            <!--索赔编号-->
            <if test="claimownref != null and claimownref != ''">
                AND UPPER(gcd.ownref) LIKE concat(concat('%', UPPER(#{claimownref})), '%')
            </if>
            <!--索赔金额币种-->
            <if test="claimcur != null and claimcur != ''">
                AND EXISTS (
                select 1 from CBB INFCONCBS where
                UPPER(INFCONCBS.CUR ) = UPPER(#{claimcur})
                <if test="claimamtfr != null and claimamtfr != '' and claimamtto != null and claimamtto != ''">
                    AND INFCONCBS.AMT &gt;= #{claimamtfr}
                    AND INFCONCBS.AMT &lt;= #{claimamtto}
                </if>
                AND (INFCONCBS.OBJTYP = 'GCD'
                AND INFCONCBS.OBJINR = GCD.INR
                AND (INFCONCBS.BEGDAT &lt;= str_to_date('2299-12-30',  '%Y-%m-%d')
                AND INFCONCBS.ENDDAT &gt; str_to_date('2299-12-30',  '%Y-%m-%d'))
                AND INFCONCBS.EXTID IN (' ', 'AMT1')
                AND INFCONCBS.CBC IN ('MAXSUM', 'PRESUM', 'NOMSUM', 'OPN'))
                )
            </if>
           </if>
        <if test="querytyp == 'FIX'.toString() ">
            UNION ALL
        </if>
        <if test="querytyp == 'FIX'.toString() or querytyp == 'NID'.toString() ">
            select
            ncd.id id,ncd.inr ,ncd.ownref,ncd.ownusr,nid.inr gidinr,nid.ownref gidownref,ncd.credat ,'NCBH' opentyp ,'GNBH' gidopentyp,cbb1.cur cur,
            case when cbb6.amt is not null then (cbb1.amt + cbb6.amt) else cbb1.amt end   gcdamt,
            case when cbb3.amt is not null then (cbb3.amt +cbb2.amt ) else cbb2.amt end   gidamt1,
            case when cbb5.amt is not null then (cbb5.amt +cbb4.amt ) else cbb4.amt end  gcdopnamt,
            case when ncd.clsdat is null then 'N' else 'Y' end  clearsta,
            case when nid.clsdat is null then 'N' else 'Y' end clssta ,
            (case
            when ncd.clsdat is not  null
            then 'CLS'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITSET', 'NITPSE')
            and relflg = 'R') > 0 then
            'SET'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITCRJ', 'NITPCJ')
            and relflg = 'R') > 0 then
            'CRJ'
            when (select count(inr)
            from trn
            where ownref = ncd.ownref
            and inifrm in ('NITCRJ', 'NITPCJ', 'NITSET', 'NITPSE')
            and relflg = 'R') = 0 then
            'TBC'
            end) claimsta,
        (select count(*) from trn trn  where trn.objinr = ncd.inr
        and trn.objtyp = 'NCD' and inifrm = 'NITPCR' and trn.RELFLG != 'R') clswatcnt
            from ncd ncd
            left join nid nid on nid.inr = ncd.PNTINR
            left join cbb cbb1 on cbb1.objtyp = 'NCD' and cbb1.objinr = ncd.inr and cbb1.cbc = 'MAXSUM' and cbb1.extid = 'AMT1' and cbb1.enddat = '2299-12-31'
            left join cbb cbb2 on cbb2.objtyp = 'NID' and cbb2.objinr = nid.inr and cbb2.cbc = 'OPN' and cbb2.extid = 'AMT1' and cbb2.enddat = '2299-12-31'
            left join cbb cbb3 on cbb3.objtyp = 'NID' and cbb3.objinr = nid.inr and cbb3.cbc = 'OPN' and cbb3.extid = 'AMT2' and cbb3.enddat = '2299-12-31'
            left join cbb cbb4 on cbb4.objtyp = 'NCD' and cbb4.objinr = ncd.inr and cbb4.cbc = 'OPN' and cbb4.extid = 'AMT1' and cbb4.enddat = '2299-12-31'
            left join cbb cbb5 on cbb5.objtyp = 'NCD' and cbb5.objinr = ncd.inr and cbb5.cbc = 'OPN' and cbb5.extid = 'AMT2' and cbb5.enddat = '2299-12-31'
            left join cbb cbb6 on cbb6.objtyp = 'NCD' and cbb6.objinr = ncd.inr and cbb6.cbc = 'MAXSUM' and cbb6.extid = 'AMT2' and cbb6.enddat = '2299-12-31'
            where  cbb1.amt is not null
            <!--机构隔离-->
            <if test="branch != '1000'">
                and ncd.bchkeyinr = #{bchinr}
            </if>
            <!--开立/通知-->
            <if test="hndtyps != null and hndtyps.length >0 ">
                and nid.hndtyp in
                <foreach item="hndtypitem" collection="hndtyps" open="(" separator="," close=")">
                    #{hndtypitem}
                </foreach>
            </if>
            <!--保函编号-->
            <if test="seaownref != null and seaownref != ''">
                AND (UPPER(nid.OWNREF) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
                <if test="searol == 'BEN'.toString()">
                    or exists (
                    select 1 from pts where pts.objinr = nid.inr
                    and pts.objtyp = 'NID' and pts.rol = 'ISS'
                    and  UPPER(pts.ref) LIKE concat(concat('%', UPPER(#{seaownref})), '%')
                    )
                </if>
                )
            </if>
            <!--当事人-->
            <if test="ptyExtkey != null and ptyExtkey != ''">
                and exists (
                select 1 from PTS
                where nid.INR = PTS.OBJINR
                AND PTS.OBJTYP = 'NID'
                AND PTS.ROL = #{searol}
                AND (
                PTS.EXTKEY LIKE concat(concat('%', #{ptyExtkey}), '%')
                or PTS.NAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                or PTS.CNNAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                or PTS.ENNAM LIKE concat(concat('%', #{ptyExtkey}), '%')
                ))
            </if>
            <!--通知方式,国内保函通知方式使用hndtyp字段-->
            <if test="hndtyp != null and hndtyp != ''">
                AND nid.hndtyp = #{hndtyp}
            </if>
            <!--电子渠道-->
            <if test="fromflg != null and fromflg != ''">
                AND nid.FROMFLG = #{fromflg}
            </if>
            <!--分离式保函-->
            <if test="fenlishi != null and fenlishi != ''">
                AND nid.FENLISHI is not null
                AND nid.FENLISHI != ''
            </if>
            <!--融资类保函-->
            <if test="fingua != null and  fingua != ''">
                AND nid.FENLISHI = '1'
            </if>
            <!--跨境人民币保函-->
            <if test="cmtflg != null and cmtflg != ''">
                AND nid.CMTFLG = #{cmtflg}
            </if>
            <!--索赔编号-->
            <if test="claimownref != null and claimownref != ''">
                AND UPPER(ncd.ownref) LIKE concat(concat('%', UPPER(#{claimownref})), '%')
            </if>
            <!--索赔金额币种-->
            <if test="claimcur != null and claimcur != ''">
                AND EXISTS (
                select 1 from CBB INFCONCBS where
                UPPER(INFCONCBS.CUR ) = UPPER(#{claimcur})
                <if test="claimamtfr != null and claimamtfr != '' and claimamtto != null and claimamtto != ''">
                    AND INFCONCBS.AMT &gt;= #{claimamtfr}
                    AND INFCONCBS.AMT &lt;= #{claimamtto}
                </if>
                AND (INFCONCBS.OBJTYP = 'NCD'
                AND INFCONCBS.OBJINR = NCD.INR
                AND (INFCONCBS.BEGDAT &lt;= str_to_date('2299-12-30',  '%Y-%m-%d')
                AND INFCONCBS.ENDDAT &gt; str_to_date('2299-12-30',  '%Y-%m-%d'))
                AND INFCONCBS.EXTID IN (' ', 'AMT1')
                AND INFCONCBS.CBC IN ('MAXSUM', 'PRESUM', 'NOMSUM', 'OPN'))
                )
            </if>
        </if>
             ) gcdtmp
                 where 1 = 1
            <if test="gidopentyps != null and gidopentyps.length>0">
                and gidopentyp in
                <foreach item="gidopentypItem" collection="gidopentyps" open="(" separator="," close=")">
                    #{gidopentypItem}
                </foreach>
            </if>
            <!--索赔状态-->
            <if test="claimsta != null and claimsta != ''">
                and claimsta = #{claimsta}
            </if>
            <!--结清状态-->
            <if test="clssta != null and clssta != ''">
                and clearsta = #{clssta}
            </if>
            <!--保函结清状态-->
            <if test="isclear != null and isclear != ''">
                and clssta = #{isclear}
            </if>
            order by credat DESC, ownref desc

    </select>

    <select id="selectOneByOwnRef" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from GCD
        where OWNREF = #{ownRef,jdbcType=VARCHAR}
        ORDER BY CREDAT desc
        limit 1
    </select>

</mapper>