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

    <resultMap type="com.game.business.domain.AppGameBetting" id="AppGameBettingResult">
        <result property="id"    column="id"    />
        <result property="classId"    column="class_id"    />
        <result property="gameId"    column="game_id"    />
        <result property="userId"    column="user_id"    />
        <result property="gameDate"    column="game_date"    />
        <result property="liveUserId"    column="live_user_id"    />
        <result property="liveRoomId"    column="live_room_id"    />
        <result property="moneyId"    column="money_id"    />
        <result property="isLive"    column="is_live"    />
        <result property="bettingType"    column="betting_type"    />
        <result property="bettingGameType"    column="betting_game_type"    />
        <result property="bettingItem"    column="betting_item"    />
        <result property="bettingMultiple"    column="betting_multiple"    />
        <result property="bettingAmount"    column="betting_amount"    />
        <result property="bettingCommission"    column="betting_commission"    />
        <result property="isWinning"    column="is_winning"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <select id="getCount" resultType="com.game.business.vo.AppGameBettingDetailsCountVO">
        select
        game_id as gameId,
        count(id) as bettingCount,
        sum(betting_amount) as orderAmount,
        sum(case when is_winning = 1 then (betting_amount * betting_multiple) else 0.00 end) as winAmount,
        sum(case when is_winning = 2 then (betting_amount) else 0.00 end) as loseAmount
        from app_game_betting  where date_format(create_time, '%Y-%m-%d') = #{strDate} and user_id = #{userId}
        and betting_type = #{bettingType}
        group by game_id
    </select>

    <select id="getUserCount" resultType="com.game.business.vo.AppUserGameBettingCountVO">
        select tmp.*,
               agl.game_lottery_succ as gameLotterySucc,agl.game_record_date as gameRecordDate,agl.is_lottery as isLottery
        from (
          select
              game_date as gameDate,
              count(id) as bettingCount,
              sum(betting_amount) as orderAmount,
              sum(case when is_winning = 1 then (betting_amount * betting_multiple) else 0.00 end) as winAmount,
              sum(case when is_winning = 2 then (betting_amount) else 0.00 end) as loseAmount
          from app_game_betting where game_id = #{gameId} and user_id = #{userId} and betting_type = #{bettingType}
          group by game_date
        ) tmp
        left join app_game_lottery agl on agl.game_date = tmp.gameDate and agl.game_id = #{gameId}
        order by tmp.gameDate desc
    </select>

    <select id="getUserBettingItemCount" resultType="com.game.business.vo.AppUserGameBettingDetailsCountVO">
          select
              betting_item as bettingTtem,
              sum(betting_amount) as orderAmount,
              sum(case when is_winning = 1 then (betting_amount * betting_multiple) else 0.00 end) as winAmount,
              sum(case when is_winning = 2 then (betting_amount) else 0.00 end) as loseAmount
          from app_game_betting where game_id = #{gameId} and game_date = #{gameDate}
          and user_id = #{userId} and betting_type = #{bettingType} and betting_game_type = 0
          group by betting_item
    </select>

    <select id="getBettingAmountSum" resultType="java.util.Map">
        select
            ifnull(sum(case when betting_type = 1 then betting_amount else 0.00 end), 0.00) as coinSum,
            ifnull(sum(case when betting_type = 0 then betting_amount else 0.00 end), 0.00) as diamondCoinSum
        from app_game_betting
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="getBettingAmountByDateSum" resultType="java.util.Map" parameterType="com.game.business.dto.FinTranRecordDTO">
        select
            ifnull(sum(case when betting_type = 1 then betting_amount else 0.00 end), 0.00) as coinSum,
            ifnull(sum(case when betting_type = 0 then betting_amount else 0.00 end), 0.00) as diamondCoinSum
        from app_game_betting
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and (date_format(create_time, '%Y-%m-%d') &gt;= #{beginTime} and date_format(create_time, '%Y-%m-%d') &lt;= #{endTime})
            </if>
        </where>
    </select>

    <select id="getBettingCount" resultType="java.lang.Integer">
        select ifnull(count(1), 0) as bettingCount from (
            select
                betting_item
            from app_game_betting where user_id = #{userId} and game_id = #{gameId} and game_date = #{gameDate} and betting_item != #{bettingItem}
            group by betting_item
        ) tmp
    </select>

    <select id="getBettingItem" resultType="java.lang.String">
        select
            betting_item
        from app_game_betting where user_id = #{userId} and game_id = #{gameId} and game_date = #{gameDate}
        group by betting_item
    </select>

    <select id="getBettingItemCount" resultType="java.lang.Integer">
        select
            ifnull(count(1), 0) as bettingCount
        from app_game_betting where user_id = #{userId} and game_id = #{gameId} and game_date = #{gameDate}
        <if test="bettingItem != null and bettingItem != ''">
            and betting_item = #{bettingItem}
        </if>
    </select>

    <select id="getBettingAmount" resultType="java.math.BigDecimal">
        select
            ifnull(sum(betting_amount), 0.00) as bettingAmount
        from app_game_betting where user_id = #{userId} and game_id = #{gameId} and game_date = #{gameDate}
        <if test="bettingItem != null and bettingItem != ''">
            and betting_item = #{bettingItem}
        </if>
    </select>

    <sql id="selectAppGameBettingVo">
        select id, class_id, game_id, user_id, game_date, live_user_id, live_room_id, money_id, is_live, betting_type, betting_game_type, betting_item, betting_multiple, betting_amount, betting_commission, is_winning, create_time, update_time from app_game_betting
    </sql>

    <select id="selectAppGameBettingList" parameterType="com.game.business.domain.AppGameBetting" resultMap="AppGameBettingResult">
        <include refid="selectAppGameBettingVo"/>
        <where>
            <if test="classId != null "> and class_id = #{classId}</if>
            <if test="gameId != null "> and game_id = #{gameId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="gameDate != null  and gameDate != ''"> and game_date = #{gameDate}</if>
            <if test="liveUserId != null "> and live_user_id = #{liveUserId}</if>
            <if test="liveRoomId != null "> and live_room_id = #{liveRoomId}</if>
            <if test="moneyId != null "> and money_id = #{moneyId}</if>
            <if test="isLive != null  and isLive != ''"> and is_live = #{isLive}</if>
            <if test="bettingType != null  and bettingType != ''"> and betting_type = #{bettingType}</if>
            <if test="bettingGameType != null  and bettingGameType != ''"> and betting_game_type = #{bettingGameType}</if>
            <if test="bettingItem != null  and bettingItem != ''"> and betting_item = #{bettingItem}</if>
            <if test="bettingMultiple != null "> and betting_multiple = #{bettingMultiple}</if>
            <if test="bettingAmount != null "> and betting_amount = #{bettingAmount}</if>
            <if test="bettingCommission != null "> and betting_commission = #{bettingCommission}</if>
            <if test="isWinning != null  and isWinning != ''"> and is_winning = #{isWinning}</if>
            <if test="beginTime != null  and beginTime != ''"> and create_time &gt; CONCAT(#{beginTime},' 00:00:00')</if>
            <if test="endTime != null  and endTime != ''"> and create_time &lt; CONCAT(#{endTime},' 23:59:59')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectAppGameBettingById" parameterType="Long" resultMap="AppGameBettingResult">
        <include refid="selectAppGameBettingVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppGameBetting" parameterType="com.game.business.domain.AppGameBetting" useGeneratedKeys="true" keyProperty="id">
        insert into app_game_betting
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="classId != null">class_id,</if>
            <if test="gameId != null">game_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="gameDate != null">game_date,</if>
            <if test="liveUserId != null">live_user_id,</if>
            <if test="liveRoomId != null">live_room_id,</if>
            <if test="moneyId != null">money_id,</if>
            <if test="isLive != null">is_live,</if>
            <if test="bettingType != null">betting_type,</if>
            <if test="bettingGameType != null">betting_game_type,</if>
            <if test="bettingItem != null">betting_item,</if>
            <if test="bettingMultiple != null">betting_multiple,</if>
            <if test="bettingAmount != null">betting_amount,</if>
            <if test="bettingCommission != null">betting_commission,</if>
            <if test="isWinning != null">is_winning,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="classId != null">#{classId},</if>
            <if test="gameId != null">#{gameId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="gameDate != null">#{gameDate},</if>
            <if test="liveUserId != null">#{liveUserId},</if>
            <if test="liveRoomId != null">#{liveRoomId},</if>
            <if test="moneyId != null">#{moneyId},</if>
            <if test="isLive != null">#{isLive},</if>
            <if test="bettingType != null">#{bettingType},</if>
            <if test="bettingGameType != null">#{bettingGameType},</if>
            <if test="bettingItem != null">#{bettingItem},</if>
            <if test="bettingMultiple != null">#{bettingMultiple},</if>
            <if test="bettingAmount != null">#{bettingAmount},</if>
            <if test="bettingCommission != null">#{bettingCommission},</if>
            <if test="isWinning != null">#{isWinning},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateAppGameBetting" parameterType="com.game.business.domain.AppGameBetting">
        update app_game_betting
        <trim prefix="SET" suffixOverrides=",">
            <if test="classId != null">class_id = #{classId},</if>
            <if test="gameId != null">game_id = #{gameId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="gameDate != null">game_date = #{gameDate},</if>
            <if test="liveUserId != null">live_user_id = #{liveUserId},</if>
            <if test="liveRoomId != null">live_room_id = #{liveRoomId},</if>
            <if test="moneyId != null">money_id = #{moneyId},</if>
            <if test="isLive != null">is_live = #{isLive},</if>
            <if test="bettingType != null">betting_type = #{bettingType},</if>
            <if test="bettingGameType != null">betting_game_type = #{bettingGameType},</if>
            <if test="bettingItem != null">betting_item = #{bettingItem},</if>
            <if test="bettingMultiple != null">betting_multiple = #{bettingMultiple},</if>
            <if test="bettingAmount != null">betting_amount = #{bettingAmount},</if>
            <if test="bettingCommission != null">betting_commission = #{bettingCommission},</if>
            <if test="isWinning != null">is_winning = #{isWinning},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppGameBettingById" parameterType="Long">
        delete from app_game_betting where id = #{id}
    </delete>

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