<?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.CarePersonsMapper">
    
    <resultMap type="CarePersons" id="CarePersonsResult">
        <result property="personId"    column="person_id"    />
        <result property="name"    column="name"    />
        <result property="dateOfBirth"    column="date_of_birth"    />
        <result property="gender"    column="gender"    />
        <result property="idNumber"    column="id_number"    />
        <result property="address"    column="address"    />
        <result property="phoneNumber"    column="phone_number"    />
        <result property="emergencyContact"    column="emergency_contact"    />
        <result property="emergencyPhone"    column="emergency_phone"    />
        <result property="healthStatus"    column="health_status"    />
        <result property="medicalConditions"    column="medical_conditions"    />
        <result property="disability"    column="disability"    />
        <result property="registeredDate"    column="registered_date"    />
        <result property="userId"    column="user_id"    />
        <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="age"    column="age"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCarePersonsVo">
        SELECT
            c.person_id,
            c.NAME,
            c.date_of_birth,
            c.gender,
            c.id_number,
            c.address,
            c.phone_number,
            c.emergency_contact,
            c.emergency_phone,
            c.health_status,
            c.medical_conditions,
            c.disability,
            c.registered_date,
            c.user_id,
            c.del_flag,
            c.create_by,
            c.create_time,
            c.update_by,
            c.update_time,
            c.remark,
            c.age
        FROM
            care_persons c
         LEFT JOIN sys_user u on u.user_id=c.create_by
    </sql>

    <select id="selectCarePersonsList" parameterType="CarePersons" resultMap="CarePersonsResult">
        <include refid="selectCarePersonsVo"/>
        <where>  
            <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="gender != null  and gender != ''"> and c.gender = #{gender}</if>
            <if test="idNumber != null  and idNumber != ''"> and c.id_number = #{idNumber}</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="emergencyContact != null  and emergencyContact != ''"> and c.emergency_contact = #{emergencyContact}</if>
            <if test="emergencyPhone != null  and emergencyPhone != ''"> and c.emergency_phone = #{emergencyPhone}</if>
            <if test="healthStatus != null  and healthStatus != ''"> and c.health_status = #{healthStatus}</if>
            <if test="medicalConditions != null  and medicalConditions != ''"> and c.medical_conditions = #{medicalConditions}</if>
            <if test="disability != null  and disability != ''"> and c.disability = #{disability}</if>
            <if test="registeredDate != null  and registeredDate != ''"> and c.registered_date = #{registeredDate}</if>
            <if test="userId != null "> and c.user_id = #{userId}</if>
            <if test="age != null "> and c.age = #{age}</if>
            <if test="params.dataScope != null and params.dataScope !=''"> ${params.dataScope}</if>
        </where>
    </select>
    
    <select id="selectCarePersonsByPersonId" parameterType="Long" resultMap="CarePersonsResult">
        <include refid="selectCarePersonsVo"/>
        where c.person_id = #{personId}
    </select>

    <insert id="insertCarePersons" parameterType="CarePersons" useGeneratedKeys="true" keyProperty="personId">
        insert into care_persons
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="dateOfBirth != null and dateOfBirth != ''">date_of_birth,</if>
            <if test="gender != null and gender != ''">gender,</if>
            <if test="idNumber != null and idNumber != ''">id_number,</if>
            <if test="address != null">address,</if>
            <if test="phoneNumber != null">phone_number,</if>
            <if test="emergencyContact != null">emergency_contact,</if>
            <if test="emergencyPhone != null">emergency_phone,</if>
            <if test="healthStatus != null">health_status,</if>
            <if test="medicalConditions != null">medical_conditions,</if>
            <if test="disability != null and disability != ''">disability,</if>
            <if test="registeredDate != null and registeredDate != ''">registered_date,</if>
            <if test="userId != null">user_id,</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>
            <if test="age != null">age,</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="gender != null and gender != ''">#{gender},</if>
            <if test="idNumber != null and idNumber != ''">#{idNumber},</if>
            <if test="address != null">#{address},</if>
            <if test="phoneNumber != null">#{phoneNumber},</if>
            <if test="emergencyContact != null">#{emergencyContact},</if>
            <if test="emergencyPhone != null">#{emergencyPhone},</if>
            <if test="healthStatus != null">#{healthStatus},</if>
            <if test="medicalConditions != null">#{medicalConditions},</if>
            <if test="disability != null and disability != ''">#{disability},</if>
            <if test="registeredDate != null and registeredDate != ''">#{registeredDate},</if>
            <if test="userId != null">#{userId},</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>
            <if test="age != null">#{age},</if>
         </trim>
    </insert>

    <update id="updateCarePersons" parameterType="CarePersons">
        update care_persons
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="dateOfBirth != null and dateOfBirth != ''">date_of_birth = #{dateOfBirth},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            <if test="idNumber != null and idNumber != ''">id_number = #{idNumber},</if>
            <if test="address != null">address = #{address},</if>
            <if test="phoneNumber != null">phone_number = #{phoneNumber},</if>
            <if test="emergencyContact != null">emergency_contact = #{emergencyContact},</if>
            <if test="emergencyPhone != null">emergency_phone = #{emergencyPhone},</if>
            <if test="healthStatus != null">health_status = #{healthStatus},</if>
            <if test="medicalConditions != null">medical_conditions = #{medicalConditions},</if>
            <if test="disability != null and disability != ''">disability = #{disability},</if>
            <if test="registeredDate != null and registeredDate != ''">registered_date = #{registeredDate},</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>
            <if test="age != null">age = #{age},</if>
        </trim>
        where person_id = #{personId}
    </update>

    <delete id="deleteCarePersonsByPersonId" parameterType="Long">
        delete from care_persons where person_id = #{personId}
    </delete>

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