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

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupSubjectPlan" id="MusicGroupSubjectPlan">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="expected_student_num_" property="expectedStudentNum"/>
        <result column="apply_student_num_" property="applyStudentNum"/>
        <result column="paid_student_num_" property="paidStudentNum"/>
        <result column="fee_" property="fee"/>
        <result column="kit_group_purchase_type_" property="kitGroupPurchaseType"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="deposit_fee_" property="depositFee"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="version" property="version"/>
        <result column="sub_name_" property="subName"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupSubjectPlan" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO music_group_subject_plan
        (id_,music_group_id_,subject_id_,expected_student_num_,apply_student_num_,fee_,kit_group_purchase_type_,deposit_fee_,create_time_,update_time_)
        VALUES(#{id},#{musicGroupId},#{subjectId},#{expectedStudentNum},#{applyStudentNum},#{fee},#{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{depositFee},now(),now())
    </insert>
    <insert id="batchAdd">
        INSERT INTO music_group_subject_plan
        (music_group_id_,subject_id_,expected_student_num_,fee_,kit_group_purchase_type_,deposit_fee_,create_time_,update_time_)
        VALUES
        <foreach collection="subjectPlans" item="item" separator=",">
            (#{musicGroupId},#{item.subjectId},#{item.expectedStudentNum},#{item.fee},#{item.kitGroupPurchaseType},
            #{item.depositFee},now(),now())
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroupSubjectPlan">
        UPDATE music_group_subject_plan
        <set>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="expectedStudentNum != null">
                expected_student_num_ = #{expectedStudentNum},
            </if>
            <if test="applyStudentNum != null">
                apply_student_num_ = #{applyStudentNum},
            </if>
            <if test="fee != null">
                fee_ = #{fee},
            </if>
            <if test="kitGroupPurchaseType != null">
                kit_group_purchase_type_ =
                #{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="depositFee != null">
                deposit_fee_ = #{depositFee},
            </if>
            <if test="paidStudentNum != null">
                paid_student_num_ = #{paidStudentNum},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="version != null">
                version = version+1,
            </if>
        </set>
        WHERE id_ = #{id} AND version =#{version}
    </update>

    <update id="updateExpectedStudentNum">
        UPDATE music_group_subject_plan SET expected_student_num_ = #{expectedStudentNum},update_time_ = now() WHERE id_ = #{musicGroupSubjectPlanId}
    </update>

    <update id="batchUpdateFee" parameterType="java.util.List">
        <foreach collection="plans" item="item" index="index" open="" close="" separator=";">
            UPDATE music_group_subject_plan
            <set>
                fee_ = #{item.fee},update_time_ = now()
            </set>
            <where>
                id_ = #{item.id}
            </where>
        </foreach>
    </update>

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

    <delete id="delByMusicGroupId">
        DELETE FROM music_group_subject_plan WHERE music_group_id_=#{musicGroupId}
    </delete>

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

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

    <!-- 查询乐团科目计划表 -->
    <select id="getMusicSubjectClassPlan" resultMap="MusicGroupSubjectPlan">
        SELECT sp.*, sb.name_ sub_name_
        FROM music_group_subject_plan sp
        LEFT JOIN subject sb ON sb.id_ = sp.subject_id_
        WHERE sp.music_group_id_=#{musicGroupId}
    </select>
    <select id="getMusicOneSubjectClassPlan" resultMap="MusicGroupSubjectPlan">
        SELECT sp.*, sb.name_
        FROM music_group_subject_plan sp
        LEFT JOIN subject sb ON sb.id_ = sp.subject_id_
        WHERE sp.music_group_id_=#{musicGroupId} AND sp.subject_id_=#{subjectId}
    </select>
    <select id="findSubjectPlan" resultMap="MusicGroupSubjectPlan">
        SELECT * FROM music_group_subject_plan WHERE music_group_id_ = #{musicGroupId} AND subject_id_ = #{subjectId} LIMIT 1
    </select>
    <select id="findSubjectMap" resultType="java.util.Map">
        SELECT subject_id_ 'key',kit_group_purchase_type_ 'value' FROM music_group_subject_plan mgsp
        WHERE mgsp.music_group_id_ = #{musicGroupId} AND subject_id_ IN
        <foreach collection="subjectIds" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>
</mapper>
