<?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>

    <resultMap id="MemberVO" type="com.hzs.bonus.base.vo.CuMemberVO">
        <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="PAR_MEMBER_CODE" property="parMemberCode"/>
        <result column="PAR_MEMBER_NAME" property="parMemberName"/>
        <result column="PLACE_MEMBER_CODE" property="placeMemberCode"/>
        <result column="PLACE_MEMBER_NAME" property="placeMemberName"/>
        <result column="PLACE_DEPT" property="placeDept"/>
        <result column="CENTER_MEMBER_CODE" property="centerMemberCode"/>
        <result column="CENTER_MEMBER_NAME" property="centerMemberName"/>
        <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="EDIT_NUMBER" property="editNumber"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="TOP_MEMBER_CODE" property="topMemberCode"/>
        <result column="TOP_MEMBER_NAME" property="topMemberName"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="WALLET_STATUS" property="walletStatus"/>
        <result column="PAY_STATUS" property="payStatus"/>
        <result column="CATEGORY" property="category"/>
        <result column="EMAIL" property="email"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="ORDER_SOURCE" property="orderSource"/>
        <result column="IS_REAL_NAME" property="isRealName"/>
        <result column="IS_BUS_LICENSE" property="isBusLicense"/>
        <result column="CHANGE_NUMBER" property="changeNumber"/>
        <result column="WECHAT" property="wechat"/>
        <result column="CARD_TYPE" property="cardType"/>
        <result column="ID_CARD_JUST" property="idCardJust"/>
        <result column="ID_CARD_BACK" property="idCardBack"/>
        <result column="GIFT_PROVINCE" property="giftProvince"/>
        <result column="GIFT_CITY" property="giftCity"/>
        <result column="GIFT_COUNTY" property="giftCounty"/>
        <result column="GIFT_ADDRESS" property="giftAddress"/>
        <result column="BANK_NAME" property="bankName"/>
        <result column="SUB_BANK_NAME" property="subBankName"/>
        <result column="ACCOUNT_NAME" property="accountName"/>
        <result column="PHONE" property="reservePhone"/>
        <result column="CARD_NUMBER" property="cardNumber"/>
        <result column="NICK_NAME" property="nickName"/>
        <result column="HEAD_PATH" property="headPath"/>
        <result column="agent_grade" property="agentGrade"/>
        <result column="DIRECT_PUSH_NUMBER" property="directPushNumber"/>
    </resultMap>

    <sql id="CuMember_Column">
        select cm.pk_id,
               cm.member_code,
               cm.member_name,
               cm.phone,
               cm.pk_settle_country,
               cm.pk_parent,
               cm.pk_place_parent,
               cm.place_dept,
               cm.pk_team_code,
               cm.pk_center_code,
               cm.category,
               cm.pk_settle_grade,
               cm.pk_awards,
               cm.purchase_status,
               cm.order_source,
               cm.is_real_name,
               cm.is_bus_license,
               cm.is_maker_space,
               cm.share_status,
               cm.pay_pwd,
               cm.register_authority,
               cm.nick_name
        from cu_member cm
        where cm.del_flag = 0
    </sql>

    <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="updateCuMemberRetailAwardsByPeriod">
        merge into cu_member a
        using (
        select pk_member, max(new_level) pk_awards
        from cu_member_awards
        where period = #{period} and award_type=#{awardType}
        and del_flag = 0
        group by pk_member
        ) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set
        <if test="awardType == 1">
            a.pk_awards = b.pk_awards,
        </if>
        <if test="awardType == 2">
            a.pk_range_awards=b.pk_awards,
        </if>
        a.modified_time = sysdate
    </update>

    <update id="updateCuMemberGrade">
        merge into cu_member a
            using (
                select a.pk_member, max(a.new_level) pk_grade
                from cu_member_grade a
                         inner join(
                    select pk_member, max(nvl(modified_time, creation_time)) creation_time
                    from cu_member_grade
                    where period = #{period}
                      and del_flag = 0
                    group by pk_member) b
                                   on a.pk_member = b.pk_member and nvl(a.modified_time, a.creation_time) = b.creation_time
                group by a.pk_member
            ) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set
                    a.pk_settle_grade = b.pk_grade,
                    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}
                   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>

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

    <update id="updateBackCuMemberRetailRangeAward">
        merge into cu_member a
        using (select pk_member, max(old_level) pk_awards from cu_member_awards ca
        inner join cu_member cm
        on ca.pk_member = cm.pk_id
        where ca.period = #{period} and ca.up_type = 1
        and award_type = #{awardType}
        <if test="awardType == 1">
            and cm.pk_awards &lt; ca.new_level
        </if>
        <if test="awardType == 2">
            and cm.pk_range_awards &lt; ca.new_level
        </if>
        group by ca.pk_member) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set
        <if test="awardType == 1">
            a.pk_awards = b.pk_awards,
        </if>
        <if test="awardType == 2">
            a.pk_range_awards = b.pk_awards,
        </if>
        a.modified_time = sysdate
    </update>

    <update id="updateCuMemberRetailRangeAwardByDate">
        merge into cu_member a
        using (
        select y.pk_member,y.pk_awards from cu_member x
        inner join (
        select a.pk_member,a.new_level pk_awards from cu_member_awards a
        inner join(
        select pk_member,max(pk_id) pk_id from cu_member_awards where del_flag=0 and up_type=2
        and end_validity_date &lt;=#{settleDate, jdbcType=DATE}
        and award_type = #{awardType}
        group by pk_member) b
        on a.pk_id=b.pk_id ) y
        on x.pk_id=y.pk_member
        where
        <if test="awardType == 1">
            x.pk_awards &lt; y.pk_awards
        </if>
        <if test="awardType == 2">
            x.pk_range_awards &lt; y.pk_awards
        </if>
        ) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set
        <if test="awardType == 1">
            a.pk_awards = b.pk_awards,
        </if>
        <if test="awardType == 2">
            a.pk_range_awards=b.pk_awards,
        </if>
        a.modified_time = sysdate
    </update>

    <!-- 初始化会员奖衔 -->
    <update id="updateCuMemberRetailRangeInitAward">
        update cu_member
        set pk_awards       = (select pk_id from bd_awards where awards_value = 0),
            pk_range_awards = (select pk_id from bd_awards where awards_value = 0)
    </update>

    <!-- 初始化会员V5奖衔 -->
    <update id="updateCuMemberRetailRangeInitV5Award">
        update cu_member
        set pk_awards=(select pk_id from bd_awards where awards_value = 5),
            pk_range_awards=(select pk_id from bd_awards where awards_value = 5)
        where pk_settle_grade = (
            select pk_id
            from bd_grade
            where grade_value = 70
              and del_flag = 0
        )
    </update>

    <!-- 更新会员的等级（会员表） -->
    <update id="updateCuMemberGradeByList">
        merge into cu_member a
        using (
        select pk_member, max(pk_grade) pk_grade from (
        <foreach item="item" index="index" collection="cuMemberGradeList" separator=" union ">
            select #{item.pkMember} pk_member,
            #{item.newLevel} pk_grade
            from dual
        </foreach>
        )
        group by pk_member
        ) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set a.pk_settle_grade = b.pk_grade, a.modified_time = sysdate
    </update>

    <update id="updateCuMemberRetailRangeByList">
        merge into cu_member a
        using (
        select pk_member, max(pk_awards) pk_awards from (
        <foreach item="item" index="index" collection="cuMemberAwardsList" separator=" union ">
            select #{item.pkMember} pk_member,#{item.newLevel} pk_awards, #{item.awardType} award_type from dual
        </foreach>
        )
        where award_type = #{awardType}
        group by pk_member
        ) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set
        <if test="awardType == 1">
            a.pk_awards = b.pk_awards,
        </if>
        <if test="awardType == 2">
            a.pk_range_awards = b.pk_awards,
        </if>
        a.modified_time = sysdate
    </update>

    <update id="updateCuMemberMinGradeByList">
        merge into cu_member a
        using (
        select pk_member,min(pk_grade) pk_grade from(
        <foreach item="item" index="index" collection="cuMemberGradeList" separator=" union ">
            select #{item.pkMember} pk_member,#{item.newLevel} pk_grade from dual
        </foreach>)
        group by pk_member
        ) b
        on (a.pk_id = b.pk_member)
        when matched then
        update set a.pk_settle_grade = b.pk_grade,a.modified_time = sysdate
    </update>
    <update id="updateCuMemberLevel">
        merge into cu_member a
            using (
                select cm.pk_id, m.new_level pk_grade
                from cu_member cm
                         inner join(
                    select a.*
                    from cu_member_level a
                             inner join (
                        select pk_member, max(pk_id) pk_id
                        from cu_member_level
                        where del_flag = 0
                          and up_type = 4
                          and upgrade_time >= #{startDate,jdbcType=DATE}
                          and upgrade_time &lt; #{endDate,jdbcType=DATE}
                        group by pk_member) b
                                        on a.pk_id = b.pk_id) m
                                   on cm.pk_id = m.pk_member
                where cm.pk_settle_grade &lt; m.new_level
            ) b
            on (a.pk_id = b.pk_id)
            when matched then
                update set a.pk_settle_grade = b.pk_grade,a.modified_time = sysdate
    </update>
</mapper>
