<?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="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" />
	</resultMap>

	<resultMap type="com.ym.mec.biz.dal.dto.PracticeGroupDto" id="PracticeGroupDto">
		<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" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<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_,single_class_minutes_,organ_id_,courses_start_date_,courses_expire_date_,create_time_,update_time_,memo_) VALUES(#{id},#{name},#{subjectId},#{userId},#{singleClassMinutes},#{organId},#{coursesStartDate},#{coursesExpireDate},NOW(),NOW(),#{memo})
	</insert>
	
	
	
	<!-- 分页查询 -->
	<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 class_group_student_mapper WHERE group_type_='PRACTICE' AND user_id_=#{userId}
    </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>
	<sql id="practiceGroupQueryCondition">
		<where>
			<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>
</mapper>
