<?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.care.mapper.CareNursesMapper">
    
    <resultMap type="CareNurses" id="CareNursesResult">
        <result property="nurseId"    column="nurse_id"    />
        <result property="name"    column="name"    />
        <result property="dateOfBirth"    column="date_of_birth"    />
        <result property="address"    column="address"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="licenseNumber"    column="license_number"    />
        <result property="experienceYears"    column="experience_years"    />
        <result property="companyId"    column="company_id"    />
        <result property="userId"    column="user_id"    />
        <result property="loginName"    column="login_name"    />
        <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"    />
        <result property="remark"    column="remark"    />
        <result property="age"    column="age"    />
        <result property="companyName"    column="company_name"    />
    </resultMap>

    <sql id="selectCareNursesVo">
        select nurse_id, name, date_of_birth, address, phone_number, license_number, experience_years, company_id, del_flag, create_by, create_time, update_by, update_time, remark,user_id,age from care_nurses
    </sql>

    <select id="selectCareNursesList" parameterType="CareNurses" resultMap="CareNursesResult">
        SELECT
        c.nurse_id,
        c.NAME,
        c.date_of_birth,
        c.address,
        c.phone_number,
        c.license_number,
        c.experience_years,
        c.company_id,
        c.del_flag,
        c.create_by,
        c.create_time,
        c.age,
        c.update_by,
        c.update_time,
        c.remark,
        c.user_id,
        u.user_name login_name ,p.`name` company_name
        FROM
        care_nurses c
        LEFT JOIN sys_user u ON u.user_id = c.user_id
        LEFT JOIN care_companies p on p.company_id=c.company_id
        <where>
            <if test="1==1"> and c.del_flag = '0'</if>
            <if test="name != null  and name != ''"> and c.name like concat('%', #{name}, '%')</if>
            <if test="dateOfBirth != null  and dateOfBirth != ''"> and c.date_of_birth = #{dateOfBirth}</if>
            <if test="address != null  and address != ''"> and c.address = #{address}</if>
            <if test="phoneNumber != null  and phoneNumber != ''"> and c.phone_number = #{phoneNumber}</if>
            <if test="licenseNumber != null  and licenseNumber != ''"> and c.license_number = #{licenseNumber}</if>
            <if test="experienceYears != null "> and c.experience_years = #{experienceYears}</if>
            <if test="companyId != null "> and c.company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectCareNursesByNurseId" parameterType="Long" resultMap="CareNursesResult">
        SELECT
            c.nurse_id,
            c.NAME,
            c.date_of_birth,
            c.address,
            c.phone_number,
            c.license_number,
            c.experience_years,
            c.company_id,
            c.del_flag,
            c.create_by,
            c.create_time,
            c.age,
            c.update_by,
            c.update_time,
            c.remark,
            c.user_id,
            u.user_name login_name ,p.`name` company_name
        FROM
            care_nurses c
                LEFT JOIN sys_user u ON u.user_id = c.user_id
                LEFT JOIN care_companies p on p.company_id=c.company_id
        where c.nurse_id = #{nurseId} and c.del_flag = '0'
    </select>

    <insert id="insertCareNurses" parameterType="CareNurses" useGeneratedKeys="true" keyProperty="nurseId">
        insert into care_nurses
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="dateOfBirth != null and dateOfBirth != ''">date_of_birth,</if>
            <if test="address != null">address,</if>
            <if test="phoneNumber != null and phoneNumber != ''">phone_number,</if>
            <if test="licenseNumber != null and licenseNumber != ''">license_number,</if>
            <if test="experienceYears != null">experience_years,</if>
            <if test="companyId != null">company_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="age != null">age,</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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="dateOfBirth != null and dateOfBirth != ''">#{dateOfBirth},</if>
            <if test="address != null">#{address},</if>
            <if test="phoneNumber != null and phoneNumber != ''">#{phoneNumber},</if>
            <if test="licenseNumber != null and licenseNumber != ''">#{licenseNumber},</if>
            <if test="experienceYears != null">#{experienceYears},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="age != null">#{age},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCareNurses" parameterType="CareNurses">
        update care_nurses
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="age != null and name != ''">age = #{age},</if>
            <if test="dateOfBirth != null and dateOfBirth != ''">date_of_birth = #{dateOfBirth},</if>
            <if test="address != null">address = #{address},</if>
            <if test="phoneNumber != null and phoneNumber != ''">phone_number = #{phoneNumber},</if>
            <if test="licenseNumber != null and licenseNumber != ''">license_number = #{licenseNumber},</if>
            <if test="experienceYears != null">experience_years = #{experienceYears},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="userId != null">user_id = #{userId},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where nurse_id = #{nurseId}
    </update>

    <delete id="deleteCareNursesByNurseId" parameterType="Long">
        update care_nurses set del_flag = '2' where nurse_id = #{nurseId}
    </delete>

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