<?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"/>
        <result column="subject_name_" property="subjectName"/>
    </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>

    <select id="findById" resultMap="DemoGroup">
        SELECT
            dg.id_,
            dg.name_,
            dg.single_class_minutes_,
            dg.organ_id_list_,
            dg.create_time_,
            dg.update_time_,
            dg.user_id_,
            dg.price_,
            dg.subject_id_,
            s.name_ subject_name_
        FROM
            demo_group dg
            LEFT JOIN `subject` s ON dg.subject_id_=s.id_
        WHERE dg.id_ = #{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
        (name_,single_class_minutes_,organ_id_list_,create_time_,update_time_,user_id_,price_,subject_id_)
        VALUES(#{name},#{singleClassMinutes},#{organIdList},now(),now(),#{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="updateTime != null">
                update_time_ = NOW(),
            </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="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
			class_group_student_mapper cgsm
			LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
		WHERE cgsm.music_group_id_=#{demoGroupId} AND cgsm.group_type_='DEMO' AND cgsm.status_ != 'QUIT'
	</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_,
			#{demoGroup.userId} actual_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
			class_group_student_mapper cgsm
		WHERE cgsm.music_group_id_=#{demoGroupId} AND cgsm.group_type_='DEMO' AND cgsm.status_ != 'QUIT'
	</select>

    <sql id="demoGroupQueryCondition">
        <where>
            dgcp.status_=0
            AND CONCAT(dgcp.course_date_,' ',dgcp.start_time_)&gt;=NOW()
            <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;=DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%S')
                AND dgcp.course_date_=DATE_FORMAT(#{startTime},'%Y-%m-%d')
            </if>
            <if test="startTime!=null and endTime!=null">
                AND (CONCAT(dgcp.course_date_,' ',dgcp.start_time_) BETWEEN DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%S') AND DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%S'))
            </if>
        </where>
    </sql>

    <sql id="demoGroupQueryCondition1">
        <where>
            dgcp.status_=0
            AND dgcp.demo_group_id_ IN
            <foreach collection="demoGroupIds" item="demoGroupId" open="(" close=")" separator=",">
                #{demoGroupId}
            </foreach>
            AND CONCAT(dgcp.course_date_,' ',dgcp.start_time_)&gt;=NOW()
            <if test="params.search!=null">
                AND su.username_ like concat('%',#{params.search},'%')
            </if>
            <if test="params.subjectIds!=null and params.subjectIds!=''">
                AND FIND_IN_SET(dg.subject_id_,#{params.subjectIds})
            </if>
            <if test="params.startTime!=null and params.endTime==null">
                AND CONCAT(dgcp.course_date_,' ',dgcp.start_time_)&gt;=DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%S')
                AND dgcp.course_date_=DATE_FORMAT(#{params.startTime},'%Y-%m-%d')
            </if>
            <if test="params.startTime!=null and params.endTime!=null">
                AND (CONCAT(dgcp.course_date_,' ',dgcp.start_time_) BETWEEN DATE_FORMAT(#{params.startTime},'%Y-%m-%d %H:%i:%S') AND DATE_FORMAT(#{params.endTime},'%Y-%m-%d %H:%i:%S'))
            </if>
        </where>
    </sql>

    <select id="findDemoGroupList" resultMap="studentDemoGroupListDto">
        SELECT
            dg.id_ demo_group_id_,
            su.real_name_ username_,
            su.avatar_,
            t.introduction_,
            s.name_ subject_name_,
            COUNT(cg.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 class_group cg ON dg.id_=cg.music_group_id_ AND cg.group_type_='DEMO'
        <include refid="demoGroupQueryCondition1"/>
        GROUP BY dgcp.id_,dg.id_
        ORDER BY dgcp.start_time_
    </select>
    <select id="countDemoGroupList" resultType="java.lang.Integer">
        SELECT
          COUNT(DISTINCT dg.id_)
        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="findDemoGoupIds" resultType="java.lang.Integer">
        SELECT
          DISTINCT dg.id_
        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"/>
        <include refid="global.limit"/>
    </select>
    <select id="findClassGroupIDByDemoGroupId" resultType="java.lang.Integer">
		SELECT
			id_
		FROM
			class_group
		WHERE music_group_id_=#{demoGroupId} AND group_type_='DEMO'
	</select>
    <select id="findDemoGroupByTeacherId" resultMap="DemoGroup">
		SELECT * FROM demo_group WHERE user_id_=#{teacherId}
	</select>

    <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(DISTINCT s.name_) subjectNames
        FROM
          demo_group dg
            LEFT JOIN class_group cg ON dg.id_=cg.music_group_id_ AND cg.group_type_='DEMO'
            LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
            LEFT JOIN `subject` s ON s.id_ = dg.subject_id_
        WHERE
        (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
        AND cs.type_ = 'DEMO' AND dg.user_id_ = #{teacherId}
        GROUP BY cs.class_date_
        <include refid="global.limit"/>
    </select>
    <select id="countTeacherDemoGroups" resultType="Integer">
		SELECT COUNT(DISTINCT cs.class_date_) FROM course_schedule cs WHERE cs.type_ = 'DEMO' AND cs.actual_teacher_id_ = #{teacherId}
	</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>

	<select id="queryDemoGroupNum" resultType="map">
		select sum(case when dgcp.status_ = 0 then 1 else 0 end) unbooked_num_,
		sum(case when dgcp.status_ = 1 then 1 else 0 end) booked_num_
		from demo_group_courses_plan dgcp left join demo_group dg on dgcp.demo_group_id_ = dg.id_
		<where>
            <if test="organIds != null">
                AND INTE_ARRAY(dg.organ_id_list_,#{organIds})
            </if>
    	</where> 
	</select>
    
    <select id="queryCurrentMonthCoursesNum" resultType="int">
    	select count(cs.id_) from course_schedule cs
        left join demo_group vg on cs.music_group_id_ = vg.id_
        where cs.type_ = 'DEMO'
  		and DATE_FORMAT(cs.class_date_, '%Y%m' ) = DATE_FORMAT(CURDATE() , '%Y%m') and (cs.del_flag_ = 0 or cs.del_flag_ is null)
        <if test="organIds != null">
            AND INTE_ARRAY(vg.organ_id_list_,#{organIds})
        </if>
    </select>
</mapper>
