<?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.activity.draw.mapper.AcDrawRewardNumMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="AcDrawRewardNumExt" type="com.hzs.common.domain.activity.draw.ext.AcDrawRewardNumExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_BASE_ID" property="pkBaseId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="TOTAL_NUM" property="totalNum"/>
        <result column="TOTAL_USED_NUM" property="totalUsedNum"/>
        <result column="TOTAL_SURPLUS_NUM" property="totalSurplusNum"/>
        <result column="AVAILABLE_NUM" property="availableNum"/>
        <result column="AVAILABLE_USED_NUM" property="availableUsedNum"/>
        <result column="AVAILABLE_SURPLUS_NUM" property="availableSurplusNum"/>
        <result column="PAY_MONEY" property="payMoney"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="AWARDS_NAME" property="awardsName"/>
        <result column="GRADE_NAME" property="gradeName"/>
        <result column="ACT_NAME" property="actName"/>
        <result column="REC_MEMBER_CODE" property="recMemberCode"/>
        <result column="REC_MEMBER_NAME" property="recMemberName"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="SOURCE_MEMBER_CODE" property="sourceMemberCode"/>
        <result column="SOURCE_MEMBER_NAME" property="sourceMemberName"/>
    </resultMap>

    <select id="queryDrawRewardNumByCondition" resultMap="AcDrawRewardNumExt">
        select ac.PAY_MONEY,
               ar.pk_id,
               ar.pk_base_id,
               ar.pk_member,
               ar.total_num,
               ar.total_used_num,
               ar.total_surplus_num,
               ar.available_num,
               ar.available_surplus_num,
               ar.available_used_num,
               ar.pk_country
        from ac_base_config ac
                 inner join AC_DRAW_REWARD_NUM ar
                            on ac.pk_id = ar.pk_base_id
        where ar.del_flag = 0
          and ac.del_flag = 0
          and ar.pk_country = #{pkCountry}
          and ar.pk_base_id = #{pkBaseId}
          and ar.pk_member = #{pkMember}
    </select>

    <select id="queryDrawRewardExtNumByCondition" resultMap="AcDrawRewardNumExt">
        select cm.member_code,cm.member_name,bg.grade_name,ba.awards_name,ar.total_num,
        ar.total_used_num,ar.total_surplus_num,ar.available_num,ar.available_used_num,
        ar.available_surplus_num,cmb.member_code rec_member_code,cmb.member_name rec_member_name,so.order_code,
        cmc.member_code source_member_code,cmc.member_name source_member_name,ab.act_name from ac_draw_reward_num ar
        left join ac_draw_reward_num_detail ad
        on ar.pk_id = ad.pk_draw_reward_num
        inner join cu_member cm
        on ar.pk_member= cm.pk_id
        and cm.pk_settle_country = #{pkCountry}
        left join sa_order so
        on so.pk_id = ad.pk_order and so.del_flag=0
        inner join ac_base_config ab
        on ab.pk_id = ar.pk_base_id
        left join bd_grade bg
        on bg.pk_id = cm.pk_settle_grade
        left join bd_awards ba
        on ba.pk_id=cm.pk_awards
        left join cu_member cmb
        on cmb.pk_id = so.pk_reference
        left join cu_member cmc
        on cmc.pk_id = so.pk_member
        where ar.del_flag=0 and cm.del_flag=0
        and ab.del_flag=0
        and ar.pk_country= #{pkCountry}
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name = #{memberName}
        </if>
        <if test="actName != null and actName != ''">
            and ab.act_name = #{actName}
        </if>
        <if test="pkGrade != null">
            and cm.pk_settle_grade = #{pkGrade}
        </if>
        <if test="pkAwards != null">
            and cm.pk_awards = #{pkAwards}
        </if>
        order by cm.member_code,ab.act_name
    </select>

    <update id="updatePurchasedTimesByBaseId">
        update AC_DRAW_REWARD_NUM
        set TOTAL_USED_NUM        = TOTAL_USED_NUM + #{num},
            AVAILABLE_NUM         = AVAILABLE_NUM + #{num},
            AVAILABLE_SURPLUS_NUM = AVAILABLE_SURPLUS_NUM + #{num},
            TOTAL_NUM             = TOTAL_NUM + #{num}
        where PK_BASE_ID = #{pkBaseId}
          and PK_MEMBER = #{pkMember}
          and DEL_FLAG = 0
    </update>

    <update id="updateUnpurchasedTimesByPkbaseId">
        update AC_DRAW_REWARD_NUM
        set TOTAL_NUM         = TOTAL_NUM + #{num},
            TOTAL_SURPLUS_NUM = TOTAL_SURPLUS_NUM + #{num}
        where PK_BASE_ID = #{pkBaseId}
          and PK_MEMBER = #{pkMember}
          and DEL_FLAG = 0
    </update>

</mapper>
