<?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.TeacherDao">
	
	<resultMap type="com.keao.edu.user.entity.Teacher" id="Teacher">
		<result column="user_id_" property="userId" />
		<result column="title_" property="title" />
		<result column="subject_id_list_" property="subjectIdList" />
		<result column="salary_settlement_type_" property="salarySettlementType" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<result column="salary_" property="salary" />
		<result column="del_flag_" property="delFlag" typeHandler="com.keao.edu.common.dal.CustomEnumTypeHandler"/>
		<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="sysUser" resultMap="com.keao.edu.user.dao.SysUserDao.SysUser"/>
	</resultMap>

	<resultMap id="TeacherDto" type="com.keao.edu.user.dto.TeacherDto" extends="Teacher">
		<result property="subjectNames" column="subject_names_"/>
	</resultMap>

	<select id="get" resultMap="Teacher">
        SELECT t.*,su.* FROM teacher t
        LEFT JOIN sys_user su ON t.user_id_ = su.id_
        WHERE t.user_id_ = #{id}
    </select>

	<update id="update" parameterType="com.keao.edu.user.entity.Teacher">
		UPDATE teacher
		<set>
			<if test="title != null">
				title_ = #{title},
			</if>
			<if test="subjectIdList != null">
				subject_id_list_ = #{subjectIdList},
			</if>
			<if test="salarySettlementType != null">
				salary_settlement_type_ = #{salarySettlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="delFlag != null">
				del_flag_ = #{delFlag,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="salary != null">
				salary_ = #{salary},
			</if>
			<if test="tenantId != null">
				tenant_id_ = #{tenantId},
			</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 user_id_ = #{userId}
	</update>


	<!-- 全查询 -->
	<select id="findAll" resultMap="Teacher">
		SELECT * FROM teacher WHERE	tenant_id_ = #{tenantId}
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.Teacher" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO teacher (user_id_,title_,subject_id_list_,salary_settlement_type_,salary_,create_time_,update_time_,tenant_id_)
		VALUES(#{userId},#{title},#{subjectIdList},#{salarySettlementType,typeHandler=com.keao.edu.common.dal.CustomEnumTypeHandler},#{salary},NOW(),NOW(),#{tenantId})
	</insert>

	<update id="delete">
		UPDATE teacher SET del_flag_ = 1,update_time_ = NOW() WHERE user_id_ = #{id}
	</update>
	<sql id="teacherQueryPage">
		<where>
			t.del_flag_ = 0 AND FIND_IN_SET(#{tenantId},t.tenant_id_)
			<if test="search != null and search != ''">
				AND (t.user_id_ = #{search}
				OR su.real_name_ LIKE CONCAT('%',#{search},'%')
				OR su.phone_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="settlementType != null and settlementType != ''">
				AND t.salary_settlement_type_ = #{settlementType}
			</if>
		</where>
	</sql>
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="Teacher" parameterType="map">
		SELECT * FROM teacher t
		LEFT JOIN sys_user su ON t.user_id_ = su.id_
		<include refid="teacherQueryPage"/>
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT t.user_id_) FROM teacher t
		LEFT JOIN sys_user su ON t.user_id_ = su.id_
		<include refid="teacherQueryPage"/>
	</select>

    <select id="getWithTeachers" resultMap="Teacher">
		SELECT * FROM teacher WHERE user_id_ IN
		<foreach collection="teacherIds" item="teacherId" separator=",">
			(#{teacherId})
		</foreach>
	</select>
	<select id="queryTeacherSubjectNames" resultType="java.util.Map">
		SELECT t.user_id_ 'key',GROUP_CONCAT(s.name_) 'value' FROM teacher t
		LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,t.subject_id_list_)
		WHERE t.user_id_ IN
		<foreach collection="teacherIds" separator="," open="(" close=")" item="item">
			#{item}
		</foreach>
		GROUP BY t.user_id_
	</select>
</mapper>
