<?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="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="updateCuMemberMaxAwardsByPeriod">
        merge into cu_member a
            using (select c1.*
                   from (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) c1
                            inner join cu_member c2
                                       on c1.pk_member = c2.pk_id
                                           and c1.pk_awards > c2.pk_max_awards) b
            on (a.pk_id = b.pk_member)
            when matched then
                update set a.pk_max_awards = b.pk_awards, 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}
        and ca.del_flag = 0
        <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
        where a.del_flag = 0
        ) 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="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>
    <select id="getByPkId" resultType="com.hzs.common.domain.member.base.CuMember"
            parameterType="java.io.Serializable">
        select * from cu_member where pk_id = ${pkId}
    </select>

</mapper>
