<?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.EmployeeDao">
	
	<resultMap type="com.keao.edu.user.entity.Employee" id="Employee">
		<result column="user_id_" property="userId" />
		<result column="job_nature_" property="jobNature" />
		<result column="education_background_" property="educationBackground" />
		<result column="graduate_school_" property="graduateSchool" />
		<result column="technical_titles_" property="technicalTitles" />
		<result column="entry_date_" property="entryDate" />
		<result column="certificate_type_" property="certificateType" />
		<result column="certificate_num_" property="certificateNum" />
		<result column="update_time_" property="updateTime" />
		<result column="create_time_" property="createTime" />
		<result column="introduction_" property="introduction" />
		<result column="demission_date_" property="demissionDate" />
		<result column="contact_address_" property="contactAddress" />
		<result column="postal_code_" property="postalCode" />
		<result column="role_name_" property="roleName" />
		<result column="del_flag_" property="delFlag" />
		<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="Employee" >
		SELECT e.*,su.*,sur.role_id_ FROM employee e
		LEFT JOIN sys_user su ON e.user_id_ = su.id_
		LEFT JOIN sys_user_role sur ON e.user_id_ = sur.user_id_
		WHERE e.user_id_ = #{userId}
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="Employee">
		SELECT * FROM employee WHERE tenant_id_ = #{tenantId} ORDER BY user_id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.keao.edu.user.entity.Employee" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO employee (user_id_,job_nature_,education_background_,graduate_school_,technical_titles_,entry_date_,
		certificate_type_,certificate_num_,update_time_,create_time_,introduction_,demission_date_,contact_address_,postal_code_,tenant_id_,organ_id_)
		VALUES(#{userId},#{jobNature},#{educationBackground},#{graduateSchool},#{technicalTitles},#{entryDate},#{certificateType},
		#{certificateNum},NOW(),NOW(),#{introduction},#{demissionDate},#{contactAddress},#{postalCode},#{tenantId},#{organId})
	</insert>
	
	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.keao.edu.user.entity.Employee">
		UPDATE employee
		<set>
            <if test="graduateSchool != null">
                graduate_school_ = #{graduateSchool},
            </if>
            <if test="introduction != null">
                introduction_ = #{introduction},
            </if>
            <if test="postalCode != null">
                postal_code_ = #{postalCode},
            </if>
            <if test="technicalTitles != null">
                technical_titles_ = #{technicalTitles},
            </if>
            <if test="entryDate != null">
                entry_date_ = #{entryDate},
            </if>
            <if test="contactAddress != null">
                contact_address_ = #{contactAddress},
            </if>
            <if test="jobNature != null">
                job_nature_ = #{jobNature},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="certificateType != null">
                certificate_type_ = #{certificateType},
            </if>
            <if test="educationBackground != null">
                education_background_ = #{educationBackground},
            </if>
            <if test="certificateNum != null">
                certificate_num_ = #{certificateNum},
            </if>
            <if test="demissionDate != null">
                demission_date_ = #{demissionDate},
            </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>
	<!-- 根据主键删除一条记录 -->
	<update id="delete" >
		UPDATE employee SET del_flag_ = 1,update_time_ = NOW() WHERE user_id_ = #{userId}
	</update>
	<sql id="employeeQueryPage">
		<where>
				e.del_flag_ = 0 AND e.tenant_id_ = #{tenantId} AND e.user_id_ != #{userId}
			<if test="search != null and search != ''">
				AND (e.user_id_ = #{search} OR su.phone_ LIKE CONCAT('%',#{search},'%') OR su.real_name_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="roleId != null">
				AND sur.role_id_ = #{roleId}
			</if>
		</where>
	</sql>
	
	<!-- 分页查询 -->
	<select id="queryPage" resultMap="Employee" parameterType="map">
		SELECT su.*,e.*,GROUP_CONCAT(sr.role_name_) role_name_ FROM employee e
		LEFT JOIN sys_user su ON e.user_id_ = su.id_
		LEFT JOIN sys_user_role sur ON sur.user_id_ = su.id_
		LEFT JOIN sys_role sr ON sr.id_ = sur.role_id_
		<include refid="employeeQueryPage"/>
		GROUP BY e.user_id_
		ORDER BY e.user_id_ <include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(DISTINCT e.user_id_) FROM employee e
		LEFT JOIN sys_user su ON e.user_id_ = su.id_
		LEFT JOIN sys_user_role sur ON sur.user_id_ = su.id_
		<include refid="employeeQueryPage"/>
	</select>
</mapper>
