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

	<resultMap type="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderDetail" id="MusicGroupPaymentCalenderDetail">
		<result column="id_" property="id" />
        <result column="tenant_id_" property="tenantId"/>
		<result column="music_group_payment_calender_id_" property="musicGroupPaymentCalenderId" />
		<result column="user_id_" property="userId" />
		<result column="expect_amount_" property="expectAmount" />
		<result column="expect_member_amount_" property="expectMemberAmount" />
		<result column="actual_amount_" property="actualAmount" />
		<result column="payment_status_" property="paymentStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="payment_type_" property="paymentType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="user_status_" property="userStatus" />
		<result column="music_group_id_" property="musicGroupId" />
		<result column="pay_time_" property="payTime" />
		<result column="start_payment_date_" property="startPaymentDate" />
		<result column="deadline_payment_date_" property="deadlinePaymentDate" />
		<result column="start_payment_date_mgpc_" property="startPaymentDateMgpc" />
		<result column="deadline_payment_date_mgpc_" property="deadlinePaymentDateMgpc" />
		<result column="payment_order_id_" property="paymentOrderId"/>
		<result column="update_time_" property="updateTime" />
		<result column="create_time_" property="createTime" />
		<result column="open_" property="open"/>
		<result column="open_" property="openFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="use_in_course_" property="useInCourse"/>
		<result column="responsible_user_id_" property="responsibleUserId"/>
		<result column="responsible_record_" property="responsibleRecord"/>
		<result column="responsible_username_" property="responsible.username"/>
		<association property="sysUser" javaType="com.ym.mec.auth.api.entity.SysUser">
			<result column="username_" property="username" />
			<result column="phone_" property="phone" />
		</association>
		<association property="studentRegistration" javaType="com.ym.mec.biz.dal.entity.StudentRegistration">
			<result column="subject_names_" property="subjectName" />
			<result column="music_group_status_" property="musicGroupStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		</association>
	</resultMap>

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

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

	<!-- 向数据库增加一条记录 -->
	<insert id="insert"
		parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderDetail"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO music_group_payment_calender_detail
		(music_group_payment_calender_id_,user_id_,expect_amount_,actual_amount_,payment_status_,
		user_status_,pay_time_,update_time_,create_time_,start_payment_date_,deadline_payment_date_,
		payment_order_id_,use_in_course_,responsible_user_id_,responsible_record_,expect_member_amount_,tenant_id_)
		VALUES(#{musicGroupPaymentCalenderId},#{userId},#{expectAmount},#{actualAmount},
		       #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{userStatus},#{payTime},NOW(),NOW(),#{startPaymentDate},#{deadlinePaymentDate},
		       #{paymentOrderId},#{useInCourse},#{responsibleUserId},#{responsibleRecord},#{expectMemberAmount},#{tenantId})
	</insert>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO music_group_payment_calender_detail
		(music_group_payment_calender_id_,user_id_,expect_amount_,actual_amount_,
		payment_status_,user_status_,pay_time_,update_time_,create_time_,start_payment_date_,
		 deadline_payment_date_,payment_order_id_,use_in_course_,responsible_user_id_,responsible_record_,expect_member_amount_,tenant_id_)
		VALUES
		<foreach collection="list" item="item" separator=",">
            (#{item.musicGroupPaymentCalenderId},#{item.userId},#{item.expectAmount},
            #{item.actualAmount},#{item.paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{item.userStatus},#{item.payTime},
            #{item.updateTime},#{item.createTime},#{item.startPaymentDate},#{item.deadlinePaymentDate},
             #{item.paymentOrderId},#{item.useInCourse},#{item.responsibleUserId},#{item.responsibleRecord},#{item.expectMemberAmount},#{item.tenantId})
        </foreach>
    </insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update"
		parameterType="com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderDetail">
		UPDATE music_group_payment_calender_detail
		<set>
			<if test="startPaymentDate != null">
				start_payment_date_ = #{startPaymentDate},
			</if>
			<if test="deadlinePaymentDate != null">
				deadline_payment_date_ = #{deadlinePaymentDate},
			</if>
			<if test="open != null">
				open_ = #{open},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="userStatus != null">
				user_status_ = #{userStatus},
			</if>
			<if test="paymentStatus != null">
				payment_status_ = #{paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="expectMemberAmount != null">
				expect_member_amount_ = #{expectMemberAmount},
			</if>
			<if test="expectAmount != null">
				expect_amount_ = #{expectAmount},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="musicGroupPaymentCalenderId != null">
				music_group_payment_calender_id_ = #{musicGroupPaymentCalenderId},
			</if>
			<if test="actualAmount != null">
				actual_amount_ = #{actualAmount},
			</if>
			<if test="payTime != null">
				pay_time_ = #{payTime},
			</if>
			<if test="paymentOrderId != null">
				payment_order_id_ = #{paymentOrderId},
			</if>
			<if test="useInCourse != null">
				use_in_course_ = #{useInCourse},
			</if>
			<if test="responsibleUserId != null">
				responsible_user_id_ = #{responsibleUserId},
			</if>
			<if test="responsibleRecord != null">
				responsible_record_ = #{responsibleRecord},
			</if>
		</set>
		WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>
	<update id="batchUpdate">
		<foreach collection="calenderDetails" item="item" index="index" open="" close="" separator=";">
			UPDATE music_group_payment_calender_detail
			<set>
				<if test="item.startPaymentDate != null">
					start_payment_date_ = #{item.startPaymentDate},
				</if>
				<if test="item.deadlinePaymentDate != null">
					deadline_payment_date_ = #{item.deadlinePaymentDate},
				</if>
				<if test="item.open != null">
					open_ = #{item.open},
				</if>
				<if test="item.userId != null">
					user_id_ = #{item.userId},
				</if>
				<if test="item.userStatus != null">
					user_status_ = #{item.userStatus},
				</if>
				<if test="item.paymentStatus != null">
					payment_status_ = #{item.paymentStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="item.expectMemberAmount != null">
					expect_member_amount_ = #{item.expectMemberAmount},
				</if>
				<if test="item.expectAmount != null">
					expect_amount_ = #{item.expectAmount},
				</if>
				<if test="item.updateTime != null">
					update_time_ = #{item.updateTime},
				</if>
				<if test="item.musicGroupPaymentCalenderId != null">
					music_group_payment_calender_id_ = #{item.musicGroupPaymentCalenderId},
				</if>
				<if test="item.actualAmount != null">
					actual_amount_ = #{item.actualAmount},
				</if>
				<if test="item.payTime != null">
					pay_time_ = #{item.payTime},
				</if>
				<if test="item.paymentOrderId != null">
					payment_order_id_ = #{item.paymentOrderId},
				</if>
				<if test="item.useInCourse != null">
					use_in_course_ = #{item.useInCourse},
				</if>
				<if test="item.responsibleUserId != null">
					responsible_user_id_ = #{item.responsibleUserId},
				</if>
				<if test="item.responsibleRecord != null">
					responsible_record_ = #{item.responsibleRecord},
				</if>
			</set>
			WHERE id_ = #{item.id} and tenant_id_ = #{item.tenantId}
		</foreach>
	</update>
	<update id="refreshUserMusicGroupPaymentStatusTask">
		UPDATE music_group_payment_calender_detail mgpcd
		SET mgpcd.open_ = 0,mgpcd.start_payment_date_ = NULL,mgpcd.deadline_payment_date_ = NULL
		WHERE mgpcd.deadline_payment_date_ &lt; DATE_FORMAT(NOW(),'%Y-%m-%d')
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM music_group_payment_calender_detail WHERE id_ = #{id}
	</delete>
	<delete id="batchDel">
		DELETE FROM music_group_payment_calender_detail WHERE FIND_IN_SET(id_,#{musicGroupPaymentCalenderIdDetails})
	</delete>
    <delete id="deleteByCalenderId">
		DELETE FROM music_group_payment_calender_detail WHERE music_group_payment_calender_id_ = #{id}
	</delete>
	
	<select id="queryNotPaymentStudentByUserIdAndMusicGroupId" resultMap="MusicGroupPaymentCalenderDetail" parameterType="map">
		select * FROM music_group_payment_calender_detail
		WHERE music_group_payment_calender_id_ IN (SELECT mgpc.id_ FROM music_group_payment_calender mgpc
		WHERE mgpc.music_group_id_ = #{musicGroupId})
		AND user_id_ = #{userId} AND payment_status_ = 'NON_PAYMENT'
	</select>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="MusicGroupPaymentCalenderDetail" parameterType="map">
		SELECT mgpcd.user_id_,su.username_,st.name_ subject_names_,sr.music_group_status_,su.phone_,
		CASE WHEN mgpcd.start_payment_date_ IS NULL THEN mgpc.start_payment_date_ ELSE mgpcd.start_payment_date_ END start_payment_date_mgpc_,
		CASE WHEN mgpcd.deadline_payment_date_ IS NULL THEN mgpc.deadline_payment_date_ ELSE mgpcd.deadline_payment_date_ END deadline_payment_date_mgpc_,
		CASE WHEN mgpc.status_ = 'OPEN' OR mgpcd.open_ = 1 THEN 1 ELSE 0 END open_,mgpc.payment_type_
		FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_
		LEFT JOIN `subject` st ON st.id_ = sr.actual_subject_id_
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		<include refid="queryPageSql"/>
		ORDER BY mgpc.update_time_ DESC
		<include refid="global.limit" />
	</select>
	<sql id="queryPageSql">
		<where>
			mgpc.music_group_id_ = sr.music_group_id_ AND mgpcd.tenant_id_ = #{tenantId}
			<if test="search != null and search != ''">
				AND (mgpcd.user_id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="id != null">
				AND mgpcd.music_group_payment_calender_id_ = #{id}
			</if>
			<if test="paymentStatus != null and paymentStatus != ''">
				AND mgpcd.payment_status_ = #{paymentStatus}
			</if>
			<if test="musicGroupId != null and musicGroupId != ''">
				AND mgpc.music_group_id_ = #{musicGroupId}
			</if>
			<if test="userId != null">
				AND mgpcd.user_id_ = #{userId}
			</if>
			<if test="musicGroupStatus != null and musicGroupStatus != ''">
				AND sr.music_group_status_ = #{musicGroupStatus}
			</if>
			<if test="subjectId != null">
				AND #{subjectId} = sr.actual_subject_id_
			</if>
<!--			<if test="responsibleRealName != null and responsibleRealName != ''">-->
<!--				AND (mgpcd.responsible_user_id_ = #{responsibleRealName} OR tu.real_name_ LIKE CONCAT('%',#{responsibleRealName},'%') OR tu.phone_ LIKE CONCAT('%',#{responsibleRealName},'%'))-->
<!--			</if>-->
		</where>
	</sql>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT mgpcd.id_) FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_
		LEFT JOIN `subject` st ON st.id_ = sr.actual_subject_id_
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		<include refid="queryPageSql"/>
	</select>
	<select id="queryListByIds" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT mgpcd.*,mgpc.music_group_id_ FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		WHERE FIND_IN_SET(mgpcd.id_,#{ids})
		GROUP BY mgpcd.id_
	</select>
	
	<select id="queryByCalenderId" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT * FROM music_group_payment_calender_detail WHERE music_group_payment_calender_id_ = #{calenderId}
		<if test="paymentStatus != null">
			AND payment_status_ = #{paymentStatus}
		</if>
	</select>
    <select id="findMusicGroupStudentWithSubject" resultType="com.ym.mec.biz.dal.dto.SimpleUserDto">
		SELECT DISTINCT su.id_ userId,su.username_ userName, su.avatar_ avatar, su.phone_ phone
		from music_group_student_fee_ mgstf
		LEFT JOIN sys_user su ON su.id_=mgstf.user_id_
		WHERE mgstf.music_group_id_=#{musicGroupId}
		AND mgstf.subject_id_=#{subjectId}
		AND mgstf.remain_network_class_times_>0
		ORDER BY su.id_
	</select>

	<select id="getUserLastCalenderDetail" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT * FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
		WHERE mgpc.music_group_id_=#{musicGroupId} AND mgpcd.user_id_=#{userId} AND mgpcd.payment_status_ = 'NON_PAYMENT'
		AND (mgpc.status_ = 'OPEN' OR mgpcd.open_ = 1)
		ORDER BY mgpc.start_payment_date_ DESC LIMIT 1
	</select>
	<select id="queryCanOpenList" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT * FROM music_group_payment_calender_detail
		WHERE FIND_IN_SET(id_,#{ids}) AND payment_status_ = 'NON_PAYMENT' AND open_ = 0
	</select>
	<select id="queryIntersectionByPaymentDate" resultType="java.lang.Integer">
		SELECT COUNT(mgpcd.id_) FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
		WHERE mgpc.music_group_id_ = #{musicGroupId} AND mgpcd.user_id_ = #{userId}
		AND  mgpcd.open_ = 1
	</select>
	<select id="queryCanPushList" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT mgpcd.*,mgpc.music_group_id_ FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		LEFT JOIN student_registration sr ON sr.music_group_id_ = mgpc.music_group_id_
		WHERE mgpcd.payment_status_ = 'NON_PAYMENT' AND sr.music_group_status_ != 'QUIT' AND mgpc.id_ = #{musicGroupPaymentCalenderId}
		GROUP BY mgpcd.id_
	</select>
	<select id="queryNoPaymentCanPushList" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT mgpcd.*,mgpc.music_group_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 DATE_FORMAT(mgpc.create_time_,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND mgpc.status_ = 'OPEN' AND mgpcd.payment_status_ = 'NON_PAYMENT'
	</select>
	<select id="queryPaymentPushMap" resultType="java.util.Map">
		SELECT mgpcd.user_id_ 'key',mgpc.music_group_id_ 'value' FROM music_group_payment_calender mgpc
		LEFT JOIN music_group_payment_calender_detail mgpcd ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		WHERE DATE_FORMAT(mgpc.create_time_,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND mgpc.status_ = 'OPEN' AND mgpcd.payment_status_ = 'NON_PAYMENT'
	</select>
	<select id="queryStudentIds" resultType="java.lang.Integer">
		SELECT user_id_ FROM music_group_payment_calender_detail WHERE music_group_payment_calender_id_ = #{calenderId}
	</select>
    <select id="countOpenPayment" 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 mgpcd.user_id_ = #{userId} AND mgpc.music_group_id_ = #{musicGroupId} AND (mgpc.status_ = 'OPEN' OR mgpcd.open_ = 1)
		AND mgpcd.payment_status_ != 'PAID_COMPLETED'
	</select>

	<select id="getCalenderDetailWithCalender" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT
			mgpcd.*
		FROM
			music_group_payment_calender_detail mgpcd
		WHERE
			mgpcd.music_group_payment_calender_id_ = #{calenderId}
	</select>

    <select id="getWithCalenderIds" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT
			mgpcd.*
		FROM
			music_group_payment_calender_detail mgpcd
		WHERE
			mgpcd.music_group_payment_calender_id_ IN
			<foreach collection="calenderIds" item="calenderId" open="(" close=")" separator=",">
				#{calenderId}
			</foreach>
	</select>
	<resultMap id="SimpleUserDtoMap" type="com.ym.mec.biz.dal.dto.SimpleUserDto">
		<result property="userName" column="username_"/>
		<result property="userId" column="id_"/>
		<result property="subjectNames" column="subject_name_"/>
		<result property="actualAmount" column="actual_amount_"/>
	</resultMap>
    <select id="querySimpleUserDto" resultMap="SimpleUserDtoMap">
		SELECT su.id_,su.username_,s.name_ subject_name_,mgpcd.actual_amount_
		FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_ AND sr.music_group_status_ != 'QUIT' AND sr.music_group_id_ = mgpc.music_group_id_
		LEFT JOIN `subject` s ON s.id_ = sr.subject_id_
		WHERE mgpc.batch_no_ = #{batchNo}
	</select>
	<select id="sumActualAmount" resultType="java.math.BigDecimal">
		SELECT SUM(spo.actual_amount_) FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN student_payment_order spo ON spo.id_ = mgpcd.payment_order_id_
		WHERE mgpcd.music_group_payment_calender_id_ = #{id} AND spo.status_ = 'SUCCESS'
	</select>
	<select id="findByCalenderIdAndUserId" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT * FROM music_group_payment_calender_detail WHERE music_group_payment_calender_id_ = #{calenderId} AND user_id_ = #{userId} LIMIT 1
	</select>
	<select id="findByOrderId" resultMap="MusicGroupPaymentCalenderDetail">
		SELECT * FROM music_group_payment_calender_detail WHERE payment_order_id_ = #{orderId} LIMIT 1
	</select>
	<select id="queryNoPaymentStudentIds" resultType="java.lang.Integer">
		SELECT user_id_ FROM music_group_payment_calender_detail WHERE music_group_payment_calender_id_ = #{calenderId} AND payment_status_ = 'NON_PAYMENT'
	</select>

	<select id="queryNoPaymentAndNotZeroStudent" resultType="java.util.Map">
		SELECT mgpcd.user_id_ 'key',su.phone_ 'value' FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		WHERE music_group_payment_calender_id_ = #{calenderId} AND (expect_amount_ + expect_member_amount_) != 0 AND payment_status_ = 'NON_PAYMENT'
		<if test="studentIds != null">
			AND mgpcd.user_id_ IN
			<foreach collection="studentIds" separator="," close=")" open="(" item="item">
				#{item}
			</foreach>
		</if>
    </select>
	<select id="queryNoPaymentTotalAmount" resultType="java.util.Map">
		SELECT mgpcd.user_id_ 'key',SUM(mgpc.current_total_amount_) 'value' FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_ AND sr.music_group_id_ = mgpc.music_group_id_
		WHERE mgpc.batch_no_ IS NOT NULL AND mgpcd.payment_status_ = 'NON_PAYMENT' AND mgpc.current_total_amount_ > 0
		  AND mgpc.music_group_id_ = #{musicGroupId} AND mgpc.status_ IN ('OPEN','OVER','PAID') AND mgpc.pay_user_type_ = 'STUDENT'
		  AND ((mgpc.member_rank_setting_id_ IS NOT NULL AND (sr.membership_end_time_ &lt; NOW() OR sr.membership_end_time_ IS NULL)) OR mgpc.member_rank_setting_id_ IS NULL)
		GROUP BY mgpcd.user_id_
	</select>

	<select id="queryNoPaymentUserIds" resultType="java.lang.Integer">
		SELECT mgpcd.user_id_ FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
		WHERE mgpc.batch_no_ IS NOT NULL AND mgpcd.payment_status_ = 'NON_PAYMENT' AND mgpc.current_total_amount_ > 0
		  AND mgpc.music_group_id_ = #{musicGroupId} AND mgpc.status_ IN ('OPEN','OVER','PAID') AND mgpc.pay_user_type_ = 'STUDENT'
		<if test="userIds != null">
			AND mgpcd.user_id_ IN
			<foreach collection="userIds" open="(" close=")" separator="," item="item">
				#{item}
			</foreach>
		</if>
		<if test="batchNo != null and batchNo != ''">
			AND mgpc.batch_no_ != #{batchNo}
		</if>
		GROUP BY mgpcd.user_id_
	</select>

	<resultMap id="MusicArrearageStudentDto" type="com.ym.mec.biz.dal.dto.MusicArrearageStudentDto">
		<result property="userId" column="user_id_"/>
		<result property="studentName" column="student_name_"/>
		<result property="organName" column="organ_name_"/>
		<result property="cooperationName" column="cooperation_name_"/>
		<result property="musicGroupId" column="music_group_id_"/>
		<result property="musicGroupName" column="music_group_name_"/>
		<result property="eduTeacherName" column="edu_teacher_name_"/>
		<result property="subjectName" column="subject_name_"/>
		<result property="gender" column="gender_"/>
		<result property="parentName" column="parent_name_"/>
		<result property="phone" column="phone_"/>
		<result property="noPaymentAmount" column="no_payment_amount_"/>
	</resultMap>

	<sql id="queryArrearageStudentsCondition">
		<where>
			mg.status_ = 'PROGRESS' and mg.tenant_id_ = #{tenantId}
			AND ((mgpc.member_rank_setting_id_ IS NOT NULL AND (sr.membership_end_time_ &lt; NOW() OR sr.membership_end_time_ IS NULL)) OR mgpc.member_rank_setting_id_ IS NULL)
			<if test="noPaymentType==null or noPaymentType==0">
				AND DATE_FORMAT(NOW(),'%Y-%m-%d') > DATE_FORMAT(mgpc.deadline_payment_date_,'%Y-%m-%d')
			</if>
			<if test="noPaymentType!=null and noPaymentType==1">
				AND DATE_FORMAT(NOW(),'%Y-%m-%d') BETWEEN DATE_FORMAT(mgpc.start_payment_date_,'%Y-%m-%d') AND DATE_FORMAT(mgpc.deadline_payment_date_,'%Y-%m-%d')
			</if>
			AND mgpcd.payment_status_ = 'NON_PAYMENT' AND mgpc.batch_no_ IS NOT NULL AND mgpc.pay_user_type_ = 'STUDENT'
			AND mgpc.current_total_amount_ > 0
			<if test="organId != null and organId != ''">
				AND FIND_IN_SET(mg.organ_id_,#{organId})
			</if>
			<if test="musicGroupId!=null and musicGroupId!=''">
				AND mgpc.music_group_id_ = #{musicGroupId}
			</if>
			<if test="courseViewType != null">
				AND mg.course_view_type_ = #{courseViewType}
			</if>
			<if test="cooperationOrganId!=null">
				AND mg.cooperation_organ_id_ = #{cooperationOrganId}
			</if>
			<if test="eduTeacherId!=null">
				AND mg.educational_teacher_id_=#{eduTeacherId}
			</if>
			<if test="search!=null and search!=''">
				AND (mgpcd.user_id_=#{search} OR su.username_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

    <select id="queryArrearageStudents" resultMap="MusicArrearageStudentDto">
		SELECT
			mgpcd.user_id_,
			mgpc.current_total_amount_ no_payment_amount_,
			su.username_ student_name_,
			organ.name_ organ_name_,
			co.name_ cooperation_name_,
		    mgpc.music_group_id_,
			mg.name_ music_group_name_,
			edu.real_name_ edu_teacher_name_,
			MAX( sub.name_ ) subject_name_,
			su.gender_ gender_,
			sut.name_ parent_name_,
			su.phone_ phone_
		FROM
			music_group_payment_calender_detail mgpcd
			LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
			LEFT JOIN music_group mg ON mgpc.music_group_id_ = mg.id_
			LEFT JOIN student_registration sr ON sr.music_group_id_ = mgpc.music_group_id_ AND mgpcd.user_id_ = sr.user_id_
			LEFT JOIN sys_user su ON mgpcd.user_id_ = su.id_
			LEFT JOIN sys_user_tsign sut ON sut.user_id_ = su.id_
			LEFT JOIN sys_user edu ON edu.id_ = mg.educational_teacher_id_
			LEFT JOIN organization organ ON organ.id_ = mg.organ_id_
			LEFT JOIN `subject` sub ON sr.actual_subject_id_ = sub.id_
			LEFT JOIN cooperation_organ co ON mg.cooperation_organ_id_ = co.id_
		<include refid="queryArrearageStudentsCondition"/>
		GROUP BY
			mgpc.music_group_id_,mgpcd.user_id_
		<include refid="global.limit" />
	</select>

	<select id="countArrearageStudents" resultType="int">
		SELECT
			COUNT(DISTINCT mgpc.music_group_id_,mgpcd.user_id_)
		FROM
			music_group_payment_calender_detail mgpcd
			LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
			LEFT JOIN music_group mg ON mgpc.music_group_id_ = mg.id_
			LEFT JOIN student_registration sr ON sr.music_group_id_ = mgpc.music_group_id_ AND mgpcd.user_id_ = sr.user_id_
			<if test="search!=null and search!=''">
				LEFT JOIN sys_user su ON mgpcd.user_id_ = su.id_
			</if>
		<include refid="queryArrearageStudentsCondition"/>
	</select>

	<select id="getNoPaymentStudentMusicGroups" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.MusicGroup">
		SELECT
			DISTINCT mg.id_,mg.name_
		FROM
		music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpcd.music_group_payment_calender_id_ = mgpc.id_
		LEFT JOIN music_group mg ON mgpc.music_group_id_ = mg.id_
		WHERE mg.status_ = 'PROGRESS' and mg.tenant_id_ = #{tenantId}
			AND DATE_FORMAT( NOW(), '%Y-%m-%d' ) >= DATE_FORMAT( mgpc.payment_valid_start_date_, '%Y-%m-%d' )
			AND mgpcd.payment_status_ = 'NON_PAYMENT'
			AND mgpc.current_total_amount_ > 0 AND mgpc.pay_user_type_ = 'STUDENT'
			<if test="organIds!=null and organIds.size()>0">
				AND mg.organ_id_ IN
				<foreach collection="organIds" item="organId" open="(" close=")" separator=",">
					#{organId}
				</foreach>
			</if>
	</select>
	<select id="queryNoPaymentCanPushByCalenderId"
			resultMap="MusicGroupPaymentCalenderDetail">
		SELECT mgpcd.* FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		WHERE (mgpc.status_ = 'OPEN' OR mgpcd.open_ = 1) AND mgpcd.payment_status_ = 'NON_PAYMENT' AND mgpc.current_total_amount_ > 0
		AND mgpcd.music_group_payment_calender_id_ = #{calenderId} AND FIND_IN_SET(mgpcd.user_id_,#{studentIds})
	</select>
	<select id="countDetailPage" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT mgpcd.id_) FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_
		LEFT JOIN `subject` st ON st.id_ = sr.actual_subject_id_
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		<include refid="queryPageSql"/>
	</select>

	<resultMap id="MusicGroupPaymentCalenderDetailDto" type="com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderDetailDto" extends="MusicGroupPaymentCalenderDetail">
	</resultMap>
	<select id="queryDetailPage" resultMap="MusicGroupPaymentCalenderDetailDto">
		SELECT mgpcd.id_,mgpcd.user_id_,su.username_,st.name_ subject_names_,sr.music_group_status_,su.phone_,
		CASE WHEN mgpcd.start_payment_date_ IS NULL THEN mgpc.start_payment_date_ ELSE mgpcd.start_payment_date_ END start_payment_date_mgpc_,
		CASE WHEN mgpcd.deadline_payment_date_ IS NULL THEN mgpc.deadline_payment_date_ ELSE mgpcd.deadline_payment_date_ END deadline_payment_date_mgpc_,
		CASE WHEN mgpc.status_ = 'OPEN' OR mgpcd.open_ = 1 THEN 1 ELSE 0 END open_,mgpc.payment_type_,mgpcd.payment_order_id_,mgpcd.payment_status_,mgpcd.pay_time_,
		mgpcd.music_group_payment_calender_id_,mgpc.current_total_amount_ expect_amount_
		FROM music_group_payment_calender_detail mgpcd
		LEFT JOIN sys_user su ON su.id_ = mgpcd.user_id_
		LEFT JOIN student_registration sr ON sr.user_id_ = mgpcd.user_id_
		LEFT JOIN `subject` st ON st.id_ = sr.actual_subject_id_
		LEFT JOIN music_group_payment_calender mgpc ON mgpc.id_ = mgpcd.music_group_payment_calender_id_
		<include refid="queryPageSql"/>
		ORDER BY mgpc.update_time_ DESC
		<include refid="global.limit" />
	</select>

	<update id="updateNoPaymentAndZeroPaymentStatus">
		UPDATE music_group_payment_calender_detail mgpcd
		SET mgpcd.payment_status_ = 'PAID_COMPLETED',mgpcd.update_time_ = NOW()
		WHERE mgpcd.music_group_payment_calender_id_ = #{calenderId}
		AND mgpcd.payment_status_ = 'NON_PAYMENT'
	</update>
</mapper>
