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

    <resultMap type="com.ym.mec.biz.dal.entity.VipGroup" id="VipGroup">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="user_id_" property="userId"/>
        <result column="username_" property="userName"/>
        <result column="single_class_minutes_" property="singleClassMinutes"/>
        <result column="payment_expire_date_" property="paymentExpireDate"/>
        <result column="courses_expire_date_" property="coursesExpireDate"/>
        <result column="registration_start_time_" property="registrationStartTime"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="teacher_school_id_" property="teacherSchoolId"/>
        <result column="online_classes_num_" property="onlineClassesNum"/>
        <result column="offline_classes_num_" property="offlineClassesNum"/>
        <result column="audit_status_" property="auditStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="organ_id_list_" property="organIdList"/>
        <result column="vip_group_activity_id_" property="vipGroupActivityId"/>
        <result column="vip_group_activity_name_" property="vipGroupActivityName"/>
        <result column="group_status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="online_classes_unit_price_" property="onlineClassesUnitPrice"/>
        <result column="offline_classes_unit_price_" property="offlineClassesUnitPrice"/>
        <result column="total_price_" property="totalPrice"/>
        <result column="student_num_" property="studentNum"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="vip_group_category_id_" property="vipGroupCategoryId"/>
        <result column="give_teach_mode_" property="giveTeachMode"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="organ_id_" property="organId"/>
        <result column="stop_reason_" property="stopReason"/>
        <result column="memo_" property="memo"/>
        <result column="courses_start_date" property="courseStartDate"/>
        <result column="course_schedules_json_" property="courseSchedulesJson"/>
        <result column="student_id_list_" property="studentIdList"/>
        <result column="organ_name_" property="organName"/>
        <result column="school_name_" property="schoolName"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="educational_teacher_id_" property="educationalTeacherId"/>
        <result column="vip_group_category_name_" property="vipGroupCategoryName"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.entity.School" id="School">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="name_" property="name"/>
        <result column="organ_id_" property="organId"/>
        <result column="address_" property="address"/>
        <result column="contact_phone_" property="contactPhone"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="contact_name_" property="contactName"/>
        <result column="longitude_latitude_" property="longitudeLatitude"/>
        <result column="subsidy_" property="subsidy"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="remark_" property="remark"/>
    </resultMap>

    <resultMap id="studentManageVipGroup" type="com.ym.mec.biz.dal.dto.StudentManageVipGroupDto">
        <result property="teachMode" column="teach_mode_"/>
        <result property="realName" column="real_name_"/>
        <result property="studentNum" column="student_num_"/>
        <result property="totalClassTimes" column="total_class_times_"/>
        <result property="currentClassTimes" column="current_class_times_"/>
        <result property="courseDate" column="course_date_"/>
        <result property="classGroupName" column="class_group_name_"/>
        <result property="courseStatus" column="course_status_"/>
    </resultMap>

    <resultMap id="vipCourseStudentInfoDto" type="com.ym.mec.biz.dal.dto.VipCourseStudentInfoDto">
        <result property="totalAmount" column="total_amount_"/>
        <result property="studentNum" column="student_num_"/>
        <result property="musicGroupId" column="music_group_id_"/>
        <result property="totalCourseTimes" column="total_times_"/>
    </resultMap>

    <resultMap id="vipGroupManageDetailDto" type="com.ym.mec.biz.dal.dto.VipGroupManageDetailDto" extends="VipGroup">
        <result property="studentNum" column="student_num_"/>
        <result property="subjectIdList" column="subject_id_list_"/>
        <result property="categoryName" column="category_name_"/>
        <result column="teacher_school_name_" property="teacherSchoolName"/>
        <!--<collection property="students" ofType="com.ym.mec.auth.api.entity.SysUser" column="student_id_">-->
        <!--<result property="id" column="student_id_"/>-->
        <!--<result property="username" column="username_"/>-->
        <!--</collection>-->
    </resultMap>

    <resultMap id="studentVipGroupDetailDto" type="com.ym.mec.biz.dal.dto.StudentVipGroupDetailDto"
               extends="studentVipGroup">
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="address_" property="address"/>
        <collection property="courseSchedules" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule"
                    column="cs.id_"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.StudentVipGroupShowListDto" id="studentVipGroup">
        <result column="id_" property="vipGroupId"/>
        <result column="name_" property="vipGroupName"/>
        <result column="single_class_minutes_" property="singleClassMinutes"/>
        <result column="payment_expire_date_" property="paymentExpireDate"/>
        <result column="online_classes_num_" property="onlineClassesNum"/>
        <result column="offline_classes_num_" property="offlineClassesNum"/>
        <result column="total_price_" property="totalPrice"/>
        <result column="expect_student_num_" property="expectStudentNum"/>
        <result column="student_num_" property="studentNum"/>
        <result column="user_id_" property="teacherId"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="introduction_" property="introduction"/>
        <result column="avatar_" property="avatar"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="attribute1_" property="attribute1"/>
        <result column="category_name_" property="categoryName"/>
        <result property="courseStartDate" column="courses_start_date"/>
    </resultMap>

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

    <select id="findStudentVipGroups" resultMap="studentManageVipGroup">
        SELECT CONCAT(cs.class_date_," ",cs.start_class_time_) course_date_,cs.teach_mode_,
        cs.status_ course_status_,cs.name_ class_group_name_
        FROM course_schedule_student_payment cssp
        LEFT JOIN course_schedule cs ON cssp.course_schedule_id_ = cs.id_
        WHERE cssp.group_type_ = 'VIP'
        <if test="vipGroupId != null">
            AND cssp.music_group_id_ = #{vipGroupId}
        </if>
        <if test="studentId != null">
            AND cssp.user_id_ = #{studentId}
        </if>
        <if test="classGroupName != null">
            AND cs.name_ LIKE CONCAT('%',#{classGroupName},'%')
        </if>
        ORDER BY course_date_
        <include refid="global.limit"/>
    </select>

    <select id="countStudentVipGroups" 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_
        WHERE cssp.group_type_ = 'VIP'
        <if test="vipGroupId != null">
            AND cssp.music_group_id_ = #{vipGroupId}
        </if>
        <if test="studentId != null">
            AND cssp.user_id_ = #{studentId}
        </if>
        <if test="classGroupName != null">
            AND cs.name_ LIKE CONCAT('%',#{classGroupName},'%')
        </if>
    </select>

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

    <sql id="vipGroupQueryCondition">
        <where>
            1=1
            AND vg.audit_status_='PASS' AND vg.group_status_ != 7
            <if test="search!=null and search!=''">
                AND (vg.name_ LIKE CONCAT('%',#{search},'%') OR vg.id_= #{search})
            </if>
            <if test="hasEducationalTeacherId != null and hasEducationalTeacherId == true">
                AND vg.educational_teacher_id_ IS NOT NULL
            </if>
            <if test="hasEducationalTeacherId != null and hasEducationalTeacherId == false">
                AND vg.educational_teacher_id_ IS NULL
            </if>
            <if test="teacherId!=null">
                AND vg.user_id_=#{teacherId}
            </if>
            <if test="status != null">
                AND vg.group_status_ = #{status}
            </if>
            <if test="activityId!=null">
                AND vg.vip_group_activity_id_=#{activityId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(vg.organ_id_,#{organId})
            </if>
            <if test="educationalTeacherId!=null">
                AND vg.educational_teacher_id_=#{educationalTeacherId}
            </if>
        </where>
    </sql>

    <select id="findAllByOrgan" resultMap="VipGroup">
        SELECT
        vg.*,
        vga.name_ vip_group_activity_name_,
        su.real_name_ username_,
        vgc.name_ vip_group_category_name_
        FROM
        vip_group vg
        LEFT JOIN sys_user su ON vg.user_id_=su.id_
        LEFT JOIN vip_group_activity vga ON vg.vip_group_activity_id_=vga.id_
        LEFT JOIN vip_group_category vgc ON vgc.id_=vg.vip_group_category_id_
        <include refid="vipGroupQueryCondition"/>
        ORDER BY vg.id_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="countVipGroupByOrgan" resultType="int">
        SELECT
        count(*)
        FROM
        vip_group vg
        <include refid="vipGroupQueryCondition"/>
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.VipGroup" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO vip_group
        (id_,user_id_,single_class_minutes_,payment_expire_date_,courses_expire_date_,create_time_,update_time_,teacher_school_id_,online_classes_num_,offline_classes_num_,registration_start_time_,name_,organ_id_list_,vip_group_activity_id_,group_status_,online_classes_unit_price_,offline_classes_unit_price_,total_price_,give_teach_mode_,vip_group_category_id_,organ_id_,stop_reason_,memo_,audit_status_,courses_start_date,course_schedules_json_,student_id_list_)
        VALUES(#{id},#{userId},#{singleClassMinutes},#{paymentExpireDate},#{coursesExpireDate},now(),now(),#{teacherSchoolId},#{onlineClassesNum},#{offlineClassesNum},#{registrationStartTime},#{name},#{organIdList},#{vipGroupActivityId},#{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{onlineClassesUnitPrice},#{offlineClassesUnitPrice},#{totalPrice},#{giveTeachMode},#{vipGroupCategoryId},#{organId},#{stopReason},#{memo},#{auditStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{courseStartDate},#{courseSchedulesJson},#{studentIdList})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.VipGroup">
        UPDATE vip_group
        <set>
            <if test="paymentExpireDate != null">
                payment_expire_date_ = #{paymentExpireDate},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="offlineClassesNum != null">
                offline_classes_num_ = #{offlineClassesNum},
            </if>
            <if test="onlineClassesNum != null">
                online_classes_num_ = #{onlineClassesNum},
            </if>
            update_time_ = NOW(),
            <if test="singleClassMinutes != null">
                single_class_minutes_ = #{singleClassMinutes},
            </if>
            <if test="teacherSchoolId != null">
                teacher_school_id_ = #{teacherSchoolId},
            </if>
            <if test="coursesExpireDate != null">
                courses_expire_date_ = #{coursesExpireDate},
            </if>
            <if test="registrationStartTime != null">
                registration_start_time_ = #{registrationStartTime},
            </if>
            <if test="organIdList!=null">
                organ_id_list_=#{organIdList},
            </if>
            <if test="auditStatus != null">
                audit_status_ = #{auditStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="vipGroupActivityId != null">
                vip_group_activity_id_ = #{vipGroupActivityId},
            </if>
            <if test="status != null">
                group_status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="onlineClassesUnitPrice != null">
                online_classes_unit_price_ = #{onlineClassesUnitPrice},
            </if>
            <if test="offlineClassesUnitPrice != null">
                offline_classes_unit_price_ = #{offlineClassesUnitPrice},
            </if>
            <if test="totalPrice != null">
                total_price_ = #{totalPrice},
            </if>
            <if test="giveTeachMode!=null">
                give_teach_mode_=#{giveTeachMode},
            </if>
            <if test="organId!=null">
                organ_id_=#{organId},
            </if>
            <if test="stopReason!=null">
                stop_reason_=#{stopReason},
            </if>
            <if test="memo!=null">
                memo_=#{memo},
            </if>
            <if test="courseSchedulesJson!=null">
                course_schedules_json_=#{courseSchedulesJson},
            </if>
            <if test="studentIdList!=null">
                student_id_list_=#{studentIdList},
            </if>
            <if test="educationalTeacherId!=null">
                educational_teacher_id_=#{educationalTeacherId},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

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

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

    <select id="getLockVipGroup" resultMap="VipGroup">
        SELECT *
        FROM vip_group
        WHERE id_ = #{vipGroupId} FOR
        UPDATE
    </select>

    <sql id="studentVipGroupQueryCondition">
        <where>
            vg.audit_status_='PASS'
            AND vg.group_status_=1
            AND vg.group_status_!=7
            AND vg.courses_expire_date_ &gt;= DATE_FORMAT(NOW(),'%Y%m%d')
            AND vg.registration_start_time_ &lt;= NOW()
            AND vg.courses_start_date &gt; NOW()
            AND (select count(1) as num from student_payment_order where vg.id_ = music_group_id_ AND user_id_=#{userId}
            AND status_='SUCCESS') = 0
            AND vg.organ_id_ = #{organId} AND IF(vg.student_id_list_ IS NULL,1,FIND_IN_SET(#{userId},
            vg.student_id_list_))
            <if test="subjectId!=null">
                AND FIND_IN_SET(#{subjectId},cg.subject_id_list_)
            </if>
            <if test="categoryId!=null">
                AND FIND_IN_SET(vg.vip_group_category_id_,#{categoryId})
            </if>
            <if test="classType!=null and classType.toString()=='0'.toString()">
                AND vg.offline_classes_num_>0
            </if>
            <if test="classType!=null and classType.toString()=='1'.toString()">
                AND vg.online_classes_num_>0
            </if>
            <if test="search!=null">
                AND vg.name_ like concat('%',#{search},'%')
            </if>
        </where>
    </sql>

    <select id="findVipGroups" resultMap="studentVipGroup">
        SELECT
        vg.id_,
        vg.name_,
        vg.single_class_minutes_,
        vg.payment_expire_date_,
        vg.courses_expire_date_,
        vg.courses_start_date,
        vg.online_classes_num_,
        vg.offline_classes_num_,
        vg.total_price_,
        cg.expect_student_num_,
        cg.student_num_,
        su.username_ teacher_name_,
        su.avatar_,
        t.introduction_,
        vga.type_,
        IF(vga.type_='GIVE_CLASS',vga.attribute2_,vga.attribute1_) attribute1_,
        vgc.name_ category_name_
        FROM
        vip_group vg
        LEFT JOIN class_group cg ON vg.id_=cg.music_group_id_ AND cg.group_type_ = 'VIP'
        LEFT JOIN teacher t ON vg.user_id_=t.id_
        LEFT JOIN sys_user su ON vg.user_id_=su.id_
        LEFT JOIN vip_group_activity vga ON vg.vip_group_activity_id_=vga.id_
        LEFT JOIN vip_group_category vgc ON vg.vip_group_category_id_=vgc.id_
        <include refid="studentVipGroupQueryCondition"/>
        ORDER BY vg.id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countVipGorups" resultType="int">
        SELECT
        count(*)
        FROM
        vip_group vg
        LEFT JOIN class_group cg ON vg.id_=cg.music_group_id_ AND cg.group_type_ = 'VIP'
        <include refid="studentVipGroupQueryCondition"/>
    </select>
    <select id="getVipGroupBaseInfo" resultMap="vipGroupManageDetailDto">
        SELECT vg.*,
               vga.name_     vip_group_activity_name_,
               su.real_name_ username_,
               cg.student_num_,
               cg.subject_id_list_,
               cg.total_class_times_,
               cg.current_class_times_,
               vgc.name_     category_name_,
               s.name_       teacher_school_name_
        FROM vip_group vg
                 LEFT JOIN sys_user su ON vg.user_id_ = su.id_
                 LEFT JOIN class_group cg ON vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
                 LEFT JOIN vip_group_activity vga ON vg.vip_group_activity_id_ = vga.id_
                 LEFT JOIN vip_group_category vgc ON vg.vip_group_category_id_ = vgc.id_
                 LEFT JOIN school s ON vg.teacher_school_id_ = s.id_
        WHERE vg.id_ = #{vipGroupId}
    </select>

    <sql id="vipGroupAttendanceQueryCondition">
        <where>
            <if test="vipGroupId!=null">
                and vg.id_=#{vipGroupId}
            </if>
            <if test="startTime != null">
                and CONCAT(cs.class_date_,' ',cs.end_class_time_) &gt; #{startTime}
            </if>
            <if test="endTime != null">
                and CONCAT(cs.class_date_,' ',cs.end_class_time_) &lt; #{endTime}
            </if>
            <if test="status != null">
                and cs.status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="complaintStatus != null">
                and cs.complaint_status_ = #{complaintStatus}
            </if>
        </where>
    </sql>
    <select id="findVipGroupAttendances" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleDao.CourseSchedule">
        SELECT
        cs.*
        FROM
        vip_group vg
        LEFT JOIN teacher_attendance ta ON vg.id_=ta.music_group_id_ AND ta.group_type_ = 'VIP'
        LEFT JOIN course_schedule cs ON ta.course_schedule_id_=cs.id_
        <include refid="vipGroupAttendanceQueryCondition"/>
        ORDER BY ta.id_
        <include refid="global.limit"/>
    </select>
    <select id="countVipGroupAttendance" resultType="int">
        SELECT
        count(*)
        FROM
        vip_group vg
        LEFT JOIN teacher_attendance ta ON vg.id_=ta.music_group_id_ AND ta.group_type_ = 'VIP'
        LEFT JOIN course_schedule cs ON ta.course_schedule_id_=cs.id_
        <include refid="vipGroupAttendanceQueryCondition"/>
        ORDER BY ta.id_
    </select>

    <select id="getVipGroupDetail" resultMap="studentVipGroupDetailDto">
        SELECT vg.*,
               cg.total_class_times_,
               cg.expect_student_num_,
               cg.student_num_,
               su.username_                                      teacher_name_,
               su.avatar_,
               t.introduction_,
               s.name_                                           address_,
               cs.id_                                            course_id_,
               cs.class_group_id_,
               cs.status_,
               cs.subsidy_,
               cs.class_date_,
               CONCAT(cs.class_date_, ' ', cs.start_class_time_) start_class_time_,
               CONCAT(cs.class_date_, ' ', cs.end_class_time_)   end_class_time_,
               cs.teacher_id_,
               cs.actual_teacher_id_,
               cs.create_time_,
               cs.update_time_,
               cs.teach_mode_,
               cs.type_,
               cs.name_
        FROM vip_group vg
                 LEFT JOIN class_group cg ON vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
                 LEFT JOIN course_schedule cs ON cg.id_ = cs.class_group_id_
                 LEFT JOIN school s ON vg.teacher_school_id_ = s.id_
                 LEFT JOIN teacher t ON vg.user_id_ = t.id_
                 LEFT JOIN sys_user su ON vg.user_id_ = su.id_
        WHERE vg.id_ = #{vipGroupId}
        ORDER BY start_class_time_
    </select>
    <select id="countSurplusClassTimes" resultType="java.util.Map">
        SELECT cs.teach_mode_ as 'key',
               COUNT(cs.id_)  as 'value'
        FROM course_schedule cs
        WHERE cs.status_ = "NOT_START"
          AND cs.type_ = 'VIP'
          AND cs.music_group_id_ = #{vipGroupId}
          AND cs.group_type_ = 'VIP'
        GROUP BY cs.teach_mode_
    </select>

    <resultMap id="vipGroupStudentDto" type="com.ym.mec.biz.dal.dto.VipGroupStudentDto">
        <result column="id_" property="id"/>
        <result column="username_" property="userName"/>
        <result column="phone_" property="phone"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="course_balance_" property="courseSalary"/>
        <result column="create_time_" property="applyDate"/>
        <result column="update_time_" property="refundDate"/>
        <result column="student_status_" property="studentStatus"/>
    </resultMap>

    <select id="findVipGroupStudents" resultMap="vipGroupStudentDto">
        SELECT
        su.id_,
        su.username_,
        su.phone_,
        cg.total_class_times_,
        cg.current_class_times_,
        suaa.course_balance_,
        cgsm.create_time_,
        cgsm.update_time_,
        CASE cgsm.status_
        WHEN 'QUIT' THEN 1
        WHEN 'QUIT_SCHOOL' THEN 3
        ELSE 0
        END student_status_
        FROM
        class_group cg
        LEFT JOIN class_group_student_mapper cgsm ON cg.id_=cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
        LEFT JOIN sys_user_cash_account suaa ON cgsm.user_id_=suaa.user_id_
        WHERE cg.music_group_id_=#{vipGroupId}
        AND cg.group_type_='VIP'
        AND cgsm.user_id_ IS NOT NULL
        ORDER BY su.id_
        <include refid="global.limit"/>
    </select>

    <select id="findHaveCourseBalanceStudents" resultMap="vipGroupStudentDto">
        SELECT
        su.id_,
        su.username_,
        su.phone_,
        suca.course_balance_
        FROM
        sys_user_cash_account suca
        LEFT JOIN sys_user su ON suca.user_id_=su.id_
        WHERE
        suca.course_balance_ >0
        <if test="search!=null and search!=''">
            AND (su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
        </if>
        <include refid="global.limit"/>
    </select>
    <select id="countHaveCourseBalanceStudentNum" resultType="int">
        SELECT
        COUNT(1)
        FROM
        sys_user_cash_account suca
        LEFT JOIN sys_user su ON suca.user_id_=su.id_
        WHERE
        suca.course_balance_ >0
        <if test="search!=null and search!=''">
            AND (su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
        </if>
    </select>

    <select id="countVipGroupStudents" resultType="int">
        SELECT count(*)
        FROM class_group cg
                 LEFT JOIN class_group_student_mapper cgsm ON cg.id_ = cgsm.class_group_id_
        WHERE cg.music_group_id_ = #{vipGroupId}
          AND cg.group_type_ = 'VIP'
    </select>

    <resultMap id="teachingRecord" type="com.ym.mec.biz.dal.dto.VipGroupCourseSchduleRecordDto">
        <result column="id_" property="id"/>
        <result column="start_class_time_" property="startClassTime"/>
        <result column="end_class_time_" property="endClassTime"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="give_teach_mode_" property="giveTeachMode"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="is_salary_" property="isSalary" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="student_num_" property="attendanceNum"/>
        <result column="school_name_" property="schoolName"/>
        <result column="schoole_id_" property="schooleId"/>
        <result column="sign_in_status_" property="signInStatus"/>
        <result column="sign_out_status_" property="signOutStatus"/>
        <result column="sign_in_time_" property="signInTime"/>
        <result column="sign_out_time_" property="signOutTime"/>
    </resultMap>

    <sql id="teachingRecordQueryCondition">
        <where>
            cs.music_group_id_=#{vipGroupId} AND cs.group_type_='VIP'
            <if test="startTime!=null and endTime!=null">
                AND (cs.class_date_ BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="teachMode!=null and teachMode!=''">
                AND cs.teach_mode_=#{teachMode}
            </if>
            <if test="status!=null and status!=''">
                AND cs.status_=#{status}
            </if>
        </where>
    </sql>

    <select id="findTeachingRecord" resultMap="teachingRecord">
        SELECT
        cs.id_,
        CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
        CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
        cs.status_,
        cs.teach_mode_,
        s.name_ school_name_,
        csts.actual_salary_ IS NOT NULL is_salary_,
        cs.schoole_id_,
        IF(ta.sign_in_status_ IS NULL,3,ta.sign_in_status_) sign_in_status_,
        IF(ta.sign_out_status_ IS NULL,3,ta.sign_out_status_) sign_out_status_,
        ta.sign_in_time_,
        ta.sign_out_time_
        FROM
        course_schedule cs
        LEFT JOIN course_schedule_teacher_salary csts ON cs.id_=csts.course_schedule_id_
        left join school s on cs.schoole_id_ = s.id_
        LEFT JOIN teacher_attendance ta ON cs.id_ = ta.course_schedule_id_ AND ta.teacher_id_ = cs.actual_teacher_id_
        <include refid="teachingRecordQueryCondition"/>
        ORDER BY start_class_time_
        <include refid="global.limit"/>
    </select>
    <select id="countTeachingRecord" resultType="int">
        SELECT
        COUNT(*)
        FROM
        course_schedule cs
        <include refid="teachingRecordQueryCondition"/>
    </select>

    <select id="getClassTimes" resultType="com.ym.mec.biz.dal.dto.TeachingRecordBaseInfo">
        SELECT cg.total_class_times_   totalClassTimes,
               cg.current_class_times_ currentClassTimes
        FROM class_group cg
        WHERE cg.music_group_id_ = #{vipGroupId}
    </select>
    <select id="countIsSalaryNum" resultType="int">
        SELECT COUNT(*)
        FROM course_schedule cs
                 LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
        WHERE cs.music_group_id_ = #{vipGroupId}
          AND cs.group_type_ = 'VIP'
          AND csts.settlement_time_ IS NOT NULL
    </select>
    <select id="countComplaintsNum" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM course_schedule cs
                 LEFT JOIN course_schedule_complaints csc ON cs.id_ = csc.course_schedule_id_
        WHERE cs.music_group_id_ = #{vipGroupId}
          AND cs.group_type_ = 'VIP'
          AND csc.course_schedule_id_ IS NOT NULL
          AND csc.score_ &lt;= 2

    </select>
    <select id="getVipGroupSalaryInfo" resultType="com.ym.mec.biz.dal.dto.VipGroupSalaryBaseInfo">
        SELECT SUM(csts.expect_salary_) expectTotalSalary,
               SUM(csts.actual_salary_) totalSalary
        FROM vip_group vg
                 LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_ AND cs.group_type_ = 'VIP'
                 LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
        WHERE vg.id_ = #{vipGroupId}
          AND cs.id_ IS NOT NULL
    </select>
    <select id="countVipGroupDeductionNum" resultType="int">
        SELECT COUNT(csts.id_)
        FROM vip_group vg
                 LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_ AND cs.group_type_ = 'VIP'
                 LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
        WHERE vg.id_ = #{vipGroupId}
          AND cs.id_ != NULL
          AND csts.settlement_time_ != NULL
          AND csts.actual_salary_ &lt; csts.expect_salary_
    </select>

    <select id="countSurplusCourseFee" resultType="map">
        SELECT cssp.user_id_           as 'key',
               SUM(cssp.expect_price_) as 'value'
        FROM vip_group vg
                 LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_ AND cs.group_type_ = 'VIP'
                 LEFT JOIN course_schedule_student_payment cssp ON cs.id_ = cssp.course_schedule_id_
        WHERE vg.id_ = #{vipGroupId}
          AND cs.status_ = 'NOT_START'
        GROUP BY cssp.user_id_
    </select>
    <select id="countTeacherVipGroups" resultType="int">
        SELECT COUNT(vg.id_)
        FROM vip_group vg
        WHERE vg.user_id_ = #{teacherId}
          AND vg.group_status_ != 3
    </select>

    <select id="countTeacherVipClass" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT vg.id_)
        FROM vip_group vg
                 LEFT JOIN class_group cg ON cg.music_group_id_ = vg.id_ AND cg.group_type_ = 'VIP'
                 LEFT JOIN course_schedule cs ON cs.class_group_id_ = cg.id_
        WHERE cs.actual_teacher_id_ = #{teacherId}
          AND cs.type_ = 'VIP'
          AND cg.del_flag_ = 0
    </select>

    <resultMap type="com.ym.mec.biz.dal.dto.TeacherVipClassInfoDto" id="getTeacherVipClassMap">
        <result column="vip_group_id_" property="vipClassId"/>
        <result column="student_num_" property="studentNum"/>
        <result column="online_classes_unit_price_" property="onlineClassesUnitPrice"/>
        <result column="offline_classes_unit_price_" property="offlineClassesUnitPrice"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="courses_expire_date_" property="coursesExpireDate"/>
        <result column="payment_expire_date_" property="paymentExpireDate"/>
        <result column="create_time_" property="createDate"/>
        <result column="current_class_times_" property="currentClassTimes"/>
        <result column="total_class_times_" property="totalClassTimes"/>
        <result column="vip_group_name_" property="vipClassName"/>
        <result column="audit_status_" property="auditStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="vip_group_activity_id_" property="activityId"/>
    </resultMap>

    <select id="getTeacherVipClass" resultMap="getTeacherVipClassMap">
        SELECT vg.id_ vip_group_id_,vg.name_ vip_group_name_,vg.group_status_,vg.audit_status_,vg.create_time_,
        vg.payment_expire_date_,vg.courses_expire_date_,vg.online_classes_unit_price_,vg.offline_classes_unit_price_,
        cg.total_class_times_,cg.current_class_times_,cg.id_ class_group_id_,cg.student_num_,vg.vip_group_activity_id_
        FROM vip_group vg
        LEFT JOIN class_group cg ON vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
        LEFT JOIN course_schedule cs ON cs.class_group_id_ = cg.id_
        WHERE cs.actual_teacher_id_ = #{teacherId} AND cs.type_ = 'VIP' AND cg.del_flag_ = 0 GROUP BY vg.id_,cg.id_
        <include refid="global.limit"/>
    </select>

    <select id="querySchoolByUserId" resultMap="School">
        SELECT *
        from school
        where user_id_ = #{userId}
           or user_id_ is null
    </select>

    <select id="findByCourseSchedule" resultMap="VipGroup">
        SELECT vg.*
        FROM vip_group vg
                 LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_ AND cs.group_type_ = 'VIP'
        WHERE cs.id_ = #{courseScheduleId}
    </select>

    <select id="queryVipGroupNum" resultType="map">
        select count(*) total,sum(case when group_status_ = 2 then 1 else 0 end) applying_num_,sum(case when
        group_status_ = 4 then 1 else 0 end) finished_num_ from vip_group
        where organ_id_ not in (36,38)
        <if test="organId != null">
            and FIND_IN_SET(organ_id_,#{organId})
        </if>
    </select>

    <select id="queryCurrentMonthCoursesNum" resultType="int">
        select count(cs.id_) from course_schedule cs left join vip_group vg on cs.music_group_id_ = vg.id_ where
        cs.type_ in ('VIP')
        and DATE_FORMAT(cs.class_date_, '%Y%m' ) = DATE_FORMAT(CURDATE() , '%Y%m') and (cs.del_flag_ = 0 or cs.del_flag_
        is null) and vg.organ_id_ not in (36,38)
        <if test="organId != null">
            AND FIND_IN_SET(vg.organ_id_,#{organId})
        </if>
    </select>

    <select id="findNoCreateSuccessVipGroup" resultMap="VipGroup">
        SELECT *
        FROM vip_group
        WHERE (payment_expire_date_ &lt; DATE_FORMAT(NOW(), '%Y-%m-%d')
            OR courses_start_date &lt; NOW())
          AND group_status_ = 1
    </select>

    <select id="countStudentPaymentNum" resultType="int">
        SELECT COUNT(*)
        FROM course_schedule_student_payment cssp
        WHERE cssp.settlement_time_ IS NOT NULL
          AND cssp.class_group_id_ = #{classGroupId}
    </select>
    <select id="countStudentPaymentFee" resultType="java.math.BigDecimal">
        SELECT SUM(actual_price_)
        FROM course_schedule_student_payment cssp
        WHERE cssp.settlement_time_ IS NOT NULL
          AND cssp.class_group_id_ = #{classGroupId}
    </select>

    <select id="queryByIds" resultMap="VipGroup" parameterType="string">
        select *
        from vip_group
        where FIND_IN_SET(id_, #{idList})
    </select>
    <resultMap id="VipBuyResult" type="com.ym.mec.biz.dal.dto.VipBuyResultDto">
        <result property="vipGroupId" column="id_"/>
        <result property="vipGroupName" column="name_"/>
        <result property="teacherName" column="username_"/>
        <result property="eduTeacherName" column="eduTeacherName"/>
        <result property="classTimes" column="total_class_times_"/>
        <result property="startClassTime" column="courses_start_date"/>
        <result property="endClassTime" column="courses_expire_date_"/>
    </resultMap>
    <select id="getVipBuyResultInfo" resultMap="VipBuyResult">
        SELECT vg.id_,
               vg.name_,
               vg.courses_start_date,
               vg.courses_expire_date_,
               cg.total_class_times_,
               su.username_
        FROM vip_group vg
                 LEFT JOIN class_group cg ON vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
                 LEFT JOIN sys_user su ON vg.user_id_ = su.id_
        WHERE vg.id_ = #{vipGroupId}
    </select>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="vipGroupList" item="item" index="index" open="" close="" separator=";">
            UPDATE vip_group
            <set>
                <if test="item.paymentExpireDate != null">
                    payment_expire_date_ = #{item.paymentExpireDate},
                </if>
                <if test="item.userId != null">
                    user_id_ = #{item.userId},
                </if>
                <if test="item.offlineClassesNum != null">
                    offline_classes_num_ = #{item.offlineClassesNum},
                </if>
                <if test="item.onlineClassesNum != null">
                    online_classes_num_ = #{item.onlineClassesNum},
                </if>
                <if test="item.updateTime != null">
                    update_time_ = NOW(),
                </if>
                <if test="item.singleClassMinutes != null">
                    single_class_minutes_ = #{item.singleClassMinutes},
                </if>
                <if test="item.teacherSchoolId != null">
                    teacher_school_id_ = #{item.teacherSchoolId},
                </if>
                <if test="item.coursesExpireDate != null">
                    courses_expire_date_ = #{item.coursesExpireDate},
                </if>
                <if test="item.registrationStartTime != null">
                    registration_start_time_ = #{item.registrationStartTime},
                </if>
                <if test="item.organIdList!=null">
                    organ_id_list_=#{item.organIdList},
                </if>
                <if test="item.auditStatus != null">
                    audit_status_ = #{item.auditStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="item.organIdList != null">
                    organ_id_list_ = #{item.organIdList},
                </if>
                <if test="item.vipGroupActivityId != null">
                    vip_group_activity_id_ = #{item.vipGroupActivityId},
                </if>
                <if test="item.status != null">
                    group_status_ = #{item.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="item.onlineClassesUnitPrice != null">
                    online_classes_unit_price_ = #{item.onlineClassesUnitPrice},
                </if>
                <if test="item.offlineClassesUnitPrice != null">
                    offline_classes_unit_price_ = #{item.offlineClassesUnitPrice},
                </if>
                <if test="item.totalPrice != null">
                    total_price_ = #{item.totalPrice},
                </if>
                <if test="item.giveTeachMode!=null">
                    give_teach_mode_=#{item.giveTeachMode},
                </if>
                <if test="item.organId!=null">
                    organ_id_=#{item.organId},
                </if>
                <if test="item.stopReason!=null">
                    stop_reason_=#{item.stopReason},
                </if>
                <if test="item.auditStatus!=null">
                    audit_status_=#{item.auditStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                <if test="item.courseSchedulesJson!=null">
                    course_schedules_json_=#{item.courseSchedulesJson},
                </if>
                <if test="item.studentIdList!=null">
                    student_id_list_=#{item.studentIdList},
                </if>
            </set>
            WHERE id_ = #{item.id}
        </foreach>
    </update>

    <select id="queryRequiredOverList" resultMap="VipGroup">
        select vg.*
        from vip_group vg
                 left join class_group cg on vg.id_ = cg.music_group_id_ AND cg.group_type_ = 'VIP'
        where vg.group_status_ = 2
          and cg.total_class_times_ = cg.current_class_times_
          and cg.total_class_times_ &gt; 0
    </select>
    <select id="queryNormalStatusList" resultMap="VipGroup">
        SELECT *
        FROM vip_group
        WHERE group_status_ != 3
          AND group_status_ != 4
    </select>

    <select id="findVipGroupInfo" resultMap="VipGroup">
        SELECT vg.*, s.name_ subject_name_, s2.name_ school_name_
        FROM vip_group vg
                 LEFT JOIN class_group cg on vg.id_ = cg.music_group_id_
                 LEFT JOIN subject s on cg.subject_id_list_ = s.id_
                 LEFT JOIN school s2 on vg.teacher_school_id_ = s2.id_
        WHERE vg.id_ = #{id}
          AND cg.id_ = #{classGroupId}
          AND cg.group_type_ = 'VIP'
    </select>
    <select id="queryUserVipNames" resultType="java.util.Map">
        SELECT cgsm.user_id_ 'key',GROUP_CONCAT(vg.name_)'value' FROM class_group_student_mapper cgsm
        LEFT JOIN vip_group vg ON cgsm.music_group_id_ = vg.id_
        WHERE vg.group_status_ IN (2,4,6) AND cgsm.group_type_='VIP' AND cgsm.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        GROUP BY cgsm.user_id_
    </select>
    <select id="queryUserVipStatus" resultType="java.util.Map">
        SELECT cgsm.user_id_ 'key',GROUP_CONCAT(CASE WHEN cgsm.status_ = 'NORMAL' THEN '在读'
        WHEN cgsm.status_ = 'LEAVE' THEN '请假'
        WHEN cgsm.status_ = 'QUIT' THEN '退班'
        WHEN cgsm.status_ = 'QUIT_SCHOOL' THEN '休学' END) 'value'
        FROM class_group_student_mapper cgsm
        LEFT JOIN vip_group vg ON cgsm.music_group_id_ = vg.id_
        WHERE vg.group_status_ IN (2,4,6) AND cgsm.group_type_='VIP' AND cgsm.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        GROUP BY cgsm.user_id_
    </select>

    <select id="queryVipCourseStudentInfo" resultMap="vipCourseStudentInfoDto">
        SELECT cssp.music_group_id_,
               ifnull(sum(cssp.expect_price_), 0)         total_amount_,
               ifnull(count(DISTINCT (cssp.user_id_)), 0) student_num_,
               ifnull(count(cssp.id_), 0)                 total_times_
        FROM course_schedule_student_payment cssp
                 LEFT JOIN course_schedule cs on cs.id_ = cssp.course_schedule_id_
        WHERE cs.group_type_ = 'VIP'
        GROUP BY cssp.music_group_id_
    </select>

    <select id="countUserRepeatVipGroupInCourseStartEndTime" resultType="int">
        SELECT COUNT(id_)
        FROM vip_group
        WHERE user_id_ = #{userId}
          AND courses_start_date = #{courseStartDate}
          AND courses_expire_date_ = #{courseEndDate}
    </select>

    <select id="getGroupTeachers" resultMap="VipBuyResult">
        SELECT vg.id_,t.real_name_ username_,et.real_name_ eduTeacherName FROM vip_group vg
        LEFT JOIN sys_user t on vg.user_id_ = t.id_
        LEFT JOIN sys_user et on vg.educational_teacher_id_ = et.id_
        <where>
            <if test="groupIds != null">
                AND vg.id_ IN
                <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
                    #{groupId}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
