<?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.CourseScheduleStudentPaymentDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment" id="CourseScheduleStudentPayment">
		<result column="id_" property="id" />
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="user_id_" property="userId" />
		<result column="expect_price_" property="expectPrice" />
		<result column="actual_price_" property="actualPrice" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="settlement_time_" property="settlementTime" />
		<result column="class_group_id_" property="classGroupId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CourseScheduleStudentPayment" >
		SELECT * FROM course_schedule_student_payment WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO course_schedule_student_payment (id_,group_type_,music_group_id_,course_schedule_id_,user_id_,expect_price_,actual_price_,create_time_,update_time_,settlement_time_,class_group_id_) VALUES(#{id},#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{courseScheduleId},#{userId},#{expectPrice},#{actualPrice},#{createTime},NOW(),#{settlementTime},#{classGroupId})
	</insert>
    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
		INSERT INTO course_schedule_student_payment (id_,group_type_,music_group_id_,course_schedule_id_,user_id_,expect_price_,actual_price_,create_time_,update_time_,settlement_time_,class_group_id_)
		VALUE
		<foreach collection="list" item="data" separator=",">
			(#{data.id},#{data.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{data.musicGroupId},#{data.courseScheduleId},#{data.userId},#{data.expectPrice},#{data.actualPrice},now(),now(),#{data.settlementTime},#{data.classGroupId})
		</foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment">
		UPDATE course_schedule_student_payment 
		<set>
			<if test="userId != null">
			user_id_ = #{userId},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="courseScheduleId != null">
			course_schedule_id_ = #{courseScheduleId},
			</if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
			<if test="updateTime != null">
			update_time_ = NOW(),
			</if>
			<if test="settlementTime != null">
			settlement_time_ = #{settlementTime},
			</if>
			<if test="expectPrice != null">
			expect_price_ = #{expectPrice},
			</if>
			<if test="actualPrice != null">
			actual_price_ = #{actualPrice},
			</if>
			<if test="createTime != null">
			create_time_ = #{createTime},
			</if>
			<if test="classGroupId != null">
			class_group_id_ = #{classGroupId},
			</if>
		</set> WHERE id_ = #{id} 
	</update>

	<update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment">
		<foreach collection="courseScheduleStudentPayments" item="courseScheduleStudentPayment" separator=";">
			UPDATE course_schedule_student_payment
			<set>
				<if test="courseScheduleStudentPayment.userId != null">
					user_id_ = #{courseScheduleStudentPayment.userId},
				</if>
				<if test="courseScheduleStudentPayment.courseScheduleId != null">
					course_schedule_id_ = #{courseScheduleStudentPayment.courseScheduleId},
				</if>
				<if test="courseScheduleStudentPayment.groupType != null">
					group_type_ = #{courseScheduleStudentPayment.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="courseScheduleStudentPayment.musicGroupId != null">
					music_group_id_ = #{courseScheduleStudentPayment.musicGroupId},
				</if>
				update_time_ = NOW(),
				<if test="courseScheduleStudentPayment.settlementTime != null">
					settlement_time_ = #{courseScheduleStudentPayment.settlementTime},
				</if>
				<if test="courseScheduleStudentPayment.expectPrice != null">
					expect_price_ = #{courseScheduleStudentPayment.expectPrice},
				</if>
				<if test="courseScheduleStudentPayment.actualPrice != null">
					actual_price_ = #{courseScheduleStudentPayment.actualPrice},
				</if>
				<if test="courseScheduleStudentPayment.createTime != null">
					create_time_ = #{courseScheduleStudentPayment.createTime},
				</if>
				<if test="courseScheduleStudentPayment.classGroupId != null">
					class_group_id_ = #{courseScheduleStudentPayment.classGroupId},
				</if>
			</set> WHERE id_ = #{courseScheduleStudentPayment.id}
		</foreach>
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM course_schedule_student_payment WHERE id_ = #{id} 
	</delete>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="CourseScheduleStudentPayment" parameterType="map">
		SELECT * FROM course_schedule_student_payment ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_student_payment
	</select>
	
    <select id="findByCourseScheduleIds" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE course_schedule_id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
    </select>

	<resultMap id="StudentAttendanceStatisticsResp" type="com.ym.mec.biz.dal.dto.StudentAttendanceStatisticsResponse">
		<result property="userId" column="user_id_"/>
		<result property="studentName" column="username_"/>
	</resultMap>

	<select id="findStudentByClassGroup" resultMap="StudentAttendanceStatisticsResp">
		SELECT
			cssp.user_id_,
			su.username_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN sys_user su ON cssp.user_id_ = su.id_
		WHERE
			cssp.class_group_id_ = #{classGroupId}
		GROUP BY
			cssp.user_id_
		ORDER BY cssp.user_id_
		<include refid="global.limit"/>
	</select>

	<select id="countStudentByClassGroup" resultType="int">
		SELECT
			COUNT(DISTINCT cssp.user_id_)
		FROM
			course_schedule_student_payment cssp
		WHERE
			cssp.class_group_id_ = #{classGroupId}
	</select>
    <select id="countSurplusCourseFee" resultType="java.math.BigDecimal">
		SELECT
			SUM(expect_price_)
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
			(cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
			AND cssp.user_id_ = #{userId}
			AND CONCAT(cs.class_date_	,' ',cs.start_class_time_) &gt; NOW()
			AND cs.class_group_id_ = #{classGroupId}
    </select>
	<select id="countAllCourseFee" resultType="java.math.BigDecimal">
		SELECT
			SUM(expect_price_)
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
		WHERE
		(cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
		AND cs.class_group_id_ = #{classGroupId}
    </select>
	<select id="findNotStartCourseStudentPaymentIdsWithClassGroupAndStudent" resultType="int">
		SELECT
			cssp.id_
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cs.id_=cssp.course_schedule_id_
		WHERE
			cssp.class_group_id_ = #{classGroupId}
			AND cssp.user_id_=#{userId}
			AND CONCAT(cs.class_date_,' ',cs.start_class_time_)>NOW()
	</select>

    <select id="findVipGroupCoursePrice" resultType="java.math.BigDecimal">
		SELECT
			cssp.expect_price_
		FROM
			course_schedule cs
			LEFT JOIN course_schedule_student_payment cssp ON cs.id_ = cssp.course_schedule_id_
		WHERE
			cs.music_group_id_ = #{vipGroupId}
			AND cs.teach_mode_ = #{teachMode}
			AND cs.group_type_ = 'VIP'
			AND cssp.expect_price_ IS NOT NULL
		ORDER BY CONCAT(cs.class_date_,' ',cs.start_class_time_) DESC LIMIT 1
	</select>
    <select id="findStudentCourseNum" resultType="java.util.Map">
		SELECT COUNT(DISTINCT cssp.id_) 'value',cssp.music_group_id_ 'key' FROM course_schedule cs
		LEFT JOIN course_schedule_student_payment cssp ON cs.music_group_id_ = cssp.music_group_id_ AND cs.id_ = cssp.course_schedule_id_
		WHERE cs.music_group_id_ IN
		<foreach collection="musicGroupIds" separator="," item="item" open="(" close=")">
			#{item}
		</foreach>
		AND cs.group_type_ = #{groupType} AND cssp.user_id_ = #{userId} AND cs.status_ = 'NOT_START' AND cs.del_flag_ = 0
		GROUP BY cssp.music_group_id_
	</select>
    <select id="countStudentCourseNumWithGroup" resultType="int">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE group_type_=#{groupType} AND music_group_id_=#{groupId}
	</select>
    <select id="queryMusicStudentPer" resultType="java.lang.Integer">
		SELECT cssp.user_id_ FROM course_schedule_student_payment cssp
		LEFT JOIN music_group mg ON cssp.music_group_id_ = mg.id_ AND cssp.group_type_ = 'MUSIC'
		LEFT JOIN student_registration sr ON cssp.user_id_ = sr.user_id_ AND cssp.music_group_id_ = sr.music_group_id_
		WHERE mg.status_ = 'PROGRESS' AND sr.music_group_status_ != 'QUIT'
		GROUP BY cssp.user_id_
	</select>
    <select id="queryVipStudentPer" resultType="java.lang.Integer">
		SELECT cssp.user_id_ FROM course_schedule_student_payment cssp
		LEFT JOIN vip_group vg ON cssp.music_group_id_ = vg.id_
		WHERE vg.group_status_ IN (2,4) AND cssp.group_type_ = 'VIP'
		GROUP BY cssp.user_id_
	</select>
    <select id="countCourseStudentNum" resultType="java.util.Map">
		SELECT
			course_schedule_id_ AS 'key',
			COUNT( DISTINCT user_id_ ) AS 'value'
		FROM
			course_schedule_student_payment
		WHERE course_schedule_id_ IN
			<foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
				#{courseId}
			</foreach>
		GROUP BY
			course_schedule_id_
	</select>
    <select id="checkStudentHaveCourse" resultType="int">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE course_schedule_id_=#{courseScheduleId} AND user_id_=#{userId}
	</select>
	<select id="findByCourseSchedule" resultMap="CourseScheduleStudentPayment">
		SELECT * FROM course_schedule_student_payment WHERE  course_schedule_id_ = #{courseScheduleId}
	</select>

	<resultMap id="VipGroupGiveCourseSortDto" type="com.ym.mec.biz.dal.dto.VipGroupGiveCourseSortDto" extends="CourseScheduleStudentPayment">
		<result property="courseStartTime" column="course_start_time_"/>
		<result property="courseEndTime" column="course_end_time_"/>
		<result property="teachMode" column="teach_mode_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
	<select id="findVipGroupSortCourseByGroup" resultMap="VipGroupGiveCourseSortDto">
		SELECT
			 cssp.*,
			 cs.teach_mode_,
			 CONCAT(cs.class_date_,' ',cs.start_class_time_) course_start_time_,
			 CONCAT(cs.class_date_,' ',cs.end_class_time_) course_end_time_
		FROM course_schedule_student_payment cssp
			LEFT JOIN course_schedule cs ON cssp.course_schedule_id_=cs.id_
		WHERE cssp.music_group_id_=#{groupId} AND cssp.group_type_='VIP'
-- 		AND CONCAT(cs.class_date_,' ',cs.start_class_time_) &gt; NOW()
		AND cssp.settlement_time_ IS NULL
	</select>
	<select id="countStudentNum" resultType="java.lang.Integer">
		SELECT COUNT(id_) FROM course_schedule_student_payment WHERE course_schedule_id_ = #{courseScheduleId}
	</select>

	<delete id="deleteStudentCourseSchedule">
		DELETE FROM course_schedule_student_payment WHERE user_id_ = #{userId} AND course_schedule_id_ IN
		<foreach collection="courseScheduleList" item="courseSchedule" index="index" open="(" close=")" separator=",">
			#{courseSchedule.id}
		</foreach>
	</delete>
    <delete id="deleteByCourseSchedule">
		DELETE FROM course_schedule_student_payment WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" index="index" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</delete>
    <delete id="batchDeleteWithID">
		DELETE FROM course_schedule_student_payment WHERE id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
    </delete>
	<delete id="deleteStudentCourseScheduleByUserId">
		DELETE FROM course_schedule_student_payment WHERE course_schedule_id_ = #{courseScheduleId} AND user_id_  IN
		<foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
			#{userId}
		</foreach>
	</delete>
	
	<delete id="deleteByMusicGroupId" parameterType="map">
		DELETE cssp FROM course_schedule_student_payment cssp left join course_schedule cs on cssp.course_schedule_id_ = cs.id_ WHERE cssp.music_group_id_=#{musicGroupId} AND cssp.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and cs.status_ = 'NOT_START'
	</delete>
</mapper>
