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

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Cfaerr">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="eblinr" column="eblinr" jdbcType="VARCHAR"/>
        <result property="rptno" column="rptno" jdbcType="VARCHAR"/>
        <result property="dat" column="dat" jdbcType="DATE"/>
        <result property="errtxt" column="errtxt" jdbcType="VARCHAR"/>
        <result property="sta" column="sta" jdbcType="VARCHAR"/>
        <result property="filnam" column="filnam" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="BaseResultMapSel" type="com.brilliance.isc.bo.Cfaerrsel">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="eblinr" column="eblinr" jdbcType="VARCHAR"/>
        <result property="rptno" column="rptno" jdbcType="VARCHAR"/>
        <result property="dat" column="dat" jdbcType="DATE"/>
        <result property="errtxt" column="errtxt" jdbcType="VARCHAR"/>
        <result property="sta" column="sta" jdbcType="VARCHAR"/>
        <result property="filnam" column="filnam" jdbcType="VARCHAR"/>
        <result property="ownref" column="ownref" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,ver,eblinr,rptno,dat,errtxt,sta,filnam
    </sql>

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

    <select id="selectByRptno" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from cfaerr
        where rptno = #{rptno,jdbcType=VARCHAR}
    </select>

    <select id="updateByRptno" resultMap="BaseResultMap">
        update cfaerr set sta = 'D'
        where rptno = #{rptno,jdbcType=VARCHAR}
    </select>

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

    <insert id="insert">
        insert into cfaerr (<include refid="Base_Column_List" />)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=VARCHAR},
            #{ver,jdbcType=VARCHAR},
            #{eblinr,jdbcType=VARCHAR},
            #{rptno,jdbcType=VARCHAR},
            #{dat,jdbcType=DATE},
            #{errtxt,jdbcType=VARCHAR},
            #{sta,jdbcType=VARCHAR},
            #{filnam,jdbcType=VARCHAR},
        </trim>
    </insert>

    <insert id="insertSelective">
        insert into cfaerr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inr != null">inr,</if>
            <if test="ver != null">ver,</if>
            <if test="eblinr != null">eblinr,</if>
            <if test="rptno != null">rptno,</if>
            <if test="dat != null">dat,</if>
            <if test="errtxt != null">errtxt,</if>
            <if test="sta != null">sta,</if>
            <if test="filnam != null">filnam,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inr != null">#{inr,jdbcType=VARCHAR}, </if>
            <if test="ver != null">#{ver,jdbcType=VARCHAR}, </if>
            <if test="eblinr != null">#{eblinr,jdbcType=VARCHAR}, </if>
            <if test="rptno != null">#{rptno,jdbcType=VARCHAR}, </if>
            <if test="dat != null">#{dat,jdbcType=DATE}, </if>
            <if test="errtxt != null">#{errtxt,jdbcType=VARCHAR}, </if>
            <if test="sta != null">#{sta,jdbcType=VARCHAR}, </if>
            <if test="filnam != null">#{filnam,jdbcType=VARCHAR}, </if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update cfaerr
        <set>
            <if test="ver != null">ver = #{ver,jdbcType=VARCHAR},</if>
            <if test="eblinr != null">eblinr = #{eblinr,jdbcType=VARCHAR},</if>
            <if test="rptno != null">rptno = #{rptno,jdbcType=VARCHAR},</if>
            <if test="dat != null">dat = #{dat,jdbcType=DATE},</if>
            <if test="errtxt != null">errtxt = #{errtxt,jdbcType=VARCHAR},</if>
            <if test="sta != null">sta = #{sta,jdbcType=VARCHAR},</if>
            <if test="filnam != null">filnam = #{filnam,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update cfaerr
        <set>
            ver = #{ver,jdbcType=VARCHAR},
            eblinr = #{eblinr,jdbcType=VARCHAR},
            rptno = #{rptno,jdbcType=VARCHAR},
            dat = #{dat,jdbcType=DATE},
            errtxt = #{errtxt,jdbcType=VARCHAR},
            sta = #{sta,jdbcType=VARCHAR},
            filnam = #{filnam,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 cfaerr
        <set>
            <if test="module.ver != null">ver = #{module.ver,jdbcType=VARCHAR},</if>
            <if test="module.eblinr != null">eblinr = #{module.eblinr,jdbcType=VARCHAR},</if>
            <if test="module.rptno != null">rptno = #{module.rptno,jdbcType=VARCHAR},</if>
            <if test="module.dat != null">dat = #{module.dat,jdbcType=DATE},</if>
            <if test="module.errtxt != null">errtxt = #{module.errtxt,jdbcType=VARCHAR},</if>
            <if test="module.sta != null">sta = #{module.sta,jdbcType=VARCHAR},</if>
            <if test="module.filnam != null">filnam = #{module.filnam,jdbcType=VARCHAR},</if>
        </set>
        ${conditions}
    </update>

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

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

    <select id="queryCfaErr" resultMap="BaseResultMapSel">
        SELECT A.RPTNO, A.DAT, A.FILNAM, A.INR ,A.ERRTXT ,C.OWNREF FROM CFAERR A, EBL B
        LEFT OUTER JOIN TRN C ON B.TRNINR = C.INR
        <where>
            A.STA='C' AND A.EBLINR = B.INR
            <if test="bchOwn != null">
                <choose>
                    <when test='cfaSel.ownextkey != null and cfaSel.ownextkey != "" '>
                        and B.OWNEXTKEY=#{cfaSel.ownextkey}
                    </when>
                    <otherwise>
                        <choose>
                            <when test='bchOwn.lev == "0"'>
                                and 1=1
                            </when>
                            <otherwise>
                                <if test="bchList != null">
                                    and B.OWNEXTKEY in
                                    <foreach collection="bchList" item="bchNo" index="index" open="(" close=")" separator=",">
                                        #{bchNo}
                                    </foreach>
                                </if>
                            </otherwise>
                        </choose>
                    </otherwise>
                </choose>
            </if>
            <if test='cfaSel.frmdat != null '>
                AND A.DAT <![CDATA[ >= ]]> date_format(#{cfaSel.frmdat},'%Y-%m-%d')
            </if>
            <if test='cfaSel.tildat != null '>
                AND A.DAT <![CDATA[ <= ]]> date_format(#{cfaSel.tildat},'%Y-%m-%d')
            </if>
            <if test='cfaSel.rptno != null and cfaSel.rptno != "" '>
                AND A.RPTNO LIKE concat(#{cfaSel.rptno},'%')
            </if>
        </where>
    </select>

</mapper>