<?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.LuckDrawLogDao">
	<resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.LuckDrawLog">
		<id column="id_" property="id" jdbcType="BIGINT" />
		<result column="group_id_" property="groupId" jdbcType="INTEGER" />
		<result column="prize_id_" property="prizeId" jdbcType="INTEGER" />
		<result column="user_id_" property="userId" jdbcType="BIGINT" />
		<result column="create_on_" property="createOn" jdbcType="TIMESTAMP" />
		<result column="name_" property="luckDrawPrize.name" jdbcType="VARCHAR" />
		<result column="reward_type_" property="luckDrawPrize.rewardType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<resultMap id="BaseResultMapExt" type="com.ym.mec.biz.dal.entity.LuckDrawLog" extends="BaseResultMap">
		<result column="username_" property="user.username" jdbcType="VARCHAR" />
		<result column="phone_" property="user.phone" jdbcType="VARCHAR" />
		<result column="organ_name_" property="user.organName" jdbcType="VARCHAR" />
	</resultMap>

	<sql id="Base_Column_List">
		l.id_,l.group_id_, l.prize_id_,d.name_,d.reward_type_, l.user_id_, l.create_on_,l.tenant_id_
	</sql>

	<select id="get" resultMap="BaseResultMap" parameterType="java.lang.Long">
		select
		<include refid="Base_Column_List" />
		from luck_draw_log l left join luck_draw_prize d on
		l.prize_id_ = d.id_
		where l.id_ = #{id,jdbcType=BIGINT}
	</select>

	<delete id="delete" parameterType="java.lang.Long">
		delete from
		luck_draw_log
		where id_ = #{id,jdbcType=BIGINT}
	</delete>

	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.LuckDrawLog">
		insert into
		luck_draw_log (id_,group_id_, prize_id_, user_id_,
		create_on_,tenant_id_)
		values
		(#{id,jdbcType=BIGINT}, #{groupId,jdbcType=INTEGER}, #{prizeId,jdbcType=INTEGER},
		#{userId,jdbcType=BIGINT},
		#{createOn,jdbcType=TIMESTAMP},#{tenantId})
	</insert>

	<update id="update" parameterType="com.ym.mec.biz.dal.entity.LuckDrawLog">
		update luck_draw_log
		<set>
			<if test="groupId != null">
				group_id_ = #{groupId,jdbcType=INTEGER},
			</if>
			<if test="prizeId != null">
				prize_id_ = #{prizeId,jdbcType=INTEGER},
			</if>
			<if test="userId != null">
				user_id_ = #{userId,jdbcType=BIGINT},
			</if>
			<if test="createOn != null">
				create_on_ = #{createOn,jdbcType=TIMESTAMP},
			</if>
		</set>
		where id_ = #{id,jdbcType=BIGINT} and tenant_id_ = #{tenantId}
	</update>

	<select id="queryCount" parameterType="map" resultType="int">
		SELECT COUNT(*) FROM luck_draw_log l
		LEFT JOIN luck_draw_prize d ON l.prize_id_ = d.id_
		LEFT JOIN sys_user u ON u.id_=l.user_id_
		<include refid="queryCondition" />
	</select>

	<select id="queryPage" parameterType="map" resultMap="BaseResultMapExt">
		SELECT <include refid="Base_Column_List" />,u.username_,u.phone_,o.name_ organ_name_ FROM luck_draw_log l
		LEFT JOIN luck_draw_prize d ON l.prize_id_ = d.id_
		LEFT JOIN sys_user u ON u.id_=l.user_id_
		LEFT JOIN organization o ON o.id_ = u.organ_id_
		<include refid="queryCondition" />
		ORDER BY l.create_on_ DESC
		<include refid="global.limit" />
	</select>
    <select id="countUseNumByIds" resultType="java.lang.String">
		SELECT GROUP_CONCAT(DISTINCT ldp.name_) FROM luck_draw_log ldl
		LEFT JOIN luck_draw_prize ldp ON ldl.prize_id_ = ldp.id_
		WHERE ldl.prize_id_ IN
		<foreach collection="prizeIds" open="(" close=")" item="id" separator=",">
			#{id}
		</foreach>
	</select>

    <sql id="queryCondition">
		<where>
			l.tenant_id_ = #{tenantId}
			<if test="prizeId != null">
				AND l.prize_id_ = #{id}
			</if>
			<if test="userId != null">
				AND l.user_id_ = #{userId}
			</if>
			<if test="rewardType != null">
				AND d.reward_type_ LIKE CONCAT('%',#{rewardType},'%')
			</if>
			<if test="groupId != null">
				AND l.group_id_ = #{groupId}
			</if>
			<if test="name != null and name != ''">
				AND d.name_ = #{name}
			</if>
			<if test="startDate != null">
				AND l.create_on_ &gt;= #{startDate}
			</if>
			<if test="endDate != null">
				AND l.create_on_ &lt;= #{endDate}
			</if>
			<if test="organId != null">
				AND u.organ_id_ = #{organId}
			</if>
			<if test="defaultFlag != null">
				AND d.is_default_ = #{defaultFlag}
			</if>
			<if test="search != null and search != ''">
				AND (u.username_ LIKE CONCAT('%',#{search},'%') OR u.id_ = #{search})
			</if>
		</where>
	</sql>
</mapper>