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

    <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 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}
        where agl.is_lottery = 1
        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>
</mapper>