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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.serviceCharge.BdServiceCharge">
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_ID" property="pkId"/>
        <result column="TYPE" property="type"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
    </resultMap>

    <!-- 根据业务类型查询扣费 -->
    <select id="getServiceChargeByType" parameterType="com.hzs.system.config.dto.ServiceChargeDTO"
            resultType="com.hzs.system.config.dto.ServiceChargeDTO">
        select t.*, ba.pk_transaction_key ACCOUNT_KEY
        from (SELECT a.TYPE, a.PK_ACCOUNT, b.QUANTITY, b.COST
        FROM BD_SERVICE_CHARGE a
        LEFT JOIN BD_SERVICE_CHARGE_DETAIL b
        ON a.PK_ID = b.PK_SERVICE_CHARGE
        WHERE a.DEL_FLAG = 0 and b.DEL_FLAG=0
        AND a.PK_COUNTRY = #{pkCountry}
        <if test="type != null ">
            AND a.TYPE = #{type}
        </if>
        <if test="quantity != null ">
            AND #{quantity} >= b.QUANTITY
        </if>
        order by b.QUANTITY desc) t
        inner join BD_ACCOUNT ba
        on ba.pk_id = t.PK_ACCOUNT
        where rownum = 1
    </select>

    <select id="selectByList" resultType="com.hzs.system.config.vo.ServiceChargeVo" >
          select sc.*,ba.ACCOUNT_NAME accountName  from BD_SERVICE_CHARGE sc
          left join BD_ACCOUNT ba on ba.PK_ID = sc.PK_ACCOUNT
          where  1 = 1 and sc.DEL_FLAG = 0
        <if test="type != null">
            AND sc.TYPE = #{type}
        </if>
        <if test="pkCountry != null">
            and sc.PK_COUNTRY = #{pkCountry}
        </if>
    </select>

</mapper>
