<?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.MusicGroupTrainPlanDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.MusicGroupTrainPlan" id="MusicGroupTrainPlan">
		<result column="id_" property="id" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="class_group_id_" property="classGroupId" />
		<result column="course_type_" property="courseType" />
		<result column="plan_" property="plan" />
		<result column="start_school_term_" property="startSchoolTerm" />
		<result column="end_school_term_" property="endSchoolTerm" />
		<result column="update_time_" property="updateTime" />
		<result column="create_time_" property="createTime" />
        <result column="tenant_id_" property="tenantId"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="MusicGroupTrainPlan" >
		SELECT * FROM music_group_train_plan WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="MusicGroupTrainPlan">
		SELECT * FROM music_group_train_plan
		<where>
			tenant_id_ = #{tenantId}
			<if test="classGroupId != null">
				AND class_group_id_ = #{classGroupId}
			</if>
			<if test="courseScheduleType != null and courseScheduleType != ''">
				AND course_type_ = #{courseScheduleType}
			</if>
			<if test="endSchoolTerm != null">
				AND end_school_term_ = #{endSchoolTerm}
			</if>
			<if test="startSchoolTerm != null">
				AND start_school_term_ = #{startSchoolTerm}
			</if>
		</where>
		ORDER BY id_
	</select>
	<select id="findAllPlan" resultType="java.lang.String">
		SELECT plan_ FROM music_group_train_plan
		<where>
			tenant_id_ = #{tenantId}
			<if test="classGroupId != null">
				AND class_group_id_ = #{classGroupId}
			</if>
			<if test="courseScheduleType != null and courseScheduleType != ''">
				AND course_type_ = #{courseScheduleType}
			</if>
			<if test="endSchoolTerm != null">
				AND end_school_term_ = #{endSchoolTerm}
			</if>
			<if test="startSchoolTerm != null">
				AND start_school_term_ = #{startSchoolTerm}
			</if>
		</where>
		ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupTrainPlan" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO music_group_train_plan (music_group_id_,class_group_id_,course_type_,plan_,start_school_term_,end_school_term_,update_time_,create_time_,tenant_id_)
		VALUES(#{musicGroupId},#{classGroupId},#{courseType},#{plan},#{startSchoolTerm},#{endSchoolTerm},NOW(),NOW(),#{tenantId})
	</insert>
    <insert id="add">
		INSERT INTO music_group_train_plan (music_group_id_,class_group_id_,course_type_,plan_,start_school_term_,end_school_term_,update_time_,create_time_,tenant_id_)
		VALUES
		<foreach collection="dto.plans" separator="," item="plan">
			(#{dto.musicGroupId},#{dto.classGroupId},#{dto.courseScheduleType},#{plan},#{dto.startSchoolTerm},#{dto.endSchoolTerm},NOW(),NOW(),#{dto.tenantId})
		</foreach>
	</insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroupTrainPlan">
		UPDATE music_group_train_plan <set>
		<if test="endSchoolTerm != null">
		end_school_term_ = #{endSchoolTerm},
		</if>
		<if test="classGroupId != null">
		class_group_id_ = #{classGroupId},
		</if>
		<if test="startSchoolTerm != null">
		start_school_term_ = #{startSchoolTerm},
		</if>
		<if test="plan != null">
		plan_ = #{plan},
		</if>
		<if test="courseType != null">
		course_type_ = #{courseType},
		</if>
		<if test="musicGroupId != null">
		music_group_id_ = #{musicGroupId},
		</if>
		update_time_ = NOW(),
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM music_group_train_plan WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MusicGroupTrainPlan" parameterType="map">
		SELECT * FROM music_group_train_plan where tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM music_group_train_plan where tenant_id_ = #{tenantId}
	</select>
	<resultMap id="WaitCourseTrainPlanDto" type="com.ym.mec.biz.dal.dto.WaitCourseTrainPlanDto">
		<result property="classGroupId" column="class_group_id_"/>
		<result property="classGroupName" column="name_"/>
		<result property="num" column="num_"/>
		<result property="scheduleType" column="type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
    <select id="queryWaitCourseTrainPlan" resultMap="WaitCourseTrainPlanDto">
		SELECT c.class_group_id_,c.type_,c.course_num_ - (CASE WHEN t.course_num_ IS NULL THEN 0 ELSE t.course_num_ END) num_,c.name_ FROM (
		SELECT cs.class_group_id_,cs.type_,COUNT(cs.id_) course_num_,cg.name_ FROM course_schedule cs
		LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
		WHERE cs.music_group_id_ = #{dto.musicGroupId} AND cs.del_flag_ = 0 AND cs.pre_course_flag_ = 0 AND cs.is_lock_ = 0
		<if test="dto.startSchoolTerm != null">
			AND cs.class_date_ >= DATE_FORMAT(#{dto.startSchoolTerm}, '%Y%m%d')
		</if>
		<if test="dto.endSchoolTerm != null">
			AND cs.class_date_ &lt;= DATE_FORMAT(#{dto.endSchoolTerm}, '%Y%m%d')
		</if>
		GROUP BY cs.class_group_id_,cs.type_) c
		LEFT JOIN (SELECT tp.class_group_id_,tp.course_type_ type_,COUNT(tp.id_) course_num_,'' name_ FROM music_group_train_plan tp
				WHERE tp.music_group_id_ = #{dto.musicGroupId}
				<if test="dto.startSchoolTerm != null">
					AND tp.start_school_term_ = #{dto.startSchoolTerm}
				</if>
				<if test="dto.endSchoolTerm != null">
					AND tp.end_school_term_ = #{dto.endSchoolTerm}
				</if>
				GROUP BY tp.class_group_id_,tp.course_type_) t ON c.type_ = t.type_ AND c.class_group_id_ = t.class_group_id_
		WHERE c.course_num_ > (CASE WHEN t.course_num_ IS NULL THEN 0 ELSE t.course_num_ END)
	</select>
    <select id="queryWaitCourseTrainPlan1" resultMap="WaitCourseTrainPlanDto">
		SELECT t.class_group_id_,t.type_,t.course_num_ - (CASE WHEN c.course_num_ IS NULL THEN 0 ELSE c.course_num_ END) num_,t.name_
		FROM (SELECT tp.class_group_id_,tp.course_type_ type_,COUNT(tp.id_) course_num_,cg.name_ FROM music_group_train_plan tp
		LEFT JOIN class_group cg ON cg.id_ = tp.class_group_id_
		WHERE tp.music_group_id_ = #{dto.musicGroupId}
		<if test="dto.startSchoolTerm != null">
			AND tp.start_school_term_ = #{dto.startSchoolTerm}
		</if>
		<if test="dto.endSchoolTerm != null">
			AND tp.end_school_term_ = #{dto.endSchoolTerm}
		</if>
		GROUP BY tp.class_group_id_,tp.course_type_) t
		LEFT JOIN (
		SELECT cs.class_group_id_,cs.type_,COUNT(cs.id_) course_num_,'' name_ FROM course_schedule cs
		WHERE cs.music_group_id_ = #{dto.musicGroupId} AND cs.del_flag_ = 0 AND cs.pre_course_flag_ = 0 AND cs.is_lock_ = 0
		<if test="dto.startSchoolTerm != null">
			AND cs.class_date_ >= DATE_FORMAT(#{dto.startSchoolTerm}, '%Y%m%d')
		</if>
		<if test="dto.endSchoolTerm != null">
			AND cs.class_date_ &lt;= DATE_FORMAT(#{dto.endSchoolTerm}, '%Y%m%d')
		</if>
		GROUP BY cs.class_group_id_,cs.type_) c ON c.class_group_id_ = t.class_group_id_ AND c.type_ = t.type_
		WHERE t.course_num_ > (CASE WHEN c.course_num_ IS NULL THEN 0 ELSE c.course_num_ END)
	</select>
	<select id="countPlanNum" resultType="int">
		SELECT COUNT(id_) FROM music_group_train_plan
		WHERE class_group_id_ = #{dto.classGroupId} AND course_type_ = #{dto.courseScheduleType}
		<if test="dto.startSchoolTerm != null">
			AND start_school_term_ = #{dto.startSchoolTerm}
		</if>
		<if test="dto.endSchoolTerm != null">
			AND end_school_term_ = #{dto.endSchoolTerm}
		</if>
	</select>
	<select id="queryPlan" resultMap="MusicGroupTrainPlan">
		SELECT * FROM music_group_train_plan
		<where>
			tenant_id_ = #{schoolTerm.tenantId}
			<if test="schoolTerm.startSchoolTerm != null">
				AND start_school_term_ = #{schoolTerm.startSchoolTerm}
			</if>
			<if test="schoolTerm.endSchoolTerm != null">
				AND end_school_term_ = #{schoolTerm.endSchoolTerm}
			</if>
			<if test="schoolTerm.classGroupId != null">
				AND class_group_id_ = #{schoolTerm.classGroupId}
			</if>
			<if test="schoolTerm.courseScheduleType != null">
				AND course_type_ = #{schoolTerm.courseScheduleType}
			</if>
		</where>
	</select>
</mapper>
