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

	<resultMap type="com.ym.mec.biz.dal.entity.MusicEnlightenmentQuestionnaire" id="MusicEnlightenmentQuestionnaire">
		<result column="id_" property="id" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="user_id_" property="userId" />
		<result column="user_name_" property="userName" />
		<result column="phone_" property="phone" />
		<result column="current_grade_" property="currentGrade" />
		<result column="current_grade_num_" property="currentGradeNum" />
		<result column="current_class_" property="currentClass" />
		<result column="teacher_recommand_subject_id_" property="teacherRecommandSubjectId" />
		<result column="subject_name_" property="subjectName"/>
		<result column="musical_instruments_learning_" property="musicalInstrumentsLearning" />
		<result column="musical_instruments_name_" property="musicalInstrumentsName" />
		<result column="join_parent_meeting_" property="joinParentMeeting" />
		<result column="remark_" property="remark" />
		<result column="create_time_" property="createTime"/>
		<result column="update_time_" property="updateTime"/>
		<result column="tenant_id_" property="tenantId" />
	</resultMap>

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

	<!-- 全查询 -->
	<select id="findAll" resultMap="MusicEnlightenmentQuestionnaire">
		SELECT * FROM music_enlightenment_questionnaire where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicEnlightenmentQuestionnaire" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO music_enlightenment_questionnaire (music_group_id_,user_id_,user_name_,phone_,current_grade_,current_grade_num_,current_class_,teacher_recommand_subject_id_,musical_instruments_learning_,musical_instruments_name_,join_parent_meeting_,remark_,create_time_,update_time_,tenant_id_)
		VALUES(#{musicGroupId},#{userId},#{userName},#{phone},#{currentGrade},#{currentGradeNum},#{currentClass},#{teacherRecommandSubjectId},#{musicalInstrumentsLearning},#{musicalInstrumentsName},#{joinParentMeeting},#{remark},NOW(),NOW(),#{tenantId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicEnlightenmentQuestionnaire">
		UPDATE music_enlightenment_questionnaire
		<set>
			<if test="userName != null">
				user_name_ = #{userName},
			</if>
			<if test="currentGrade != null">
				current_grade_ = #{currentGrade},
			</if>
			<if test="joinParentMeeting != null">
				join_parent_meeting_ = #{joinParentMeeting},
			</if>
			<if test="musicalInstrumentsLearning != null">
				musical_instruments_learning_ = #{musicalInstrumentsLearning},
			</if>
				teacher_recommand_subject_id_ = #{teacherRecommandSubjectId},
			<if test="currentClass != null">
				current_class_ = #{currentClass},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="remark != null">
				remark_ = #{remark},
			</if>
			<if test="musicalInstrumentsName != null">
				musical_instruments_name_ = #{musicalInstrumentsName},
			</if>
			<if test="currentGradeNum != null">
				current_grade_num_ = #{currentGradeNum},
			</if>
			<if test="phone != null">
				phone_ = #{phone},
			</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_enlightenment_questionnaire WHERE id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			meq.tenant_id_ = #{tenantId}
			<if test="musicGroupId!=null and musicGroupId!=''">
				AND meq.music_group_id_=#{musicGroupId}
			</if>
			<if test="currentGradeNum!=null">
				AND meq.current_grade_num_ = #{currentGradeNum}
			</if>
			<if test="teacherRecommandSubjectId!=null">
				AND meq.teacher_recommand_subject_id_ = #{teacherRecommandSubjectId}
			</if>
			<if test="musicalInstrumentsLearning!=null">
				AND meq.musical_instruments_learning_ = #{musicalInstrumentsLearning}
			</if>
			<if test="search!=null and search!=''">
				AND (meq.user_id_ = #{search} OR meq.user_name_ LIKE CONCAT('%', #{search}, '%') OR meq.phone_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MusicEnlightenmentQuestionnaire" parameterType="map">
		SELECT meq.*,sub.name_ subject_name_ FROM music_enlightenment_questionnaire meq
		LEFT JOIN subject sub ON meq.teacher_recommand_subject_id_=sub.id_
		<include refid="queryCondition" />
		ORDER BY meq.id_ DESC
		<include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM music_enlightenment_questionnaire meq
		<include refid="queryCondition" />
		ORDER BY meq.id_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="lockUserMusicEnlightenmentQuestionnaire" resultMap="MusicEnlightenmentQuestionnaire">
		SELECT * FROM music_enlightenment_questionnaire WHERE music_group_id_=#{musicGroupId} AND phone_=#{userPhone} FOR UPDATE
	</select>
	<select id="musicEnlightenmentQuestionnaireStat"
			resultType="com.ym.mec.biz.dal.dto.MusicEnlightenmentQuestionnaireStatDto">
		SELECT sub.name_ subjectName,COUNT(meq.id_) userNum,SUM(meq.musical_instruments_learning_) learningUserNum
		FROM music_enlightenment_questionnaire meq
				 LEFT JOIN `subject` sub ON meq.teacher_recommand_subject_id_=sub.id_
		WHERE meq.music_group_id_=#{musicGroupId}
		GROUP BY meq.teacher_recommand_subject_id_;
	</select>

	<select id="findByMusicGroupId" resultMap="MusicEnlightenmentQuestionnaire">
		SELECT * FROM music_enlightenment_questionnaire WHERE music_group_id_=#{musicGroupId}
	</select>
</mapper>
