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

    <resultMap
            type="com.ym.mec.biz.dal.entity.MusicGroupPaymentStudentCourseDetail"
            id="MusicGroupPaymentStudentCourseDetail">
        <result column="id_" property="id"/>
        <result column="music_group_payment_calender_id_" property="musicGroupPaymentCalenderId"/>
        <result column="music_group_payment_calender_detail_id_"
                property="musicGroupPaymentCalenderDetailId"/>
        <result column="user_id_" property="userId"/>
        <result column="course_type_" property="courseType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="course_original_price_" property="courseOriginalPrice"/>
        <result column="course_current_price_" property="courseCurrentPrice"/>
        <result column="total_course_minutes_" property="totalCourseMinutes"/>
        <result column="used_course_minutes_" property="usedCourseMinutes"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert"
            parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentStudentCourseDetail"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO music_group_payment_student_course_detail
        (music_group_payment_calender_id_, music_group_payment_calender_detail_id_, user_id_, course_type_,
         total_course_minutes_, used_course_minutes_, create_time_, update_time_, course_original_price_,
         course_current_price_, tenant_id_)
        VALUES (#{musicGroupPaymentCalenderId}, #{musicGroupPaymentCalenderDetailId}, #{userId},
                #{courseType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{totalCourseMinutes},
                #{usedCourseMinutes},
                NOW(), NOW(), #{courseOriginalPrice}, #{courseCurrentPrice}, #{tenantId})
    </insert>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO music_group_payment_student_course_detail
        (music_group_payment_calender_id_,music_group_payment_calender_detail_id_,user_id_,
        course_type_,total_course_minutes_,used_course_minutes_,create_time_,update_time_,course_original_price_,course_current_price_,tenant_id_)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.musicGroupPaymentCalenderId},#{item.musicGroupPaymentCalenderDetailId},
            #{item.userId},#{item.courseType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            #{item.totalCourseMinutes},#{item.usedCourseMinutes},NOW(),NOW(),#{item.courseOriginalPrice},#{item.courseCurrentPrice},#{item.tenantId})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update"
            parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentStudentCourseDetail">
        UPDATE music_group_payment_student_course_detail
        <set>
            <if test="courseOriginalPrice != null">
                course_original_price_ = #{courseOriginalPrice},
            </if>
            <if test="courseCurrentPrice != null">
                course_current_price_ = #{courseCurrentPrice},
            </if>
            <if test="usedCourseMinutes != null">
                used_course_minutes_ = #{usedCourseMinutes},
            </if>
            <if test="totalCourseMinutes != null">
                total_course_minutes_ = #{totalCourseMinutes},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="musicGroupPaymentCalenderDetailId != null">
                music_group_payment_calender_detail_id_ = #{musicGroupPaymentCalenderDetailId},
            </if>
            <if test="musicGroupPaymentCalenderId != null">
                music_group_payment_calender_id_ = #{musicGroupPaymentCalenderId},
            </if>
            <if test="courseType != null">
                course_type_ = #{courseType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <update id="batchUpdate"
            parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentStudentCourseDetail">
        <foreach collection="paymentCourseDetails" item="paymentCourseDetail" separator=";">
            UPDATE music_group_payment_student_course_detail
            <set>
                <if test="paymentCourseDetail.courseOriginalPrice != null">
                    course_original_price_ = #{paymentCourseDetail.courseOriginalPrice},
                </if>
                <if test="paymentCourseDetail.courseCurrentPrice != null">
                    course_current_price_ = #{paymentCourseDetail.courseCurrentPrice},
                </if>
                <if test="paymentCourseDetail.usedCourseMinutes != null">
                    used_course_minutes_ = #{paymentCourseDetail.usedCourseMinutes},
                </if>
                <if test="paymentCourseDetail.totalCourseMinutes != null">
                    total_course_minutes_ = #{paymentCourseDetail.totalCourseMinutes},
                </if>
                <if test="paymentCourseDetail.userId != null">
                    user_id_ = #{paymentCourseDetail.userId},
                </if>
                <if test="paymentCourseDetail.musicGroupPaymentCalenderDetailId != null">
                    music_group_payment_calender_detail_id_ = #{paymentCourseDetail.musicGroupPaymentCalenderDetailId},
                </if>
                <if test="paymentCourseDetail.musicGroupPaymentCalenderId != null">
                    music_group_payment_calender_id_ = #{paymentCourseDetail.musicGroupPaymentCalenderId},
                </if>
                <if test="paymentCourseDetail.courseType != null">
                    course_type_ =
                    #{paymentCourseDetail.courseType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                update_time_ = NOW()
            </set>
            WHERE id_ = #{paymentCourseDetail.id} and tenant_id_ = #{paymentCourseDetail.tenantId}
        </foreach>
    </update>
    <update id="updateByCourseType">
        UPDATE music_group_payment_student_course_detail
        SET course_current_price_ = #{courseCurrentPrice},
            update_time_          = NOW()
        WHERE course_type_ = #{courseType}
          AND FIND_IN_SET(music_group_payment_calender_detail_id_, #{musicGroupPaymentCalenderIds})
    </update>

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

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

    <delete id="deleteByMusicGroupPaymentCalenderDetailId">
        DELETE FROM music_group_payment_student_course_detail
        <where>
            music_group_payment_calender_detail_id_ IN
            <foreach item="item" collection="list" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
        </where>
    </delete>

    <!-- 分页查询 -->
    <select id="queryByMusicGroupPaymentStudentCourseDetailId" resultMap="MusicGroupPaymentStudentCourseDetail"
            parameterType="map">
        SELECT * FROM music_group_payment_student_course_detail
        <where>
            music_group_payment_calender_detail_id_ in
            <foreach item="item" collection="list" separator="," open="(" close=")" index="">
                #{item}
            </foreach>
        </where>
    </select>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="MusicGroupPaymentStudentCourseDetail"
            parameterType="map">
        SELECT * FROM music_group_payment_student_course_detail where tenant_id_ = #{tenantId} ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM music_group_payment_student_course_detail
        where tenant_id_ = #{tenantId}
    </select>

    <select id="getUnUseWithStudentAndCourseTypeAndCourseMinutes" resultMap="MusicGroupPaymentStudentCourseDetail">
        SELECT mgpscd.*
        FROM music_group_payment_student_course_detail mgpscd
                 LEFT JOIN music_group_payment_calender mgpc ON mgpscd.music_group_payment_calender_id_ = mgpc.id_
        WHERE FIND_IN_SET(mgpc.batch_no_, #{batchNo})
          AND mgpscd.user_id_ = #{studentId}
          AND mgpscd.course_type_ = #{courseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND mgpscd.used_course_minutes_ &lt;= 0
        ORDER BY mgpscd.id_;
    </select>

    <select id="getUnUseBatchNoWithStudentAndCourseTypeAndCourseMinutes" resultType="string">
        SELECT
        mgpc.batch_no_
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group_payment_student_course_detail mgpscd ON mgpscd.music_group_payment_calender_id_ = mgpc.id_
        WHERE
        mgpc.music_group_id_ = #{musicGroupId}
        <if test="studentId != null">
            AND mgpscd.user_id_ = #{studentId}
        </if>
        <if test="batchNos != null">
            AND mgpc.batch_no_ NOT IN
            <foreach collection="batchNos" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="courseType != null">
            AND mgpscd.course_type_ = #{courseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        </if>
        GROUP BY mgpc.batch_no_ HAVING min(mgpscd.used_course_minutes_) &lt;=0 ORDER BY MIN(mgpc.id_) LIMIT 1;
    </select>

    <select id="getUnUseWithStudents" resultMap="MusicGroupPaymentStudentCourseDetail">
        SELECT
        MIN(mgpscd.id_) id_,
        MIN(mgpscd.music_group_payment_calender_id_) music_group_payment_calender_id_,
        mgpscd.user_id_,
        mgpscd.course_type_,
        SUM(mgpscd.total_course_minutes_) total_course_minutes_,
        SUM(mgpscd.used_course_minutes_) used_course_minutes_
        FROM music_group_payment_student_course_detail mgpscd
        LEFT JOIN music_group_payment_calender mgpc ON mgpscd.music_group_payment_calender_id_ = mgpc.id_
        WHERE mgpc.music_group_id_ = #{musicGroupId}
        AND user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
        GROUP BY mgpscd.user_id_,mgpscd.course_type_,mgpc.batch_no_
        HAVING used_course_minutes_ &lt;= 0
    </select>

    <delete id="deleteByUserIdAndMusicGroupId">
        DELETE
        FROM music_group_payment_student_course_detail
        WHERE user_id_ = #{userId}
          AND music_group_payment_calender_id_ IN (
            SELECT id_
            FROM music_group_payment_calender
            WHERE music_group_id_ = #{musicGroupId}
        )
    </delete>

    <resultMap id="StudentSubTotalCourseTimesDto" type="com.ym.mec.biz.dal.dto.StudentSubTotalCourseTimesDto">
        <result property="userId" column="user_id_"/>
        <result property="username" column="username_"/>
        <result property="phone" column="phone_"/>
        <result property="classGroupId" column="class_group_id_"/>
        <result property="courseType" column="course_type_"/>
        <collection property="mapDtos" ofType="com.ym.mec.biz.dal.dto.MapDto">
            <result property="key" column="key"/>
            <result property="value" column="value"/>
        </collection>
    </resultMap>

    <select id="queryStudentSubTotalCourseTimes" resultMap="StudentSubTotalCourseTimesDto">
        SELECT mgpscd.user_id_,
               su.username_,
               su.phone_,
               mgpscd.course_type_ 'key', SUM(CASE
                                                  WHEN mgpscd.used_course_minutes_ = 0 THEN mgpscd.total_course_minutes_
                                                  ELSE 0 END) 'value'
        FROM music_group_payment_student_course_detail mgpscd
                 LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpscd.music_group_payment_calender_id_
                 LEFT JOIN sys_user su ON su.id_ = mgpscd.user_id_
        WHERE mgpc.music_group_id_ = #{musicGroupId}
        GROUP BY mgpscd.user_id_, mgpscd.course_type_
    </select>

    <sql id="sqlStudentSubTotalCourseTimes">
        WHERE mgpc.music_group_id_ = #{musicGroupId}
        <if test="search != null and search != ''">
            and (
            mgpscd.user_id_ like CONCAT('%',#{search},'%')
            or su.phone_ like CONCAT('%',#{search},'%')
            or su.username_ like CONCAT('%',#{search},'%')
            )
        </if>
        <if test="hastimer != null and hastimer == true">
            and (mgpscd.used_course_minutes_ is null or mgpscd.used_course_minutes_ = 0)
        </if>
        <if test="hastimer != null and hastimer == false">
            and mgpscd.used_course_minutes_ &gt; 0
        </if>
        <if test="courseType != null and courseType != ''">
            and mgpscd.course_type_ = #{courseType}
        </if>
        <if test="classGroupId != null and classGroupId != ''">
            AND find_in_set(#{classGroupId},v.class_group_id_)
        </if>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="countStudentSubTotalCourseTimes" resultType="java.lang.Integer">
    select count(*) from (
        SELECT mgpscd.user_id_ FROM music_group_payment_student_course_detail mgpscd
        LEFT JOIN sys_user su ON su.id_ = mgpscd.user_id_
        LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpscd.music_group_payment_calender_id_
        left join (
            SELECT
                user_id_ ,GROUP_CONCAT(class_group_id_) as class_group_id_
            FROM class_group_student_mapper
            WHERE music_group_id_ = #{musicGroupId} AND status_ = 'NORMAL'
            GROUP BY user_id_
        ) v on mgpscd.user_id_ = v.user_id_
        <include refid="sqlStudentSubTotalCourseTimes"/>
        GROUP BY mgpscd.user_id_
    ) a
    </select>

    <select id="pageStudentSubTotalCourseTimes" resultMap="StudentSubTotalCourseTimesDto"
            parameterType="map">
            SELECT
                mgpscd.user_id_,su.username_,su.phone_,
                mgpscd.course_type_ 'key',
                SUM(CASE WHEN mgpscd.used_course_minutes_ = 0 THEN mgpscd.total_course_minutes_ ELSE 0 END) 'value'
            FROM music_group_payment_student_course_detail mgpscd
            LEFT JOIN sys_user su ON su.id_ = mgpscd.user_id_
            LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpscd.music_group_payment_calender_id_
            left join (
                SELECT
                    user_id_ ,GROUP_CONCAT(class_group_id_) as class_group_id_
                FROM class_group_student_mapper
                WHERE music_group_id_ = #{musicGroupId} AND status_ = 'NORMAL'
                GROUP BY user_id_
            ) v on mgpscd.user_id_ = v.user_id_
            <include refid="sqlStudentSubTotalCourseTimes"/>
            GROUP BY mgpscd.user_id_,mgpscd.course_type_
    </select>

    <select id="findByCalenderAndUserId" resultMap="MusicGroupPaymentStudentCourseDetail">
        SELECT * FROM music_group_payment_student_course_detail WHERE music_group_payment_calender_id_ = #{calenderId}
        <if test="userId != null">
            AND user_id_ = #{userId}
        </if>
    </select>
    <select id="checkCourseTimes" resultType="integer">
        SELECT SUM(c.a) >= SUM(c.b) FROM (
        SELECT COUNT(DISTINCT mgpscd.course_type_) a,COUNT(DISTINCT mgpscd.total_course_minutes_) b FROM
        music_group_payment_student_course_detail mgpscd
        LEFT JOIN music_group_payment_calender mgpc ON mgpscd.music_group_payment_calender_id_ = mgpc.id_
        WHERE mgpc.music_group_id_ = #{musicGroupId} AND mgpscd.used_course_minutes_ = 0
        AND mgpscd.course_type_ IN
        <foreach collection="courseTypes" item="courseType" open="(" close=")" separator=",">
            #{courseType}
        </foreach>
        AND mgpscd.user_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>)c
    </select>
</mapper>
