<?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.MusicGroupDao">

	<resultMap type="com.ym.mec.biz.dal.entity.MusicGroup" id="MusicGroup">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="organ_id_" property="organId" />
		<result column="school_id_" property="schoolId" />
		<result column="apply_expire_date_" property="applyExpireDate" />
		<result column="team_teacher_id_" property="teamTeacherId" />
		<result column="educational_teacher_id_" property="educationalTeacherId" />
		<result column="charge_type_id_" property="chargeTypeId" />
		<result column="course_form_" property="courseForm" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="bill_start_date_" property="billStartDate" />
		<result column="improvent_classes_num_" property="improventClassesNum" />
		<result column="enroll_classes_" property="enrollClasses" />
		<result column="payment_expire_date_" property="paymentExpireDate" />
		<result column="is_extra_class_" property="isExtraClass" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="cooperation_organ_id_" property="cooperationOrganId" />
		<result column="enlightenment_course_time_" property="enlightenmentCourseTime" />
		<result column="parent_meeting_time_" property="parentMeetingTime" />
		<result column="img_" property="img" />
		<result column="director_user_id_" property="directorUserId" />
		<result column="settlement_type_" property="settlementType" />
		<result column="expect_start_group_date_" property="expectStartGroupDate" />
		<result column="group_member_num_" property="groupMemberNum" />
		<result column="memo_" property="memo" />
        <result column="ownership_type_" property="ownershipType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="MusicGroup">
		SELECT * FROM
		music_group WHERE id_ = #{id}
	</select>
	
	<select id="getLocked" resultMap="MusicGroup">
		SELECT * FROM music_group WHERE id_ = #{id} for update
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="MusicGroup">
		SELECT * FROM music_group ORDER
		BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroup"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO music_group
		(id_,name_,organ_id_,school_id_,apply_expire_date_,team_teacher_id_,educational_teacher_id_,charge_type_id_,course_form_,create_time_,update_time_,status_,
		bill_start_date_,improvent_classes_num_,enroll_classes_,payment_expire_date_,is_extra_class_,settlement_type_,cooperation_organ_id_,enlightenment_course_time_,
		parent_meeting_time_,img_,director_user_id_,is_classroom_lessons_,memo_,expect_start_group_date_,ownership_type_)
		VALUES(#{id},#{name},#{organId},#{schoolId},#{applyExpireDate},#{teamTeacherId},#{educationalTeacherId},#{chargeTypeId},#{courseForm},now(),now(),
		#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{billStartDate},#{improventClassesNum},#{enrollClasses},#{paymentExpireDate},
		#{isExtraClass, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{settlementType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{cooperationOrganId},#{enlightenmentCourseTime},#{parentMeetingTime},#{img},#{directorUserId},#{isClassroomLessons},#{memo},#{expectStartGroupDate},#{ownershipType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroup">
		UPDATE music_group
		<set>
			<if test="ownershipType != null">
				ownership_type_ = #{ownershipType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="status != null">
				status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="improventClassesNum != null">
				improvent_classes_num_ = #{improventClassesNum},
			</if>
			<if test="courseForm != null">
				course_form_ = #{courseForm},
			</if>
			<if test="chargeTypeId != null">
				charge_type_id_ = #{chargeTypeId},
			</if>
			<if test="educationalTeacherId != null">
				educational_teacher_id_ = #{educationalTeacherId},
			</if>
			<if test="enrollClasses != null">
				enroll_classes_ = #{enrollClasses},
			</if>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="isExtraClass != null">
				is_extra_class_ = #{isExtraClass, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="paymentExpireDate != null">
				payment_expire_date_ = #{paymentExpireDate},
			</if>
			<if test="schoolId != null">
				school_id_ = #{schoolId},
			</if>
			<if test="applyExpireDate != null">
				apply_expire_date_ = #{applyExpireDate},
			</if>
			<if test="billStartDate != null">
				bill_start_date_ = #{billStartDate},
			</if>
			<if test="cooperationOrganId != null">
				cooperation_organ_id_ = #{cooperationOrganId},
			</if>
			<if test="updateTime != null">
				update_time_ = NOW(),
			</if>
			<if test="enlightenmentCourseTime != null">
				enlightenment_course_time_ = #{enlightenmentCourseTime},
			</if>
			<if test="teamTeacherId != null">
				team_teacher_id_ = #{teamTeacherId},
			</if>
			<if test="parentMeetingTime != null">
				parent_meeting_time_ = #{parentMeetingTime},
			</if>
			<if test="img != null">
				img_ = #{img},
			</if>
			<if test="directorUserId != null">
				director_user_id_ = #{directorUserId},
			</if>
			<if test="settlementType != null">
				settlement_type_ = #{settlementType},
			</if>
			<if test="isClassroomLessons != null">
				is_classroom_lessons_ = #{isClassroomLessons},
			</if>
			<if test="expectStartGroupDate != null">
				expect_start_group_date_ = #{expectStartGroupDate},
			</if>
			<if test="groupMemberNum != null">
				group_member_num_ = #{groupMemberNum},
			</if>
			<if test="memo != null">
				memo_ = #{memo},
			</if>
		</set>
		WHERE id_ = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM music_group WHERE id_ = #{id}
	</delete>

	<select id="queryPage" resultMap="MusicGroup" parameterType="map">
		SELECT * FROM music_group
		<include refid="musicGroupQueryPageSql"/>
		ORDER BY create_time_ DESC
		<include refid="global.limit" />
	</select>

	<sql id="musicGroupQueryPageSql">
		<where>
			<if test="organId != null">
				AND FIND_IN_SET(organ_id_,#{organId})
			</if>
			<if test="musicGroupName != null and musicGroupName != ''">
				AND (name_ LIKE CONCAT('%',#{musicGroupName},'%') or id_ like CONCAT('%',#{musicGroupName},'%'))
			</if>
			<if test="chargeTypeId != null">
				AND charge_type_id_ = #{chargeTypeId}
			</if>
			<if test="musicGroupStatus != null">
				AND status_ = #{musicGroupStatus}
			</if>
			<if test="musicGroupId != null">
				AND FIND_IN_SET(id_,#{musicGroupId})
			</if>
		</where>
	</sql>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM music_group
		<include refid="musicGroupQueryPageSql"/>
	</select>

	<resultMap type="com.ym.mec.biz.dal.dto.MusicCardDto" id="queryUserMusicGroupsMap">
		<result column="register_id_" property="registerId" />
		<result column="user_name_" property="userName" />
		<result column="subject_name_" property="subjectName" />
		<result column="subject_id_" property="subjectId" />
		<result column="current_class_" property="currentClass" />
		<result column="current_grade_" property="currentGrade" />
		<result column="music_group_name_" property="musicGroupName" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="vip_class_num_" property="vipClassNum" />
		<result column="music_group_num_" property="musicGroupNum" />
		<!--<result column="payment_status_" property="paymentStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>-->
	</resultMap>

    <select id="queryUserMusicGroups" resultMap="queryUserMusicGroupsMap">
		SELECT sr.id_ register_id_,su.username_ user_name_,sr.current_class_,sr.current_grade_
		,mg.name_ music_group_name_,s.name_ subject_name_,s.id_ subject_id_,mg.id_ music_group_id_
		FROM student_registration sr
		LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
		LEFT JOIN `subject` s ON sr.actual_subject_id_ = s.id_
		LEFT JOIN sys_user su ON  su.id_ = sr.user_id_
		WHERE sr.user_id_ = #{userId} AND mg.status_ IN ('APPLY','PAY','PREPARE','PROGRESS') AND sr.music_group_status_ != 'QUIT'
		ORDER BY mg.create_time_ DESC
	</select>

	<select id="queryPersonalMusicGroups" resultMap="queryUserMusicGroupsMap">
		SELECT sr.name_ user_name_,mg.name_ music_group_name_,s.name_ subject_name_
		FROM student_registration sr
		LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
		LEFT JOIN `subject` s ON sr.actual_subject_id_ = s.id_
		WHERE sr.user_id_ = #{userId} AND sr.music_group_status_ != 'QUIT'
		ORDER BY mg.update_time_ DESC LIMIT 1
	</select>

	<select id="countUserMusicGroups" resultType="int">
		SELECT COUNT(mg.id_)
		FROM student_registration sr
		LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
		WHERE sr.user_id_ = #{userId} AND sr.music_group_status_ != 'QUIT'
	</select>
    <select id="countCourseByType" resultType="java.lang.Integer">
		SELECT SUM(cg.total_class_times_) - SUM(cg.current_class_times_) FROM class_group_student_mapper cgsm
		LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
		WHERE cgsm.user_id_ = #{userId} AND cg.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		AND cg.del_flag_ = 0 AND cgsm.status_ != 'QUIT'
	</select>

	<resultMap type="com.ym.mec.biz.dal.dto.BasicUserDto" id="BasicUserDto">
		<result column="subject_name_" property="subjectName" />
		<result column="username_" property="name" />
		<result column="user_id_" property="userId" />
		<result column="head_url_" property="headUrl" />
		<result column="gender_" property="gender"/>
		<result column="music_group_id_" property="musicGroupId"/>
		<result column="user_type_" property="userType"/>
	</resultMap>

	<select id="findTeachersByStuId" resultMap="BasicUserDto">
		SELECT DISTINCT
			su.real_name_ username_,
			su.id_ user_id_,
			su.avatar_ head_url_,
			gender_,
			GROUP_CONCAT(s.name_) subject_name_
		  FROM
			class_group_student_mapper cgsp
			LEFT JOIN class_group cg ON cgsp.class_group_id_ = cg.id_
			LEFT JOIN class_group_teacher_mapper cgtm ON cg.id_ = cgtm.class_group_id_
			LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
			LEFT JOIN teacher t ON su.id_ = t.id_
			LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,t.subject_id_)
		  WHERE
			cgsp.user_id_ = #{studentId} AND cgsp.status_ != 'QUIT'
		  GROUP BY su.id_
	</select>

    <select id="queryUserGroups" resultMap="com.ym.mec.biz.dal.dao.ClassGroupDao.imGroupModel">
        SELECT mg.id_,mg.name_,mg.img_
		FROM student_registration sr
		LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
		<where>
				sr.music_group_status_ != 'QUIT'
			<if test="userId != null">
				AND sr.user_id_ = #{userId}
			</if>
			<if test="search != null">
				AND mg.name_ LIKE CONCAT('%',#{search},'%')
			</if>
		</where>
	</select>

    <select id="countMusicGroupUserNum" resultType="map" parameterType="list">
		SELECT SUM(student_num_) 'value',music_group_id_ 'key' FROM class_group
		WHERE FIND_IN_SET(music_group_id_,#{musicGroupIds})
		GROUP BY music_group_id_
    </select>

	<resultMap id="ImUserModel" type="com.ym.mec.common.entity.ImUserModel">
		<result column="id_" property="id"/>
		<result property="name" column="username_"/>
		<result property="portrait" column="avatar_"/>
	</resultMap>
	<!--<select id="findGroupUsers" resultMap="ImUserModel">
		SELECT su.id_,su.avatar_,su.username_ FROM class_group cg
		LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
		LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
		WHERE cg.music_group_id_ = #{musicGroupId}  AND cgsm.status_ != 'QUIT' GROUP BY su.id_
	</select>-->
	<select id="queryTeacherGroups" resultMap="com.ym.mec.biz.dal.dao.ClassGroupDao.imGroupModel">
		SELECT mg.id_,mg.name_,mg.img_,GROUP_CONCAT(DISTINCT cgtm.user_id_) teacher_ids_
		FROM music_group mg
		LEFT JOIN class_group cg ON mg.id_ = cg.music_group_id_
		LEFT JOIN class_group_teacher_mapper cgtm ON cg.id_ = cgtm.class_group_id_
		WHERE (cgtm.user_id_ = #{userId} OR mg.team_teacher_id_ = #{userId} OR mg.educational_teacher_id_ = #{userId}
		 OR director_user_id_ = #{userId})
		<if test="search != null and search != ''">
			AND mg.name_ LIKE CONCAT('%',#{search},'%')
		</if>
		GROUP BY mg.id_
	</select>

	<!--<select id="queryTeacherIds" resultType="java.lang.Integer">
		SELECT DISTINCT cgtm.user_id_ FROM class_group cg
		LEFT JOIN class_group_teacher_mapper cgtm ON cg.id_ = cgtm.class_group_id_
		WHERE cg.music_group_id_ = #{musicGroupId}
	</select>-->

    <select id="countPayNum" resultType="java.util.Map" parameterType="list">
		SELECT cg.music_group_id_ `key`,COUNT(cgsm.id_) `value` FROM class_group cg
		LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
		WHERE FIND_IN_SET(cg.music_group_id_,#{musicGroupIds})
		AND cgsm.status_ != 'QUIT' AND cg.type_ != 'MIX' AND cg.group_type_ = 'MUSIC'
		GROUP BY cg.music_group_id_
	</select>

    <select id="queryListByIds" resultMap="MusicGroup" parameterType="list">
		SELECT * FROM music_group WHERE FIND_IN_SET(id_,#{musicGroupIds})
	</select>
    <select id="findSettlementTypeByMusicGroups" resultType="map">
		SELECT
			id_ AS 'key',
			settlement_type_ AS 'value'
		FROM
			music_group
		WHERE
			id_ IN
			<foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
				#{musicGroupId}
			</foreach>
    </select>
    
  	<select id="queryOrganMusicGroupNum" resultType="map">
    	select count(*) total,sum(case when status_ = 'PREPARE' then 1 else 0 end) prepare_num_,sum(case when status_ = 'PROGRESS' then 1 else 0 end) progress_num_,sum(case when status_ = 'PAUSE' then 1 else 0 end) pause_num_ from music_group
    	<where>
    		<if test="organId != null">
    			FIND_IN_SET(organ_id_,#{organId})
    		</if>
    	</where>
  	</select>
  	
  	<select id="queryCurrentMonthCoursesNum" resultType="int">
  		SELECT count(cs.id_) from course_schedule cs
		left join class_group cg on cs.class_group_id_ = cg.id_
		left join music_group mg on cg.music_group_id_ = mg.id_
		where cs.group_type_ = 'MUSIC' AND cg.group_type_ = 'MUSIC'
  		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="organId != null">
   			AND FIND_IN_SET(mg.organ_id_,#{organId})
   		</if>
  	</select>
	<select id="queryTeacherMap" resultType="java.util.Map">
		SELECT su.id_ 'key',su.phone_ 'value' FROM class_group_teacher_mapper cgtm
		LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
		WHERE cgtm.music_group_id_ = #{musicGroupId} AND su.phone_ IS NOT NULL
		GROUP BY su.id_
	</select>
	<select id="queryUserIdByRoleId" resultType="java.lang.Integer" parameterType="list">
		SELECT DISTINCT sur.user_id_ FROM sys_user_role sur
		LEFT JOIN employee e ON sur.user_id_ = e.user_id_
		WHERE sur.role_id_ IN
		<foreach collection="roleIds" open="(" separator="," close=")" item="item">
			#{item}
		</foreach>
		<if test="organId != null">
			AND FIND_IN_SET(#{organId},e.organ_id_list_)
		</if>
	</select>
	<select id="queryTeacherIdMap" resultType="java.util.Map">
		SELECT cgtm.user_id_ 'key',cgtm.user_id_ 'value' FROM class_group_teacher_mapper cgtm
		WHERE cgtm.music_group_id_ = #{musicGroupId}
		GROUP BY cgtm.user_id_
	</select>
	<select id="queryEducationalMusicGroupId" resultType="java.lang.String">
		SELECT GROUP_CONCAT(mg.id_) FROM music_group mg WHERE mg.educational_teacher_id_ = #{userIds}
	</select>

	<select id="findUserMusicGroup" resultMap="MusicGroup">
		SELECT mg.* FROM music_group mg
		LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
		WHERE sr.user_id_ = #{userId} ORDER BY sr.id_ ASC LIMIT 1
	</select>

	<select id="findUserMusicGroups" resultMap="MusicGroup">
		SELECT mg.* FROM music_group mg
		LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
		WHERE sr.user_id_ = #{userId}
	</select>

    <select id="findMusicGroup" resultMap="MusicGroup">
		SELECT mg.* FROM student_registration sr
		LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
		WHERE sr.id_ IN
		<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
		AND mg.id_ IS NOT NULL LIMIT 1
	</select>
	<select id="queryMusicGroupNameMap" resultType="java.util.Map">
		SELECT id_ 'key',name_ 'value' FROM music_group WHERE id_ IN
		<foreach collection="musicGroupIds" open="(" close=")" item="item" separator=",">
			#{item}
		</foreach>
		AND name_ IS NOT NULL
	</select>

	<select id="findUserSchool" resultMap="com.ym.mec.biz.dal.dao.SchoolDao.School">
		SELECT s.* FROM music_group mg
		LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
		LEFT JOIN school s on mg.school_id_ = s.id_
		WHERE sr.user_id_ = #{userId} ORDER BY sr.id_ DESC LIMIT 1
	</select>
</mapper>
