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

    <resultMap id="BdSpecialCurrencyExt" type="com.hzs.common.domain.system.config.ext.BdSpecialCurrencyExt">
        <id column="PK_ID" property="pkId"/>
        <id column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="TEMP_NAME" property="tempName"/>
        <result column="PK_TRANSACTION_KEY" property="pkTransactionKey"/>
        <collection property="specialCurrencyDetailList"
                    ofType="com.hzs.common.domain.system.config.ext.BdSpecialCurrencyDetailExt">
            <result column="PK_DETAIL_ID" property="pkId"/>
            <result column="PK_SPECIAL_CURRENCY" property="pkSpecialCurrency"/>
            <result column="PK_ACCOUNT" property="pkAccount"/>
            <result column="PK_ACCOUNT_TRAN" property="pkAccountTran"/>
            <result column="DEDUCT_SORT" property="deductSort"/>
            <result column="DEDUCT_RATIO" property="deductRatio"/>
            <result column="DEDUCT_MAX_RATIO" property="deductMaxRatio"/>
            <result column="ACCOUNT_NAME" property="accountName"/>
            <result column="FIELD_VALUE" property="fieldValue"/>
        </collection>
    </resultMap>

    <resultMap id="BdSpecialCurrencyDetail" type="com.hzs.common.domain.system.config.ext.BdSpecialCurrencyDetailExt">
        <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"/>
        <result column="FIELD_VALUE" property="fieldValue"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <select id="querySpecialCurrencyList" resultMap="BdSpecialCurrencyExt">
        select bc.pk_id,bc.temp_name,bc.creation_time,bc.pk_creator,bd.pk_id pk_detail_id,bd.pk_account,
        ba.pk_transaction_key pk_account_tran,
        bd.deduct_sort,bd.deduct_ratio, bd.deduct_max_ratio, ba.account_name,ba.field_value from
        bd_special_currency bc
        inner join bd_special_currency_detail bd
        on bc.pk_id = bd.pk_special_currency
        left join bd_account ba
        on ba.pk_id=bd.pk_account
        where bc.del_flag=0 and bd.del_flag=0
        <if test="pkCountry != null">
            and bc.pk_country=#{pkCountry}
        </if>
        <if test="pkId != null">
            and bc.pk_id = #{pkId}
        </if>
        order by bc.pk_id,bd.deduct_sort
    </select>

    <select id="queryAreaCurrencyBySpecialArea" resultMap="BdSpecialCurrencyDetail" >

         select bd.*,ba.FIELD_VALUE  from    bd_special_currency bc
         inner join bd_special_currency_detail bd on bd.pk_special_currency = bc.pk_id
         inner join BD_ACCOUNT ba on ba.pk_id = bd.PK_ACCOUNT
         where 1 = 1 and bd.del_flag = 0
        <if test="null != pkCurrencySet and pkCurrencySet.size > 0">
            and bc.PK_ID in
            <foreach collection="pkCurrencySet" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        and ba.FIELD_VALUE = #{account}
        order by bd.pk_special_currency, bd.deduct_sort
    </select>

</mapper>
