<?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.keao.edu.user.dao.StudentDao">
	
	<resultMap type="com.keao.edu.user.api.entity.Student" id="Student" extends="com.keao.edu.user.dao.SysUserDao.SysUser">
		<result column="user_id_" property="userId" />
		<result column="certificate_photo_" property="certificatePhoto" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="organ_id_" property="organId" />
		<association property="sysUser" resultMap="com.keao.edu.user.dao.SysUserDao.SysUser"/>
	</resultMap>

	<select id="get" resultMap="Student">
		SELECT * FROM student stu
		LEFT JOIN sys_user su ON su.id_=stu.user_id_
		WHERE stu.user_id_=#{id}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="Student">
		SELECT * FROM student WHERE tenant_id_=#{tenantId}
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.api.entity.Student" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO student (user_id_,certificate_photo_,create_time_,update_time_,tenant_id_,organ_id_)
		VALUES(#{userId},#{certificatePhoto},NOW(),NOW(),#{tenantId},#{organId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.api.entity.Student">
		UPDATE student
		<set>
			<if test="certificatePhoto != null">
				certificate_photo_ = #{certificatePhoto},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			update_time_ = NOW()
		</set> WHERE user_id_ = #{userId}
	</update>

	<delete id="delete" >
		DELETE FROM student WHERE user_id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			stu.tenant_id_ = #{tenantId}
			<if test="search != null and search != ''">
				AND (stu.user_id_ = #{search} OR su.real_name_ LIKE CONCAT (#{search}, '%'))
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="Student" parameterType="map">
		SELECT * FROM student stu
		LEFT JOIN sys_user su ON su.id_=stu.user_id_
		<include refid="queryCondition"/>
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM student stu
		LEFT JOIN sys_user su ON su.id_ = stu.user_id_
		<include refid="queryCondition"/>
	</select>
	<resultMap id="queryApplyListMap" type="com.keao.edu.user.dto.StudentExamPaymentDto">
		<association property="examRegistration" resultMap="com.keao.edu.user.dao.ExamRegistrationDao.ExamRegistration"/>
		<association property="studentExamResult" resultMap="com.keao.edu.user.dao.StudentExamResultDao.StudentExamResult"/>
		<association property="examRegistrationPayment" resultMap="com.keao.edu.user.dao.ExamRegistrationPaymentDao.ExamRegistrationPayment"/>
	</resultMap>
    <select id="queryApplyList" resultMap="queryApplyListMap">
		SELECT * FROM exam_registration er
		LEFT JOIN exam_registration_payment erp ON er.id_ = erp.exam_registration_id_
		LEFT JOIN student_exam_result ser ON ser.examination_basic_id_ = er.examination_basic_id_
		<include refid="queryApplyListSql"/>
		<include refid="global.limit"/>
	</select>
	<select id="countApplyList" resultType="java.lang.Integer">
		SELECT COUNT(er.id_) FROM exam_registration er
		LEFT JOIN exam_registration_payment erp ON er.id_ = erp.exam_registration_id_
		LEFT JOIN student_exam_result ser ON ser.examination_basic_id_ = er.examination_basic_id_
		<include refid="queryApplyListSql"/>
	</select>
	<sql id="queryApplyListSql">
		<where>
			er.student_id_ = erp.student_id_ AND er.student_id_ = ser.student_id_ AND erp.trans_status_ = 'SUCCESS'
			<if test="studentId != null">
				AND er.student_id_ = #{studentId}
			</if>
			<if test="tenantId != null">
				AND er.tenant_id_ = #{tenantId}
			</if>
		</where>
	</sql>
    <select id="getStudent" resultMap="Student">
		SELECT s.*,su.* FROM student s
		LEFT JOIN sys_user su ON s.user_id_ = su.id_
		WHERE user_id_ = #{userId}
	</select>
</mapper>
