<?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.LiveLotteryConfMapper">

    <resultMap type="LiveLotteryConf" id="LiveLotteryConfResult">
        <result property="lotteryId"    column="lottery_id"    />
        <result property="liveId"    column="live_id"    />
        <result property="require"    column="require"    />
        <result property="requireConf"    column="require_conf"    />
        <result property="duration"    column="duration"    />
        <result property="desc"    column="desc"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="lotteryStatus"    column="lottery_status"    />
    </resultMap>

    <sql id="selectLiveLotteryConfVo">
        select lottery_id, live_id, `require`, require_conf, duration, `desc`, create_time, update_time, create_by, update_by, lottery_status from live_lottery_conf
    </sql>

    <select id="selectLiveLotteryConfList" parameterType="LiveLotteryConf" resultMap="LiveLotteryConfResult">
        <include refid="selectLiveLotteryConfVo"/>
        <where>
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="require != null "> and `require` = #{require}</if>
            <if test="desc != null  and desc != ''"> and `desc` = #{desc}</if>
            <if test="createTime != null "> and create_time >= #{createTime} and create_time &lt; date_add(#{createTime}, interval 1 day)</if>
            <if test="lotteryStatus != null "> and lottery_status = #{lotteryStatus}</if>
        </where>
order by create_time desc
    </select>

    <select id="selectLiveLotteryConfByLotteryId" parameterType="Long" resultMap="LiveLotteryConfResult">
        <include refid="selectLiveLotteryConfVo"/>
        where lottery_id = #{lotteryId}
    </select>

    <insert id="insertLiveLotteryConf" parameterType="LiveLotteryConf" useGeneratedKeys="true" keyProperty="lotteryId">
        insert into live_lottery_conf
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="require != null">`require`,</if>
            <if test="requireConf != null">require_conf,</if>
            <if test="duration != null">duration,</if>
            <if test="desc != null and desc != ''">`desc`,</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>
            <if test="lotteryStatus != null">lottery_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="require != null">#{require},</if>
            <if test="requireConf != null">#{requireConf},</if>
            <if test="duration != null">#{duration},</if>
            <if test="desc != null and desc != ''">#{desc},</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>
            <if test="lotteryStatus != null">#{lotteryStatus},</if>
         </trim>
    </insert>

    <update id="updateLiveLotteryConf" parameterType="LiveLotteryConf">
        update live_lottery_conf
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="require != null">`require` = #{require},</if>
            <if test="requireConf != null">require_conf = #{requireConf},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="desc != null and desc != ''">`desc` = #{desc},</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>
            <if test="lotteryStatus != null">lottery_status = #{lotteryStatus},</if>
        </trim>
        where lottery_id = #{lotteryId}
    </update>

    <delete id="deleteLiveLotteryConfByLotteryId" parameterType="Long">
        delete from live_lottery_conf where lottery_id = #{lotteryId}
    </delete>

    <delete id="deleteLiveLotteryConfByLotteryIds" parameterType="String">
        delete from live_lottery_conf where lottery_id in
        <foreach item="lotteryId" collection="array" open="(" separator="," close=")">
            #{lotteryId}
        </foreach>
    </delete>

    <select id="selectByLotteryIds" resultType="com.fs.live.vo.LiveLotteryConfVo">
        select * from live_lottery_conf where lottery_id in
        <foreach item="lotteryId" collection="ids" open="(" separator="," close=")">
            #{lotteryId}
        </foreach>
    </select>

    <update id="finishStatusByLotteryIds" >
        update live_lottery_conf set lottery_status = 2 where lottery_id in
        <foreach item="lotteryId" collection="ids" open="(" separator="," close=")">
            #{lotteryId}
        </foreach>
    </update>

    <select id="selectLiveLotteryConfVoByLotteryId" resultType="com.fs.live.vo.LiveLotteryConfVo">
        select * from live_lottery_conf where lottery_id = #{lotteryId}
    </select>
</mapper>
