<?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.ym.mec.biz.dal.dao.LuckDrawPrizeDao">
	<resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.LuckDrawPrize">
		<id column="id_" property="id" jdbcType="INTEGER" />
		<result column="name_" property="name" jdbcType="VARCHAR" />
		<result column="chances_" property="chances" jdbcType="DOUBLE" />
		<result column="stock_" property="stock" jdbcType="INTEGER" />
		<result column="enabled_" property="enabled" jdbcType="BIT" />
		<result column="reward_type_" property="rewardType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="memo_" property="memo" jdbcType="VARCHAR" />
		<result column="group_id_" property="groupId" jdbcType="INTEGER" />
		<result column="is_default_" property="isDefault" />
		<result column="create_on_" property="createOn" jdbcType="TIMESTAMP" />
		<result column="modify_on_" property="modifyOn" jdbcType="TIMESTAMP" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>

	<sql id="Base_Column_List">
		id_, name_, chances_, stock_, enabled_, reward_type_, memo_, group_id_, is_default_, create_on_, modify_on_, tenant_id_
	</sql>

	<sql id="queryCondition">
		<where>
			tenant_id_ = #{tenantId}
			<if test="enabled != null">
				and enabled_ = #{enabled}
			</if>
			<if test="id != null">
				and id_ = #{id}
			</if>
			<if test="name != null">
				and name_ like concat('%',#{name},'%')
			</if>
			<if test="rewardType != null">
				and reward_type_ like concat('%',#{rewardType},'%')
			</if>
			<if test="groupId != null">
				and group_id_ = #{groupId}
			</if>
		</where>
	</sql>

	<select id="get" resultMap="BaseResultMap" parameterType="java.lang.Integer">
		select
		<include refid="Base_Column_List" />
		from luck_draw_prize
		where id_ = #{id,jdbcType=INTEGER}
	</select>

	<select id="getLock" resultMap="BaseResultMap" parameterType="java.lang.Integer">
		select
		<include refid="Base_Column_List" />
		from luck_draw_prize
		where id_ = #{id,jdbcType=INTEGER} for update
	</select>

	<delete id="delete" parameterType="java.lang.Integer">
		delete from luck_draw_prize
		where id_ = #{id,jdbcType=INTEGER}
	</delete>
	<delete id="deleteByIds">
		DELETE FROM luck_draw_prize WHERE id_ IN
		<foreach collection="prizeIds" open="(" close=")" item="id" separator=",">
			#{id}
		</foreach>
	</delete>

	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.LuckDrawPrize" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
		insert into luck_draw_prize (id_, name_, chances_,
		stock_, enabled_, reward_type_, memo_, group_id_, is_default_, create_on_,
		modify_on_,tenant_id_)
		values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
		#{chances,jdbcType=DOUBLE},
		#{stock,jdbcType=INTEGER}, #{enabled,jdbcType=BIT},#{rewardType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{memo,jdbcType=VARCHAR}, #{groupId,jdbcType=INTEGER}, #(isDefault), #{createOn,jdbcType=TIMESTAMP},
		#{modifyOn,jdbcType=TIMESTAMP},#{tenantId})
	</insert>

	<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
		insert into luck_draw_prize (id_, name_, chances_,
		stock_, enabled_, reward_type_, memo_, group_id_, is_default_, create_on_,
		modify_on_,tenant_id_) values 
		<foreach collection="list" item="item" separator=",">
		(#{item.id,jdbcType=INTEGER}, #{item.name,jdbcType=VARCHAR},
		#{item.chances,jdbcType=DOUBLE}, #{item.stock,jdbcType=INTEGER}, 
		#{item.enabled,jdbcType=BIT},#{item.rewardType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{item.memo,jdbcType=VARCHAR}, #{item.groupId,jdbcType=INTEGER}, #{item.isDefault}, now(), now(),#{tenantId})
		</foreach>
	</insert>

	<update id="update" parameterType="com.ym.mec.biz.dal.entity.LuckDrawPrize">
		update luck_draw_prize
		<set>
			<if test="name != null">
				name_ = #{name,jdbcType=VARCHAR},
			</if>
			<if test="chances != null">
				chances_ = #{chances,jdbcType=DOUBLE},
			</if>
			<if test="stock != null">
				stock_ = #{stock,jdbcType=INTEGER},
			</if>
			<if test="enabled != null">
				enabled_ = #{enabled,jdbcType=BIT},
			</if>
			<if test="rewardType != null">
				reward_type_ = #{rewardType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="memo != null">
				memo_ = #{memo,jdbcType=VARCHAR},
			</if>
			<if test="groupId != null">
				group_id_ = #{groupId,jdbcType=INTEGER},
			</if>
			<if test="isDefault != null">
				is_default_ = #{isDefault},
			</if>
			<if test="createOn != null">
				create_on_ = #{createOn,jdbcType=TIMESTAMP},
			</if>
			<if test="modifyOn != null">
				modify_on_ = #{modifyOn,jdbcType=TIMESTAMP},
			</if>
		</set>
		where id_ = #{id,jdbcType=INTEGER} and tenant_id_ = #{tenantId}
	</update>
	<update id="batchUpdate">
		<foreach collection="updateDraw" item="draw" separator=";">
			UPDATE luck_draw_prize
			<set>
				<if test="draw.name != null">
					name_ = #{draw.name,jdbcType=VARCHAR},
				</if>
				<if test="draw.chances != null">
					chances_ = #{draw.chances,jdbcType=DOUBLE},
				</if>
				<if test="draw.stock != null">
					stock_ = #{draw.stock,jdbcType=INTEGER},
				</if>
				<if test="draw.enabled != null">
					enabled_ = #{draw.enabled,jdbcType=BIT},
				</if>
				<if test="draw.rewardType != null">
					reward_type_ = #{draw.rewardType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="draw.memo != null">
					memo_ = #{draw.memo,jdbcType=VARCHAR},
				</if>
				<if test="draw.groupId != null">
					group_id_ = #{draw.groupId,jdbcType=INTEGER},
				</if>
				<if test="draw.isDefault != null">
					is_default_ = #{draw.isDefault},
				</if>
				modify_on_ = NOW()
			</set>
			WHERE id_ = #{draw.id,jdbcType=INTEGER} and tenant_id_ = #{draw.tenantId}
		</foreach>
	</update>

	<select id="queryCount" parameterType="map" resultType="int">
		select count(*) from luck_draw_prize
		<include refid="queryCondition" />
	</select>

	<select id="queryPage" parameterType="map" resultMap="BaseResultMap">
		select * from luck_draw_prize
		<include refid="queryCondition" />
		order by create_on_ desc
		<include refid="global.limit" />
	</select>

	<select id="queryEnabledList" resultMap="BaseResultMap" parameterType="int">
		select
		<include refid="Base_Column_List" />
		from luck_draw_prize
		where enabled_ = 1 and group_id_ = #{group} order by id_ asc
	</select>

	<select id="queryDefaultPrize" resultMap="BaseResultMap" parameterType="int">
		select
		<include refid="Base_Column_List" />
		from luck_draw_prize
		where enabled_ = 1 and group_id_ = #{group} and is_default_ = 1 order by id_ asc
	</select>
    <select id="queryHorseRaceLampDto" resultMap="com.ym.mec.biz.dal.dao.SysCouponCodeDao.HorseRaceLampDto">
		SELECT su.username_,ldp.name_ prize_name_ FROM luck_draw_log ldl
		LEFT JOIN sys_user su ON su.id_ = ldl.user_id_
		LEFT JOIN luck_draw_prize ldp ON ldp.id_ = ldl.prize_id_
		WHERE ldl.group_id_ = #{groupId} AND ldl.prize_id_ != 8
	</select>
	<select id="findByGroupId" resultMap="BaseResultMap">
		SELECT * FROM luck_draw_prize WHERE group_id_ = #{groupId}
	</select>
</mapper>