<?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.game.business.mapper.AppGameItemMapper">
    
    <resultMap type="com.game.business.domain.AppGameItem" id="AppGameItemResult">
        <result property="id"    column="id"    />
        <result property="gameId"    column="game_id"    />
        <result property="itemName"    column="item_name"    />
        <result property="itemMultiple"    column="item_multiple"    />
        <result property="itemLocation"    column="item_location"    />
        <result property="bettingCount"    column="betting_count"    />
        <result property="bettingMoney"    column="betting_money"    />
        <result property="lotteryCount"    column="lottery_count"    />
        <result property="consecutive"    column="consecutive"    />
    </resultMap>

    <sql id="selectAppGameItemVo">
        select id, game_id, item_name, item_multiple, item_location, betting_count, betting_money, lottery_count, consecutive from app_game_item
    </sql>

    <select id="selectAppGameItemList" parameterType="com.game.business.domain.AppGameItem" resultMap="AppGameItemResult">
        <include refid="selectAppGameItemVo"/>
        <where>
            <if test="gameId != null "> and game_id = #{gameId}</if>
            <if test="itemName != null  and itemName != ''"> and item_name like concat('%', #{itemName}, '%')</if>
            <if test="itemMultiple != null "> and item_multiple = #{itemMultiple}</if>
            <if test="itemLocation != null  and itemLocation != ''"> and item_location = #{itemLocation}</if>
            <if test="bettingCount != null "> and betting_count = #{bettingCount}</if>
            <if test="bettingMoney != null "> and betting_money = #{bettingMoney}</if>
            <if test="lotteryCount != null "> and lottery_count = #{lotteryCount}</if>
            <if test="consecutive != null "> and consecutive = #{consecutive}</if>
        </where>
        order by cast(item_location as UNSIGNED) asc
    </select>
    
    <select id="selectAppGameItemById" parameterType="Long" resultMap="AppGameItemResult">
        <include refid="selectAppGameItemVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertAppGameItem" parameterType="com.game.business.domain.AppGameItem" useGeneratedKeys="true" keyProperty="id">
        insert into app_game_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gameId != null">game_id,</if>
            <if test="itemName != null">item_name,</if>
            <if test="itemMultiple != null">item_multiple,</if>
            <if test="itemLocation != null">item_location,</if>
            <if test="bettingMoney != null">betting_money,</if>
            <if test="bettingCount != null">betting_count,</if>
            <if test="lotteryCount != null">lottery_count,</if>
            <if test="consecutive != null">consecutive,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="gameId != null">#{gameId},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="itemMultiple != null">#{itemMultiple},</if>
            <if test="itemLocation != null">#{itemLocation},</if>
            <if test="bettingCount != null">#{bettingCount},</if>
            <if test="bettingMoney != null">#{bettingMoney},</if>
            <if test="lotteryCount != null">#{lotteryCount},</if>
            <if test="consecutive != null">#{consecutive},</if>
         </trim>
    </insert>

    <update id="updateAppGameItem" parameterType="com.game.business.domain.AppGameItem">
        update app_game_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="gameId != null">game_id = #{gameId},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="itemMultiple != null">item_multiple = #{itemMultiple},</if>
            <if test="itemLocation != null">item_location = #{itemLocation},</if>
            <if test="bettingCount != null">betting_count = #{bettingCount},</if>
            <if test="bettingMoney != null">betting_money = #{bettingMoney},</if>
            <if test="lotteryCount != null">lottery_count = #{lotteryCount},</if>
            <if test="consecutive != null">consecutive = #{consecutive},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppGameItemById" parameterType="Long">
        delete from app_game_item where id = #{id}
    </delete>

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

    <update id="updateLotteryCount" parameterType="com.game.business.domain.AppGameItem">
        update app_game_item set lottery_count = lottery_count + 1
        where game_id = #{gameId} and item_location = #{itemLocation}
    </update>

    <update id="updateConsecutive" parameterType="com.game.business.dto.AppItemCountDTO">
        update app_game_item set consecutive = consecutive + 1
        where game_id = #{gameId} and item_location = #{itemLocation} and consecutive &lt; #{consecutive}
    </update>

    <update id="emptyLotteryCount">
        update app_game_item set lottery_count = 0 where lottery_count > 0
    </update>

</mapper>