<?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.member.empty.mapper.CuMemberEmptyMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberEmpty" type="com.hzs.common.domain.member.empty.ext.CuMemberEmptyExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="BUSINESS_CODE" property="businessCode"/>
        <result column="REMARK" property="remark"/>
        <result column="STATUS" property="status"/>
        <collection property="cuMemberEmptyDetailExtList" ofType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
            <result column="PK_MEMBER_EMPTY" property="pkMemberEmpty"/>
            <result column="MEMBER_CODE" property="memberCode"/>
            <result column="MEMBER_NAME" property="memberName"/>
            <result column="PHONE" property="phone"/>
            <result column="PARENT_CODE" property="parentCode"/>
            <result column="PLACE_PARENT_CODE" property="placeParentCode"/>
            <result column="PLACE_DEPT" property="placeDept"/>
            <result column="PK_REGISTER_GRADE" property="pkRegisterGrade"/>
            <result column="REGISTER_ACHIEVE" property="registerAchieve"/>
            <result column="ORDER_SOURCE" property="orderSource"/>
            <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
            <result column="PK_AWARDS" property="pkAwards"/>
            <result column="BACK_AMOUNT" property="backAmount"/>
            <result column="POINT_STATUS" property="pointStatus"/>
            <result column="SETTLEMENT_FLAG" property="settlementFlag"/>
            <result column="SETTLEMENT_ACHIEVE" property="settlementAchieve"/>
            <result column="TRANSFER_ACHIEVE" property="transferAchieve"/>
            <result column="REC_NAME" property="recName"/>
            <result column="REC_PHONE" property="recPhone"/>
            <result column="REC_PROVINCE" property="recProvince"/>
            <result column="REC_CITY" property="recCity"/>
            <result column="REC_COUNTY" property="recCounty"/>
            <result column="REC_ADDRESS" property="recAddress"/>
            <result column="CONTENT_ABSTRACT" property="contentAbstract"/>
            <result column="PK_CREATOR" property="pkCreator"/>
        </collection>
    </resultMap>

    <!-- 查询生成列表 -->
    <select id="queryList" resultMap="CuMemberEmpty">
        select cme.*
        from cu_member_empty cme
        where cme.del_flag = 0
        and cme.pk_country = #{pkCountry}
        <if test="param.businessCode != null and param.businessCode != ''">
            and cme.business_code like #{param.businessCode} || '%'
        </if>
        <if test="param.remark != null and param.remark != ''">
            and cme.remark like #{param.remark} || '%'
        </if>
        <if test="param.creationTime != null">
            and cme.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= cme.creation_time
        </if>
        order by cme.creation_time desc
    </select>

    <!-- 空单注册列表 -->
    <select id="emptyRegisterList" resultType="com.hzs.common.domain.member.empty.ext.CuMemberEmptyDetailExt">
        select
        cme.business_code,
        cmed.pk_id,
        cmed.member_code,
        cmed.member_name,
        cmed.phone,
        cmed.parent_code,
        cmed.place_parent_code,
        cmed.place_dept,
        bg.pk_transaction pk_grade_key,
        cmed.register_achieve,
        cmed.order_source,
        bcsc.short_name settle_country_name,
        bcc.short_name country_name,
        ba.pk_transaction pk_award_key,
        cmed.order_type,
        cmed.back_amount,
        cmed.settlement_flag,
        cmed.settlement_achieve,
        cmed.transfer_achieve,
        cmed.product_flag,
        cmed.rec_name,
        cmed.rec_phone,
        bap.name rec_province_val,
        bac.name rec_city_val,
        baco.name rec_county_val,
        cmed.rec_address
        from cu_member_empty cme
        inner join CU_MEMBER_EMPTY_DETAIL cmed
        on cmed.pk_member_empty = cme.pk_id
        and cmed.del_flag = 0
        left join bd_grade bg
        on bg.pk_id = cmed.pk_register_grade
        left join bd_awards ba
        on ba.pk_id = cmed.pk_awards
        inner join bd_country bcc
        on bcc.pk_id = cmed.pk_country
        inner join bd_country bcsc
        on bcsc.pk_id = cmed.pk_settle_country
        left join bd_area bap
        on bap.pk_id = cmed.rec_province
        left join bd_area bac
        on bac.pk_id = cmed.rec_city
        left join bd_area baco
        on baco.pk_id = cmed.rec_county
        where cme.del_flag = 0
        and cme.pk_country = #{pkCountry}
        and cme.status = 5
        <if test="param.businessCode != null and param.businessCode != ''">
            and cme.business_code like #{param.businessCode} || '%'
        </if>
        <if test="param.memberCode != null and param.memberCode != ''">
            and cme.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.memberName != null and param.memberName != ''">
            and cme.member_name like #{param.memberName} || '%'
        </if>
        <if test="param.phone != null and param.phone != ''">
            and cme.phone like #{param.phone} || '%'
        </if>
        <if test="param.pkRegisterGrade != null">
            and cmed.pk_register_grade = #{param.pkRegisterGrade}
        </if>
        <if test="param.pkAwards != null">
            and cmed.pk_awards = #{param.pkAwards}
        </if>
        <if test="param.pkSettleCountry != null">
            and cmed.pk_settle_country = #{param.pkSettleCountry}
        </if>
        <if test="param.pkCountry != null">
            and cmed.pk_country = #{param.pkCountry}
        </if>
        <if test="param.settlementFlag != null">
            and cmed.settlement_flag = #{param.settlementFlag}
        </if>
        <if test="param.creationTime != null">
            and cmed.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= cmed.creation_time
        </if>
        order by cme.business_code desc, cmed.pk_id
    </select>

    <!-- 查询最新的编号 -->
    <select id="queryLastCode" resultType="string">
        select max(cme.BUSINESS_CODE) code
        from CU_MEMBER_EMPTY cme
    </select>

</mapper>
