<?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.iamberry.wechat.service.mapper.GameMapper">

	<!-- 通过游戏类型查询游戏信息 -->
	<select id="selectGameByGameType" parameterType="GameSystemDto" resultType="GameSystemDto">
		SELECT 
			GAME_ID gameId, 
			GAME_NAME gameName, 
			GAME_TYPE gameType, 
			GAME_START_DATE gameStartDate, 
			GAME_END_DATE gameEndDate, 
			GAME_DESC gameDesc, 
			GAME_THRESHOLD gameThreshold, 
			GAME_THRESHOLD_NUM gameThresholdNum 
		FROM 
			TB_IAMBERRY_GAME_SYSTEM 
		WHERE
			GAME_ID = #{gameId} 
	</select>
	
	<!-- 通过游戏id查询游戏信息 -->
	<select id="selectGameByGameId" parameterType="GameSystemDto" resultType="GameSystemDto">
		SELECT 
			GAME_ID gameId, 
			GAME_THRESHOLD gameThreshold, 
			GAME_THRESHOLD_NUM gameThresholdNum 
		FROM 
			TB_IAMBERRY_GAME_SYSTEM 
		WHERE
			GAME_ID = #{gameId} 
	</select>

<!-- 通过游戏id查询游戏奖品信息 -->
	<select id="selectGamePrizeByGameId" parameterType="GamePrizeDto"  resultType="GamePrizeDto">
		SELECT 
			PRIZE_NAME prizeName, 
			PRIZE_NUM prizeNum, 
			PRIZE_PRICE prizePrice, 
			PRIZE_GAME_ID prizeGameId, 
			PRIZE_IMAGE prizeImage, 
			PRIZE_LEVEL prizeLevel, 
			PRIZE_DESC prizeDesc, 
			PRIZE_PROBABILITY prizeProbability, 
			PRIZE_TYPE prizeType, 
			PRIZE_WINNING_NUM prizeWinningNum, 
			PRIZE_WINNING_TYPE prizeWinningType,
			PRIZE_ID prizeId
		FROM 
			TB_IAMBERRY_GAME_PRIZE 
		<where>
			<if test="prizeGameId != null and prizeGameId != ''">
				PRIZE_GAME_ID = #{prizeGameId} 
			</if>
		</where>
	</select>
	
	<!-- 修改奖品的数量 -->
	<update id="updatePrizeNum" parameterType="java.lang.Integer">
		UPDATE 
			TB_IAMBERRY_GAME_PRIZE 
		SET  
			PRIZE_NUM = (PRIZE_NUM - 1)
		WHERE 
			PRIZE_NUM >= 1
		AND
			PRIZE_ID = #{prizeID}
	</update>
	
	<!-- 根据openid和订单id和游戏id查询游戏日志信息 -->
	<select id="selectGameLogsByGameId" parameterType="GameLogsDto" resultType="GameLogsDto">
		SELECT 
			LOGS_OPENID logsOpenId, 
			LOGS_ORDERID logsOrderId, 
			LOGS_GAME_ID logsGameId, 
			LOGS_STATE logsState, 
			LOGS_DATE logsDate 
		FROM 
			TB_IAMBERRY_GAME_LOGS 
		WHERE 
			LOGS_OPENID = #{logsOpenId} 
		AND 
			LOGS_ORDERID = #{logsOrderId} 
		AND 
			LOGS_GAME_ID = #{logsGameId} 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 插入游戏信息 -->
	<insert id="saveLogs" parameterType="GameLogsDto">
		INSERT INTO 
		 	TB_IAMBERRY_GAME_LOGS
		 		(
			 		LOGS_OPENID , 
					LOGS_ORDERID , 
					LOGS_GAME_ID , 
					LOGS_PRIZE_ID , 
					LOGS_STATE , 
					LOGS_DATE 
				)
		 VALUES 
		  	( 
		  	  #{logsOpenId},
			  #{logsOrderId},  
			  #{logsGameId}, 
			  #{logsPrizeId},
			  #{logsState},
			  NOW()
		  	)
	</insert>
	
	<!-- 查询游戏已玩次数 -->
	<select id="selectGameLogsCount" parameterType="GameLogsDto" resultType="Integer">
		SELECT 
			COUNT(LOGS_ID)  
		FROM 
			TB_IAMBERRY_GAME_LOGS 
		WHERE 
			LOGS_OPENID = #{logsOpenId} 
		AND 
			LOGS_GAME_ID = #{logsGameId} 
		<if test="logsOrderId != null and logsOrderId != ''">
			AND LOGS_ORDERID = #{logsOrderId} 
		</if>
	</select>
	
	<!-- 根据gameid获取当前用户的中奖纪录 -->
	<select id="selectWinningLogsByGameId" parameterType="GameLogsDto" resultType="GameLogsDto">
		SELECT 
			L.LOGS_OPENID logsOpenId, 
			L.LOGS_ORDERID logsOrderId, 
			L.LOGS_GAME_ID logsGameId, 
			S.GAME_NAME logsGameName,
			L.LOGS_STATE logsState, 
			L.LOGS_DATE logsDate,
			P.PRIZE_ID logsPrizeId,
			P.PRIZE_NAME logsPrizeName,
			P.PRIZE_IMAGE logsPrizeImage,
			P.PRIZE_LEVEL logsPrizeLevel, 
			COUNT(DISTINCT L.LOGS_ID) logsSum 
		FROM 
			TB_IAMBERRY_GAME_LOGS L 
		LEFT JOIN 
			TB_IAMBERRY_GAME_SYSTEM S ON L.LOGS_GAME_ID = S.GAME_ID
		LEFT JOIN 
			TB_IAMBERRY_GAME_PRIZE P ON L.LOGS_PRIZE_ID = P.PRIZE_ID
		WHERE 
			L.LOGS_OPENID = #{logsOpenId} 
		<if test="logsGameId != null and logsGameId != ''">
			AND 
				L.LOGS_GAME_ID = #{logsGameId} 
		</if>
		AND 
			L.LOGS_STATE = 2 
		GROUP BY 
			L.LOGS_GAME_ID,L.LOGS_PRIZE_ID 
		ORDER BY 
			L.LOGS_DATE DESC 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="selectWinningCount" parameterType="GameLogsDto" resultType="Integer">
		SELECT COUNT(t.logsId) FROM (SELECT 
			DISTINCT L.LOGS_ID logsId 
		FROM 
			TB_IAMBERRY_GAME_LOGS L 
		LEFT JOIN 
			TB_IAMBERRY_GAME_SYSTEM S ON L.LOGS_GAME_ID = S.GAME_ID
		LEFT JOIN 
			TB_IAMBERRY_GAME_PRIZE P ON L.LOGS_PRIZE_ID = P.PRIZE_ID
		WHERE 
			L.LOGS_OPENID = #{logsOpenId} 
		<if test="logsGameId != null and logsGameId != ''">
			AND 
				L.LOGS_GAME_ID = #{logsGameId} 
		</if>
		AND L.LOGS_STATE = 2 
		GROUP BY 
			L.LOGS_GAME_ID,L.LOGS_PRIZE_ID 
		ORDER BY 
			L.LOGS_DATE DESC ) t 
	</select>
	
	<!-- 根据时间和openid查询订单条目数 -->
	<select id="selectOrderByCreateDate" parameterType="Order" resultType="Integer">
		SELECT 
			COUNT(DISTINCT SALES_ORDERID) 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER 
		WHERE 
			SALES_STATUS IN (2,5,12) 
		AND 
			SALES_OPENID = #{salesOpenid} 
		AND 
			<![CDATA[DATE_FORMAT(SALES_CREATE_DATE,'%Y-%m-%d') >= DATE_FORMAT(#{beginDate},'%Y-%m-%d')]]> 
		AND 
			<![CDATA[DATE_FORMAT(SALES_CREATE_DATE,'%Y-%m-%d') <= DATE_FORMAT(#{endDate},'%Y-%m-%d')]]>
	</select>
	
</mapper>