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

    <resultMap id="studentManageListDto" type="com.ym.mec.biz.dal.dto.StudentManageListDto">
        <result property="userId" column="user_id_"/>
        <result property="realName" column="real_name_"/>
        <result property="nation" column="nation_"/>
        <result property="parentsPhone" column="parents_phone_"/>
        <result property="parentsName" column="real_name_"/>
        <result property="gender" column="gender_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="username" column="username_"/>
        <result property="birthdate" column="birthdate_"/>
        <result property="currentGrade" column="current_grade_"/>
        <result property="currentClass" column="current_class_"/>
        <result property="isActive" column="isActive_"/>
        <result property="isMake" column="isMake_"/>
        <!--<collection property="musicGroups" resultMap="musicGroup" javaType="list"/>-->
    </resultMap>

    <resultMap id="studentManageCourseListDto" type="com.ym.mec.biz.dal.dto.StudentManageCourseListDto">
        <result property="classGroupId" column="class_group_id_" />
        <result property="classGroupName" column="class_group_name_" />
        <result property="classGroupType" column="class_group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="courseDate" column="course_date_" />
        <result property="courseStatus" column="course_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="teacherName" column="teacher_name_" />
        <result property="currentCLassTimes" column="current_class_times_" />
        <result property="totalClassTimes" column="total_class_times_" />
        <result property="attendanceStatus" column="status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

    <resultMap id="studentManageAccountBaseInfo" type="com.ym.mec.biz.dal.dto.StudentManageAccountBaseInfoDto">
        <result property="balance" column="balance_"/>
        <result property="courseBalance" column="course_balance_"/>
        <result property="bankName" column="bank_name_"/>
        <result property="cardNo" column="card_no_"/>
        <result property="courseBalance" column="course_balance_"/>
    </resultMap>

    <sql id="queryCondition">
        <where>
            cgsm.user_id_ IS NOT NULL
            <if test="organID != null">
              AND s.organ_id_=#{organID}
            </if>
            <if test="musicGroupId != null">
                and mg.id_ = #{musicGroupId}
            </if>
            <if test="studentStatus != null">
                and cgsm.status_ = #{studentStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="queryString != null">
                and (su.username_ like CONCAT("%",#{queryString},"%")
                    or su.phone_ like CONCAT("%",#{queryString},"%")
                  )
            </if>
        </where>
    </sql>
    <update id="updateUser" parameterType="com.ym.mec.auth.api.entity.SysUser">
        UPDATE sys_user
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="wxOpenid != null">
                wx_openid_ = #{wxOpenid},
            </if>
            <if test="avatar != null">
                avatar_ = #{avatar},
            </if>
            <if test="email != null">
                email_ = #{email},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            <if test="salt != null">
                salt_ = #{salt},
            </if>
            <if test="username != null">
                username_ = #{username},
            </if>
            <if test="userType != null">
                user_type_ = #{userType},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="lockFlag != null">
                lock_flag_ = #{lockFlag},
            </if>
            <if test="birthdate != null">
                birthdate_ = #{birthdate},
            </if>
            <if test="phone != null">
                phone_ = #{phone},
            </if>
            <if test="qqOpenid != null">
                qq_openid_ = #{qqOpenid},
            </if>
            <if test="nation != null">
                nation_ = #{nation},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="imToken != null">
                im_token_ = #{imToken},
            </if>
            <if test="idCardNo != null">
                id_card_no_ = #{idCardNo},
            </if>
            <if test="wechatId != null">
                wechat_id_ = #{wechatId},
            </if>
            <if test="realName != null">
                real_name_ = #{realName},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <select id="findStudentsByOrganId" resultMap="studentManageListDto">
        SELECT su.id_ user_id_,su.username_,su.gender_,su.phone_ parents_phone_,su.real_name_,
        su.birthdate_,su.nation_,
        CASE WHEN su.password_ IS NULL THEN 0 ELSE 1 END isActive_,
        CASE WHEN pla.id_ IS NULL THEN 0 ELSE 1 END isMake_
        FROM sys_user su
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        <include refid="findStudentsByOrganIdSql"/>
        ORDER BY su.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="findStudentsByOrganIdSql">
        <where>
                su.user_type_ LIKE '%STUDENT%'
            <if test="organId != null">
                AND FIND_IN_SET(su.organ_id_,#{organId})
            </if>
            <if test="search != null and search != ''">
                AND (su.phone_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="isActive != null and isActive == true">
                AND su.password_ is not null
            </if>
            <if test="isActive != null and isActive == false">
                AND su.password_ is null
            </if>
            <if test="isMake != null and isMake == true">
                AND pla.id_ IS NOT NULL
            </if>
            <if test="isMake != null and isMake == false">
                AND pla.id_ IS NULL
            </if>
            <if test="userIds != null">
                AND su.id_ IN
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </sql>

    <select id="countStudentByOrganId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT su.id_)
        FROM sys_user su
        LEFT JOIN student_registration sr ON su.id_ = sr.user_id_
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        WHERE su.user_type_ LIKE '%STUDENT%'
        <include refid="queryHasCourseSql"/>
    </select>
    <select id="findStudentBaseInfoByUserID" resultMap="studentManageListDto">
        SELECT su.username_,su.gender_,su.birthdate_,su.real_name_,su.id_ user_id_,
        su.phone_ parents_phone_,sr.current_class_,sr.current_grade_,su.nation_
        FROM sys_user su
        LEFT JOIN student_registration sr ON su.id_ = sr.user_id_
        WHERE su.id_ =#{userId} AND su.user_type_ LIKE '%STUDENT%' LIMIT 1
    </select>

    <select id="findGoodsNamesByIds" resultType="string">
      SELECT name_ FROM goods WHERE id_ IN
      <foreach collection="ids" item="id" open="(" close=")" separator=",">
          #{id}
      </foreach>
    </select>

    <resultMap id="musicGroup" type="com.ym.mec.biz.dal.dto.StudentManageBaseInfoOfMusicGroupDto">
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="musicGroupStatus" column="music_group_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="joinMusicGroupDate" column="join_date_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="kitPurchaseMethod" column="kit_purchase_method_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="depositFee" column="deposit_fee_"/>
        <result property="userId" column="user_id_"/>
        <result property="userMusicGroupStatus" column="user_music_group_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="findStudentMusicGroupsByUserId" resultMap="musicGroup">
        SELECT mg.id_ music_group_id_,sr.create_time_ join_date_,mg.name_ music_group_name_,mg.status_ music_group_status_,
        s.id_ subject_id_,s.name_ subject_name_,sr.kit_purchase_method_,mgsp.deposit_fee_,sr.user_id_,sr.music_group_status_ user_music_group_status_
        FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        LEFT JOIN `subject` s ON s.id_ = sr.actual_subject_id_
        LEFT JOIN music_group_subject_plan mgsp ON mg.id_ = mgsp.music_group_id_ AND s.id_ = mgsp.subject_id_
        WHERE sr.user_id_=#{userId} AND (sr.music_group_status_ != 'QUIT' OR (sr.music_group_status_ = 'QUIT' AND mgsp.id_ IS NOT NULL))
        ORDER BY mg.create_time_ DESC
    </select>

    <sql id="studentManageCourseQueryCondition">
        <where>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            AND cgsm.status_ != 'QUIT'
            <if test="studentId!=null">
                and cgsm.user_id_ = #{studentId}
            </if>
            <if test="musicGroupId!=null">
                and cg.music_group_id_ = #{musicGroupId} AND cg.group_type_='MUSIC'
            </if>
            <if test="classGroupType!=null">
                and cg.type_ = #{classGroupType}
            </if>
            <if test="courseStatus!=null">
                and cs.status_=#{courseStatus}
            </if>
            <if test="classGroupName!=null">
                and cg.name_ like CONCAT("%",#{classGroupName},"%")
            </if>
            <if test="teacherName!=null">
                and su.username_ like CONCAT("%",#{teacherName},"%")
            </if>
            <if test="vipGroupId!=null">
                and cg.music_group_id_ = #{vipGroupId} AND cg.group_type_='VIP'
            </if>
        </where>
    </sql>

    <select id="findStudentCourseList" resultMap="studentManageCourseListDto">
        SELECT
            cg.id_ class_group_id_,
            cg.name_ class_group_name_,
            cg.type_ class_group_type_,
            CONCAT(cs.class_date_," ",cs.start_class_time_) course_date_,
            cs.status_ course_status_,
            su.real_name_ teacher_name_
        FROM class_group cg
            LEFT JOIN class_group_student_mapper cgsm ON cg.id_=cgsm.class_group_id_
            LEFT JOIN course_schedule cs ON cg.id_=cs.class_group_id_
            LEFT JOIN sys_user su ON cs.teacher_id_=su.id_
        <include refid="studentManageCourseQueryCondition"/>
        ORDER BY course_date_
        <include refid="global.limit"/>
    </select>

    <select id="countStudentCourses" resultType="int">
        SELECT count(*) FROM class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_=cgsm.class_group_id_
        LEFT JOIN course_schedule cs ON cg.id_ = cs.class_group_id_
        LEFT JOIN sys_user su ON cs.teacher_id_=su.id_
        <include refid="studentManageCourseQueryCondition"/>
    </select>

    <sql id="studentManageAttendanceQueryCondition">
        <where>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            AND CONCAT(cs.class_date_," ",cs.start_class_time_) &lt; NOW()
            <if test="studentId!=null">
                AND cssp.user_id_ = #{studentId}
            </if>
            <if test="classGroupType!=null">
                AND cg.type_ = #{classGroupType}
            </if>
            <if test="attendanceStatus != null and attendanceStatus != 'TRUANT'">
                AND sa.status_ = #{attendanceStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="attendanceStatus != null and attendanceStatus == 'TRUANT'">
                AND (sa.status_ = #{attendanceStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} OR sa.status_ IS NULL)
            </if>
            <if test="classGroupName!=null">
                AND (cg.name_ LIKE CONCAT("%",#{classGroupName},"%") OR cs.name_ LIKE CONCAT("%",#{classGroupName},"%"))
            </if>
            <if test="teacherName!=null">
                AND su.real_name_ LIKE CONCAT("%",#{teacherName},"%")
            </if>
        </where>
    </sql>

    <select id="findStudentAttendances" resultMap="studentManageCourseListDto">
        SELECT
            cg.id_ class_group_id_,
            cg.name_ class_group_name_,
            cg.type_ class_group_type_,
            CONCAT(cs.class_date_," ",cs.start_class_time_) course_date_,
            su.real_name_ teacher_name_,
            IF(sa.status_ IS NULL,'TRUANT',sa.status_) status_,
            sa.current_class_times_,
            cg.total_class_times_
        FROM course_schedule_student_payment cssp
            LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
            LEFT JOIN class_group cg ON cssp.class_group_id_ = cg.id_
            LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
            LEFT JOIN sys_user su ON cs.actual_teacher_id_ = su.id_
        <include refid="studentManageAttendanceQueryCondition"/>
        ORDER BY course_date_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="countStudentAttendances" resultType="int">
        SELECT
        count(cssp.id_)
        FROM course_schedule_student_payment cssp
        LEFT JOIN student_attendance sa ON cssp.course_schedule_id_ = sa.course_schedule_id_ AND cssp.user_id_ = sa.user_id_
        LEFT JOIN class_group cg ON cssp.class_group_id_ = cg.id_
        LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN sys_user su ON sa.teacher_id_ = su.id_
        <include refid="studentManageAttendanceQueryCondition"/>
    </select>

    <select id="getStudentAccountBaseInfo" resultMap="studentManageAccountBaseInfo">
        SELECT
            suca.balance_,
            suca.course_balance_,
            subc.bank_name_,
            subc.card_no_,
            suca.course_balance_
        FROM
            sys_user_cash_account suca
            LEFT JOIN sys_user_bank_card subc ON suca.user_id_ = subc.user_id_
            AND subc.is_default_ = 1
        WHERE
            suca.user_id_ =#{userId}
    </select>

    <resultMap id="StudentPaymentDto" type="com.ym.mec.biz.dal.dto.StudentPaymentDto">
        <result property="classDate" column="class_date_"/>
        <result property="startClassTime" column="start_class_time_"/>
        <result property="classGroupType" column="class_group_type_"/>
        <result property="classGroupName" column="class_group_name_"/>
        <result property="currentClassTimes" column="current_class_times_"/>
        <result property="totalClassTimes" column="total_class_times_"/>
        <result property="status" column="status_"/>
        <result property="expectPrice" column="expect_price_"/>
        <result property="actualPrice" column="actual_price_"/>
    </resultMap>
    <select id="queryStudentPayment" resultMap="StudentPaymentDto">
        SELECT cs.class_date_,cs.start_class_time_,cs.type_ class_group_type_ ,cg.name_ class_group_name_,
        cg.total_class_times_,sa.current_class_times_,IF(sa.status_ IS NULL,'TRUANT',sa.status_) status_,cssp.expect_price_,cssp.actual_price_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN class_group cg ON cg.id_ = cs.class_group_id_
        LEFT JOIN student_attendance sa ON sa.course_schedule_id_ = cssp.course_schedule_id_ AND sa.user_id_ = cssp.user_id_
        <include refid="studentPaymentSql"/>
        ORDER BY cs.class_date_ DESC,cs.start_class_time_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="studentPaymentSql">
        <where>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            AND cs.id_ IS NOT NULL AND CONCAT(cs.class_date_,' ',cs.start_class_time_) &lt; NOW()
            <if test="classGroupType != null">
                AND cs.type_ = #{classGroupType}
            </if>
            <if test="classGroupName != null">
                AND cs.name_ = #{classGroupName}
            </if>
            <if test="attendanceStatus != null">
                AND sa.status_ = #{attendanceStatus}
            </if>
            <if test="studentId != null">
                AND cssp.user_id_ = #{studentId}
            </if>
        </where>
    </sql>
    <select id="countStudentPayment" resultType="java.lang.Integer">
        SELECT COUNT(cssp.id_)
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN student_attendance sa ON sa.course_schedule_id_ = cssp.course_schedule_id_ AND sa.user_id_ = cssp.user_id_
        <include refid="studentPaymentSql"/>
    </select>

    <sql id="queryMusicGroupStudentSql">
        <where>
            <if test="search != null and search != ''">
                AND (su.username_ like CONCAT("%",#{search},"%") OR sr.parents_phone_ like CONCAT("%",#{search},"%"))
            </if>
            <if test="subjectId != null">
                AND sr.actual_subject_id_ = #{subjectId}
            </if>
            <if test="classGroupId != null">
                AND cgsm.class_group_id_ = #{classGroupId} AND sr.user_id_ = cgsm.user_id_ AND cgsm.status_ != 'QUIT'
            </if>
            <if test="studentStatus != null and studentStatus != ''">
                AND sr.music_group_status_ = #{studentStatus}
            </if>
            <if test="paymentStatus != null">
                AND mgsf.payment_status_ = #{paymentStatus}
            </if>
            <if test="isLock != null">
                AND mgsf.is_lock_ = #{isLock}
            </if>
            <if test="musicGroupId != null and musicGroupId != ''">
                AND sr.music_group_id_ = #{musicGroupId}
            </if>
            <if test="isActive != null and isActive == true">
                and su.password_ is not null
            </if>
            <if test="isActive != null and isActive == false">
                and su.password_ is null
            </if>
        </where>
    </sql>
    <resultMap id="MusicGroupStudentsDto" type="com.ym.mec.biz.dal.dto.MusicGroupStudentsDto">
        <result property="userId" column="user_id_"/>
        <result property="realName" column="real_name_"/>
        <result property="phone" column="parents_phone_"/>
        <result property="gender" column="gender_"/>
        <result property="currentGrade" column="current_grade_"/>
        <result property="currentClass" column="current_class_"/>
        <result property="subjectName" column="subject_name_"/>
        <result property="studentStatus" column="music_group_status_"/>
        <result property="paymentStatus" column="payment_status_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="isNewStudent" column="is_new_student_"/>
        <result property="courseFee" column="course_fee_"/>
        <result property="nextPaymentDate" column="next_payment_date_"/>
        <result property="isActive" column="isActive_"/>
        <result property="isLock" column="is_lock_"/>
        <result property="paymentPeriodList" column="payment_period_list_"/>
    </resultMap>
    <select id="queryMusicGroupStudent" resultMap="MusicGroupStudentsDto">
        SELECT sr.user_id_,su.username_ real_name_,su.gender_,su.phone_ parents_phone_,sr.current_grade_,mgsf.payment_status_,
        sr.current_class_,sr.music_group_status_,mgsf.next_payment_date_,mgsf.course_fee_,mgsf.payment_period_list_,
        s.name_ subject_name_,sr.music_group_id_,case when su.password_ is null then 0 else 1 end isActive_,mgsf.is_lock_,
        IF(DATE_FORMAT(sr.create_time_,'%Y-%m-%d') > DATE_FORMAT(mg.payment_expire_date_,'%Y-%m-%d'),1,0) is_new_student_
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.user_id_ = su.id_
        LEFT JOIN `subject` s ON s.id_ = sr.actual_subject_id_
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        <if test="classGroupId != null">
            LEFT JOIN class_group_student_mapper cgsm ON cgsm.music_group_id_ = sr.music_group_id_
        </if>
        LEFT JOIN music_group_student_fee_ mgsf ON mgsf.music_group_id_ = mg.id_ AND mgsf.user_id_ = su.id_
        <include refid="queryMusicGroupStudentSql"/>
        ORDER BY sr.id_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="countMusicGroupStudent" resultType="java.lang.Integer">
        SELECT COUNT(sr.user_id_)
        FROM student_registration sr
        LEFT JOIN sys_user su ON sr.user_id_ = su.id_
        LEFT JOIN `subject` s ON s.id_ = sr.actual_subject_id_
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        <if test="classGroupId != null">
            LEFT JOIN class_group_student_mapper cgsm ON cgsm.music_group_id_ = sr.music_group_id_
        </if>
        LEFT JOIN music_group_student_fee_ mgsf ON mgsf.music_group_id_ = mg.id_ AND mgsf.user_id_ = su.id_
        <include refid="queryMusicGroupStudentSql"/>
    </select>

    <select id="countStudyNum" resultType="java.lang.Integer">
        SELECT COUNT(sr.user_id_) FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.payment_status_ = 2
        AND sr.music_group_status_ != 'QUIT'
    </select>

    <select id="countQuitNum" resultType="java.lang.Integer">
        SELECT COUNT(sr.user_id_) FROM student_registration sr
        WHERE sr.music_group_id_ = #{musicGroupId}
        AND sr.music_group_status_ = 'QUIT'
    </select>
    <select id="countAddNum" resultType="java.lang.Integer">
        SELECT COUNT(sr.user_id_) FROM student_registration sr
        LEFT JOIN music_group mg ON sr.music_group_id_ = mg.id_
        WHERE sr.music_group_id_ = #{musicGroupId} AND sr.music_group_status_ != 'QUIT'
        AND DATE_FORMAT(sr.create_time_,'%Y-%m-%d') > DATE_FORMAT(mg.payment_expire_date_,'%Y-%m-%d')
    </select>

    <resultMap id="MusicGroupStudentSignDto" type="com.ym.mec.biz.dal.dto.MusicGroupStudentSignDto">
        <result property="signTime" column="create_time_"/>
        <result property="name" column="real_name_"/>
        <result property="signStatus" column="status_"/>
        <result property="courseScheduleName" column="name_"/>
        <result property="classGroupName" column="class_group_name_"/>
    </resultMap>
    <select id="queryMusicGroupStudentsSign" resultMap="MusicGroupStudentSignDto">
        SELECT sa.create_time_,su.username_ real_name_,sa.status_,cs.name_,cg.name_ class_group_name_
        FROM student_attendance sa
        LEFT JOIN sys_user su ON sa.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_ = cs.id_
        LEFT JOIN class_group cg ON sa.class_group_id_=cg.id_
        <include refid="queryMusicGroupStudentsSignSql"/>
        <include refid="global.limit"/>
    </select>

    <sql id="queryMusicGroupStudentsSignSql">
        <where>
            (cs.del_flag_ != 1 OR cs.del_flag_ IS NULL)
            <if test="musicGroupId != null">
                AND sa.music_group_id_ = #{musicGroupId}
            </if>
            <if test="courseScheduleName != null">
                AND cs.name_ LIKE CONCAT('%',#{courseScheduleName},'%')
            </if>
            <if test="startTime != null">
                AND sa.create_time_ &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND sa.create_time_ &lt;= #{endTime}
            </if>
            <if test="signStatus != null">
                AND sa.status_ = #{signStatus}
            </if>
            <if test="userName != null">
                AND su.username_ LIKE CONCAT('%',#{userName},'%')
            </if>
            <if test="classGroupId != null">
                AND sa.class_group_id_ = #{classGroupId}
            </if>
        </where>
    </sql>

    <select id="countMusicGroupStudentsSign" resultType="java.lang.Integer">
        SELECT COUNT(sa.id_)
        FROM student_attendance sa
        LEFT JOIN sys_user su ON sa.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON sa.course_schedule_id_ = cs.id_
        <include refid="queryMusicGroupStudentsSignSql"/>
    </select>
    <resultMap id="StudentManageVipGroupClassDto" type="com.ym.mec.biz.dal.dto.StudentManageVipGroupClassDto">
        <result property="vipGroupId" column="id_"/>
        <result property="vipGroupName" column="vip_group_name_"/>
        <result property="vipGroupStatus" column="status_"/>
        <result property="teacherId" column="user_id_"/>
        <result property="studentNum" column="student_num_"/>
        <result property="activeName" column="name_"/>
        <result property="courseStartDate" column="courses_start_date"/>
        <result property="currentClassTimes" column="current_class_times_"/>
        <result property="totalClassTimes" column="total_class_times_"/>
    </resultMap>
    <select id="findStudentVipGroupClass" resultMap="StudentManageVipGroupClassDto">
        SELECT vg.id_,vg.name_ vip_group_name_,vg.group_status_ status_,vg.user_id_,cg.student_num_,
        vgc.name_,vg.courses_start_date,cg.current_class_times_,cg.total_class_times_
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN vip_group vg ON cg.music_group_id_ = vg.id_
        LEFT JOIN vip_group_activity vgc ON vgc.id_ = vg.vip_group_activity_id_
        WHERE cg.group_type_ = 'VIP' AND cgsm.user_id_ = #{studentId} AND vg.group_status_ != 3
        <if test="vipGroupName != null">
            AND vg.name_ LIKE CONCAT('%',#{vipGroupName},'%')
        </if>
        <if test="vipGroupStatus != null">
            AND vg.group_status_ = #{vipGroupStatus}
        </if>
        ORDER BY vg.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countStudentVipGroupClass" resultType="java.lang.Integer">
        SELECT COUNT(vg.id_)
        FROM class_group_student_mapper cgsm
        LEFT JOIN class_group cg ON cgsm.class_group_id_ = cg.id_
        LEFT JOIN vip_group vg ON cg.music_group_id_ = vg.id_
        WHERE cg.group_type_ = 'VIP' AND cgsm.user_id_ = #{studentId} AND vg.group_status_ != 3
        <if test="vipGroupName != null">
            AND vg.name_ LIKE CONCAT('%',#{vipGroupName},'%')
        </if>
        <if test="vipGroupStatus != null">
            AND vg.group_status_ = #{vipGroupStatus}
        </if>
    </select>
    <sql id="queryHasCourseSql">
        <if test="organId != null">
            AND FIND_IN_SET(su.organ_id_,#{organId})
        </if>
        <if test="search != null and search != ''">
            AND (su.phone_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_ LIKE CONCAT('%',#{search},'%'))
        </if>
        <if test="isActive != null and isActive == true">
            AND su.password_ IS NOT NULL
        </if>
        <if test="isActive != null and isActive == false">
            AND su.password_ IS NULL
        </if>
        <if test="isMake != null and isMake == true">
            AND pla.id_ IS NOT NULL
        </if>
        <if test="isMake != null and isMake == false">
            AND pla.id_ IS NULL
        </if>
    </sql>
    <select id="queryHasCourse" resultType="java.lang.Integer">
        SELECT su.id_ FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        WHERE su.user_type_ LIKE '%STUDENT%' AND cs.status_ != 'OVER'
        <include refid="queryHasCourseSql"/>
        GROUP BY su.id_
        ORDER BY su.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countHasCourse" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT su.id_) FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        WHERE su.user_type_ LIKE '%STUDENT%' AND cs.status_ != 'OVER'
        <include refid="queryHasCourseSql"/>
    </select>
    <select id="queryNotCourse" resultType="java.lang.Integer">
        SELECT su.id_ FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cs.status_ != 'OVER'
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        <include refid="queryNotCourseSql"/>
        GROUP BY su.id_
        HAVING COUNT(cs.id_) = 0
        ORDER BY su.create_time_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countNotCourse" resultType="java.lang.Integer">
        SELECT COUNT(e.id_) FROM (SELECT su.id_ FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_ AND cs.status_ != 'OVER'
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        <include refid="queryNotCourseSql"/>
        GROUP BY su.id_
        HAVING COUNT(cs.id_) = 0)e
    </select>
    <sql id="queryNotCourseSql">
        <where>
                su.user_type_ LIKE '%STUDENT%'
            <if test="organId != null">
                AND FIND_IN_SET(su.organ_id_,#{organId})
            </if>
            <if test="search != null and search != ''">
                AND (su.phone_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.id_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="isActive != null and isActive == true">
                and su.password_ is not null
            </if>
            <if test="isActive != null and isActive == false">
                and su.password_ is null
            </if>
            <if test="isMake != null and isMake == true">
                and pla.id_ IS NOT NULL
            </if>
            <if test="isMake != null and isMake == false">
                and pla.id_ IS NULL
            </if>
        </where>
    </sql>
    <select id="queryStudentHasCourse" resultType="java.util.Map">
        SELECT cssp.user_id_ 'key',CASE WHEN COUNT(cssp.id_ AND cs.status_ != 'OVER') = 0 THEN 0 ELSE 1 END 'value'
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        <where>
            <if test="userIds != null">
                cssp.user_id_ IN
                <foreach collection="userIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY cssp.user_id_
    </select>
    <resultMap id="StudentHasCourseDtoMap" type="com.ym.mec.biz.dal.dto.StudentHasCourseDto">
        <result property="userName" column="username_"/>
        <result property="musicGroupName" column="music_group_name_"/>
        <result property="vipGroupName" column="vip_group_name_"/>
        <result property="organName" column="organ_name_"/>
        <result property="subjectName" column="music_group_subject_"/>
        <result property="userId" column="id_"/>
        <result property="isActive" column="is_active_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="isMake" column="is_make_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <select id="queryCourseStudent" resultMap="StudentHasCourseDtoMap">
        SELECT su.id_,su.username_,o.name_ organ_name_,
        CASE WHEN su.password_ IS NULL THEN 0 ELSE 1 END is_active_,
        CASE WHEN pla.id_ IS NULL THEN 0 ELSE 1 END is_make_
        FROM sys_user su
        LEFT JOIN organization o ON su.organ_id_ = o.id_
        LEFT JOIN practice_lesson_apply pla ON pla.user_id_ = su.id_
        WHERE su.id_ IN
        <foreach collection="userIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>
    <select id="queryHasCourseStudent" resultMap="StudentHasCourseDtoMap">
        SELECT su.id_,GROUP_CONCAT(DISTINCT su.username_) username_,GROUP_CONCAT(DISTINCT mg.name_) music_group_name_,
        GROUP_CONCAT(DISTINCT vg.name_) vip_group_name_,GROUP_CONCAT(DISTINCT o.name_) organ_name_,GROUP_CONCAT(DISTINCT s.name_) music_group_subject_,
        CASE WHEN su.password_ IS NULL THEN 0 ELSE 1 END is_active_,
        CASE WHEN pla.id_ IS NULL THEN 0 ELSE 1 END is_make_
        FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON su.id_ = cssp.user_id_
        LEFT JOIN music_group mg ON cssp.music_group_id_ = mg.id_ AND cssp.group_type_ = 'MUSIC'
        LEFT JOIN vip_group vg ON cssp.music_group_id_ = vg.id_ AND cssp.group_type_ = 'VIP'
        LEFT JOIN organization o ON su.organ_id_ = o.id_
        LEFT JOIN student_registration sr ON su.id_ = sr.user_id_ AND cssp.music_group_id_ = sr.music_group_id_
        LEFT JOIN `subject` s ON s.id_ = sr.actual_subject_id_
        LEFT JOIN practice_lesson_apply pla ON su.id_ = pla.user_id_
        WHERE cssp.id_ IS NOT NULL AND su.user_type_ LIKE '%STUDENT%' AND (sr.music_group_status_ != 'QUIT' OR sr.id_ IS NULL)
        AND (mg.status_ = 'PROGRESS' OR vg.group_status_ IN (2,4))
        <if test="organId != null and organId != ''">
            AND FIND_IN_SET(su.organ_id_,#{organId})
        </if>
        GROUP BY su.id_,pla.id_
    </select>
    <select id="getHasCourse" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT su.id_) FROM sys_user su
        LEFT JOIN course_schedule_student_payment cssp ON cssp.user_id_ = su.id_
        LEFT JOIN course_schedule cs ON cs.id_ = cssp.course_schedule_id_
        WHERE cs.status_ != 'OVER' AND su.id_ = #{userId}
    </select>
    <select id="countStudentSignInNum" resultType="java.lang.Integer">
        SELECT COUNT(id_) FROM student_attendance WHERE course_schedule_id_ = #{courseScheduleId} AND status_ = #{status}
    </select>
    <select id="countHomeworkNum" resultType="java.lang.Integer">
        SELECT COUNT(id_) FROM student_course_homework WHERE course_schedule_id_ = #{courseScheduleId} AND status_ = 1
    </select>
    <select id="countRepliedNum" resultType="java.lang.Integer">
        SELECT COUNT(id_) FROM student_course_homework WHERE course_schedule_id_ = #{courseScheduleId} AND is_replied_ = 1 AND status_ = 1
    </select>
</mapper>
