<?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.ExaminationBasicDao">
	
	<resultMap type="com.keao.edu.user.entity.ExaminationBasic" id="ExaminationBasic">
		<result column="id_" property="id" />
		<result column="name_" property="name" />
		<result column="exam_mode_" property="examMode" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="exam_location_id_list_" property="examLocationIdList" />
		<result column="exam_location_name_list_" property="examLocationNameList" />
		<result column="status_" property="status" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="enroll_start_time_" property="enrollStartTime" />
		<result column="enroll_end_time_" property="enrollEndTime" />
		<result column="expect_exam_start_time_" property="expectExamStartTime" />
		<result column="expect_exam_end_time_" property="expectExamEndTime" />
		<result column="actual_exam_start_time_" property="actualExamStartTime" />
		<result column="actual_exam_end_time_" property="actualExamEndTime" />
		<result column="poster_title_" property="posterTitle" />
		<result column="poster_profile_" property="posterProfile" />
		<result column="poster_background_img_" property="posterBackgroundImg" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<result column="memo_" property="memo" />
		<result column="organ_id_" property="organId"/>
		<result column="creator_id_" property="creatorId"/>
	</resultMap>

	<resultMap id="ExaminationBasicDto" type="com.keao.edu.user.dto.ExaminationBasicDto" extends="ExaminationBasic">
	</resultMap>

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

	<select id="getExaminationBasic" resultMap="ExaminationBasicDto" >
		SELECT * FROM examination_basic WHERE id_ = #{examId}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="ExaminationBasic">
		SELECT * FROM examination_basic WHERE tenant_id_=#{tenantId}
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExaminationBasic" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO examination_basic (id_,name_,exam_mode_,exam_location_id_list_,exam_location_name_list_,status_,enroll_start_time_,enroll_end_time_,
		expect_exam_start_time_,expect_exam_end_time_,actual_exam_start_time_,actual_exam_end_time_,
		poster_title_,poster_profile_,poster_background_img_,creator_id_,create_time_,update_time_,tenant_id_,memo_,organ_id_)
		VALUES(#{id},#{name},#{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{examLocationIdList},#{examLocationNameList},
		#{status,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{enrollStartTime},#{enrollEndTime},
		#{expectExamStartTime},#{expectExamEndTime},#{actualExamStartTime},#{actualExamEndTime},
		#{posterTitle},#{posterProfile},#{posterBackgroundImg},#{creatorId},NOW(),NOW(),#{tenantId},#{memo},#{organId})
	</insert>

	<update id="update" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation">
		UPDATE examination_basic
		<set>
			<if test="name != null">
				name_ = #{name},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="examLocationIdList != null">
				exam_location_id_list_ = #{examLocationIdList},
			</if>
			<if test="examLocationNameList != null">
				exam_location_name_list_ = #{examLocationNameList},
			</if>
			<if test="status != null">
				status_ = #{status,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="enrollStartTime != null">
				enroll_start_time_ = #{enrollStartTime},
			</if>
			<if test="enrollEndTime != null">
				enroll_end_time_ = #{enrollEndTime},
			</if>
			<if test="expectExamStartTime != null">
				expect_exam_start_time_ = #{expectExamStartTime},
			</if>
			<if test="expectExamEndTime != null">
				expect_exam_end_time_ = #{expectExamEndTime},
			</if>
			<if test="actualExamStartTime != null">
				actual_exam_start_time_ = #{actualExamStartTime},
			</if>
			<if test="actualExamEndTime != null">
				actual_exam_end_time_ = #{actualExamEndTime},
			</if>
			<if test="posterTitle != null">
				poster_title_ = #{posterTitle},
			</if>
			<if test="posterProfile != null">
				poster_profile_ = #{posterProfile},
			</if>
			<if test="posterBackgroundImg != null">
				poster_background_img_ = #{posterBackgroundImg},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="memo!=null">
				memo_=#{memo},
			</if>
			<if test="creatorId!=null">
				creator_id_=#{creatorId},
			</if>
			update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>

	<update id="batchUpdate" parameterType="com.keao.edu.user.entity.ExamOrganizationRelation">
		<foreach collection="exams" item="exam" separator=";">
			UPDATE examination_basic
			<set>
				<if test="exam.name != null">
					name_ = #{exam.name},
				</if>
				<if test="exam.examMode != null">
					exam_mode_ = #{exam.examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="exam.examLocationIdList != null">
					exam_location_id_list_ = #{exam.examLocationIdList},
				</if>
				<if test="exam.examLocationNameList != null">
					exam_location_name_list_ = #{exam.examLocationNameList},
				</if>
				<if test="exam.status != null">
					status_ = #{exam.status,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="exam.enrollStartTime != null">
					enroll_start_time_ = #{exam.enrollStartTime},
				</if>
				<if test="exam.enrollEndTime != null">
					enroll_end_time_ = #{exam.enrollEndTime},
				</if>
				<if test="exam.expectExamStartTime != null">
					expect_exam_start_time_ = #{exam.expectExamStartTime},
				</if>
				<if test="exam.expectExamEndTime != null">
					expect_exam_end_time_ = #{exam.expectExamEndTime},
				</if>
				<if test="exam.actualExamStartTime != null">
					actual_exam_start_time_ = #{exam.actualExamStartTime},
				</if>
				<if test="exam.actualExamEndTime != null">
					actual_exam_end_time_ = #{exam.actualExamEndTime},
				</if>
				<if test="exam.posterTitle != null">
					poster_title_ = #{exam.posterTitle},
				</if>
				<if test="exam.posterProfile != null">
					poster_profile_ = #{exam.posterProfile},
				</if>
				<if test="exam.posterBackgroundImg != null">
					poster_background_img_ = #{exam.posterBackgroundImg},
				</if>
				<if test="exam.tenantId != null">
					tenant_id_ = #{exam.tenantId},
				</if>
				<if test="exam.memo!=null">
					memo_=#{exam.memo},
				</if>
				<if test="exam.creatorId!=null">
					creator_id_=#{exam.creatorId},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{exam.id}
		</foreach>
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM examination_basic WHERE id_ = #{id}
	</delete>

	<sql id="queryCondition">
		<where>
			<if test="organIds!=null">
				AND organ_id_ IN
				<foreach collection="organIds" item="organId" separator="," open="(" close=")">
					#{organId}
				</foreach>
			</if>
			<if test="examStatus!=null">
				AND status_=#{examStatus}
			</if>
			<if test="search!=null">
				AND (id_=#{search} OR name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExaminationBasic" parameterType="map">
		SELECT * FROM examination_basic
		<include refid="queryCondition"/>
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM examination_basic
		<include refid="queryCondition"/>
	</select>

	<sql id="findExaminationBasicsCondition">
		<where>
			eb.status_!='DELETE'
			<if test="organId!=null">
				AND eor.organ_id_ = #{organId}
			</if>
			<if test="examStatus!=null">
				AND eb.status_=#{examStatus}
			</if>
			<if test="search!=null">
				AND (eb.id_=#{search} OR eb.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
		</where>
	</sql>

	<select id="findExaminationBasics" resultMap="ExaminationBasicDto" parameterType="map">
		SELECT DISTINCT eb.*
		FROM
		exam_organization_relation eor
		LEFT JOIN examination_basic eb ON eor.examination_basic_id_ = eb.id_
		<include refid="findExaminationBasicsCondition"/>
		ORDER BY id_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="countExaminationBasics" resultType="int">
		SELECT COUNT(DISTINCT eb.id_)
		FROM
			exam_organization_relation eor
		LEFT JOIN examination_basic eb ON eor.examination_basic_id_ = eb.id_
		<include refid="findExaminationBasicsCondition"/>
	</select>

    <select id="getNeedUpdateStatusExams" resultMap="ExaminationBasic">
		SELECT * FROM examination_basic WHERE status_ IN ('NOT_START', 'APPLYING', 'APPLIED', 'EXAM_ING')
	</select>

	<select id="getExams" resultMap="ExaminationBasicDto">
		SELECT * FROM examination_basic
		WHERE id_ IN
		<foreach collection="examIds" item="examId" separator="," open="(" close=")">
			#{examId}
		</foreach>
	</select>
    <select id="lockExam" resultMap="ExaminationBasic">
		SELECT id_,name_ FROM examination_basic WHERE id_=#{examId}
	</select>
</mapper>
