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

    <resultMap id="BaseResultMap" type="com.brilliance.isc.bo.Cty">
        <result property="inr" column="inr" jdbcType="VARCHAR"/>
        <result property="cod" column="cod" jdbcType="VARCHAR"/>
        <result property="cur" column="cur" jdbcType="VARCHAR"/>
        <result property="reg" column="reg" jdbcType="VARCHAR"/>
        <result property="ver" column="ver" jdbcType="VARCHAR"/>
        <result property="fmtdomadrtyp" column="fmtdomadrtyp" jdbcType="VARCHAR"/>
        <result property="fmtintadrtyp" column="fmtintadrtyp" jdbcType="VARCHAR"/>
        <result property="dhlreg" column="dhlreg" jdbcType="VARCHAR"/>
        <result property="bopcod" column="bopcod" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        inr,cod,cur,reg,ver,fmtdomadrtyp,fmtintadrtyp,dhlreg,bopcod
    </sql>

    <select id="select" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from cty
        <where>
            <if test="inr != null">
                and inr = #{inr,jdbcType=VARCHAR}
            </if>
            <if test="cod != null">
                and cod = #{cod,jdbcType=VARCHAR}
            </if>
            <if test="cur != null">
                and cur = #{cur,jdbcType=VARCHAR}
            </if>
            <if test="reg != null">
                and reg = #{reg,jdbcType=VARCHAR}
            </if>
            <if test="ver != null">
                and ver = #{ver,jdbcType=VARCHAR}
            </if>
            <if test="fmtdomadrtyp != null">
                and fmtdomadrtyp = #{fmtdomadrtyp,jdbcType=VARCHAR}
            </if>
            <if test="fmtintadrtyp != null">
                and fmtintadrtyp = #{fmtintadrtyp,jdbcType=VARCHAR}
            </if>
            <if test="dhlreg != null">
                and dhlreg = #{dhlreg,jdbcType=VARCHAR}
            </if>
            <if test="bopcod != null">
                and bopcod = #{bopcod,jdbcType=VARCHAR}
            </if>
        </where>
    </select>

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

    <select id="selectByCod" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from cty
        where cod = #{cod,jdbcType=VARCHAR}
    </select>

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

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

    <insert id="insert">
        insert into cty (<include refid="Base_Column_List"/>)
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{inr,jdbcType=VARCHAR},
            #{cod,jdbcType=VARCHAR},
            #{cur,jdbcType=VARCHAR},
            #{reg,jdbcType=VARCHAR},
            #{ver,jdbcType=VARCHAR},
            #{fmtdomadrtyp,jdbcType=VARCHAR},
            #{fmtintadrtyp,jdbcType=VARCHAR},
            #{dhlreg,jdbcType=VARCHAR},
            #{bopcod,jdbcType=VARCHAR},
        </trim>
    </insert>

    <insert id="insertSelective">
        insert into cty
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="inr != null">inr,</if>
            <if test="cod != null">cod,</if>
            <if test="cur != null">cur,</if>
            <if test="reg != null">reg,</if>
            <if test="ver != null">ver,</if>
            <if test="fmtdomadrtyp != null">fmtdomadrtyp,</if>
            <if test="fmtintadrtyp != null">fmtintadrtyp,</if>
            <if test="dhlreg != null">dhlreg,</if>
            <if test="bopcod != null">bopcod,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="inr != null">#{inr,jdbcType=VARCHAR},</if>
            <if test="cod != null">#{cod,jdbcType=VARCHAR},</if>
            <if test="cur != null">#{cur,jdbcType=VARCHAR},</if>
            <if test="reg != null">#{reg,jdbcType=VARCHAR},</if>
            <if test="ver != null">#{ver,jdbcType=VARCHAR},</if>
            <if test="fmtdomadrtyp != null">#{fmtdomadrtyp,jdbcType=VARCHAR},</if>
            <if test="fmtintadrtyp != null">#{fmtintadrtyp,jdbcType=VARCHAR},</if>
            <if test="dhlreg != null">#{dhlreg,jdbcType=VARCHAR},</if>
            <if test="bopcod != null">#{bopcod,jdbcType=VARCHAR},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective">
        update cty
        <set>
            <if test="cod != null">cod = #{cod,jdbcType=VARCHAR},</if>
            <if test="cur != null">cur = #{cur,jdbcType=VARCHAR},</if>
            <if test="reg != null">reg = #{reg,jdbcType=VARCHAR},</if>
            <if test="ver != null">ver = #{ver,jdbcType=VARCHAR},</if>
            <if test="fmtdomadrtyp != null">fmtdomadrtyp = #{fmtdomadrtyp,jdbcType=VARCHAR},</if>
            <if test="fmtintadrtyp != null">fmtintadrtyp = #{fmtintadrtyp,jdbcType=VARCHAR},</if>
            <if test="dhlreg != null">dhlreg = #{dhlreg,jdbcType=VARCHAR},</if>
            <if test="bopcod != null">bopcod = #{bopcod,jdbcType=VARCHAR},</if>
        </set>
        where inr = #{inr,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey">
        update cty
        <set>
            cod = #{cod,jdbcType=VARCHAR},
            cur = #{cur,jdbcType=VARCHAR},
            reg = #{reg,jdbcType=VARCHAR},
            ver = #{ver,jdbcType=VARCHAR},
            fmtdomadrtyp = #{fmtdomadrtyp,jdbcType=VARCHAR},
            fmtintadrtyp = #{fmtintadrtyp,jdbcType=VARCHAR},
            dhlreg = #{dhlreg,jdbcType=VARCHAR},
            bopcod = #{bopcod,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 cty
        <set>
            <if test="module.cod != null">cod = #{module.cod,jdbcType=VARCHAR},</if>
            <if test="module.cur != null">cur = #{module.cur,jdbcType=VARCHAR},</if>
            <if test="module.reg != null">reg = #{module.reg,jdbcType=VARCHAR},</if>
            <if test="module.ver != null">ver = #{module.ver,jdbcType=VARCHAR},</if>
            <if test="module.fmtdomadrtyp != null">fmtdomadrtyp = #{module.fmtdomadrtyp,jdbcType=VARCHAR},</if>
            <if test="module.fmtintadrtyp != null">fmtintadrtyp = #{module.fmtintadrtyp,jdbcType=VARCHAR},</if>
            <if test="module.dhlreg != null">dhlreg = #{module.dhlreg,jdbcType=VARCHAR},</if>
            <if test="module.bopcod != null">bopcod = #{module.bopcod,jdbcType=VARCHAR},</if>
        </set>
        ${conditions}
    </update>

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

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

    <select id="getDhlregByCode" resultType="String">
        select DHLREG from CTY where COD = #{code, jdbcType=VARCHAR}
    </select>
    <insert id="insertCty">

        insert into CTY(INR, COD, CUR, REG, VER, FMTDOMADRTYP, FMTINTADRTYP, DHLREG, BOPCOD)
        values (#{inr}, #{cod}, #{cur}, #{reg}, '0000', #{fmtdomadrtyp}, #{fmtintadrtyp}, #{dhlreg}, #{bopcod})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update CTY
        <set>
            <if test="cod != null and cod != ''">
                COD = #{cod},
            </if>
            <if test="cur != null and cur != ''">
                CUR = #{cur},
            </if>
            REG = #{reg},
            <if test="ver != null and ver != ''">
                VER = #{ver},
            </if>
            <if test="fmtdomadrtyp != null and fmtdomadrtyp != ''">
                FMTDOMADRTYP = #{fmtdomadrtyp},
            </if>
            <if test="fmtintadrtyp != null and fmtintadrtyp != ''">
                FMTINTADRTYP = #{fmtintadrtyp},
            </if>
            <if test="dhlreg != null and dhlreg != ''">
                DHLREG = #{dhlreg},
            </if>
            BOPCOD = #{bopcod},
        </set>
        where INR= #{inr}
    </update>

    <delete id="deleteById">
        delete from CTY where  INR= #{inr}
    </delete>

    <select id="getCtyVerFromInr" resultType="String">
        select VER from CTY where INR= #{inr}
    </select>

    <select id="queryCod" resultType="java.lang.Integer">
        select count(*) from cty where rtrim(cod) = #{cod}
    </select>

    <select id="queryById" resultType="com.brilliance.isc.vo.manager.CtyVo">
        select A.INR inr, A.COD cod, A.CUR cur, A.REG reg, A.VER ver, A.FMTDOMADRTYP fmtdomadrtyp, A.FMTINTADRTYP fmtintadrtyp, A.DHLREG dhlreg, A.BOPCOD bopcod,
       B.INR inrBopcty, B.NUMCOD numcodBopcty, B.TXT txtBopcty, B.FULTXT fultxtBopcty, B.VER verBopcty
       from CTY A LEFT JOIN BOPCTY B on A.BOPCOD=B.COD where A.INR = #{inr}
    </select>

    <select id="queryAllByLimit" resultType="com.brilliance.isc.vo.manager.CtyVo">
        select
        A.INR inr, A.COD cod, A.CUR cur, A.REG reg, A.BOPCOD bopcod, B.TXT txtBopcty,
        (SELECT txt FROM stb C WHERE C.COD = A.COD AND  c.uil = 'EN' AND c.tbl='CTYTXT') ctynam
        from CTY A LEFT JOIN BOPCTY B on A.BOPCOD=B.COD

        <where>
            <if test="cod != null and cod != ''">
                and A.COD = #{cod}
            </if>
            <if test="bopcod != null and bopcod != ''">
                and A.BOPCOD = #{bopcod}
            </if>
        </where>
        order by
        <if test='columnStr != null and columnStr!="" '>
            ${columnStr} ${orderStr}
        </if>
        <if test='columnStr == null or columnStr=="" '>
            A.INR desc
        </if>
    </select>

    <select id="queryCodByBopCod" resultType="String">
        select cod from CTY where rtrim(bopcod)= #{bopcod}
    </select>

</mapper>