<?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.ExamSubjectDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamSubject" id="ExamSubject">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="subject_id_" property="subjectId" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamSubject" >
		SELECT * FROM exam_subject WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamSubject">
		SELECT * FROM exam_subject ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamSubject" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_subject (id_,examination_basic_id_,subject_id_,create_time_,update_time_,tenant_id_)
		VALUES(#{id},#{examinationBasicId},#{subjectId},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="batchInsert">
		INSERT INTO exam_subject (examination_basic_id_,subject_id_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="examSubjects" item="examSubject" separator=",">
			(#{examSubject.examinationBasicId},#{examSubject.subjectId},NOW(),NOW(),#{examSubject.tenantId})
		</foreach>
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamSubject">
		UPDATE exam_subject <set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="subjectId != null">
				subject_id_ = #{subjectId},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
		<if test="tenantId != null and tenantId != 0">
				tenant_id_ = #{tenantId},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_subject WHERE id_ = #{id} 
	</delete>

    <delete id="deleteWithExamSubject">
		DELETE FROM exam_subject WHERE examination_basic_id_=#{examId} AND subject_id_=#{subjectId}
    </delete>

    <!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamSubject" parameterType="map">
		SELECT * FROM exam_subject ORDER BY id_
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_subject
	</select>
	<select id="getWithTenant" resultMap="ExamSubject">
		SELECT * FROM exam_subject WHERE examination_basic_id_=#{examId}
	</select>

	<resultMap id="ExamSubjectDto" type="com.keao.edu.user.dto.ExamSubjectDto" extends="com.keao.edu.user.dao.SubjectDao.Subject" >
		<result column="exam_subject_id_" property="examSubjectId" />
	</resultMap>
	<select id="getSubjectWithExamId" resultMap="ExamSubjectDto">
		SELECT es.id_ exam_subject_id_ ,s.* FROM exam_subject es
		LEFT JOIN subject s on es.subject_id_ = s.id_ WHERE examination_basic_id_ = #{examId}
	</select>

    <select id="getUnRelatedWithExamSubjects" resultMap="com.keao.edu.user.dao.SubjectDao.Subject">
		SELECT
			*
		FROM
			`subject` s
		WHERE
			s.parent_subject_id_ != 0
			AND s.del_flag_ = 0
			AND s.tenant_id_ = #{tenantId}
			AND NOT EXISTS (SELECT subject_id_ FROM exam_subject WHERE examination_basic_id_=#{examId} AND subject_id_=s.id_)
    </select>
</mapper>
