<?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.LgoMapper">

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Lgo">
        <result property="inr" column="inr" jdbcType="CHAR"/>
        <result property="objtyp" column="objtyp" jdbcType="VARCHAR"/>
        <result property="objinr" column="objinr" jdbcType="CHAR"/>
        <result property="extid" column="extid" jdbcType="VARCHAR"/>
        <result property="cur" column="cur" jdbcType="VARCHAR"/>
        <result property="amt" column="amt" jdbcType="DECIMAL"/>
        <result property="matdat" column="matdat" jdbcType="DATE"/>
        <result property="xrfcur" column="xrfcur" jdbcType="VARCHAR"/>
        <result property="xrfamt" column="xrfamt" jdbcType="DECIMAL"/>
        <result property="ownref" column="ownref" jdbcType="VARCHAR"/>
        <result property="bchkeyinr" column="bchkeyinr" jdbcType="CHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="ptyinr" column="ptyinr" jdbcType="CHAR"/>
        <result property="rate" column="rate" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,objtyp,objinr,extid,cur,amt,matdat,xrfcur,xrfamt,ownref,bchkeyinr,ver,ptyinr
    </sql>

    <insert id="insert">
        insert into LGO (<include refid="Base_Column_List" />)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=CHAR},
            #{objtyp,jdbcType=VARCHAR},
            #{objinr,jdbcType=CHAR},
            #{extid,jdbcType=VARCHAR},
            #{cur,jdbcType=VARCHAR},
            #{amt,jdbcType=DECIMAL},
            #{matdat,jdbcType=DATE},
            #{xrfcur,jdbcType=VARCHAR},
            #{xrfamt,jdbcType=DECIMAL},
            #{ownref,jdbcType=VARCHAR},
            #{bchkeyinr,jdbcType=CHAR},
            #{ver,jdbcType=VARCHAR},
            #{ptyinr,jdbcType=CHAR},
        </trim>
    </insert>

    <select id="queryLgoByObjtypAndObjinr" resultMap="BaseResultMap">
        select
            <include refid="Base_Column_List" />
        from lgo
        where objtyp = #{objtyp,jdbcType=VARCHAR}
        and objinr = #{objinr,jdbcType=VARCHAR}
    </select>

    <select id="getLgoByObjtypAndObjinr" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from lgo
        where objtyp = #{objtyp,jdbcType=VARCHAR}
        and objinr = #{objinr,jdbcType=VARCHAR}
    </select>

    <select id="queryLgoByIdLgbInr" resultMap="BaseResultMap">
        SELECT ownref, objtyp, objinr, cur, amt, xrfcur, xrfamt, if(CUR='CNY',ROUND(amt/xrfamt, 6),ROUND(xrfamt/amt, 6)) rate,
        (SELECT lgeflg FROM lge WHERE inr=(SELECT MAX(inr) FROM lge WHERE lge.lgoinr=lgo.inr AND lge.xrfamt &lt;&gt; 0)) status
        FROM lgo WHERE xrfamt &gt; 0 AND inr IN (SELECT DISTINCT(LGOINR) FROM LGL
        WHERE lgbinr = #{lgbinr,jdbcType=VARCHAR})
    </select>

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

    <update id="updateByPrimaryKeySelective">
        update lgo
        <set>
            <if test="objtyp != null">objtyp = #{objtyp,jdbcType=VARCHAR},</if>
            <if test="objinr != null">objinr = #{objinr,jdbcType=VARCHAR},</if>
            <if test="extid != null">extid = #{extid,jdbcType=VARCHAR},</if>
            <if test="cur != null">cur = #{cur,jdbcType=VARCHAR},</if>
            <if test="amt != null">amt = #{amt,jdbcType=DECIMAL},</if>
            <if test="matdat != null">matdat = #{matdat,jdbcType=DATE},</if>
            <if test="xrfcur != null">xrfcur = #{xrfcur,jdbcType=VARCHAR},</if>
            <if test="xrfamt != null">xrfamt = #{xrfamt,jdbcType=DECIMAL},</if>
            <if test="ownref != null">ownref = #{ownref,jdbcType=VARCHAR},</if>
            <if test="bchkeyinr != null">bchkeyinr = #{bchkeyinr,jdbcType=DATE},</if>
            <if test="ver != null">ver = #{ver,jdbcType=VARCHAR},</if>
            <if test="ptyinr != null">ptyinr = #{ptyinr,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update lge
        <set>
            objtyp = #{objtyp,jdbcType=VARCHAR},
            objinr = #{objinr,jdbcType=VARCHAR},
            extid = #{extid,jdbcType=VARCHAR},
            cur = #{cur,jdbcType=VARCHAR},
            amt = #{amt,jdbcType=DECIMAL},
            matdat = #{matdat,jdbcType=DATE},
            xrfcur = #{xrfcur,jdbcType=VARCHAR},
            xrfamt = #{xrfamt,jdbcType=DECIMAL},
            ownref = #{ownref,jdbcType=VARCHAR},
            bchkeyinr = #{bchkeyinr,jdbcType=DATE},
            ver = #{ver,jdbcType=VARCHAR},
            ptyinr = #{ptyinr,jdbcType=VARCHAR},
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

</mapper>