<?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.user_id  as  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 sys_user 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">
        select  t.user_id  as  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 sys_user 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
        <where>  t.is_del = '0' and t.user_staff='1'
            <if test="staffName != null  and staffName != ''"> and t.staff_name like concat('%', #{staffName}, '%')</if>
            <if test="staffCode != null  and staffCode != ''"> and t.staff_code like concat('%', #{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>
        group by t.user_id desc
    </select>

    <select id="selectTStaffById" parameterType="Long" resultMap="TStaffResult">
        <include refid="selectTStaffVo"/>
        where t.user_id = #{staffId}
    </select>

    <insert id="insertTStaff" parameterType="TStaff">
        insert into sys_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_staff,
            <if test="password != null">password,</if>
            <if test="userName != null">user_name,</if>
            <if test="staffName != null">nick_name,</if>
            <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=",">
            '1',
            <if test="password != null">#{password},</if>
            <if test="userName != null">#{userName},</if>
            <if test="staffName != null">#{staffName},</if>
            <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 sys_user
        <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 user_id = #{staffId}
    </update>

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

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