<?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.ExamTeacherSalaryDao">
	
	<resultMap type="com.keao.edu.user.entity.ExamTeacherSalary" id="ExamTeacherSalary">
		<result column="id_" property="id" />
		<result column="examination_basic_id_" property="examinationBasicId" />
		<result column="exam_mode_" property="examMode" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="teacher_id_" property="teacherId" />
		<result column="settlement_type_" property="settlementType" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler" />
		<result column="share_profit_amount_" property="shareProfitAmount" />
		<result column="total_invigilation_num_" property="totalInvigilationNum"/>
		<result column="total_invigilation_student_num_" property="totalInvigilationStudentNum"/>
		<result column="total_settlement_fee_" property="totalSettlementCost"/>
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="tenant_id_" property="tenantId" />
		<association property="teacher" columnPrefix="teacher_" resultMap="com.keao.edu.user.dao.SysUserDao.SysUser"/>
 	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="ExamTeacherSalary" >
		SELECT * FROM exam_teacher_salary WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="ExamTeacherSalary">
		SELECT * FROM exam_teacher_salary ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.ExamTeacherSalary" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_teacher_salary (id_,examination_basic_id_,exam_mode_,teacher_id_,settlement_type_,share_profit_amount_,
		total_invigilation_num_,total_invigilation_student_num_,total_settlement_fee_,create_time_,update_time_,tenant_id_)
		VALUES(#{id},#{examinationBasicId},#{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{teacherId},#{settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{shareProfitAmount},
		#{totalInvigilationNum},#{totalInvigilationStudentNum},#{totalSettlementCost},NOW(),NOW(),#{tenantId})
	</insert>

	<insert id="batchInsert" parameterType="com.keao.edu.user.entity.ExamTeacherSalary" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO exam_teacher_salary (examination_basic_id_,exam_mode_,teacher_id_,settlement_type_,share_profit_amount_,
		total_invigilation_num_,total_invigilation_student_num_,total_settlement_fee_,create_time_,update_time_,tenant_id_)
		VALUES
		<foreach collection="examTeacherSalaries" item="ets" separator=",">
			(#{ets.examinationBasicId},#{ets.examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{ets.teacherId},
			#{ets.settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{ets.shareProfitAmount},
			#{ets.totalInvigilationNum},#{ets.totalInvigilationStudentNum},#{ets.totalSettlementCost},NOW(),NOW(),#{ets.tenantId})
		</foreach>
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.ExamTeacherSalary">
		UPDATE exam_teacher_salary
		<set>
			<if test="examinationBasicId != null">
				examination_basic_id_ = #{examinationBasicId},
			</if>
			<if test="teacherId != null">
				teacher_id_ = #{teacherId},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</if>
			<if test="examMode != null">
				exam_mode_ = #{examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="shareProfitAmount != null">
				share_profit_amount_ = #{shareProfitAmount},
			</if>
			<if test="settlementType != null">
				settlement_type_ = #{settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="totalInvigilationNum != null">
				total_invigilation_num_ = #{totalInvigilationNum},
			</if>
			<if test="totalInvigilationStudentNum != null">
				total_invigilation_student_num_ = #{totalInvigilationStudentNum},
			</if>
			<if test="totalSettlementCost != null">
				total_settlement_fee_ = #{totalSettlementCost},
			</if>
				update_time_ = NOW()
		</set> WHERE id_ = #{id}
	</update>

	<update id="batchUpdate" parameterType="com.keao.edu.user.entity.ExamTeacherSalary">
		<foreach collection="teacherSalaries" item="ts" separator=";">
			UPDATE exam_teacher_salary
			<set>
				<if test="examinationBasicId != null">
					examination_basic_id_ = #{ts.examinationBasicId},
				</if>
				<if test="teacherId != null">
					teacher_id_ = #{ts.teacherId},
				</if>
				<if test="tenantId != null">
					tenant_id_ = #{ts.tenantId},
				</if>
				<if test="examMode != null">
					exam_mode_ = #{ts.examMode,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="shareProfitAmount != null">
					share_profit_amount_ = #{ts.shareProfitAmount},
				</if>
				<if test="settlementType != null">
					settlement_type_ = #{ts.settlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
				</if>
				<if test="totalInvigilationNum != null">
					total_invigilation_num_ = #{ts.totalInvigilationNum},
				</if>
				<if test="totalInvigilationStudentNum != null">
					total_invigilation_student_num_ = #{ts.totalInvigilationStudentNum},
				</if>
				<if test="totalSettlementCost != null">
					total_settlement_fee_ = #{ts.totalSettlementCost},
				</if>
				update_time_ = NOW()
			</set> WHERE id_ = #{id}
		</foreach>
	</update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM exam_teacher_salary WHERE id_ = #{id} 
	</delete>

	<sql id="queryPageCondition">
		<where>
			examination_basic_id_ = #{examId}
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="ExamTeacherSalary" parameterType="map">
		SELECT ets.*,su.real_name_ teacher_real_name_ FROM exam_teacher_salary ets
		LEFT JOIN sys_user su ON ets.teacher_id_=su.id_
		<include refid="queryPageCondition"/>
		ORDER BY id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM exam_teacher_salary ets
		<include refid="queryPageCondition"/>
	</select>

    <select id="queryWithExam" resultMap="ExamTeacherSalary">
		SELECT * FROM exam_teacher_salary WHERE examination_basic_id_=#{examId}
	</select>

    <select id="getTeachersWithExam" resultMap="com.keao.edu.user.dao.SysUserDao.BaseUserInfoDto">
		SELECT <include refid="com.keao.edu.user.dao.SysUserDao.BaseUserColumns"/>
		FROM exam_teacher_salary ets
		LEFT JOIN sys_user sys_user ON ets.teacher_id_=sys_user.id_
		WHERE ets.examination_basic_id_=#{examId}
	</select>

	<select id="getWithExamAndTeacher" resultMap="ExamTeacherSalary">
		SELECT ets.*,su.real_name_ teacher_real_name_ FROM exam_teacher_salary ets
		LEFT JOIN sys_user su ON ets.teacher_id_=su.id_
		WHERE ets.examination_basic_id_=#{examId} AND ets.teacher_id_ IN
		<foreach collection="teacherIds" item="teacherId" separator="," open="(" close=")">
			#{teacherId}
		</foreach>
	</select>

	<sql id="queryUnRelatedWithExamTeachersCondition">
		<where>
			t.tenant_id_=#{tenantId}
			AND NOT EXISTS ( SELECT teacher_id_ FROM exam_teacher_salary WHERE teacher_id_ = t.user_id_ AND examination_basic_id_ = #{examId} )
		</where>
	</sql>

	<select id="queryUnRelatedWithExamTeachers" resultMap="com.keao.edu.user.dao.TeacherDao.TeacherDto">
		SELECT
			t.*,
			su.real_name_ teacher_real_name_
		FROM
			teacher t
			LEFT JOIN sys_user su ON t.user_id_ = su.id_
		<include refid="queryUnRelatedWithExamTeachersCondition"/>
		ORDER BY t.user_id_
		<include refid="global.limit"/>
	</select>

	<select id="countUnRelatedWithExamTeachers" resultType="int">
		SELECT
			COUNT(t.user_id_)
		FROM
			teacher t
			LEFT JOIN sys_user su ON t.user_id_ = su.id_
		<include refid="queryUnRelatedWithExamTeachersCondition"/>
	</select>

</mapper>
