<?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.DemoGroupDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.DemoGroup" id="DemoGroup">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="single_class_minutes_" property="singleClassMinutes" />
		<result column="organ_id_list_" property="organIdList" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="user_id_" property="userId" />
		<result column="price_" property="price"/>
		<result column="subject_id_" property="subjectId"/>
	</resultMap>

	<resultMap id="studentDemoGroupListDto" type="com.ym.mec.biz.dal.dto.StudentDemoGroupListDto">
		<result property="demoGroupId" column="demo_group_id_"/>
		<result property="userId" column="user_id_"/>
		<result property="userName" column="username_"/>
		<result property="avatar" column="avatar_"/>
		<result property="introduction" column="introduction_"/>
		<result property="numberOfClasses" column="number_of_classes_"/>
		<result property="subjectName" column="subject_name_"/>
		<collection property="coursesPlans" resultMap="com.ym.mec.biz.dal.dao.DemoGroupCoursesPlanDao.DemoGroupCoursesPlan" column="dgcp.id_"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="DemoGroup" >
		SELECT * FROM demo_group WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="DemoGroup">
		SELECT * FROM demo_group ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.DemoGroup" 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 (id_,name_,single_class_minutes_,organ_id_list_,create_time_,update_time_,user_id_,price_,subject_id_) VALUES(#{id},#{name},#{singleClassMinutes},#{organIdList},#{createTime},#{updateTime},#{userId},#{price},#{subjectId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.DemoGroup">
		UPDATE demo_group <set>
<if test="userId != null">
user_id_ = #{userId},
</if>
<if test="id != null">
id_ = #{id},
</if>
<if test="updateTime != null">
update_time_ = #{updateTime},
</if>
<if test="singleClassMinutes != null">
single_class_minutes_ = #{singleClassMinutes},
</if>
<if test="organIdList != null">
organ_id_list_ = #{organIdList},
</if>
<if test="name != null">
name_ = #{name},
</if>
<if test="createTime != null">
create_time_ = #{createTime},
</if>
		<if test="price != null">
			price_ = #{price},
		</if>
		<if test="subjectId != null">
			subject_id_ = #{subjectId},
		</if>
