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

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

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

    <select id="selectWhiteList" resultType="com.hzs.common.domain.system.config.ext.BdAgreementRenewalWhiteExt">
        SELECT c.pk_id           pkId,
               c.pk_member       pkMember,
               m.member_code     memberCode,
               m.member_name     memberName,
               c.creation_time   creationTime,
               ba.PK_TRANSACTION pkAwards,
               bv.VERTEX_NAME    vertexName
        FROM bd_agreement_renewal_white c, cu_member m
        LEFT JOIN bd_vertex bv
          ON bv.pk_id = m.PK_VERTEX
        LEFT JOIN bd_awards ba
          ON ba.pk_id = m.PK_AWARDS
        WHERE c.pk_member = m.pk_id
          AND c.del_flag = 0
          AND c.pk_country = #{pkCountry}
          AND c.type = #{type}
        <if test="memberCode != null and memberCode != ''">
            and m.member_code = #{memberCode}
        </if>
        <if test="startDate != null and startDate != ''">
            and c.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and c.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        order by c.creation_time desc
    </select>


    <select id="isIncludeMemberId" resultType="int">
        SELECT COUNT(1)
        FROM bd_agreement_renewal_white rw
        WHERE rw.del_flag = 0
          AND rw.pk_country =  #{pkCountry}
        <if test="memberPkIds != null and memberPkIds.size > 0">
            and
            rw.pk_member  in
            <foreach collection="memberPkIds" item="item" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 999 == 0">) or rw.pk_member  IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{item}
            </foreach>
        </if>
    </select>


    <select id="selectAgreementWhiteByPkMember" resultType="int">
        SELECT COUNT(1)
        FROM bd_agreement_renewal_white rw
        WHERE rw.del_flag = 0
          AND rw.type = 2
          AND rw.pk_member = #{pkMember}
    </select>

</mapper>
