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

	<resultMap type="com.ym.mec.biz.dal.entity.StudentCompetition" id="StudentCompetition">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="username_" property="username" />
		<result column="id_card_no_" property="idCardNo" />
		<result column="age_" property="age" />
		<result column="gender_" property="gender" />
		<result column="grade_" property="grade" />
		<result column="subject_" property="subject" />
		<result column="chapter_" property="chapter" />
		<result column="picture_url_" property="pictureUrl" />
		<result column="video_url_" property="videoUrl" />
		<result column="score_" property="score" />
		<result column="prize_level_" property="prizeLevel" />
		<result column="is_show_" property="isShow" />
		<result column="comment_" property="comment" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="StudentCompetition" >
		SELECT * FROM student_competition WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="StudentCompetition">
		SELECT * FROM student_competition ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentCompetition" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" >
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
		</selectKey>
		-->
		INSERT INTO student_competition (id_,user_id_,username_,id_card_no_,age_,gender_,grade_,subject_,chapter_,picture_url_,video_url_,score_,prize_level_,is_show_,comment_,create_time_,update_time_)
		VALUES(#{id},#{userId},#{username},#{idCardNo},#{age},#{gender},#{grade},#{subject},#{chapter},#{pictureUrl},#{videoUrl},#{score},#{prizeLevel},#{isShow},#{comment},NOW(),NOW())
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentCompetition">
		UPDATE student_competition <set>
		<if test="isShow != null">
			is_show_ = #{isShow},
		</if>
		<if test="id != null">
			id_ = #{id},
		</if>
		<if test="gender != null">
			gender_ = #{gender},
		</if>
		<if test="score != null">
			score_ = #{score},
		</if>
		<if test="prizeLevel != null">
			prize_level_ = #{prizeLevel},
		</if>
		<if test="age != null">
			age_ = #{age},
		</if>
		<if test="videoUrl != null">
			video_url_ = #{videoUrl},
		</if>
		<if test="username != null">
			username_ = #{username},
		</if>
		<if test="chapter != null">
			chapter_ = #{chapter},
		</if>
		<if test="userId != null">
			user_id_ = #{userId},
		</if>
		<if test="subject != null">
			subject_ = #{subject},
		</if>
		<if test="comment != null">
			comment_ = #{comment},
		</if>
		<if test="pictureUrl != null">
			picture_url_ = #{pictureUrl},
		</if>
		<if test="idCardNo != null">
			id_card_no_ = #{idCardNo},
		</if>
		<if test="grade != null">
			grade_ = #{grade},
		</if>
			update_time_ = NOW()
	</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM student_competition WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="StudentCompetition" parameterType="map">
		SELECT * FROM student_competition ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student_competition
	</select>

	<sql id="queryStudentCondition">
		<where>
			<if test="grade!=null">
				AND grade_=#{grade}
			</if>
			<if test="gender!=null">
				AND gender_=#{gender}
			</if>
			<if test="subject != null and subject != ''">
				AND subject_=#{subject}
			</if>
			<if test="prizeLevel!=null">
				AND prize_level_=#{prizeLevel}
			</if>
			<if test="isShow!=null">
				AND is_show_=#{isShow}
			</if>
			<if test="isReview!=null and isReview==0">
				AND score_ IS NULL
			</if>
			<if test="isReview!=null and isReview==1">
				AND score_ IS NOT NULL
			</if>
			<if test="search!=null and search!=''">
				AND (CAST(age_ AS CHAR)=#{search} OR CAST(user_id_ AS CHAR)=#{search} OR username_ LIKE CONCAT('%', #{search}, '%') OR id_card_no_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<select id="queryStudentCompetitions" resultMap="StudentCompetition">
		SELECT
			id_,
			user_id_,
			username_,
			id_card_no_,
			age_,
			gender_,
			grade_,
			subject_,
			chapter_,
			picture_url_,
			video_url_,
			CASE WHEN score_ IS NULL THEN -1 ELSE score_ END score_,
			CASE WHEN prize_level_ IS NULL THEN -1 ELSE prize_level_ END prize_level_,
			is_show_,
			comment_,
			create_time_,
			update_time_
		FROM student_competition
		<include refid="queryStudentCondition"/>
		ORDER BY id_ DESC
		<include refid="global.limit"/>
	</select>
	<select id="countStudentCompetitions" resultType="int">
		SELECT COUNT(*) FROM student_competition
		<include refid="queryStudentCondition"/>
	</select>

	<select id="getWinnerList" resultMap="StudentCompetition">
		SELECT * FROM student_competition WHERE is_show_ = 1
		ORDER BY ISNULL(score_),score_ DESC
	</select>

	<select id="getCompetitionWithUser" resultMap="StudentCompetition">
		SELECT * FROM student_competition WHERE user_id_ = #{userId}
	</select>

	<select id="getCompetitionsWithPrizeLevel" resultType="long">
		SELECT id_ FROM student_competition WHERE prize_level_ = #{prizeLevel} FOR UPDATE
	</select>
</mapper>
