<?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.zehong.system.mapper.TStaffMapper">
    
    <resultMap type="TStaffVo" id="TStaffResult">
        <result property="staffId"    column="staff_id"    />
        <result property="staffName"    column="staff_name"    />
        <result property="staffCode"    column="staff_code"    />
        <result property="sex"    column="sex"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="postId"    column="post_id"    />
        <result property="postName"    column="post_name"    />
        <result property="roleId"    column="role_id"    />
        <result property="roleName"    column="role_name"    />
        <result property="positionalTitles"    column="positional_titles"    />
        <result property="profession"    column="profession"    />
        <result property="specialOperators"    column="special_operators"    />
        <result property="certificateUrl"    column="certificate_url"    />
        <result property="certificateName"    column="certificate_name"    />
        <result property="effectiveDate"    column="effective_date"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <select id="getStaffCode" resultType="String">
        select nextval('seq_staff_code') as staff_code
    </select>

    <sql id="selectTStaffVo">
        select t.staff_id, t.staff_name, t.staff_code, t.sex, t.dept_id, t.phonenumber, t.post_id, t.role_id, t.positional_titles, t.profession, t.special_operators, t.certificate_url, t.certificate_name, t.effective_date, t.create_time, t.update_time, t.remark, t.is_del,
               d.dept_name, r.role_name, p.post_name
        from t_staff t
		left join sys_dept d on t.dept_id = d.dept_id
		left join sys_role r on t.role_id = r.role_id
		left join sys_post p on t.post_id = p.post_id
    </sql>

    <select id="selectTStaffList" parameterType="TStaffForm" resultMap="TStaffResult">
        <include refid="selectTStaffVo"/>
        <where>  t.is_del = '0'
            <if test="staffName != null  and staffName != ''"> and t.staff_name like concat('%', #{staffName}, '%')</if>
            <if test="staffCode != null  and staffCode != ''"> and t.staff_code = #{staffCode}</if>
            <if test="sex != null  and sex != ''"> and t.sex = #{sex}</if>
            <if test="deptId != null "> and t.dept_id = #{deptId}</if>
            <if test="phonenumber != null and phonenumber != ''">
                AND t.phonenumber like concat('%', #{phonenumber}, '%')
            </if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(t.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(t.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
    </select>
    
    <select id="selectTStaffById" parameterType="Long" resultMap="TStaffResult">
        <include refid="selectTStaffVo"/>
        where t.staff_id = #{staffId}
    </select>
        
    <insert id="insertTStaff" parameterType="TStaff">
        insert into t_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="staffName != null">staff_name,</if>
            <if test="staffCode != null">staff_code,</if>
            <if test="sex != null">sex,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="phonenumber != null">phonenumber,</if>
            <if test="postId != null">post_id,</if>
            <if test="roleId != null">role_id,</if>
            <if test="positionalTitles != null">positional_titles,</if>
            <if test="profession != null">profession,</if>
            <if test="specialOperators != null">special_operators,</if>
            <if test="certificateUrl != null">certificate_url,</if>
            <if test="certificateName != null">certificate_name,</if>
            <if test="effectiveDate != null">effective_date,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="staffName != null">#{staffName},</if>
            <if test="staffCode != null">#{staffCode},</if>
            <if test="sex != null">#{sex},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="phonenumber != null">#{phonenumber},</if>
            <if test="postId != null">#{postId},</if>
            <if test="roleId != null">#{roleId},</if>
            <if test="positionalTitles != null">#{positionalTitles},</if>
            <if test="profession != null">#{profession},</if>
            <if test="specialOperators != null">#{specialOperators},</if>
            <if test="certificateUrl != null">#{certificateUrl},</if>
            <if test="certificateName != null">#{certificateName},</if>
            <if test="effectiveDate != null">#{effectiveDate},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateTStaff" parameterType="TStaff">
        update t_staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="staffName != null">staff_name = #{staffName},</if>
            <if test="staffCode != null">staff_code = #{staffCode},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
            <if test="postId != null">post_id = #{postId},</if>
            <if test="roleId != null">role_id = #{roleId},</if>
            <if test="positionalTitles != null">positional_titles = #{positionalTitles},</if>
            <if test="profession != null">profession = #{profession},</if>
            <if test="specialOperators != null">special_operators = #{specialOperators},</if>
            <if test="certificateUrl != null">certificate_url = #{certificateUrl},</if>
            <if test="certificateName != null">certificate_name = #{certificateName},</if>
            effective_date = #{effectiveDate},
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where staff_id = #{staffId}
    </update>

    <delete id="deleteTStaffById" parameterType="Long">
        delete from t_staff where staff_id = #{staffId}
    </delete>

    <delete id="deleteTStaffByIds" parameterType="String">
        delete from t_staff where staff_id in 
        <foreach item="staffId" collection="array" open="(" separator="," close=")">
            #{staffId}
        </foreach>
    </delete>
</mapper>