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

    <resultMap type="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender" id="MusicGroupPaymentCalender">
        <result column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="tenant_id_" property="tenantId"/>
        <result column="organ_id_" property="organId"/>
        <result column="music_group_organization_course_settings_id_" property="musicGroupOrganizationCourseSettingId"/>
        <result column="pay_user_type_" property="payUserType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="start_payment_date_" property="startPaymentDate"/>
        <result column="deadline_payment_date_" property="deadlinePaymentDate"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="expect_num_" property="expectNum"/>
        <result column="actual_num_" property="actualNum"/>
        <result column="memo_" property="memo"/>
        <result column="audit_memo_" property="auditMemo"/>
        <result column="reject_reason_" property="rejectReason"/>
        <result column="is_give_music_network_" property="isGiveMusicNetwork"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="payment_valid_start_date_" property="paymentValidStartDate"/>
        <result column="payment_valid_end_date_" property="paymentValidEndDate"/>
        <result column="payment_pattern_" property="paymentPattern"/>
        <result column="payment_type_" property="paymentType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="batch_no_" property="batchNo"/>
        <result column="payment_amount_" property="paymentAmount"/>
        <result column="attribute1_" property="attribute1"/>
        <result column="attribute2_" property="attribute2"/>
        <result column="student_ids_" property="studentIds"/>
        <result column="member_payment_amount_" property="memberPaymentAmount"/>
        <result column="original_member_payment_amount_" property="originalMemberPaymentAmount"/>
        <result column="member_rank_setting_id_" property="memberRankSettingId"/>
        <result column="member_valid_date_" property="memberValidDate"/>
        <result column="current_total_amount_" property="currentTotalAmount"/>
        <result column="original_total_amount_" property="originalTotalAmount"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender" useGeneratedKeys="true"
            keyColumn="id_" keyProperty="id">
        INSERT INTO music_group_payment_calender
        (music_group_id_,music_group_organization_course_settings_id_,pay_user_type_,reject_reason_,start_payment_date_,deadline_payment_date_,status_,expect_num_,
        actual_num_,memo_,is_give_music_network_,create_time_,update_time_,payment_valid_start_date_,payment_valid_end_date_,
        payment_pattern_,payment_type_,payment_amount_,batch_no_,audit_memo_,operator_,attribute1_,
         attribute2_,student_ids_,member_payment_amount_,member_rank_setting_id_,member_valid_date_
         ,original_member_payment_amount_,current_total_amount_,original_total_amount_,tenant_id_,organ_id_,cloud_teacher_payment_flag_)
        VALUES(#{musicGroupId},#{musicGroupOrganizationCourseSettingId},#{payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{rejectReason},#{startPaymentDate},#{deadlinePaymentDate},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{expectNum},#{actualNum},#{memo},#{isGiveMusicNetwork},now(),now(),
        #{paymentValidStartDate},#{paymentValidEndDate},#{paymentPattern},#{paymentType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{paymentAmount},#{batchNo},#{auditMemo},#{operator},#{attribute1},#{attribute2},#{studentIds},
               #{memberPaymentAmount},#{memberRankSettingId},#{memberValidDate},#{originalMemberPaymentAmount},
               #{currentTotalAmount},#{originalTotalAmount},#{tenantId},#{organId},#{cloudTeacherPaymentFlag})
    </insert>


    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender">
        UPDATE music_group_payment_calender
        <set>
            <if test="currentTotalAmount != null">
                current_total_amount_ = #{currentTotalAmount},
            </if>
            <if test="originalTotalAmount != null">
                original_total_amount_ = #{originalTotalAmount},
            </if>
            <if test="originalMemberPaymentAmount != null">
                original_member_payment_amount_ = #{originalMemberPaymentAmount},
            </if>
            <if test="memberPaymentAmount != null">
                member_payment_amount_ = #{memberPaymentAmount},
            </if>
            <if test="memberRankSettingId != null">
                member_rank_setting_id_ = #{memberRankSettingId},
            </if>
            <if test="memberValidDate != null">
                member_valid_date_ = #{memberValidDate},
            </if>
            <if test="studentIds != null and studentIds != ''">
                student_ids_ = #{studentIds},
            </if>
            <if test="auditMemo != null and auditMemo != ''">
                audit_memo_ = #{auditMemo},
            </if>
            <if test="paymentType != null">
                payment_type_ = #{paymentType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="paymentValidStartDate != null">
                payment_valid_start_date_ = #{paymentValidStartDate},
                payment_valid_end_date_ = #{paymentValidEndDate},
            </if>
            <if test="paymentPattern != null">
                payment_pattern_ = #{paymentPattern},
            </if>
            <if test="isGiveMusicNetwork != null">
                is_give_music_network_ = #{isGiveMusicNetwork},
            </if>
            <if test="deadlinePaymentDate != null">
                deadline_payment_date_ = #{deadlinePaymentDate},
            </if>
            <if test="status != null">
                status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="expectNum != null">
                expect_num_ = #{expectNum},
            </if>
            <if test="actualNum != null">
                actual_num_ = #{actualNum},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
            <if test="startPaymentDate != null">
                start_payment_date_ = #{startPaymentDate},
            </if>
            <if test="musicGroupOrganizationCourseSettingId != null">
                music_group_organization_course_settings_id_ = #{musicGroupOrganizationCourseSettingId},
            </if>
            <if test="payUserType != null">
                pay_user_type_ = #{payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="rejectReason != null">
                reject_reason_ = #{rejectReason},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="batchNo != null">
                batch_no_ = #{batchNo},
            </if>
            <if test="paymentAmount != null">
                payment_amount_ = #{paymentAmount},
            </if>
            <if test="attribute1 != null">
                attribute1_ = #{attribute1},
            </if>
            <if test="attribute2 != null">
                attribute2_ = #{attribute2},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>
    
    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender">
	    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
	        UPDATE music_group_payment_calender
	        <set>
                <if test="item.currentTotalAmount != null">
                    current_total_amount_ = #{item.currentTotalAmount},
                </if>
                <if test="item.originalTotalAmount != null">
                    original_total_amount_ = #{item.originalTotalAmount},
                </if>
                <if test="item.originalMemberPaymentAmount != null">
                    original_member_payment_amount_ = #{item.originalMemberPaymentAmount},
                </if>
                <if test="item.memberPaymentAmount != null">
                    member_payment_amount_ = #{item.memberPaymentAmount},
                </if>
                <if test="item.memberRankSettingId != null">
                    member_rank_setting_id_ = #{item.memberRankSettingId},
                </if>
                <if test="item.memberValidDate != null">
                    member_valid_date_ = #{item.memberValidDate},
                </if>
                <if test="item.studentIds != null and item.studentIds != ''">
                    student_ids_ = #{item.studentIds},
                </if>
                <if test="item.paymentPattern != null">
                    payment_pattern_ = #{item.paymentPattern},
                </if>
	            <if test="item.isGiveMusicNetwork != null">
	                is_give_music_network_ = #{item.isGiveMusicNetwork},
	            </if>
	            <if test="item.deadlinePaymentDate != null">
	                deadline_payment_date_ = #{item.deadlinePaymentDate},
	            </if>
	            <if test="item.status != null">
	                status_ = #{item.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
	            <if test="item.expectNum != null">
	                expect_num_ = #{item.expectNum},
	            </if>
	            <if test="item.actualNum != null">
	                actual_num_ = #{item.actualNum},
	            </if>
	            <if test="item.memo != null">
	                memo_ = #{item.memo},
	            </if>
	            <if test="item.auditMemo != null">
	                audit_memo_ = #{item.auditMemo},
	            </if>
	            <if test="item.startPaymentDate != null">
	                start_payment_date_ = #{item.startPaymentDate},
	            </if>
	            <if test="item.musicGroupOrganizationCourseSettingId != null">
	                music_group_organization_course_settings_id_ = #{item.musicGroupOrganizationCourseSettingId},
	            </if>
	            <if test="item.payUserType != null">
	                pay_user_type_ = #{item.payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
	            </if>
	            <if test="item.rejectReason != null">
	                reject_reason_ = #{item.rejectReason},
	            </if>
	            <if test="item.updateTime != null">
	                update_time_ = NOW(),
	            </if>
	            <if test="item.musicGroupId != null">
	                music_group_id_ = #{item.musicGroupId},
	            </if>
                <if test="item.batchNo != null">
                    batch_no_ = #{item.batchNo},
                </if>
	            <if test="item.paymentAmount != null">
	                payment_amount_ = #{item.paymentAmount},
	            </if>
	            <if test="item.attribute1 != null">
	                attribute1_ = #{item.attribute1},
	            </if>
	            <if test="item.attribute2 != null">
	                attribute2_ = #{item.attribute2},
	            </if>
	        </set>
	        WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
		</foreach>
    </update>
    <update id="cutCalenderExpectNum">
        UPDATE music_group_payment_calender
        SET expect_num_ = expect_num_ - 1
        WHERE id_ IN (SELECT music_group_payment_calender_id_ FROM music_group_payment_calender_detail
        WHERE user_id_ = #{userId} AND payment_status_ = 'NON_PAYMENT')
        AND expect_num_ > 0 AND music_group_id_ = #{musicGroupId}
    </update>
    <select id="findStudentNoPaymentCalender" resultType="java.lang.Long">
        SELECT DISTINCT mgpc.id_ FROM music_group_payment_calender mgpc
        WHERE mgpc.student_ids_ = #{userId} AND mgpc.payment_type_ = 'ADD_STUDENT'
        AND mgpc.music_group_id_ = #{musicGroupId} AND status_ in ('AUDITING','REJECT')
    </select>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM music_group_payment_calender WHERE id_ = #{id} 
	</delete>
    <!-- 根据主键删除一条记录 -->
    <delete id="delByIds">
		DELETE FROM music_group_payment_calender WHERE id_ IN
        <foreach collection="calenderIds" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
	</delete>

    <delete id="delByGroupId">
        DELETE FROM music_group_payment_calender WHERE music_group_id_ = #{musicGroupId}
    </delete>

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

    <sql id="queryPageSql">
        <where>
        	mgpc.tenant_id_ = #{tenantId}
            <if test="musicGroupId != null">
                AND mgpc.music_group_id_ = #{musicGroupId}
            </if>
            <if test="batchNo != null">
                AND mgpc.batch_no_ = #{batchNo}
            </if>
            <if test="payUserType != null">
                AND mgpc.pay_user_type_ = #{payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="paymentType != null">
                AND mgpc.payment_type_ = #{paymentType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                AND mgpc.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(mgpc.id_) FROM music_group_payment_calender mgpc
		<include refid="queryPageSql"/>
	</select>
	
    <select id="findByMusicGroupId" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender mgpc
        WHERE music_group_id_ = #{musicGroupId}
        ORDER BY id_
    </select>
	
    <select id="queryByPaymentStatus" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender mgpc
        <where>
        	<if test="status != null">
        		status_ in
        		<foreach collection="status" item="item" open="(" close=")" separator=",">
		            #{item,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		        </foreach>
        	</if>
        	<if test="payUserType != null">
                AND pay_user_type_ = #{payUserType}
        	</if>
        	<if test="musicGroupId != null">
                AND music_group_id_ = #{musicGroupId}
        	</if>
        </where>
    </select>

    <select id="getForLock" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE id_ = #{id} FOR UPDATE
    </select>
	
    <select id="queryIntersectionByPaymentDate" resultType="int" parameterType="map">
        SELECT count(*) FROM music_group_payment_calender mgpc
        WHERE music_group_id_ = #{musicGroupId} 
        and (start_payment_date_ between #{startPaymentDate} and #{deadlinePaymentDate}
        or deadline_payment_date_ between #{startPaymentDate} and #{deadlinePaymentDate})
        <if test="calenderId != null">
            AND mgpc.id_ != #{calenderId}
        </if>
    </select>
    
    <select id="getLastStartOne" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE music_group_id_ = #{musicGroupId} ORDER BY start_payment_date_ DESC LIMIT 1
    </select>
    <select id="queryIntersectionByDate" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT mgpcd.id_) FROM music_group_payment_calender mgpc
        LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
        WHERE mgpc.music_group_id_ = #{musicGroupId} AND mgpcd.payment_status_ != 'PAID_COMPLETED'
        AND (DATE_FORMAT(mgpcd.start_payment_date_,'%Y-%m-%d') between #{startTime} AND #{endTime}
        OR DATE_FORMAT(mgpcd.deadline_payment_date_,'%Y-%m-%d') between #{startTime} AND #{endTime})
    </select>
    <select id="queryCalenderByDay" resultType="java.util.Map">
        SELECT mgpcd.music_group_payment_calender_id_ 'key',COUNT(mgpcd.payment_status_) 'value'
        FROM music_group_payment_calender_detail mgpcd
        WHERE mgpcd.music_group_payment_calender_id_ IN
        (SELECT id_ FROM music_group_payment_calender WHERE status_ != 0
        AND DATEDIFF(#{format},DATE_FORMAT(start_payment_date_,'%Y-%m-%d')) = #{configValue})
        AND mgpcd.payment_status_ = 'NON_PAYMENT'
        GROUP BY mgpcd.music_group_payment_calender_id_
    </select>
    <select id="queryByIds" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE id_ IN
        <foreach collection="calenderId" open="(" close=")" item="item" separator=",">
            #{item}
        </foreach>
    </select>
    <select id="queryEndIds" resultType="java.lang.Long">
        SELECT DISTINCT mgpc.id_ FROM music_group_payment_calender mgpc
        WHERE mgpc.payment_valid_end_date_ IS NOT NULL AND DATEDIFF(mgpc.payment_valid_end_date_,#{format}) = #{configValue}
        AND mgpc.payment_type_ IN ('MUSIC_APPLY','MUSIC_RENEW')
        <if test="musicGroupIds != null">
            AND mgpc.music_group_id_ IN
            <foreach collection="musicGroupIds" open="(" close=")" item="item" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
    <resultMap id="CalenderPushDto" type="com.ym.mec.biz.dal.dto.CalenderPushDto">
        <result property="paymentValidEndDate" column="payment_valid_end_date_"/>
        <result property="paymentValidStartDate" column="payment_valid_start_date_"/>
        <result property="teacherId" column="educational_teacher_id_"/>
        <result property="musicGroupName" column="name_"/>
        <result property="teacherPhone" column="phone_"/>
        <result property="calenderId" column="id_"/>
    </resultMap>
    <select id="getCalenderPushDto" resultMap="CalenderPushDto">
        SELECT mgpc.id_,mgpc.payment_valid_start_date_,mgpc.payment_valid_end_date_,
        mg.educational_teacher_id_,mg.name_,su.phone_
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mgpc.music_group_id_ = mg.id_
        LEFT JOIN sys_user su ON su.id_ = mg.educational_teacher_id_
        WHERE mgpc.id_ IN
        <foreach collection="calenderIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>
    <select id="getMusicCalenderPushDto" resultMap="CalenderPushDto">
        SELECT mg.payment_valid_start_date_,mg.payment_valid_end_date_,
        mg.educational_teacher_id_,su.phone_,mg.name_ FROM music_group mg
        LEFT JOIN sys_user su ON mg.educational_teacher_id_ = su.id_
        WHERE mg.payment_valid_end_date_ IS NOT NULL AND
        DATEDIFF(mg.payment_valid_end_date_,#{format}) = #{configValue}
    </select>
    <select id="getUnusedFirstPaymentCalenderWithCalenders" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender mgpc
        WHERE id_ IN
        <foreach collection="calenderIds" item="calenderId" open="(" close=")" separator=",">
            #{calenderId}
        </foreach>
        ORDER BY create_time_ LIMIT 1;
    </select>

	<select id="findByBatchNo" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE FIND_IN_SET(batch_no_,#{batchNo})
    </select>

    <resultMap id="MusicGroupPaymentCalenderAuditDtoMap" type="com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderAuditDto">
        <result property="musicGroupId" column="music_group_id_"/>
        <result column="music_group_organization_course_settings_id_" property="musicGroupOrganizationCourseSettingsId" />
        <result property="batchNo" column="batch_no_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="organId" column="organ_id_"/>
        <result property="organName" column="organ_name_"/>
        <result property="createTime" column="create_time_"/>
        <result property="paymentType" column="payment_type_"/>
        <result property="memo" column="memo_"/>
        <result property="operator" column="operator_"/>
        <result property="auditStatus" column="status_"/>
        <result property="addCourseTotalTime" column="course_total_minuties_"/>
        <result property="courseCurrentPrice" column="course_current_price_"/>
        <result property="courseOriginalPrice" column="course_original_price_"/>
        <result property="currentTotalAMount" column="current_total_amount_"/>
        <result property="originalTotalAMount" column="original_total_amount_"/>
        <result property="musicGroupPaymentCalenderId" column="id_"/>
        <result property="payUserType" column="pay_user_type_"/>
        <result property="calenderSettingsName" column="calender_settings_name_"/>
        <result property="paymentPattern" column="payment_pattern_"/>
        <result property="paymentValidStartDate" column="payment_valid_start_date_"/>
        <result property="paymentValidEndDate" column="payment_valid_end_date_"/>
        <result property="startPaymentDate" column="start_payment_date_"/>
        <result property="deadlinePaymentDate" column="deadline_payment_date_"/>
        <result property="auditMemo" column="audit_memo_"/>
        <result property="paymentAmount" column="payment_amount_"/>
        <result property="isGiveMusicNetwork" column="is_give_music_network_"/>
        <result property="memberPaymentAmount" column="member_payment_amount_"/>
        <result property="originalMemberPaymentAmount" column="original_member_payment_amount_"/>
    </resultMap>
    <sql id="MusicGroupPaymentCalenderAuditDtoSql">
        <where>
                mgpc.batch_no_ IS NOT NULL and mgpc.tenant_id_ = #{tenantId}
            <if test="paymentType != null">
                AND mgpc.payment_type_ = #{paymentType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="payUserType != null">
                AND mgpc.pay_user_type_ = #{payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                AND mgpc.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(mg.organ_id_,#{organId})
            </if>
            <if test="search != null and search != ''">
                AND (mg.id_ LIKE CONCAT('%',#{search},'%') OR mg.name_ LIKE CONCAT('%',#{search},'%'))
            </if>
        </where>
    </sql>
    <select id="countAuditList" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT mgpc.batch_no_)
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        <include refid="MusicGroupPaymentCalenderAuditDtoSql"/>
    </select>
    <select id="queryAuditList" resultMap="MusicGroupPaymentCalenderAuditDtoMap">
        SELECT mgpc.batch_no_,MAX(mgpc.music_group_id_) music_group_id_
        ,MAX(mgpc.create_time_) create_time_,MIN(mgpc.payment_type_) payment_type_,MAX(mgpc.operator_) operator_,
        MAX(mgpc.pay_user_type_) pay_user_type_,MAX(mgpc.memo_) memo_,MAX(mgpc.audit_memo_) audit_memo_,MAX(mgpc.status_) status_,
        CASE WHEN MAX(mgpc.payment_type_) = 'SPAN_GROUP_CLASS_ADJUST' THEN MAX(mgsca.master_class_course_times_) ELSE SUM(mgpccs.course_total_minuties_) END course_total_minuties_,
        CASE WHEN MAX(mgpc.payment_type_) = 'SPAN_GROUP_CLASS_ADJUST' THEN MAX(mgpc.payment_amount_) ELSE SUM(mgpccs.course_original_price_) END course_original_price_,
        SUM(mgpccs.course_current_price_) course_current_price_,
        mgpc.member_payment_amount_,
        mgpc.original_member_payment_amount_,mgpc.current_total_amount_,mgpc.original_total_amount_,
        MAX(mg.name_) music_group_name_,MAX(mg.organ_id_) organ_id_,MAX(mgpc.payment_pattern_)payment_pattern_
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        LEFT JOIN music_group_payment_calender_course_settings mgpccs ON mgpc.id_ = mgpccs.music_group_payment_calender_id_
        LEFT JOIN music_group_student_class_adjust mgsca ON  mgsca.batch_no_ = mgpc.batch_no_
        <include refid="MusicGroupPaymentCalenderAuditDtoSql"/>
        GROUP BY mgpc.batch_no_
        ORDER BY create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countExpectNum" resultType="java.util.Map">
        SELECT mgpcd.music_group_payment_calender_id_ 'key',COUNT(DISTINCT mgpcd.user_id_) 'value' FROM music_group_payment_calender_detail mgpcd
        WHERE mgpcd.music_group_payment_calender_id_ = #{id}
        GROUP BY mgpcd.music_group_payment_calender_id_
    </select>
    <select id="countActualNum" resultType="java.util.Map">
        SELECT mgpcd.music_group_payment_calender_id_ 'key',COUNT(DISTINCT mgpcd.user_id_) 'value' FROM music_group_payment_calender_detail mgpcd
        WHERE mgpcd.music_group_payment_calender_id_ = #{id}
        AND mgpcd.payment_status_ = 'PAID_COMPLETED'
        GROUP BY mgpcd.music_group_payment_calender_id_
    </select>
    <select id="getAuditDetail" resultMap="MusicGroupPaymentCalenderAuditDtoMap">
        SELECT mgpc.batch_no_,MAX(mgpc.music_group_id_) music_group_id_
        ,MAX(mgpc.create_time_) create_time_,MAX(mgpc.payment_type_) payment_type_,MAX(mgpc.music_group_organization_course_settings_id_) music_group_organization_course_settings_id_,
        MAX(mgpc.pay_user_type_) pay_user_type_,MAX(mg.name_) music_group_name_,MAX(mgpc.payment_pattern_)payment_pattern_,MAX(o.name_) organ_name_
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        LEFT JOIN organization o ON o.id_ = mg.organ_id_
        WHERE mgpc.batch_no_ = #{batchNo}
        GROUP BY mgpc.batch_no_
    </select>
    <select id="findByMusicGroupRegCalender" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE music_group_id_=#{musicGroupId} AND payment_type_='MUSIC_APPLY'
    </select>
    <select id="getCalenderSettingsName" resultType="java.lang.String">
        SELECT name_ FROM music_group_payment_calender_course_settings
        WHERE music_group_payment_calender_id_ = #{calenderId} LIMIT 1
    </select>
    <select id="countAuditReject" resultType="java.lang.Integer">
        SELECT COUNT(0) FROM music_group_payment_calender
        WHERE music_group_id_ = #{musicGroupId} AND payment_type_='MUSIC_APPLY' AND status_ IN ('REJECT','AUDITING')
        <if test="calenderId != null">
            AND id_ != #{calenderId}
        </if>
    </select>

    <select id="getMusicGroupRegCalender" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender
        WHERE music_group_id_ = #{musicGroupId}
        AND payment_type_ = 'MUSIC_APPLY'
        AND pay_user_type_='STUDENT'
    </select>
    <select id="queryUserPaymentStatus" resultType="java.util.Map">
        SELECT mg.id_ 'key',CASE WHEN MIN(mgpcd.payment_status_) IS NULL THEN 'PAID_COMPLETED' ELSE MIN(mgpcd.payment_status_) END 'value'
        FROM music_group mg
        LEFT JOIN music_group_payment_calender mgpc ON mgpc.music_group_id_ = mg.id_
        AND mgpc.status_ IN ('NO','OPEN','OVER')
        LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_ AND mgpcd.user_id_ = #{userId}
        WHERE FIND_IN_SET(mg.id_,#{musicGroupId}) AND mgpc.batch_no_ IS NOT NULL AND mgpc.pay_user_type_ = 'STUDENT'
        GROUP BY mg.id_
    </select>
    <select id="queryUserCoursePaymentStatus" resultType="java.util.Map">
        SELECT mgpcd.user_id_ 'key',
        CASE WHEN MIN(mgpcd.payment_status_) IS NULL THEN 'PAID_COMPLETED' ELSE MIN(mgpcd.payment_status_) END 'value'
        FROM music_group mg
        LEFT JOIN music_group_payment_calender mgpc ON mgpc.music_group_id_ = mg.id_
        AND mgpc.status_ IN ('NO','OPEN','OVER')
        LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
        WHERE mg.id_ = #{musicGroupId} AND mgpcd.expect_amount_ > 0 AND mgpc.batch_no_ IS NOT NULL AND mgpc.pay_user_type_ = 'STUDENT' AND mgpcd.user_id_ IN
        <foreach collection="studentId" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
        GROUP BY mgpcd.user_id_
    </select>
    <select id="findByBatchNoAndStudentId" resultType="Long">
        SELECT DISTINCT mgpc.id_ FROM music_group_payment_calender mgpc
        LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
        WHERE FIND_IN_SET(batch_no_,#{batchNo}) AND mgpcd.user_id_ = #{studentId}
    </select>

    <select id="getPaymentCalenderWithCalenderIdsAndPayUserType" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender
        WHERE id_ IN
        <foreach collection="calenderIds" item="calenderId" open="(" close=")" separator=",">
            #{calenderId}
        </foreach>
        AND pay_user_type_ = #{payUserType}
        AND status_ IN ('NO', 'OPEN', 'OVER', 'PAID');
    </select>
    <select id="getSchoolCalender" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE id_ = #{calenderId} AND status_ IN ('NO', 'OPEN', 'OVER', 'PAID');
    </select>
    <select id="queryMusicGroupIds" resultType="java.lang.String">
        SELECT DISTINCT music_group_id_ FROM music_group_payment_calender
        WHERE id_ IN
        <foreach collection="calenderIds" item="calenderId" open="(" close=")" separator=",">
            #{calenderId}
        </foreach>
    </select>

    <select id="getMusicGroupCalenderBatchNoTimes" resultMap="MusicGroupPaymentCalender">
        SELECT batch_no_, COUNT(*) expect_num_ FROM music_group_payment_calender
        WHERE music_group_id_ = #{musicGroupId}
        AND status_ IN ('AUDITING','NO', 'OPEN', 'OVER', 'PAID')
        GROUP BY batch_no_
    </select>

    <select id="getMusicGroupRegCalenderByIds" resultMap="MusicGroupPaymentCalender">
        SELECT * FROM music_group_payment_calender WHERE music_group_id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND payment_type_='MUSIC_APPLY' AND batch_no_ IS NOT NULL
    </select>

    <select id="getMusicGroupCalenderBatchNoByIds" resultMap="MusicGroupPaymentCalender">
        SELECT music_group_id_,batch_no_, COUNT(*) expect_num_ FROM music_group_payment_calender
        WHERE music_group_id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND status_ IN ('AUDITING','NO', 'OPEN', 'OVER', 'PAID')
        AND batch_no_ IS NOT NULL
        GROUP BY music_group_id_,batch_no_
    </select>
    <select id="queryCalenderStudentIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(DISTINCT student_ids_) FROM music_group_payment_calender
        WHERE status_ IN ('AUDITING','REJECT') AND music_group_id_ = #{musicGroupId}
        <if test="batchNo != null and batchNo != ''">
            AND batch_no_ != #{batchNo}
        </if>
    </select>
    <sql id="queryCalenderPageSql">
        <where>
            <if test="search != null and search != ''">
                AND (mgpc.music_group_id_ = #{search} OR mg.name_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="musicGroupId != null">
                AND mgpc.music_group_id_ = #{musicGroupId}
            </if>
            <if test="batchNo != null">
                AND mgpc.batch_no_ = #{batchNo}
            </if>
            <if test="payUserType != null">
                AND mgpc.pay_user_type_ = #{payUserType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="paymentType != null">
                AND mgpc.payment_type_ = #{paymentType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="status != null">
                AND mgpc.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(mgpc.organ_id_,#{organId})
            </if>
            <if test="tenantId != null">
                AND mgpc.tenant_id_ = #{tenantId}
            </if>
        </where>
    </sql>
    <select id="countCalender" resultType="java.lang.Integer">
        SELECT COUNT(mgpc.id_) FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        <include refid="queryCalenderPageSql"/>
    </select>

    <resultMap id="MusicGroupPaymentCalenderResultDto" type="com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderResultDto"
               extends="MusicGroupPaymentCalender">
        <result column="music_group_name_" property="musicGroupName"/>
        <result column="operator_name_" property="operatorName"/>
    </resultMap>
    <select id="queryCalenderPage" resultMap="MusicGroupPaymentCalenderResultDto">
        SELECT mgpc.id_,mgpc.music_group_id_,mgpc.music_group_organization_course_settings_id_,mgpc.pay_user_type_,mgpc.reject_reason_,
        mgpc.start_payment_date_,mgpc.deadline_payment_date_,mgpc.status_,mgpc.expect_num_,
        mgpc.actual_num_,mgpc.memo_,mgpc.create_time_,mgpc.update_time_,
        mgpc.payment_pattern_,mgpc.payment_type_,mgpc.batch_no_,mgpc.audit_memo_,mgpc.operator_,mgpc.attribute1_,
        mgpc.attribute2_,mgpc.student_ids_,mgpc.current_total_amount_,mgpc.original_total_amount_,mg.name_ music_group_name_,mgpc.organ_id_
        FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        <include refid="queryCalenderPageSql"/>
        ORDER BY mgpc.id_ DESC
        <include refid="global.limit"/>
    </select>
    
    <select id="queryChargeStandard" resultType="java.util.Map">
        SELECT `music_group_id_` 'key',`current_total_amount_` 'value'  FROM `music_group_payment_calender` WHERE `id_` in
		(
		SELECT max(pc.`id_`)   FROM `music_group_payment_calender` pc WHERE pc.`payment_type_` in ('MUSIC_APPLY','MUSIC_RENEW') and FIND_IN_SET(pc.music_group_id_, #{musicGroupIdList})
		GROUP BY pc.`music_group_id_` 
		    )
    </select>
    <select id="findCooperationByIds" resultType="java.util.Map">
        SELECT mgpc.id_ 'key',mg.cooperation_organ_id_ 'value' FROM music_group_payment_calender mgpc
        LEFT JOIN music_group mg ON mg.id_ = mgpc.music_group_id_
        WHERE mgpc.id_ IN
        <foreach collection="calenderIds" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
</mapper>
