<?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.bonus.base.mapper.CuMemberMapper">

    <!-- 查询登录会员以及状态 -->
    <resultMap id="MemberResultMap" type="com.hzs.common.domain.member.ext.CuMemberExt">
        <id column="PK_ID" property="pkId"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="CUSTOMER_TYPE" property="customerType"/>
        <result column="PHONE" property="phone"/>
        <result column="ID_CARD" property="idCard"/>
        <result column="SEX" property="sex"/>
        <result column="BIRTH_DATE" property="birthDate"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="PARENT" property="parent"/>
        <result column="PK_PARENT" property="pkParent"/>
        <result column="PK_PLACE_PARENT" property="pkPlaceParent"/>
        <result column="PLACE_DEPT" property="placeDept"/>
        <result column="PK_TEAM_CODE" property="pkTeamCode"/>
        <result column="PK_CENTER_CODE" property="pkCenterCode"/>
        <result column="SOURCE" property="source"/>
        <result column="PK_REGISTER_GRADE" property="pkRegisterGrade"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="CONSUME_MONEY" property="consumeMoney"/>
        <result column="CONSUME_ACHIEVE" property="consumeAchieve"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus"/>
        <result column="SHARE_STATUS" property="shareStatus"/>
        <result column="PAY_TIME" property="payTime"/>
        <result column="PAY_STATUS" property="payStatus"/>
        <result column="LOGIN_PWD" property="loginPwd"/>
        <result column="PAY_PWD" property="payPwd"/>
        <result column="LOGIN_PASSWORD" property="loginPassword"/>
        <result column="PAY_PASSWORD" property="payPassword"/>
        <result column="EDIT_NUMBER" property="editNumber"/>
        <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="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="PK_VERTEX" property="pkVertex"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="PAY_STATUS" property="payStatus"/>
        <result column="CATEGORY" property="category"/>
        <result column="EMAIL" property="email"/>
        <result column="ORDER_SOURCE" property="orderSource"/>
        <result column="IS_REAL_NAME" property="isRealName"/>
        <result column="IS_BUS_LICENSE" property="isBusLicense"/>
        <result column="IS_MAKER_SPACE" property="isMakerSpace"/>
        <result column="CHANGE_NUMBER" property="changeNumber"/>
        <result column="WECHAT" property="wechat"/>
        <result column="CARD_TYPE" property="cardType"/>
        <result column="NICK_NAME" property="nickName"/>
        <result column="HEAD_PATH" property="headPath"/>
        <result column="DIRECT_PUSH_NUMBER" property="directPushNumber"/>
        <result column="IS_DEALER" property="isDealer"/>
        <result column="SYSTEM_LEVEL" property="systemLevel"/>
        <result column="LECTURER_LEVEL" property="lecturerLevel"/>
        <result column="SUPER_STUDIO" property="superStudio"/>
        <result column="ID_FRONT" property="idFront"/>
        <result column="ID_BACK" property="idBack"/>
        <result column="PK_PROVINCE" property="pkProvince"/>
        <result column="PK_CITY" property="pkCity"/>
        <result column="PK_COUNTY" property="pkCounty"/>
        <result column="ADDRESS" property="address"/>
        <result column="BANK_NAME" property="bankName"/>
        <result column="SUB_BANK_NAME" property="subBankName"/>
        <result column="ACCOUNT_NAME" property="accountName"/>
        <result column="reservedPhone" property="reservedPhone"/>
        <result column="CARD_NUMBER" property="cardNumber"/>
        <result column="certificationTime" property="certificationTime"/>
        <result column="directPushNumber" property="directPushNumber"/>
        <result column="ID_TYPE" property="idType"/>
        <result column="GLOBAL_CODE" property="globalCode"/>
        <result column="COUNTRY_CODE" property="countryCode"/>
        <result column="recommendedOrderNumber" property="recommendedOrderNumber"/>
        <result column="GLOBAL_CODE" property="globalCode"/>
        <result column="COUNTRY_CODE" property="countryCode"/>
        <result column="GRADE_VALUE" property="gradeValue"/>
        <result column="REGISTER_GRADEVAL" property="registerGradeVal"/>
        <result column="AWARDS_VAL" property="awardsVal"/>
        <result column="TEAM_CODE" property="teamCode"/>
        <result column="TEAM_NAME" property="teamName"/>
    </resultMap>

    <!-- 更新会员奖衔根据临时表 -->
    <update id="updateCuMemberByPeriod">
        merge into cu_member a
            using (
                select pk_member, max(new_level) pk_awards
                from cu_member_awards
                where period = #{period}
                  and del_flag = 0
                group by pk_member
            ) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_awards = b.pk_awards,
                    a.modified_time = sysdate
    </update>

    <update id="updateCuMemberRangeByPeriod">
        merge into cu_member a
            using (
                select pk_member, pk_range_awards, ba.pk_awards
                from (
                         select pk_member, max(new_level) pk_range_awards
                         from cu_member_awards
                         where period = #{period}
                           and del_flag = 0
                         group by pk_member) ca
                         inner join bd_range_awards ba
                                    on ca.pk_range_awards = ba.pk_id
                where ba.del_flag = 0
                order by pk_member
            ) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_range_awards = b.pk_range_awards,
                    a.pk_awards = b.pk_awards,
                    a.modified_time = sysdate
    </update>

    <!-- 通用查询映射结果 -->
    <select id="queryLoginMember" resultMap="MemberResultMap">
        select cm.*
        from cu_member cm
        where cm.del_flag = 0
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code = #{memberCode}
        </if>
        <if test="pkMember != null">
            and cm.pk_id = #{pkMember}
        </if>
    </select>

    <select id="queryCuMemberShareTotal" resultType="com.hzs.common.domain.member.base.CuMember">
        select cm.pk_awards pkAwards, count(cm.pk_id) shareStatus
        from cu_member cm
                 inner join bd_awards ba
                            on cm.pk_awards = ba.pk_id
        where cm.del_flag = 0
          and cm.pay_status = 1
          and pk_settle_country = #{pkCountry}
          and ba.awards_value > 0
        group by cm.pk_awards
        order by pk_awards
    </select>

    <!-- 回退会员奖衔 -->
    <update id="updateBackCuMemberAward">
        merge into cu_member a
            using (select pk_member, max(old_level) old_level
                   from cu_member_awards
                   where period = #{period}
                     and del_flag = 0
                   group by pk_member) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_awards = b.old_level,
                    a.modified_time = sysdate
    </update>

    <!-- 回退会员等级 -->
    <delete id="updateBackCuMemberGrade">
        merge into cu_member a
            using (
                select t.*, bg.register_authority
                from (
                         select pk_member, max(old_level) pk_grade
                         from cu_member_level
                         where period = #{period}
                           and del_flag = 0
                         group by pk_member) t
                         left join bd_grade bg
                                   on bg.pk_id = t.pk_grade
            ) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_settle_grade = b.pk_grade,
                    a.register_authority =
                            (select case
                                        when a.register_authority > b.register_authority then
                                            a.register_authority
                                        else
                                            b.register_authority
                                        end
                             from dual),
                    a.modified_time = sysdate
    </delete>

    <!-- 更新会员等级根据临时表 -->
    <update id="updateCuMemberLevelByPeriod">
        merge into cu_member a
            using (
                select t.*, bg.register_authority
                from (select pk_member, max(new_level) pk_grade
                      from cu_member_level
                      where period = #{period}
                        and del_flag = 0
                      group by pk_member) t
                         left join bd_grade bg
                                   on bg.pk_id = t.pk_grade
            ) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_settle_grade = b.pk_grade,
                    a.register_authority =
                            (select case
                                        when b.register_authority > a.register_authority then
                                            b.register_authority
                                        else
                                            a.register_authority
                                        end
                             from dual),
                    a.modified_time = sysdate
    </update>

    <!-- 查询推荐伞上 -->
    <select id="listParentMember" resultType="com.hzs.common.domain.member.ext.CuMemberExt">
        select cm.pk_id
        from cu_member cm
            start with cm.pk_id = #{pkMember}
        connect by cm.pk_id = prior cm.pk_parent
    </select>

</mapper>
