<?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.ExtracurricularExercisesDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.ExtracurricularExercises" id="ExtracurricularExercises">
		<result column="id_" property="id" />
		<result column="teacher_id_" property="teacherId" />
		<result column="username_" property="teacher.username" />
		<result column="student_id_list_" property="studentIdList" />
		<result column="batch_no_" property="batchNo" />
		<result column="title_" property="title" />
		<result column="attachments_" property="attachments" />
		<result column="content_" property="content" />
		<result column="expire_date_" property="expireDate" />
		<result column="completed_num_" property="completedNum" />
		<result column="expect_num_" property="expectNum" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="organ_name_" property="organName" />
	</resultMap>

	<sql id="queryPageCondition">
		<where>
			<if test="teacherId != null">
				and teacher_id_ = #{teacherId}
			</if>
			<if test="title != null">
				and title_ like '%' #{title} '%'
			</if>
			<if test="batchNo != null">
				and batch_no_ = #{batchNo}
			</if>
			<if test="organIdList != null">
				AND FIND_IN_SET(o.id_, #{organIdList})
			</if>
			<if test="assignStartTime != null">
				AND date(ee.create_time_) &gt;= #{assignStartTime}
			</if>
			<if test="assignEndTime != null">
				AND date(ee.create_time_) &lt;= #{assignEndTime}
			</if>
		</where>
	</sql>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExtracurricularExercises" >
		SELECT * FROM extracurricular_exercises WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExtracurricularExercises">
		SELECT * FROM extracurricular_exercises ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercises" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO extracurricular_exercises (id_,teacher_id_,student_id_list_,batch_no_,title_,attachments_,content_,expire_date_,completed_num_,expect_num_,create_time_,update_time_)
		VALUES(#{id},#{teacherId},#{studentIdList},#{batchNo},#{title},#{attachments},#{content},#{expireDate},#{completedNum},#{expectNum},NOW(), NOW())
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.ExtracurricularExercises">
		UPDATE extracurricular_exercises
		<set>
			<if test="studentIdList != null">
			student_id_list_ = #{studentIdList},
			</if>
			<if test="expireDate != null">
			expire_date_ = #{expireDate},
			</if>
			<if test="batchNo != null">
			batch_no_ = #{batchNo},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="teacherId != null">
			teacher_id_ = #{teacherId},
			</if>
			<if test="title != null">
			title_ = #{title},
			</if>
			<if test="expectNum != null">
			expect_num_ = #{expectNum},
			</if>
			<if test="content != null">
			content_ = #{content},
			</if>
			<if test="attachments != null">
			attachments_ = #{attachments},
			</if>
			<if test="completedNum != null">
			completed_num_ = #{completedNum},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM extracurricular_exercises WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExtracurricularExercises" parameterType="map">
		SELECT ee.*,u.real_name_ username_,o.name_ organ_name_
		FROM extracurricular_exercises ee left join sys_user u on ee.teacher_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryPageCondition"/>
		ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(ee.id_) FROM extracurricular_exercises ee left join sys_user u on ee.teacher_id_ = u.id_
		left join teacher t on t.id_ = ee.teacher_id_
		left join organization o on o.id_ = t.organ_id_
		<include refid="queryPageCondition"/>
	</select>

	<sql id="queryExtraExercisesCondition">
		<where>
			<if test="teacherId!=null">
				teacher_id_=#{teacherId}
			</if>
			<if test="createTime!=null">
				AND DATE_FORMAT(create_time_, '%Y-%m') = DATE_FORMAT(#{createTime}, '%Y-%m')
			</if>
		</where>
	</sql>

	<select id="countExtraExercises" resultType="int">
		SELECT COUNT(id_) FROM extracurricular_exercises
		<include refid="queryExtraExercisesCondition"/>
	</select>
	<select id="findExtraExercises" resultMap="ExtracurricularExercises">
		SELECT * FROM extracurricular_exercises
		<include refid="queryExtraExercisesCondition"/>
		ORDER BY create_time_ DESC
		<include refid="global.limit"/>
	</select>
</mapper>
