<?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.jeeplus.test.luckyDraw.mapper.LuckyDrawInfoMapper">
    <resultMap id="BaseResultMap" type="com.jeeplus.test.luckyDraw.service.dto.LuckyDrawInfoDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
        <result property="awardsName" column="name" jdbcType="VARCHAR"/>
        <result property="prizeName" column="prize_name" jdbcType="VARCHAR"/>
        <result property="number" column="number" jdbcType="VARCHAR"/>
        <collection property="luckyDrawMembersDtos" ofType="com.jeeplus.test.luckyDraw.service.dto.LuckyDrawMembersDto" column="id" select="getUserInfo"></collection>
    </resultMap>

    <sql id="User_Info_List">
        ldm.id,
        ldm.create_by,
        ldm.create_date,
        ldm.update_by,
        ldm.update_date,
        ldm.del_flag,
        ldm.name,
        ldm.office_name,
        ldm.phone
    </sql>

    <sql id="Events_Column_List">
        lda.id,
        lda.create_by,
        lda.create_date,
        lda.update_by,
        lda.update_date,
        lda.del_flag,
        lda.remarks,
        lda.events_id,
        lda.name,
        lda.number,
        lda.prize_name
    </sql>

    <select id="findByEventId" resultMap="BaseResultMap">
        select
        <include refid="Events_Column_List"></include>
        from lucky_draw_awards lda
        left join lucky_draw_info lui on lda.events_id = lui.event_id
        left join lucky_draw_members ldm on lui.member_id = ldm.id
        where lda.events_id = #{id}
    </select>
    <select id="selectUserId" resultType="java.lang.String">
        select id from `lucky_draw_members`
        WHERE id not in (SELECT member_id FROM lucky_draw_info b WHERE b.event_id = #{eventId}) and event_id = #{eventId} and del_flag = 0
        order by rand() LIMIT #{number}
    </select>
    <select id="getUserInfo" resultType="com.jeeplus.test.luckyDraw.service.dto.LuckyDrawMembersDto">
        select
        <include refid="User_Info_List"></include>
        from lucky_draw_members ldm
        left join lucky_draw_info lui on ldm.id = lui.member_id and lui.del_flag = '0'
        left join lucky_draw_awards lda on lda.id = lui.award_id and lda.del_flag = '0'
        where lda.id = #{id} and ldm.del_flag = '0'
    </select>
    <select id="findList" resultMap="BaseResultMap">
        select
        <include refid="Events_Column_List"></include>
        from lucky_draw_awards lda
        left join lucky_draw_events lde on lda.events_id = lde.id
        ${ew.customSqlSegment}
    </select>

    <delete id="deleteByEventIdAndAwardId">
        delete from lucky_draw_info
        <where>
            event_id = #{id}
            and award_id = #{awardId}
        </where>
    </delete>

    <select id="getLuckyDrawUserCount" resultType="java.lang.Integer">
        select count(1) from lucky_draw_info where event_id = #{eventId}
    </select>

    <select id="getLuckyDrawMemberCount" resultType="java.lang.Integer">
        select count(1) from lucky_draw_members where event_id = #{eventId}
    </select>
</mapper>
