<?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.hzs.system.config.mapper.BdSpecialCurrencyDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BdSpecialCurrencyDetail" type="com.hzs.common.domain.system.config.BdSpecialCurrencyDetail">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="DEDUCT_SORT" property="deductSort"/>
        <result column="DEDUCT_RATIO" property="deductRatio"/>
        <result column="DEDUCT_MAX_RATIO" property="deductMaxRatio"/>
    </resultMap>

    <update id="saveBdSpecialCurrencyDetail">
        merge into bd_special_currency_detail a
        using (
        select pk_id,pk_special_currency,pk_account,deduct_sort,deduct_ratio, deduct_max_ratio,
        #{specialCurrencyParam.currentTime} current_time,#{specialCurrencyParam.userId} user_id,#{specialCurrencyParam.pkCountry} pk_country from(
        <foreach collection="specialCurrencyParam.specialCurrencyDetailParamList" item="item" index="index" separator=" union ">select
            #{item.pkId,jdbcType=INTEGER} pk_id,#{specialCurrencyParam.pkId,jdbcType=INTEGER} pk_special_currency,#{item.pkAccount,jdbcType=INTEGER} pk_account,
            #{item.deductSort,jdbcType=INTEGER} deduct_sort,#{item.deductRatio, jdbcType=NUMERIC} deduct_ratio, #{item.deductMaxRatio, jdbcType=NUMERIC} deduct_max_ratio
            from dual
        </foreach>
        )
        ) b
        on (a.pk_id = b.pk_id)
        WHEN NOT MATCHED THEN
        insert (pk_id,pk_special_currency,pk_account,deduct_sort,deduct_ratio, deduct_max_ratio, creation_time,pk_creator, pk_country)
        values (bd_special_currency_detail_seq.nextval,b.pk_special_currency,b.pk_account,b.deduct_sort,b.deduct_ratio, b.deduct_max_ratio,
        b.current_time,b.user_id,b.pk_country)
        WHEN MATCHED THEN
        update set a.pk_special_currency = b.pk_special_currency,a.pk_account=b.pk_account,a.deduct_sort=b.deduct_sort,
        a.deduct_ratio=b.deduct_ratio, a.deduct_max_ratio=b.deduct_max_ratio,
        a.modified_time=b.current_time,a.pk_modified=b.user_id
    </update>

    <update id="deleteSpecialCurrencyDetail">
        update bd_special_currency_detail set del_flag=1,modified_time=#{specialCurrencyParam.currentTime},pk_modified=#{specialCurrencyParam.userId}
        where pk_special_currency=#{specialCurrencyParam.pkId,jdbcType=INTEGER} and
        pk_id not in
        <foreach collection="specialCurrencyDetailParamList" item="item" open="(" close=")" separator=",">
            #{item.pkId,jdbcType=INTEGER}
        </foreach>
    </update>

</mapper>
