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

    <resultMap type="com.ym.mec.biz.dal.entity.PracticeGroup" id="PracticeGroup">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="user_id_" property="userId"/>
        <result column="student_id_" property="studentId"/>
        <result column="single_class_minutes_" property="singleClassMinutes"/>
        <result column="organ_id_" property="organId"/>
        <result column="courses_start_date_" property="coursesStartDate"/>
        <result column="courses_expire_date_" property="coursesExpireDate"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="memo_" property="memo"/>
        <result column="buy_months_" property="buyMonths"/>
        <result column="drill_times_on_week_" property="drillTimesOnWeek"/>
        <result column="drill_times_json_" property="drillTimesJson"/>
        <result column="group_status_" property="groupStatus"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="be_renew_group_id_" property="beRenewGroupId"/>
    </resultMap>

    <resultMap id="PracticeCourseDto" type="com.ym.mec.biz.dal.dto.PracticeCourseDto" extends="PracticeGroup">
        <result property="teacherName" column="real_name_"/>
        <result property="avatar" column="avatar_"/>
        <result property="subjectName" column="subject_name_"/>
    </resultMap>

    <resultMap type="com.ym.mec.biz.dal.dto.PracticeGroupDto" id="PracticeGroupDto" extends="PracticeGroup">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="user_id_" property="userId"/>
        <result column="single_class_minutes_" property="singleClassMinutes"/>
        <result column="organ_id_" property="organId"/>
        <result column="courses_start_date_" property="coursesStartDate"/>
        <result column="courses_expire_date_" property="coursesExpireDate"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="memo_" property="memo"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="teacher_name_" property="teacherName"/>
        <result column="evaluate_id_" property="evaluateId"/>
    </resultMap>
    <update id="updateUserId">
        UPDATE practice_group
        SET user_id_     = #{teacherId},
            update_time_ = NOW()
        WHERE id_ = #{practiceGroupId}
    </update>

    <update id="update" parameterType="com.ym.mec.biz.dal.entity.PracticeGroup">
        UPDATE practice_group
        <set>
            <if test="subjectId!=null">
                subject_id_=#{subjectId},
            </if>
            <if test="memo!=null">
                memo_=#{memo},
            </if>
            <if test="name!=null">
                name_=#{name},
            </if>
            <if test="groupStatus!=null">
                group_status_=#{groupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            update_time_ = NOW()
        </set>
        WHERE id_ = #{id}
    </update>

    <update id="batchUpdate">
        <foreach collection="groups" item="group" separator=";">
            UPDATE practice_group
            <set>
                <if test="group.groupStatus!=null">
                    group_status_=#{group.groupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                </if>
                update_time_ = NOW()
            </set>
            WHERE id_ = #{group.id}
        </foreach>
    </update>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.PracticeGroup" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO practice_group
        (id_,name_,subject_id_,user_id_,student_id_,single_class_minutes_,organ_id_,courses_start_date_,courses_expire_date_,create_time_,update_time_,memo_,buy_months_,drill_times_on_week_,drill_times_json_,group_status_,be_renew_group_id_)
        VALUES(#{id},#{name},#{subjectId},#{userId},#{studentId},#{singleClassMinutes},#{organId},#{coursesStartDate},#{coursesExpireDate},NOW(),NOW(),#{memo},#{buyMonths},#{drillTimesOnWeek},#{drillTimesJson},#{groupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{beRenewGroupId})
    </insert>

    <select id="getUserFreePracticeGroup" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND buy_months_ IS NULL
    </select>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="PracticeGroup" parameterType="map">
        SELECT * FROM practice_group
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM practice_group
    </select>
    <select id="getUserPracticeCoursesWithDateRange" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE user_id_ = #{userId}
          AND (courses_start_date_ BETWEEN DATE_FORMAT(#{startDate}, '%Y-%m-%d') AND DATE_FORMAT(#{endDate}, '%Y-%m-%d'))
    </select>
    <select id="countUserPracticeApplyRecord" resultType="int">
        SELECT COUNT(*)
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND group_status_ = 'NORMAL'
          AND buy_months_ IS NULL;
    </select>
    <select id="countPracticeGroupByOrgan" resultType="java.lang.Integer">
        SELECT count(*) FROM practice_group pg
        <include refid="practiceGroupQueryCondition"/>
    </select>
    <select id="findAllByOrgan" resultMap="PracticeGroupDto">
        SELECT pg.*,
        su.real_name_ teacher_name_
        FROM
        practice_group pg
        LEFT JOIN sys_user su ON pg.user_id_ = su.id_
        <include refid="practiceGroupQueryCondition"/>
        ORDER BY pg.id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countPracticeGroupOverCourse" resultType="java.util.Map">

    </select>

    <select id="findUserLatestPracticeGroup" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND buy_months_ IS NOT NULL
        ORDER BY create_time_ DESC
        LIMIT 1;
    </select>

    <select id="findUserBuyPracticeGroups" resultMap="PracticeCourseDto">
        SELECT pg.*,
               su.real_name_,
               su.avatar_,
               s.name_ subject_name_
        FROM practice_group pg
                 LEFT JOIN sys_user su ON pg.user_id_ = su.id_
                 LEFT JOIN `subject` s ON pg.subject_id_ = s.id_
        WHERE student_id_ = #{userId}
          AND (pg.group_status_ = 'NORMAL' OR pg.group_status_ = 'LOCK')
          AND pg.buy_months_ IS NOT NULL
    </select>

    <select id="findUserBuyPracticeGroupsWithDate" resultMap="PracticeCourseDto">
        SELECT pg.*,
               su.real_name_,
               su.avatar_,
               s.name_ subject_name_
        FROM practice_group pg
                 LEFT JOIN sys_user su ON pg.user_id_ = su.id_
                 LEFT JOIN `subject` s ON pg.subject_id_ = s.id_
        WHERE student_id_ = #{userId}
          AND courses_start_date_ = #{date}
          AND group_status_ = 'LOCK'
          AND pg.buy_months_ IS NOT NULL
    </select>
    <select id="findUserPracticeGroup" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND id_ = #{groupId};
    </select>
    <select id="findUserPracticeGroup2" resultMap="PracticeGroupDto">
        SELECT pg.*, su.real_name_ teacher_name_
        FROM practice_group pg
                 LEFT JOIN sys_user su ON pg.user_id_ = su.id_
        WHERE pg.student_id_ = #{userId}
          AND pg.id_ = #{groupId};
    </select>
    <select id="findHistoryPracticeGroups" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE courses_expire_date_ &lt; NOW()
          AND group_status_ = 'NORMAL'
    </select>

    <sql id="practiceGroupQueryCondition">
        <where>
            pg.group_status_='NORMAL'
            <if test="search!=null and search!=''">
                AND (pg.name_ LIKE CONCAT('%',#{search},'%') OR pg.id_= #{search})
            </if>
            <if test="teacherId!=null">
                AND pg.user_id_=#{teacherId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(pg.organ_id_,#{organId})
            </if>
        </where>
    </sql>

    <!-- 根据主键查询一条记录 -->
    <select id="lockPracticeGroup" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE id_ = #{groupId} FOR
        UPDATE
    </select>
    <select id="findUserStatusPracticeGroups" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND group_status_ = #{groupStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>
    <select id="findUserLockPracticeGroupWithDate" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND courses_start_date_ = #{date}
          AND group_status_ = 'LOCK'
    </select>
    <select id="findUserLockPracticeGroupWithDateBefore" resultMap="PracticeGroup">
        SELECT *
        FROM practice_group
        WHERE student_id_ = #{userId}
          AND courses_start_date_ &lt;= #{date}
          AND group_status_ = 'LOCK'
    </select>

    <sql id="practiceGroupReviewsQueryCondition">
        <where>
            pg.group_status_='NORMAL'
            <if test="search!=null and search!=''">
                AND (pg.name_ LIKE CONCAT('%',#{search},'%') OR pg.id_= #{search})
            </if>
            <if test="teacherId!=null">
                AND pg.user_id_=#{teacherId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(pg.organ_id_,#{organId})
            </if>
            <if test="month != null">
                AND pg.courses_expire_date_ >= #{month}
            </if>
            <if test='isFree !=null and isFree=="0"'>
                AND pg.buy_months_ > 0
            </if>
            <if test='isFree !=null and isFree=="1"'>
                AND pg.buy_months_ IS NULL
            </if>
            <if test='isOver !=null and isOver=="0"'>
                <![CDATA[AND pg.courses_expire_date_ > NOW()
			]]>
            </if>
            <if test='isOver !=null and isOver=="1"'>
                <![CDATA[ AND pg.courses_expire_date_ <= NOW()
			]]></if>
            <if test='hasReport !=null and hasReport=="0"'>
                <![CDATA[AND cse.id_ IS NULL
			]]>
            </if>
            <if test='hasReport !=null and hasReport=="1"'>
                <![CDATA[ AND cse.id_ > 0
			]]></if>
        </where>
    </sql>

    <select id="findPracticeGroupsReviews" resultMap="PracticeGroupDto">
        SELECT pg.*,cse.id_ evaluate_id_,
        su.real_name_ teacher_name_
        FROM
        practice_group pg
        LEFT JOIN sys_user su ON pg.user_id_ = su.id_
        LEFT JOIN course_schedule_evaluate cse ON pg.id_=cse.music_group_id_ AND DATE_FORMAT(cse.create_time_, '%Y%m') =
        DATE_FORMAT(#{month},'%Y%m')
        <include refid="practiceGroupReviewsQueryCondition"/>
        ORDER BY pg.id_ DESC
        <include refid="global.limit"/>
    </select>


    <select id="countPracticeGroupReviews" resultType="java.lang.Integer">
        SELECT count(*) FROM practice_group pg
        LEFT JOIN course_schedule_evaluate cse ON pg.id_=cse.music_group_id_ AND DATE_FORMAT(cse.create_time_, '%Y%m') =
        DATE_FORMAT(#{month},'%Y%m')
        <include refid="practiceGroupReviewsQueryCondition"/>
    </select>

    <select id="getNeedPostReportPracticeGroups"
            resultMap="com.ym.mec.biz.dal.dao.CourseScheduleEvaluateDao.CourseScheduleEvaluate">
        <![CDATA[
        SELECT pg.id_ music_group_id_, pg.user_id_ teacher_id_, cg.id_ class_group_id_
        FROM practice_group pg LEFT JOIN class_group cg on pg.id_ = cg.music_group_id_ AND cg.group_type_ = 'PRACTICE'
        WHERE pg.courses_start_date_ <= #{nowDate}
          AND pg.courses_expire_date_ >= #{afterDate}
          AND DATE_FORMAT(pg.courses_expire_date_, '%m%d') = DATE_FORMAT(#{afterDate}, '%m%d')
          AND cg.del_flag_ = 0
          AND pg.group_status_ != 'CANCEL'
          AND pg.group_status_ != 'LOCK'
        ]]>
    </select>
</mapper>
