<?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.ruoyi.app.mapper.StaffMapper">

    <resultMap type="Staff" id="StaffResult">
        <result property="id"    column="id"    />
        <result property="realname"    column="realname"    />
        <result property="postId"    column="post_id"    />
        <result property="phone"    column="phone"    />
        <result property="technicalTitle"    column="technical_title"    />
        <result property="technicalCert"    column="technical_cert"    />
        <result property="healthCert"    column="health_cert"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <association property="post"    javaType="SysPost"   resultMap="PostResult" />
    </resultMap>
    <resultMap id="PostResult" type="SysPost">
        <id     property="postId"        column="post_id"       />
        <result property="postName"      column="post_name"     />
    </resultMap>
    <sql id="selectStaffVo">
        select id, realname, post_id, phone, technical_title, technical_cert, health_cert, create_time, create_by, update_time, update_by, del_flag from staff
    </sql>

    <select id="selectStaffList" parameterType="Staff" resultMap="StaffResult">
        select s.id, s.realname, s.post_id, s.phone, s.technical_title, s.technical_cert, s.health_cert,
        s.create_time, s.create_by, s.update_time, s.update_by,p.post_name from staff as s
        left join sys_post p on p.post_id = s.post_id
        <where>
            s.del_flag = '0'
            <if test="realname != null  and realname != ''"> and s.realname like concat('%', #{realname}, '%')</if>
            <if test="postId != null "> and s.post_id = #{postId}</if>
            <if test="phone != null  and phone != ''"> and s.phone = #{phone}</if>
        </where>
    </select>
    
    <select id="selectStaffById" parameterType="Long" resultMap="StaffResult">
        <include refid="selectStaffVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertStaff" parameterType="Staff" useGeneratedKeys="true" keyProperty="id">
        insert into staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="realname != null and realname != ''">realname,</if>
            <if test="postId != null">post_id,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="technicalTitle != null">technical_title,</if>
            <if test="technicalCert != null">technical_cert,</if>
            <if test="healthCert != null">health_cert,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="realname != null and realname != ''">#{realname},</if>
            <if test="postId != null">#{postId},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="technicalTitle != null">#{technicalTitle},</if>
            <if test="technicalCert != null">#{technicalCert},</if>
            <if test="healthCert != null">#{healthCert},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateStaff" parameterType="Staff">
        update staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="realname != null and realname != ''">realname = #{realname},</if>
            <if test="postId != null">post_id = #{postId},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="technicalTitle != null">technical_title = #{technicalTitle},</if>
            <if test="technicalCert != null">technical_cert = #{technicalCert},</if>
            <if test="healthCert != null">health_cert = #{healthCert},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStaffById" parameterType="Long">
        update staff set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteStaffByIds" parameterType="String">
        update staff set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>