<?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"/>
    </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_)
		VALUES(#{id},#{musicGroupId},#{userId},#{courseFee},now(),now(),#{nextPaymentDate},#{latestPaidTime},#{continuousAbsenteeismTimes},#{subjectId},#{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{temporaryCourseFee})
	</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_ = #{updateTime},
            </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>
        </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>

    <!-- 分页查询 -->
    <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 payment_status_ = 'PAID_COMPLETED' and date_add(now(), interval #{days} day) >= next_payment_date_
    </select>
    
    <update id="batchUpdate" parameterType="java.util.List">
    	<foreach collection="list" item="item" index="index" open="" close="" separator=";">
        UPDATE music_group_student_fee_
        <set>
            <if test="item.userId != null">
                user_id_ = #{item.userId},
            </if>
            <if test="item.subjectId != null">
                subject_id_ = #{item.subjectId},
            </if>
            <if test="item.updateTime != null">
                update_time_ = #{item.updateTime},
            </if>
            <if test="item.courseFee != null">
                course_fee_ = #{item.courseFee},
            </if>
            <if test="item.musicGroupId != null">
                music_group_id_ = #{item.musicGroupId},
            </if>
            <if test="item.nextPaymentDate != null">
                next_payment_date_ = #{item.nextPaymentDate},
            </if>
            <if test="item.latestPaidTime != null">
                latest_paid_time_ = #{item.latestPaidTime},
            </if>
            <if test="item.continuousAbsenteeismTimes != null">
                continuous_absenteeism_times_ = #{item.continuousAbsenteeismTimes},
            </if>
            <if test="item.paymentStatus != null">
                payment_status_ = #{item.paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="item.temporaryCourseFee != null">
                temporary_course_fee_ = #{item.temporaryCourseFee},
            </if>
        </set>
        WHERE id_ = #{id}
        </foreach>
    </update>
</mapper>
