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

    <resultMap type="com.ym.mec.biz.dal.entity.Group" id="Group">
        <result column="organ_id_" property="organId"/>
        <result column="organ_name_" property="organName"/>
        <result column="group_id_" property="id"/>
        <result column="group_name_" property="groupName"/>
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="class_group_name_" property="classGroupName"/>
    </resultMap>

    <resultMap id="GroupBaseCourseInfoDto" type="com.ym.mec.biz.dal.dto.GroupBaseCourseInfoDto" extends="Group">
        <result column="student_num_" property="studentNum" javaType="java.lang.Integer"/>
        <result column="total_class_times_" property="totalClassTimes" javaType="java.lang.Integer"/>
        <result column="current_class_times_" property="currentClassTimes" javaType="java.lang.Integer"/>
        <result column="first_classes_start_time_" property="firstClassesStartTime"/>
        <result column="last_classes_end_time_" property="lastClassesEndTime"/>
    </resultMap>

    <sql id="searchMusicGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
            <if test="musicGroupStatus!=null">
                AND g.status_=#{musicGroupStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <sql id="searchVipGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
            <if test="vipGroupStatus!=null">
                AND g.group_status_=#{vipGroupStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <sql id="searchPracticeGroupsCondition">
        <where>
            <if test="search!=null">
                AND (g.id_ = #{search} OR g.name_ LIKE CONCAT( '%', #{search}, '%' ))
            </if>
            <if test="organIdList!=null">
                AND FIND_IN_SET(g.organ_id_,#{organIdList})
            </if>
        </where>
    </sql>

    <delete id="deleteGroup">
        DELETE FROM
        <if test="groupType!=null and groupType=='MUSIC'.toString()">
            music_group
        </if>
        <if test="groupType!=null and groupType=='VIP'.toString()">
            vip_group
        </if>
        <if test="groupType!=null and groupType=='PRACTICE'.toString()">
            practice_group
        </if>
        WHERE id_=#{groupId}
    </delete>

    <select id="searchGroups" resultMap="Group">
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,g.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM	music_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='MUSIC'
        <if test="search!=null">
            AND (g.name_ LIKE CONCAT( '%', #{search}, '%' ) OR g.id_ LIKE CONCAT( '%', #{search}, '%' ))
        </if>
        <if test="organIdList!=null">
            AND FIND_IN_SET(g.organ_id_,#{organIdList})
        </if>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM	vip_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='VIP'
        <if test="search!=null">
            AND (g.name_ LIKE CONCAT( '%', #{search}, '%' ) OR g.id_ LIKE CONCAT( '%', #{search}, '%' ))
        </if>
        <if test="organIdList!=null">
            AND FIND_IN_SET(g.organ_id_,#{organIdList})
        </if>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM	practice_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='PRACTICE'
        <if test="search!=null">
            AND (g.name_ LIKE CONCAT( '%', #{search}, '%' ) OR g.id_ LIKE CONCAT( '%', #{search}, '%' ))
        </if>
        <if test="organIdList!=null">
            AND FIND_IN_SET(g.organ_id_,#{organIdList})
        </if>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM courses_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='COMM'
        <if test="search!=null">
            AND (g.name_ LIKE CONCAT( '%', #{search}, '%' ) OR g.id_ LIKE CONCAT( '%', #{search}, '%' ))
        </if>
        <if test="organIdList!=null">
            AND FIND_IN_SET(g.organ_id_,#{organIdList})
        </if>
    </select>
    <select id="findByClassGroupIds" resultMap="Group">
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,g.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM music_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='MUSIC'
        AND cg.id_ IN
        <foreach collection="classGroupIds" item="classGroupId" separator="," open="(" close=")">
            #{classGroupId}
        </foreach>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM vip_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='VIP'
        AND cg.id_ IN
        <foreach collection="classGroupIds" item="classGroupId" separator="," open="(" close=")">
            #{classGroupId}
        </foreach>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM	practice_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='PRACTICE'
        AND cg.id_ IN
        <foreach collection="classGroupIds" item="classGroupId" separator="," open="(" close=")">
            #{classGroupId}
        </foreach>
        UNION ALL
        SELECT cg.music_group_id_ group_id_,cg.group_type_,cg.id_ class_group_id_,cg.name_ group_name_,g.organ_id_,o.name_ organ_name_,cg.name_ class_group_name_
        FROM courses_group g
        LEFT JOIN class_group cg ON cg.music_group_id_=g.id_
        LEFT JOIN organization o ON o.id_=g.organ_id_
        WHERE cg.group_type_='COMM'
        AND cg.id_ IN
        <foreach collection="classGroupIds" item="classGroupId" separator="," open="(" close=")">
            #{classGroupId}
        </foreach>
    </select>

    <select id="countGroupTotalClassesTimes" resultMap="GroupBaseCourseInfoDto">
        SELECT
            music_group_id_ group_id_,
            COUNT(id_) total_class_times_,
            MIN( CONCAT( class_date_, ' ', start_class_time_ ) ) first_classes_start_time_,
            MAX(CONCAT(class_date_, ' ', end_class_time_)) last_classes_end_time_
        FROM course_schedule
        WHERE group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND music_group_id_ IN
        <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
        GROUP BY music_group_id_;
    </select>

    <select id="countGroupFinishedClassesTimes" resultMap="GroupBaseCourseInfoDto">
        SELECT music_group_id_ group_id_,COUNT(id_) current_class_times_ FROM course_schedule
        WHERE group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND music_group_id_ IN
        <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
        AND CONCAT(class_date_, ' ', start_class_time_)&lt;NOW()
        GROUP BY music_group_id_;
    </select>

    <select id="countGroupStudentNum" resultMap="GroupBaseCourseInfoDto">
        SELECT  music_group_id_ group_id_,COUNT(id_) student_num_
        FROM class_group_student_mapper
        WHERE group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND status_='NORMAL'
        AND music_group_id_ IN
        <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
        GROUP BY music_group_id_;
    </select>

    <resultMap id="GroupUserDto" type="com.ym.mec.biz.dal.dto.GroupUserDto">
        <result property="groupId" column="group_id_"/>
        <result property="groupType" column="group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result property="userId" column="user_id_"/>
        <result property="userName" column="username_"/>
    </resultMap>

    <select id="findGroupsStudents" resultMap="GroupUserDto">
        SELECT
          music_group_id_ group_id_,
          group_type_,
          cgsm.user_id_,
          su.username_
        FROM class_group_student_mapper cgsm
        LEFT JOIN sys_user su ON cgsm.user_id_=su.id_
        WHERE group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        AND status_ IN ('NORMAL', 'QUIT_SCHOOL')
        AND music_group_id_ IN
        <foreach collection="groupIds" item="groupId" open="(" close=")" separator=",">
            #{groupId}
        </foreach>
    </select>

</mapper>