</set> WHERE id_ = #{id} 
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM demo_group WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="DemoGroup" parameterType="map">
		SELECT * FROM demo_group ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM demo_group
	</select>

	<select id="organIdListByDemoGroupId" resultType="int">
		SELECT
			su.organ_id_
		FROM
			demo_group_class_group_mapper dgcgm
			LEFT JOIN class_group_student_mapper cgsm ON dgcgm.class_group_id_=cgsm.class_group_id_
			LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
		WHERE dgcgm.demo_group_id_=#{demoGroupId}
	</select>
	<select id="getOrganIdByUserId" resultType="java.lang.Integer">
		select organ_id_ from sys_user where id_=#{userId}
	</select>
	<select id="createCourseSchedulesByDemoGroup" parameterType="com.ym.mec.biz.dal.entity.DemoGroup" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
		SELECT
			#{classGroupId} class_group_id_,
			'NOT_START' status_,
			DATE_FORMAT(dgcp.courses_start_time_,'%Y-%m-%d') class_date_,
			dgcp.courses_start_time_ start_class_time_,
			DATE_ADD(dgcp.courses_start_time_,INTERVAL #{demoGroup.singleClassMinutes} MINUTE) end_class_time_,
			#{demoGroup.userId} teacher_id_,
			'ONLINE' teach_mode_,
			'demo' type_
		FROM
			demo_group_courses_plan dgcp
			WHERE dgcp.demo_group_id_=#{demoGroup.id} AND dgcp.courses_start_time_ = #{startTime}
	</select>
	<select id="createCourseScheduleByDemoGroupPlan" parameterType="com.ym.mec.biz.dal.entity.DemoGroup" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
		SELECT
			#{classGroupId} class_group_id_,
			'NOT_START' status_,
			dgcp.course_date_ class_date_,
			CONCAT(dgcp.course_date_,' ',dgcp.start_time_) start_class_time_,
			DATE_ADD( CONCAT(dgcp.course_date_,' ',dgcp.start_time_),INTERVAL #{demoGroup.singleClassMinutes} MINUTE) end_class_time_,
			#{demoGroup.userId} teacher_id_,
			'ONLINE' teach_mode_,
			'demo' type_
		FROM
			demo_group_courses_plan dgcp
			WHERE dgcp.id_=#{coursePlanId}
	</select>
	<select id="countDemoGroupStudents" resultType="java.lang.Integer">
		SELECT
			count(cgsm.user_id_)
		FROM
			demo_group_class_group_mapper dgcgm
			LEFT JOIN class_group_student_mapper cgsm ON dgcgm.class_group_id_=cgsm.class_group_id_
		WHERE dgcgm.demo_group_id_=#{demoGroupId}
	</select>

	<sql id="demoGroupQueryCondition">
		<where>
			dgcp.status_=0
			<if test="search!=null">
				AND su.username_ like concat('%',#{search},'%')
			</if>
			<if test="subjectIds!=null and subjectIds!=''">
				AND FIND_IN_SET(dg.subject_id_,#{subjectIds})
			</if>
			<if test="startTime!=null and endTime==null">
				AND CONCAT(dgcp.course_date_,' ',dgcp.start_time_)&gt;=#{startTime}
			</if>
			<if test="startTime!=null and endTime!=null">
				AND (CONCAT(dgcp.course_date_,' ',dgcp.start_time_) BETWEEN #{startTime} AND #{endTime})
			</if>
		</where>
	</sql>

	<select id="findDemoGroupList" resultMap="studentDemoGroupListDto">
		SELECT
		dg.id_ demo_group_id_,
		su.username_,
		su.avatar_,
		t.introduction_,
		s.name_ subject_name_,
		COUNT(dgcgm.class_group_id_) number_of_classes_,
		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 dg
		LEFT JOIN demo_group_courses_plan dgcp ON dg.id_=dgcp.demo_group_id_
		LEFT JOIN teacher t ON dg.user_id_=t.id_
		LEFT JOIN sys_user su ON dg.user_id_=su.id_
		LEFT JOIN `subject` s ON dg.subject_id_=s.id_
		LEFT JOIN demo_group_class_group_mapper dgcgm ON dg.id_=dgcgm.demo_group_id_
		<include refid="demoGroupQueryCondition"/>
		GROUP BY dgcp.id_,dg.id_
		ORDER BY dgcp.start_time_
		<include refid="global.limit"/>
	</select>
	<select id="countDemoGroupList" resultType="java.lang.Integer">
		SELECT
			count(*)
		FROM
			demo_group dg
			LEFT JOIN demo_group_courses_plan dgcp ON dg.id_=dgcp.demo_group_id_
			LEFT JOIN sys_user su ON dg.user_id_=su.id_
		<include refid="demoGroupQueryCondition"/>
	</select>
	<select id="findClassGroupIDByDemoGroupId" resultType="java.lang.Integer">
		SELECT
			class_group_id_
		FROM
			demo_group_class_group_mapper
		WHERE demo_group_id_=#{demoGroupId}
	</select>
    <select id="findDemoGroupByTeacherId" resultMap="DemoGroup">
		SELECT * FROM demo_group WHERE user_id_=#{teacherId}
	</select>

	<sql id="teacherDemoGroupsQueryCondition">
		<where>
			dg.user_id_=#{teacherId}
		</where>
	</sql>

	<select id="findTeacherDemoGroups" resultType="com.ym.mec.biz.dal.dto.TeacherManageDemoGroupListDto">
		SELECT
			cs.class_date_ classDate,
			GROUP_CONCAT(DISTINCT CONCAT(cs.start_class_time_,' ',cs.end_class_time_)) startTimes,
			GROUP_CONCAT(s.name_) subjectNames
		FROM
			demo_group dg
		LEFT JOIN demo_group_class_group_mapper dgcgm ON dg.id_=dgcgm.demo_group_id_
		LEFT JOIN class_group cg ON dgcgm.class_group_id_=cg.id_
		LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
		LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,cg.subject_id_list_)
		<include refid="teacherDemoGroupsQueryCondition"/>
		GROUP BY cs.class_date_
		<include refid="global.limit"/>
	</select>
	<select id="countTeacherDemoGroups" resultType="int">
		SELECT
			count(*)
		FROM
			demo_group dg
		LEFT JOIN demo_group_class_group_mapper dgcgm ON dg.id_=dgcgm.demo_group_id_
		LEFT JOIN class_group cg ON dgcgm.class_group_id_=cg.id_
		LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
		<include refid="teacherDemoGroupsQueryCondition"/>
		GROUP BY cs.class_date_
	</select>

	<select id="findDemoGroupStartClassTimesWithWeekByTeacherId" resultType="com.ym.mec.biz.dal.dto.EducationDemoGroupListDto">
		SELECT
			class_date_ classDate,
			GROUP_CONCAT(start_class_time_) startClassTimes
		FROM
			course_schedule
		WHERE
			YEARWEEK(class_date_) = YEARWEEK( now( ) )
		GROUP BY class_date_
	</select>
	<select id="countTeacherDemoGroupNum" resultType="java.util.Map">
		SELECT dg.user_id_ `key`,COUNT(dg.id_) `value` FROM demo_group dg
        WHERE FIND_IN_SET(dg.user_id_,#{teacherIds}) GROUP BY user_id_
	</select>

</mapper>
