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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.config.ext.BdMemberSpecialCurrencyExt">
        <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_MEMBER" property="pkMember"/>
        <result column="SPECIAL_AREA" property="specialArea"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <collection property="detailList" ofType="com.hzs.common.domain.system.config.ext.BdMemberSpecialDetailExt">
            <result column="DETAIL_ID" property="pkId"/>
            <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="ACCOUNT_NAME" property="accountName"/>
        </collection>
    </resultMap>

    <!-- 查询配置列表 -->
    <select id="listData" resultMap="BaseResultMap">
        select bmsc.*,
        cm.member_code,
        cm.member_name,
        bmd.pk_id detail_id,
        bmd.pk_account,
        bmd.deduct_sort,
        bmd.deduct_ratio,
        bmd.deduct_max_ratio,
        ba.account_name
        from BD_MEMBER_SPECIAL_CURRENCY bmsc
        left join cu_member cm
        on cm.pk_id = bmsc.pk_member
        left join BD_MEMBER_SPECIAL_DETAIL bmd
        on bmd.pk_member_special_currency = bmsc.pk_id
        left join bd_account ba
        on ba.pk_id = bmd.pk_account
        where bmsc.del_flag = 0
        and bmd.del_flag = 0
        <if test="param.pkCountry != null">
            and bmsc.pk_country = #{param.pkCountry}
        </if>
        <if test="param.systemType != null">
            and bmsc.system_type = #{param.systemType}
        </if>
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code = #{param.memberCode}
        </if>
        order by bmsc.CREATION_TIME desc, bmd.deduct_sort
    </select>

    <!-- 查询配置数据 -->
    <select id="getData" resultMap="BaseResultMap">
        select bmsc.*,
               cm.member_code,
               cm.member_name,
               bmd.pk_id detail_id,
               bmd.pk_account,
               bmd.deduct_sort,
               bmd.deduct_ratio,
               bmd.deduct_max_ratio,
               ba.account_name
        from BD_MEMBER_SPECIAL_CURRENCY bmsc
                 left join cu_member cm
                           on cm.pk_id = bmsc.pk_member
                 left join BD_MEMBER_SPECIAL_DETAIL bmd
                           on bmd.pk_member_special_currency = bmsc.pk_id
                 left join bd_account ba
                           on ba.pk_id = bmd.pk_account
        where bmsc.del_flag = 0
          and bmd.del_flag = 0
          and bmsc.pk_id = #{pkId}
    </select>

    <!-- 查询会员配置 -->
    <select id="listMemberDetail" resultType="com.hzs.common.domain.system.config.ext.BdMemberSpecialDetailExt">
        select bmsd.*, ba.field_value, ba.IS_POSTAGE, ba.account_name
        from BD_MEMBER_SPECIAL_CURRENCY bmsc
                 left join BD_MEMBER_SPECIAL_DETAIL bmsd
                           on bmsd.pk_member_special_currency = bmsc.pk_id
                               and bmsd.del_flag = 0
                 left join bd_account ba
                           on ba.pk_id = bmsd.pk_account
        where bmsc.del_flag = 0
          and bmsc.pk_member = #{pkMember}
          and bmsc.special_area = #{specialArea}
        order by bmsd.deduct_sort
    </select>

</mapper>
