<?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.activity.policy.mapper.AcPolicyConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.policy.AcPolicyConfig">
        <id column="PK_ID" property="pkId"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="ACHIEVE" property="achieve"/>
        <result column="POLICYCURRENCY" property="policycurrency"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        CREATION_TIME,
        PK_CREATOR,
        MODIFIED_TIME,
        PK_MODIFIED,
        PK_COUNTRY,
        DEL_FLAG,
        PK_ID, ACHIEVE, POLICYCURRENCY
    </sql>

    <delete id="deleteByPkCountryAll">
        delete from AC_POLICY_CONFIG where PK_COUNTRY = #{pkCountry} and PK_BASE_ID = #{pkBaseId}
        <if test="notIdlList != null and notIdlList.size > 0">
            and PK_ID not in
            <foreach collection="notIdlList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </delete>

    <!-- 查询赠送政策币最新配置 -->
    <select id="queryLastConfig" resultMap="BaseResultMap">
        select *
        from (select apc.*
              from AC_BASE_CONFIG abc
                       left join AC_POLICY_CONFIG apc
                                 on apc.pk_base_id = abc.pk_id
              where abc.del_flag = 0
                and abc.act_type = 20
                and abc.pk_country = #{pkCountry}
                and apc.del_flag = 0
                and #{orderAchieve} >= apc.achieve
                and trunc(#{payDate}) between abc.act_start_date and abc.act_end_date
              order by apc.achieve desc) t
        where rownum = 1
    </select>

</mapper>
