<?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.xxfc.platform.activity.mapper.ActivityAttendanceRecordMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.xxfc.platform.activity.entity.ActivityAttendanceRecord" id="activityAttendanceRecordMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="phone" column="phone"/>
        <result property="activityId" column="activity_id"/>
        <result property="activityName" column="activity_name"/>
        <result property="positionId" column="position_id"/>
        <result property="positionName" column="position_name"/>
        <result property="onlineLotteryNum" column="online_lottery_num"/>
        <result property="localeLotteryNum" column="locale_lottery_num"/>
        <result property="crtTime" column="crt_time"/>
    </resultMap>

    <select id="selectActivityAttendanceRecord"
            resultType="com.xxfc.platform.activity.dto.ActivityAttendanceRecordDTO">
        select apu.user_id as `userId`,
        apu.`current_progress` as `amount`,
        apu.`status`,
        apu.crt_time as `attendTime`,
        aar.user_name as `userName`,
        aar.phone,
        aar.position_name as `positionName`,
        IFNULL(apr.inviteNum,0) as `inviteNum`,
        awr.prizes
        from (select `user_id`,`popularize_id`,`current_progress`,`status`,`crt_time` from activity_popularize_user where
        `popularize_id`=#{activityId}
        <if test="status!=null">
            and `status`=#{status}
        </if>
        <if test="startTime!=null and endTime!=null">
            and `crt_time` between #{startTime} and #{endTime}
        </if>
        <if test="startTime!=null and endTime==null">
            and <![CDATA[
            `crt_time`>=#{startTime}
            ]]>
        </if>
        <if test="startTime==null and endTime!=null">
            and <![CDATA[
            `crt_time`<=#{endTime}
            ]]>
        </if>
        ) as `apu`
        inner join (select * from `activity_attendance_record` where `activity_id`=#{activityId}
        <if test="phone!=null and phone!=''">
            and `phone`=#{phone}
        </if>
        ) as `aar`
        on aar.user_id = apu.user_id and apu.popularize_id = aar.activity_id
        left join (select `major_user_id`, count(minor_user_id) as `inviteNum`
        from activity_popularize_relation where `popularize_id`=#{activityId}
        group by major_user_id) as `apr` on apr.major_user_id = apu.user_id
        left join (select `user_id`, `activity_id`, group_concat(`prize_name`) as `prizes`
        from `activity_winning_record` where `activity_id`=#{activityId} and `has_winning`=1
        group by user_id, `activity_id`) as `awr`
        on awr.user_id = apu.user_id and awr.activity_id = apu.popularize_id
        order by apu.current_progress desc
    </select>

    <update id="updateLotteryNumByActivityIdAndUserIdAndPrizeType">
        update `activity_attendance_record`
        <if test="prizeType==1">
            set `online_lottery_num`=`online_lottery_num`-1
        </if>
        <if test="prizeType==2">
            set `locale_lottery_num`=`locale_lottery_num`-1
        </if>
        where `activity_id`=#{activityId} and `user_id`=#{userId}
        <if test="prizeType==1">
            and  `online_lottery_num`>0
        </if>
        <if test="prizeType==2">
            and `locale_lottery_num`>0
        </if>
    </update>

</mapper>