<?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.DemoGroupCoursesPlanDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.DemoGroupCoursesPlan" id="DemoGroupCoursesPlan">
		<result column="id_" property="id" />
		<result column="demo_group_id_" property="demoGroupId" />
		<result column="create_time_" property="createTime" />
		<result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="course_date_" property="courseDate"/>
		<result column="start_time_" property="startTime"/>
		<result column="end_time_" property="endTime"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="DemoGroupCoursesPlan" >
		SELECT * FROM demo_group_courses_plan WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="DemoGroupCoursesPlan">
		SELECT * FROM demo_group_courses_plan ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="batchInsertByDates" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO demo_group_courses_plan (demo_group_id_,create_time_,course_date_,start_time_,end_time_)
		VALUE
		<foreach collection="dates" item="date" separator=",">
			(#{demoGroupId},now(),#{date},#{date},DATE_ADD(#{date},INTERVAL #{classMinutes} MINUTE))
		</foreach>
	</insert>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.DemoGroupCoursesPlan" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" >
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
		</selectKey>
		-->
		INSERT INTO demo_group_courses_plan (id_,demo_group_id_,course_date_,create_time_,status_,start_time_,end_time_)
		VALUES(#{id},#{demoGroupId},#{courseDate},now(),#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{startTime},#{endTime})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.DemoGroupCoursesPlan">
		UPDATE demo_group_courses_plan <set>
		<if test="id != null">
		id_ = #{id},
		</if>
		<if test="demoGroupId != null">
		demo_group_id_ = #{demoGroupId},
		</if>
		<if test="courseDate != null">
			course_date_ = #{courseDate},
		</if>
		<if test="createTime != null">
		create_time_ = #{createTime},
		</if>
		<if test="status!=null">
			status_=#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		</if>
		<if test="startTime!=null">
			start_time_=#{startTime},
		</if>
		<if test="endTime!=null">
			end_time_=#{endTime},
		</if>
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM demo_group_courses_plan WHERE id_ = #{id} 
	</delete>
	<delete id="deleteAfterDate">
		DELETE FROM demo_group_courses_plan WHERE demo_group_id_=#{demoGroupId} AND course_date_>=#{date} AND status_=0
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="DemoGroupCoursesPlan" parameterType="map">
		SELECT * FROM demo_group_courses_plan ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM demo_group_courses_plan
	</select>

    <select id="findByDateZone" resultMap="DemoGroupCoursesPlan">
		SELECT
			dgcp.id_,
			dgcp.demo_group_id_,
			dgcp.create_time_,
			dgcp.status_,
			dgcp.course_date_,
			CONCAT(dgcp.course_date_,' ',dgcp.start_time_) start_time_,
			CONCAT(dgcp.course_date_,' ',dgcp.end_time_) end_time_
		FROM
			`demo_group_courses_plan` dgcp
			LEFT JOIN demo_group dg ON dgcp.demo_group_id_=dg.id_
			WHERE dg.user_id_=#{userId}
			AND (DATE_FORMAT(dgcp.course_date_,'%Y%m') BETWEEN DATE_FORMAT(#{startTime},'%Y%m') AND DATE_FORMAT(#{endTime},'%Y%m'))
	</select>
</mapper>
