<?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.PracticeLessonApplyDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.PracticeLessonApply" id="PracticeLessonApply">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="create_time_" property="createTime" />
		<result column="memo_" property="memo" />
	</resultMap>


	<!-- 全查询 -->
	<select id="findAll" resultMap="PracticeLessonApply">
		SELECT * FROM practice_lesson_apply
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.PracticeLessonApply" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" > 
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL 
		</selectKey>
		-->
		INSERT INTO practice_lesson_apply (id_,user_id_,create_time_,memo_) VALUES(#{id},#{userId},#{createTime},#{memo})
	</insert>
	
	
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="PracticeLessonApply" parameterType="map">
		SELECT * FROM practice_lesson_apply <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM practice_lesson_apply
	</select>
    <select id="findByUserId" resultMap="PracticeLessonApply">
		SELECT * FROM practice_lesson_apply WHERE user_id_ = #{userId} LIMIT 1 FOR UPDATE
	</select>
	<resultMap id="practiceSumMap" type="com.ym.mec.biz.dal.dto.PracticeSumDto">
		<result property="organName" column="organName"/>
		<result property="per" column="per"/>
		<result property="activeNum" column="activeNum"/>
		<result property="makeNum" column="makeNum"/>
		<result property="totalNum" column="totalNum"/>
	</resultMap>
    <select id="practiceSum" resultMap="practiceSumMap">
		SELECT o.name_ organName,COUNT(su.id_) totalNum,COUNT(ps.id_) activeNum,COUNT(pla.user_id_) makeNum,COUNT(pla.user_id_) / COUNT(su.id_) per FROM sys_user su
		LEFT JOIN practice_lesson_apply pla ON pla.user_id_ = su.id_
		LEFT JOIN organization o ON o.id_ = su.organ_id_
		LEFT JOIN sys_user ps ON ps.id_ = su.id_ AND ps.password_ IS NOT NULL
		WHERE su.user_type_ LIKE '%STUDENT%' AND su.organ_id_ NOT IN (36,38,39) AND su.id_ IN
		<foreach collection="userIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
		GROUP BY su.organ_id_
		ORDER BY per DESC
	</select>
	<select id="getTotalNum" resultType="java.lang.Integer">
		SELECT COUNT(su.id_) FROM sys_user su
		WHERE su.user_type_ LIKE '%STUDENT%' AND su.organ_id_ NOT IN (36,38,39) AND su.id_ IN
		<foreach collection="userIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
	</select>
	<select id="getActiveNum" resultType="java.lang.Integer">
		SELECT COUNT(su.id_) FROM sys_user su
		WHERE su.user_type_ LIKE '%STUDENT%' AND su.password_ IS NOT NULL AND su.organ_id_ NOT IN (36,38,39) AND su.id_ IN
		<foreach collection="userIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
	</select>
	<select id="getMakeNum" resultType="java.lang.Integer">
		SELECT COUNT(id_) FROM practice_lesson_apply WHERE user_id_ IN
		<foreach collection="userIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
	</select>
	<select id="checkUserIsApply" resultType="int">
		SELECT COUNT(user_id_) FROM practice_lesson_apply WHERE user_id_=#{userId}
	</select>
</mapper>
