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

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Msg">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="cortyp" column="cortyp" jdbcType="VARCHAR"/>
        <result property="msgtyp" column="msgtyp" jdbcType="VARCHAR"/>
        <result property="dir" column="dir" jdbcType="VARCHAR"/>
        <result property="credattim" column="credattim" jdbcType="DATE"/>
        <result property="msgid" column="msgid" jdbcType="VARCHAR"/>
        <result property="sender" column="sender" jdbcType="VARCHAR"/>
        <result property="recver" column="recver" jdbcType="VARCHAR"/>
        <result property="trninr" column="trninr" jdbcType="VARCHAR"/>
        <result property="rtndattim" column="rtndattim" jdbcType="DATE"/>
        <result property="prcsts" column="prcsts" jdbcType="VARCHAR"/>
        <result property="rjctinf" column="rjctinf" jdbcType="VARCHAR"/>
        <result property="prcdat" column="prcdat" jdbcType="DATE"/>
        <result property="prccod" column="prccod" jdbcType="VARCHAR"/>
        <result property="mesgid" column="mesgid" jdbcType="VARCHAR"/>
        <result property="smhinr" column="smhinr" jdbcType="VARCHAR"/>
        <result property="orimsgid" column="orimsgid" jdbcType="VARCHAR"/>
        <result property="ownref" column="ownref" jdbcType="VARCHAR"/>
        <result property="sptinr" column="sptinr" jdbcType="VARCHAR"/>
        <result property="sta" column="sta" jdbcType="VARCHAR"/>
        <result property="prtcnt" column="prtcnt" jdbcType="INTEGER"/>
        <result property="bchkeyinr" column="bchkeyinr" jdbcType="VARCHAR"/>
        <result property="attsta" column="attsta" jdbcType="VARCHAR"/>
        <result property="sndcor" column="sndcor" jdbcType="VARCHAR"/>
        <result property="sndkey" column="sndkey" jdbcType="VARCHAR"/>
        <result property="valdat" column="valdat" jdbcType="DATE"/>
        <result property="chksta" column="chksta" jdbcType="VARCHAR"/>
        <result property="orgnlpath" column="orgnlpath" jdbcType="VARCHAR"/>
        <result property="f21" column="f21" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,cortyp,msgtyp,dir,credattim,msgid,sender,recver,trninr,rtndattim,prcsts,rjctinf,prcdat,prccod,mesgid,smhinr,orimsgid,ownref,sptinr,sta,prtcnt,bchkeyinr,attsta,sndcor,
        sndkey,valdat,chksta,orgnlpath,f21
    </sql>

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

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

    <delete id="deleteBySmhinr">
        delete from msg
        where smhinr = #{smhinr,jdbcType=VARCHAR}
    </delete>
    <insert id="insert">
        insert into msg (<include refid="Base_Column_List" />)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=VARCHAR},
            #{cortyp,jdbcType=VARCHAR},
            #{msgtyp,jdbcType=VARCHAR},
            #{dir,jdbcType=VARCHAR},
            #{credattim,jdbcType=DATE},
            #{msgid,jdbcType=VARCHAR},
            #{sender,jdbcType=VARCHAR},
            #{recver,jdbcType=VARCHAR},
            #{trninr,jdbcType=VARCHAR},
            #{rtndattim,jdbcType=DATE},
            #{prcsts,jdbcType=VARCHAR},
            #{rjctinf,jdbcType=VARCHAR},
            #{prcdat,jdbcType=DATE},
            #{prccod,jdbcType=VARCHAR},
            #{mesgid,jdbcType=VARCHAR},
            #{smhinr,jdbcType=VARCHAR},
            #{orimsgid,jdbcType=VARCHAR},
            #{ownref,jdbcType=VARCHAR},
            #{sptinr,jdbcType=VARCHAR},
            #{sta,jdbcType=VARCHAR},
            #{prtcnt,jdbcType=INTEGER},
            #{bchkeyinr,jdbcType=VARCHAR},
            #{attsta,jdbcType=VARCHAR},
            #{sndcor,jdbcType=VARCHAR},
            #{sndkey,jdbcType=VARCHAR},
            #{valdat,jdbcType=DATE},
            #{chksta,jdbcType=VARCHAR},
            #{orgnlpath,jdbcType=VARCHAR},
            #{f21,jdbcType=VARCHAR}
        </trim>
    </insert>

    <insert id="insertSelective">
        insert into msg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inr != null">inr,</if>
            <if test="cortyp != null">cortyp,</if>
            <if test="msgtyp != null">msgtyp,</if>
            <if test="dir != null">dir,</if>
            <if test="credattim != null">credattim,</if>
            <if test="msgid != null">msgid,</if>
            <if test="sender != null">sender,</if>
            <if test="recver != null">recver,</if>
            <if test="trninr != null">trninr,</if>
            <if test="rtndattim != null">rtndattim,</if>
            <if test="prcsts != null">prcsts,</if>
            <if test="rjctinf != null">rjctinf,</if>
            <if test="prcdat != null">prcdat,</if>
            <if test="prccod != null">prccod,</if>
            <if test="mesgid != null">mesgid,</if>
            <if test="smhinr != null">smhinr,</if>
            <if test="orimsgid != null">orimsgid,</if>
            <if test="ownref != null">ownref,</if>
            <if test="sptinr != null">sptinr,</if>
            <if test="sta != null">sta,</if>
            <if test="prtcnt != null">prtcnt,</if>
            <if test="bchkeyinr != null">bchkeyinr,</if>
            <if test="attsta != null">attsta,</if>
            <if test="sndcor != null">sndcor,</if>
            <if test="sndkey != null">sndkey,</if>
            <if test="valdat != null">valdat,</if>
            <if test="chksta != null">chksta,</if>
            <if test="orgnlpath != null">orgnlpath,</if>
            <if test="f21 != null">f21,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inr != null">#{inr,jdbcType=VARCHAR}, </if>
            <if test="cortyp != null">#{cortyp,jdbcType=VARCHAR}, </if>
            <if test="msgtyp != null">#{msgtyp,jdbcType=VARCHAR}, </if>
            <if test="dir != null">#{dir,jdbcType=VARCHAR}, </if>
            <if test="credattim != null">#{credattim,jdbcType=DATE}, </if>
            <if test="msgid != null">#{msgid,jdbcType=VARCHAR}, </if>
            <if test="sender != null">#{sender,jdbcType=VARCHAR}, </if>
            <if test="recver != null">#{recver,jdbcType=VARCHAR}, </if>
            <if test="trninr != null">#{trninr,jdbcType=VARCHAR}, </if>
            <if test="rtndattim != null">#{rtndattim,jdbcType=DATE}, </if>
            <if test="prcsts != null">#{prcsts,jdbcType=VARCHAR}, </if>
            <if test="rjctinf != null">#{rjctinf,jdbcType=VARCHAR}, </if>
            <if test="prcdat != null">#{prcdat,jdbcType=DATE}, </if>
            <if test="prccod != null">#{prccod,jdbcType=VARCHAR}, </if>
            <if test="mesgid != null">#{mesgid,jdbcType=VARCHAR}, </if>
            <if test="smhinr != null">#{smhinr,jdbcType=VARCHAR}, </if>
            <if test="orimsgid != null">#{orimsgid,jdbcType=VARCHAR}, </if>
            <if test="ownref != null">#{ownref,jdbcType=VARCHAR}, </if>
            <if test="sptinr != null">#{sptinr,jdbcType=VARCHAR}, </if>
            <if test="sta != null">#{sta,jdbcType=VARCHAR}, </if>
            <if test="prtcnt != null">#{prtcnt,jdbcType=INTEGER}, </if>
            <if test="bchkeyinr != null">#{bchkeyinr,jdbcType=VARCHAR}, </if>
            <if test="attsta != null">#{attsta,jdbcType=VARCHAR}, </if>
            <if test="sndcor != null">#{sndcor,jdbcType=VARCHAR}, </if>
            <if test="sndkey != null">#{sndkey,jdbcType=VARCHAR}, </if>
            <if test="valdat != null">#{valdat,jdbcType=VARCHAR}, </if>
            <if test="chksta != null">#{chksta,jdbcType=VARCHAR}, </if>
            <if test="orgnlpath != null">#{orgnlpath,jdbcType=VARCHAR}, </if>
            <if test="f21 != null">#{f21,jdbcType=VARCHAR}, </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update msg
        <set>
            <if test="cortyp != null">cortyp = #{cortyp,jdbcType=VARCHAR},</if>
            <if test="msgtyp != null">msgtyp = #{msgtyp,jdbcType=VARCHAR},</if>
            <if test="dir != null">dir = #{dir,jdbcType=VARCHAR},</if>
            <if test="credattim != null">credattim = #{credattim,jdbcType=DATE},</if>
            <if test="msgid != null">msgid = #{msgid,jdbcType=VARCHAR},</if>
            <if test="sender != null">sender = #{sender,jdbcType=VARCHAR},</if>
            <if test="recver != null">recver = #{recver,jdbcType=VARCHAR},</if>
            <if test="trninr != null">trninr = #{trninr,jdbcType=VARCHAR},</if>
            <if test="rtndattim != null">rtndattim = #{rtndattim,jdbcType=DATE},</if>
            <if test="prcsts != null">prcsts = #{prcsts,jdbcType=VARCHAR},</if>
            <if test="rjctinf != null">rjctinf = #{rjctinf,jdbcType=VARCHAR},</if>
            <if test="prcdat != null">prcdat = #{prcdat,jdbcType=DATE},</if>
            <if test="prccod != null">prccod = #{prccod,jdbcType=VARCHAR},</if>
            <if test="mesgid != null">mesgid = #{mesgid,jdbcType=VARCHAR},</if>
            <if test="smhinr != null">smhinr = #{smhinr,jdbcType=VARCHAR},</if>
            <if test="orimsgid != null">orimsgid = #{orimsgid,jdbcType=VARCHAR},</if>
            <if test="ownref != null">ownref = #{ownref,jdbcType=VARCHAR},</if>
            <if test="sptinr != null">sptinr = #{sptinr,jdbcType=VARCHAR},</if>
            <if test="sta != null">sta = #{sta,jdbcType=VARCHAR},</if>
            <if test="prtcnt != null">prtcnt = #{prtcnt,jdbcType=INTEGER},</if>
            <if test="bchkeyinr != null">bchkeyinr = #{bchkeyinr,jdbcType=VARCHAR},</if>
            <if test="attsta != null">attsta = #{attsta,jdbcType=VARCHAR},</if>
            <if test="sndcor != null">sndcor = #{sndcor,jdbcType=VARCHAR},</if>
            <if test="sndkey != null">sndkey = #{sndkey,jdbcType=VARCHAR},</if>
            <if test="valdat != null">valdat = #{valdat,jdbcType=VARCHAR},</if>
            <if test="chksta != null">chksta = #{chksta,jdbcType=VARCHAR},</if>
            <if test="orgnlpath != null">orgnlpath = #{orgnlpath,jdbcType=VARCHAR},</if>
            <if test="f21 != null">f21 = #{f21,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update msg
        <set>
            cortyp = #{cortyp,jdbcType=VARCHAR},
            msgtyp = #{msgtyp,jdbcType=VARCHAR},
            dir = #{dir,jdbcType=VARCHAR},
            credattim = #{credattim,jdbcType=DATE},
            msgid = #{msgid,jdbcType=VARCHAR},
            sender = #{sender,jdbcType=VARCHAR},
            recver = #{recver,jdbcType=VARCHAR},
            trninr = #{trninr,jdbcType=VARCHAR},
            rtndattim = #{rtndattim,jdbcType=DATE},
            prcsts = #{prcsts,jdbcType=VARCHAR},
            rjctinf = #{rjctinf,jdbcType=VARCHAR},
            prcdat = #{prcdat,jdbcType=DATE},
            prccod = #{prccod,jdbcType=VARCHAR},
            mesgid = #{mesgid,jdbcType=VARCHAR},
            smhinr = #{smhinr,jdbcType=VARCHAR},
            orimsgid = #{orimsgid,jdbcType=VARCHAR},
            ownref = #{ownref,jdbcType=VARCHAR},
            sptinr = #{sptinr,jdbcType=VARCHAR},
            sta = #{sta,jdbcType=VARCHAR},
            prtcnt = #{prtcnt,jdbcType=INTEGER},
            bchkeyinr = #{bchkeyinr,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 msg
        <set>
            <if test="module.cortyp != null">cortyp = #{module.cortyp,jdbcType=VARCHAR},</if>
            <if test="module.msgtyp != null">msgtyp = #{module.msgtyp,jdbcType=VARCHAR},</if>
            <if test="module.dir != null">dir = #{module.dir,jdbcType=VARCHAR},</if>
            <if test="module.credattim != null">credattim = #{module.credattim,jdbcType=DATE},</if>
            <if test="module.msgid != null">msgid = #{module.msgid,jdbcType=VARCHAR},</if>
            <if test="module.sender != null">sender = #{module.sender,jdbcType=VARCHAR},</if>
            <if test="module.recver != null">recver = #{module.recver,jdbcType=VARCHAR},</if>
            <if test="module.trninr != null">trninr = #{module.trninr,jdbcType=VARCHAR},</if>
            <if test="module.rtndattim != null">rtndattim = #{module.rtndattim,jdbcType=DATE},</if>
            <if test="module.prcsts != null">prcsts = #{module.prcsts,jdbcType=VARCHAR},</if>
            <if test="module.rjctinf != null">rjctinf = #{module.rjctinf,jdbcType=VARCHAR},</if>
            <if test="module.prcdat != null">prcdat = #{module.prcdat,jdbcType=DATE},</if>
            <if test="module.prccod != null">prccod = #{module.prccod,jdbcType=VARCHAR},</if>
            <if test="module.mesgid != null">mesgid = #{module.mesgid,jdbcType=VARCHAR},</if>
            <if test="module.smhinr != null">smhinr = #{module.smhinr,jdbcType=VARCHAR},</if>
            <if test="module.orimsgid != null">orimsgid = #{module.orimsgid,jdbcType=VARCHAR},</if>
            <if test="module.ownref != null">ownref = #{module.ownref,jdbcType=VARCHAR},</if>
            <if test="module.sptinr != null">sptinr = #{module.sptinr,jdbcType=VARCHAR},</if>
            <if test="module.sta != null">sta = #{module.sta,jdbcType=VARCHAR},</if>
            <if test="module.prtcnt != null">prtcnt = #{module.prtcnt,jdbcType=INTEGER},</if>
            <if test="module.bchkeyinr != null">bchkeyinr = #{module.bchkeyinr,jdbcType=VARCHAR},</if>
        </set>
        ${conditions}
    </update>

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

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

    <select id="getMsgByTrninr" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from msg
        where trninr = #{trninr,jdbcType=VARCHAR}
    </select>

    <select id="getMsgBySptinr" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from msg
        where sptinr = #{sptinr,jdbcType=VARCHAR}
    </select>

    <select id="getMsgByMsgid" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from msg
        where msgid = #{msgid,jdbcType=VARCHAR}
    </select>

    <select id="getMsgOwnrefOfSendByMsgid" resultType="java.lang.String">
        select ownref from msg where msgid = #{msgid,jdbcType=VARCHAR}
        and dir='>' limit 1
    </select>

    <select id="selectByWhere" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from msg WHERE 1=1
        <if test="cortyp != null and cortyp != ''">and cortyp = #{cortyp,jdbcType=VARCHAR}</if>
        <if test="msgtyp != null and msgtyp != ''">and msgtyp = #{msgtyp,jdbcType=VARCHAR}</if>
        <if test="dir != null and dir != ''">and dir = #{dir,jdbcType=VARCHAR}</if>
        <if test="credattim != null">and credattim = #{credattim,jdbcType=DATE}</if>
        <if test="msgid != null and msgid != ''">and msgid = #{msgid,jdbcType=VARCHAR}</if>
        <if test="sender != null and sender != ''">and sender = #{sender,jdbcType=VARCHAR}</if>
        <if test="recver != null and recver != ''">and recver = #{recver,jdbcType=VARCHAR}</if>
        <if test="trninr != null and trninr != ''">and trninr = #{trninr,jdbcType=VARCHAR}</if>
        <if test="rtndattim != null">and rtndattim = #{rtndattim,jdbcType=DATE}</if>
        <if test="prcsts != null and prcsts != ''">and prcsts = #{prcsts,jdbcType=VARCHAR}</if>
        <if test="rjctinf != null and rjctinf != ''">and rjctinf = #{rjctinf,jdbcType=VARCHAR}</if>
        <if test="prcdat != null">and prcdat = #{prcdat,jdbcType=DATE}</if>
        <if test="prccod != null and prccod != ''">and prccod = #{prccod,jdbcType=VARCHAR}</if>
        <if test="mesgid != null and mesgid != ''">and mesgid = #{mesgid,jdbcType=VARCHAR}</if>
        <if test="smhinr != null and smhinr != ''">and smhinr = #{smhinr,jdbcType=VARCHAR}</if>
        <if test="orimsgid != null and orimsgid != ''">and orimsgid = #{orimsgid,jdbcType=VARCHAR}</if>
        <if test="ownref != null and ownref != ''">and ownref = #{ownref,jdbcType=VARCHAR}</if>
        <if test="sptinr != null and sptinr != ''">and sptinr = #{sptinr,jdbcType=VARCHAR}</if>
        <if test="sta != null and sta != ''">and sta = #{sta,jdbcType=VARCHAR}</if>
        <if test="prtcnt != null">and prtcnt = #{prtcnt,jdbcType=INTEGER}</if>
        <if test="bchkeyinr != null and bchkeyinr != ''">and bchkeyinr = #{bchkeyinr,jdbcType=VARCHAR}</if>
        <if test="msgtypList != null and msgtypList.size() > 0">
            and msgtyp in
            <foreach collection="msgtypList" open="(" close=")" index="index" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        limit 1
    </select>

    <select id="getMsgIdBySmhInr" resultType="java.lang.String">
        select msgid from msg where smhinr = #{smhinr,jdbcType=VARCHAR}
    </select>

    <select id="getMsgByOrimsgidDirEtyextkey"  resultMap="BaseResultMap">
        select
        msg.inr,msg.cortyp,msg.msgtyp,msg.dir,msg.credattim,msg.msgid,msg.sender,msg.recver,msg.trninr,msg.rtndattim,msg.prcsts,msg.rjctinf,msg.prcdat,msg.prccod,msg.mesgid,msg.smhinr,msg.orimsgid,msg.ownref,msg.sptinr,msg.sta,msg.prtcnt,msg.bchkeyinr,msg.attsta,msg.sndcor,msg.sndkey,msg.valdat,msg.chksta,msg.orgnlpath,msg.f21
         from msg join smh on msg.smhinr = smh.inr
         where  msg.msgid = #{msgid,jdbcType=VARCHAR} and smh.dir = #{dir,jdbcType=VARCHAR} and smh.etyextkey = #{etyextkey,jdbcType=VARCHAR}
    </select>
    
    <update id="updateRjctinfbySmhinr">
        update msg join smh on msg.smhinr = smh.inr set
        msg.rjcinf = #{rjcinf,jdbcType=VARCHAR}
        where(smh.staflg is null or smh.staflg in ('SEND','FSCG')) and msg.smhinr = #{jdbcType=VARCHAR}
    </update>

    <update id="updateOrimsgidAndOripathBymsgid">
        update msg set orimsgid = #{orimsgid,jdbcType=VARCHAR}, orgnlpath = #{orgnlpath,jdbcType=VARCHAR} where msgid  = #{msgid,jdbcType=VARCHAR}
    </update>

    <select id="selectByDMC" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from msg WHERE 1=1 AND UPPER(SUBSTR(MSGTYP,1,6)) IN ('ELCS.1','ELCS.2') and PRCSTS='SUCD'
        <if test="dir != null and dir != ''">and dir = #{dir,jdbcType=VARCHAR}</if>
        <if test="msgtyp != null and msgtyp != ''">and msgtyp = #{msgtyp,jdbcType=VARCHAR}</if>
        <if test="credattim != null">and credattim = #{credattim,jdbcType=DATE}</if>
    </select>

    <select id="selectByMsgidDir" resultType="int">
        select
        count(*)
        from msg
        WHERE msgid = #{msgid} and dir = #{dir}
    </select>

    <select id="selectBySmhinrList" resultType="java.lang.String">
        select msgid from msg
        where smhinr in
        <foreach item="item" index="index" collection="smhinrList" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="selectByMsgidList" resultType="java.lang.String">
        select smhinr from msg
        where orimsgid in
        <foreach item="item" index="index" collection="msgidList" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMsgIdByElcadd" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        msg
        where
        msgtyp in('elcs.103.001.02','ELCS.103.001.02','elcs.201.001.02','ELCS.201.001.02','elcs.202.001.02','ELCS.202.001.02')
        and dir = '&lt;'
    </select>
</mapper>