<?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.cyl.manager.staff.mapper.StaffMapper">
    
    <resultMap type="Staff" id="StaffResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="nickName"    column="nick_name"    />
        <result property="lastName"    column="last_name"    />
        <result property="firstName"    column="first_name"    />
        <result property="gender"    column="gender"    />
        <result property="birthDay"    column="birth_day"    />
        <result property="staffType"    column="staff_type"    />
        <result property="serviceAge"    column="service_age"    />
        <result property="serviceLevel"    column="service_level"    />
        <result property="serviceTime"    column="service_time"    />
        <result property="address"    column="address"    />
        <result property="shortDescription"    column="short_description"    />
        <result property="description"    column="description"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectStaffVo">
        select id, name, nick_name, last_name, first_name, gender, birth_day, staff_type, service_age, service_level, service_time, address, short_description, description, del_flag, create_by, create_time, update_by, update_time from staff
    </sql>

    <select id="selectStaffList" parameterType="Staff" resultMap="StaffResult">
        <include refid="selectStaffVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="lastName != null  and lastName != ''"> and last_name like concat('%', #{lastName}, '%')</if>
            <if test="firstName != null  and firstName != ''"> and first_name like concat('%', #{firstName}, '%')</if>
            <if test="gender != null "> and gender = #{gender}</if>
            <if test="birthDay != null  and birthDay != ''"> and birth_day = #{birthDay}</if>
            <if test="staffType != null "> and staff_type = #{staffType}</if>
            <if test="serviceAge != null "> and service_age = #{serviceAge}</if>
            <if test="serviceLevel != null "> and service_level = #{serviceLevel}</if>
            <if test="serviceTime != null  and serviceTime != ''"> and service_time = #{serviceTime}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="shortDescription != null  and shortDescription != ''"> and short_description = #{shortDescription}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectStaffById" parameterType="Long" resultMap="StaffResult">
        <include refid="selectStaffVo"/>
        where id = #{id}
    </select>
    <select id="selectStaffByCondition" resultType="com.cyl.manager.staff.domain.Staff">
        <include refid="selectStaffVo"/>
        <where>
            id in
            <foreach item="id" collection="staffIds" open="(" separator="," close=")">
                #{id}
            </foreach>
            <if test="petTypes != null and petTypes.size() > 0">
                AND
                <foreach item="type" collection="petTypes" open="(" separator=" OR " close=")" >
                   pet_type like concat('%', #{type}, '%')
                </foreach>
            </if>
            <if test="name != null and name != ''">
                AND name like concat('%', #{name}, '%')
            </if>
        </where>
    </select>

    <insert id="insertStaff" parameterType="Staff" useGeneratedKeys="true" keyProperty="id">
        insert into staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="nickName != null and nickName != ''">nick_name,</if>
            <if test="lastName != null">last_name,</if>
            <if test="firstName != null">first_name,</if>
            <if test="gender != null">gender,</if>
            <if test="birthDay != null">birth_day,</if>
            <if test="staffType != null">staff_type,</if>
            <if test="serviceAge != null">service_age,</if>
            <if test="serviceLevel != null">service_level,</if>
            <if test="serviceTime != null">service_time,</if>
            <if test="address != null">address,</if>
            <if test="shortDescription != null and shortDescription != ''">short_description,</if>
            <if test="description != null">description,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="nickName != null and nickName != ''">#{nickName},</if>
            <if test="lastName != null">#{lastName},</if>
            <if test="firstName != null">#{firstName},</if>
            <if test="gender != null">#{gender},</if>
            <if test="birthDay != null">#{birthDay},</if>
            <if test="staffType != null">#{staffType},</if>
            <if test="serviceAge != null">#{serviceAge},</if>
            <if test="serviceLevel != null">#{serviceLevel},</if>
            <if test="serviceTime != null">#{serviceTime},</if>
            <if test="address != null">#{address},</if>
            <if test="shortDescription != null and shortDescription != ''">#{shortDescription},</if>
            <if test="description != null">#{description},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateStaff" parameterType="Staff">
        update staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="lastName != null">last_name = #{lastName},</if>
            <if test="firstName != null">first_name = #{firstName},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="birthDay != null">birth_day = #{birthDay},</if>
            <if test="staffType != null">staff_type = #{staffType},</if>
            <if test="serviceAge != null">service_age = #{serviceAge},</if>
            <if test="serviceLevel != null">service_level = #{serviceLevel},</if>
            <if test="serviceTime != null">service_time = #{serviceTime},</if>
            <if test="address != null">address = #{address},</if>
            <if test="shortDescription != null and shortDescription != ''">short_description = #{shortDescription},</if>
            <if test="description != null">description = #{description},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStaffById" parameterType="Long">
        delete from staff where id = #{id}
    </delete>

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