<?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.fs.live.mapper.LiveUserLotteryRecordMapper">

    <resultMap type="LiveUserLotteryRecord" id="LiveUserLotteryRecordResult">
        <result property="id"    column="id"    />
        <result property="lotteryId"    column="lottery_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="userId"    column="user_id"    />
        <result property="productId"    column="product_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectLiveUserLotteryRecordVo">
        select id, lottery_id, live_id, user_id, product_id, create_time, update_time, create_by, update_by from live_user_lottery_record
    </sql>

    <select id="selectLiveUserLotteryRecordList" parameterType="LiveUserLotteryRecord" resultMap="LiveUserLotteryRecordResult">
        <include refid="selectLiveUserLotteryRecordVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
        </where>
    </select>

    <select id="selectLiveUserLotteryRecordById" parameterType="Long" resultMap="LiveUserLotteryRecordResult">
        <include refid="selectLiveUserLotteryRecordVo"/>
        where id = #{id}
    </select>
    <select id="selectRecordByLiveIdAndLotteryId"  resultType="com.fs.live.vo.LiveUserLotteryRecordVo">
        select lulr.*,fu.nickname as user_name,fsp.product_name as product_name
        from live_user_lottery_record lulr left join fs_user fu on lulr.user_id = fu.user_id
        left join live_goods lg on lulr.product_id = lg.goods_id
        left join fs_store_product fsp on lg.product_id = fsp.product_id
        where lulr.lottery_id = #{lotteryId} and lulr.live_id = #{liveId}
    </select>

    <insert id="insertLiveUserLotteryRecord" parameterType="LiveUserLotteryRecord" useGeneratedKeys="true" keyProperty="id">
        insert into live_user_lottery_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="lotteryId != null">lottery_id,</if>
            <if test="liveId != null">live_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="productId != null">product_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="lotteryId != null">#{lotteryId},</if>
            <if test="liveId != null">#{liveId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="productId != null">#{productId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateLiveUserLotteryRecord" parameterType="LiveUserLotteryRecord">
        update live_user_lottery_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="lotteryId != null">lottery_id = #{lotteryId},</if>
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveUserLotteryRecordById" parameterType="Long">
        delete from live_user_lottery_record where id = #{id}
    </delete>

    <delete id="deleteLiveUserLotteryRecordByIds" parameterType="String">
        delete from live_user_lottery_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
