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

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupStudentFee" id="MusicGroupStudentFee">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="user_id_" property="userId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="course_fee_" property="courseFee"/>
        <result column="next_payment_date_" property="nextPaymentDate"/>
        <result column="latest_paid_time_" property="latestPaidTime"/>
        <result column="continuous_absenteeism_times_" property="continuousAbsenteeismTimes"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="payment_status_" property="paymentStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="temporary_course_fee_" property="temporaryCourseFee"/>
        <result column="is_lock_" property="isLock"/>
        <result column="payment_period_list_" property="paymentPeriodList"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupStudentFee" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
		INSERT INTO music_group_student_fee_ (id_,music_group_id_,user_id_,course_fee_,create_time_,update_time_,next_payment_date_,latest_paid_time_,continuous_absenteeism_times_,subject_id_,payment_status_,temporary_course_fee_,payment_period_list_)
		VALUES(#{id},#{musicGroupId},#{userId},#{courseFee},now(),now(),#{nextPaymentDate},#{latestPaidTime},#{continuousAbsenteeismTimes},#{subjectId},#{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{temporaryCourseFee},#{paymentPeriodList})
	</insert>

    <insert id="batchInsert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupStudentFee">
        INSERT INTO music_group_student_fee_ (music_group_id_,user_id_,create_time_,update_time_,next_payment_date_,subject_id_,payment_status_,course_fee_,payment_period_list_)
		VALUES
		<foreach collection="musicGroupStudentFees" item="item" separator=",">
            (#{item.musicGroupId},#{item.userId},NOW(),NOW(),#{nextPaymentDate},#{item.subjectId},#{paymentStatus},#{item.courseFee},#{item.paymentPeriodList})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroupStudentFee">
        UPDATE music_group_student_fee_
        <set>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="courseFee != null">
                course_fee_ = #{courseFee},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="nextPaymentDate != null">
                next_payment_date_ = #{nextPaymentDate},
            </if>
            <if test="latestPaidTime != null">
                latest_paid_time_ = #{latestPaidTime},
            </if>
            <if test="continuousAbsenteeismTimes != null">
                continuous_absenteeism_times_ = #{continuousAbsenteeismTimes},
            </if>
            <if test="paymentStatus != null">
                payment_status_ = #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="temporaryCourseFee != null">
                temporary_course_fee_ = #{temporaryCourseFee},
            </if>
            <if test="isLock != null">
                is_lock_ = #{isLock},
            </if>
            <if test="paymentPeriodList != null">
                payment_period_list_ = #{paymentPeriodList},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>
    
    <update id="updateStudentAbsenteeismTimes">
        update music_group_student_fee_
        <if test="operate==0">
            set continuous_absenteeism_times_=0
        </if>
        <if test="operate==1">
            set continuous_absenteeism_times_=continuous_absenteeism_times_+1
        </if>
        WHERE music_group_id_ = #{musicGroupId} AND user_id_ = #{userId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM music_group_student_fee_ WHERE id_ = #{id} 
	</delete>
	
    <delete id="deleteByUserIdAndMusicGroupId" parameterType="map">
		DELETE FROM music_group_student_fee_ WHERE user_id_ = #{userId} and music_group_id_ = #{musicGroupId}
	</delete>
	
    <delete id="deleteByMusicGroupId" parameterType="map">
		DELETE FROM music_group_student_fee_ WHERE music_group_id_ = #{musicGroupId}
		<if test="isLock != null">
            AND is_lock_ = #{isLock}
        </if>
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="MusicGroupStudentFee" parameterType="map">
        SELECT * FROM music_group_student_fee_ ORDER BY id_
        <include refid="global.limit"/>
    </select>

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

    <select id="findByUser" resultMap="MusicGroupStudentFee">
        SELECT * FROM music_group_student_fee_ WHERE music_group_id_ = #{musicGroupId} AND user_id_ = #{userId}
    </select>
    
    <update id="updateNextPaymentDate" parameterType="map">
        update music_group_student_fee_ set next_payment_date_ = #{nextPaymentDate},update_time_ = now() WHERE music_group_id_ = #{musicGroupId}
    </update>

    <select id="queryWillRenewList" resultMap="MusicGroupStudentFee">
        SELECT * FROM music_group_student_fee_
        WHERE next_payment_date_ IS NOT NULL AND payment_status_ = 'PAID_COMPLETED'
        and date_add(now(), interval #{days} day) >= next_payment_date_
    </select>

    <select id="countContinuosAbsenteeismStudentNum" resultType="java.util.Map">
        SELECT
            music_group_id_ AS 'key',
            COUNT( id_ ) AS 'value'
        FROM
            music_group_student_fee_
        WHERE
            continuous_absenteeism_times_ &gt;=3
            AND music_group_id_ IN
            <foreach collection="musicGroupIds" item="musicGroupId" open="(" close=")" separator=",">
                #{musicGroupId}
            </foreach>
        GROUP BY music_group_id_
    </select>
    <select id="queryUserPaymentStatus" resultType="java.util.Map">
        SELECT DISTINCT music_group_id_ 'key',payment_status_ 'value' FROM music_group_student_fee_
        WHERE user_id_ = #{userId} AND FIND_IN_SET(music_group_id_,#{musicGroupIds})
    </select>
    <select id="countStudentNoPayNum" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT sr.id_) FROM student_registration sr LEFT JOIN music_group_student_fee_ sf ON sr.user_id_ = sf.user_id_
        WHERE sr.music_group_id_ = #{musicGroupId} AND sr.music_group_status_ != 'QUIT' AND sf.payment_status_ != 'PAID_COMPLETED'
        AND sr.music_group_id_ = sf.music_group_id_
    </select>

    <select id="findContinuousAbsenteeismTimesByUsersAndMusicGroup" resultType="map">
        SELECT
            mgsf.user_id_ AS 'key',
            mgsf.continuous_absenteeism_times_ AS 'value'
        FROM
            music_group_student_fee_ mgsf
            WHERE mgsf.music_group_id_=#{musicGroupId}
            AND mgsf.user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
    </select>

    <select id="initMusicGroupStudentFee" resultMap="MusicGroupStudentFee">
        SELECT sr.music_group_id_,sr.user_id_,sr.actual_subject_id_ subject_id_,mgsp.fee_ course_fee_ FROM student_registration sr
        LEFT JOIN music_group_student_fee_ mgsf ON sr.user_id_ = mgsf.user_id_ AND sr.music_group_id_ = mgsf.music_group_id_
        LEFT JOIN music_group_subject_plan mgsp ON mgsp.music_group_id_ = sr.music_group_id_ AND mgsp.subject_id_ = sr.actual_subject_id_
        WHERE sr.music_group_status_ != 'QUIT' AND sr.payment_status_ = 2 AND mgsf.id_ IS NULL AND sr.music_group_id_ = #{musicGroupId}
    </select>
    <select id="findPaymentStatusMap" resultType="java.util.Map">
        SELECT user_id_ 'key',payment_status_ 'value' FROM music_group_student_fee_ WHERE music_group_id_ = #{musicGroupId}
    </select>
    <select id="queryByMusicGroupId" resultMap="MusicGroupStudentFee">
        SELECT * FROM music_group_student_fee_ WHERE music_group_id_ = #{musicGroupId}
    </select>

    <update id="batchUpdate" parameterType="java.util.List">
    	<foreach collection="list" item="item" index="index" open="" close="" separator=";">
        UPDATE music_group_student_fee_
        SET update_time_ = NOW(),payment_period_list_ = #{item.paymentPeriodList},
            next_payment_date_ = #{item.nextPaymentDate},subject_id_ = #{item.subjectId},
            course_fee_ = #{item.courseFee},latest_paid_time_ = #{item.latestPaidTime},
            continuous_absenteeism_times_ = #{item.continuousAbsenteeismTimes},
            payment_status_ = #{item.paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            temporary_course_fee_ = #{item.temporaryCourseFee}
        WHERE id_ = #{item.id}
        </foreach>
    </update>
    <update id="updateCourseFee">
        UPDATE music_group_student_fee_ SET course_fee_ = #{courseFee}
        WHERE music_group_id_ = #{musicGroupId} AND subject_id_ = #{subjectId} AND is_lock_ = 0
    </update>
    <update id="updateStudentFee">
        UPDATE music_group_student_fee_
        SET course_fee_ = #{fee.amount},payment_period_list_ = #{fee.month},update_time_ = NOW()
        WHERE music_group_id_ = #{fee.musicGroupId} AND user_id_ = #{fee.studentId}
    </update>
    <update id="updateStudentFeeIsLock">
        UPDATE music_group_student_fee_
        SET is_lock_ = #{fee.isLock},update_time_ = NOW()
        WHERE music_group_id_ = #{fee.musicGroupId} AND user_id_ = #{fee.studentId}
    </update>
    <update id="batchUpdateCalender">
        UPDATE music_group_student_fee_
        SET payment_period_list_ = #{month},update_time_ = NOW()
        WHERE music_group_id_ = #{musicGroupId} AND is_lock_ = 0
    </update>
    <update id="batchUpdateCalenders">
        UPDATE music_group_student_fee_ mgsf SET mgsf.payment_period_list_ = (
        SELECT GROUP_CONCAT(mgpc.payment_month_) FROM music_group_payment_calender mgpc WHERE mgpc.music_group_id_ = mgsf.music_group_id_)
        WHERE mgsf.music_group_id_ = #{musicGroupId}
    </update>
    <update id="updateCalender">
        UPDATE music_group_student_fee_ mgsf SET mgsf.payment_period_list_ = (
        SELECT GROUP_CONCAT(mgpc.payment_month_) FROM music_group_payment_calender mgpc WHERE mgpc.music_group_id_ = mgsf.music_group_id_)
        WHERE mgsf.music_group_id_ = #{musicGroupId} AND mgsf.user_id_ = #{userId}
    </update>
</mapper>
