<?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.system.mapper.ResidentInfoMapper">

    <resultMap type="ResidentInfo" id="ResidentInfoResult">
        <result property="residentId"    column="resident_id"    />
        <result property="houseId"    column="house_id"    />
        <result property="userId"    column="user_id"    />
        <result property="detailedAddress"    column="detailed_address"    />
        <result property="residentName"    column="resident_name"    />
        <result property="residentIdCard"    column="resident_id_card"    />
        <result property="residentBirthday"    column="resident_birthday"    />
        <result property="residentPhone"    column="resident_phone"    />
        <result property="residentGender"    column="resident_gender"    />
        <result property="residentRelationship"    column="resident_relationship"    />
        <result property="isHouseholder"    column="is_householder"    />
        <result property="isTenant"    column="is_tenant"    />
        <result property="residentEmployer"    column="resident_employer"    />
        <result property="specialType"    column="special_type"    />
        <result property="residentHobby"    column="resident_hobby"    />
        <result property="residentAppearance"    column="resident_appearance"    />
        <result property="idCardFront"    column="id_card_front"    />
        <result property="idCardBack"    column="id_card_back"    />
        <result property="idCardAddress"    column="id_card_address"    />
        <result property="facePhoto"    column="face_photo"    />
        <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"    />
    </resultMap>

    <sql id="selectResidentInfoVo">
        select resident_id, house_id, user_id, detailed_address, resident_name, resident_id_card, resident_birthday, resident_phone, resident_gender, resident_relationship, is_householder, is_tenant, resident_employer, special_type, resident_hobby, resident_appearance, id_card_front, id_card_back, id_card_address, face_photo, create_by, create_time, update_by, update_time, remark from resident_info
    </sql>

    <select id="selectResidentInfoList" parameterType="ResidentInfo" resultMap="ResidentInfoResult">
        <include refid="selectResidentInfoVo"/>
        <where>
            <if test="houseId != null "> and house_id = #{houseId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="detailedAddress != null  and detailedAddress != ''"> and detailed_address = #{detailedAddress}</if>
            <if test="residentName != null  and residentName != ''"> and resident_name like concat('%', #{residentName}, '%')</if>
            <if test="residentIdCard != null  and residentIdCard != ''"> and resident_id_card = #{residentIdCard}</if>
            <if test="residentBirthday != null  and residentBirthday != ''"> and resident_birthday = #{residentBirthday}</if>
            <if test="residentPhone != null  and residentPhone != ''"> and resident_phone = #{residentPhone}</if>
            <if test="residentGender != null "> and resident_gender = #{residentGender}</if>
            <if test="residentRelationship != null  and residentRelationship != ''"> and resident_relationship = #{residentRelationship}</if>
            <if test="isHouseholder != null  and isHouseholder != ''"> and is_householder = #{isHouseholder}</if>
            <if test="isTenant != null  and isTenant != ''"> and is_tenant = #{isTenant}</if>
            <if test="residentEmployer != null  and residentEmployer != ''"> and resident_employer = #{residentEmployer}</if>
            <if test="specialType != null  and specialType != ''"> and special_type = #{specialType}</if>
            <if test="residentHobby != null  and residentHobby != ''"> and resident_hobby = #{residentHobby}</if>
            <if test="residentAppearance != null  and residentAppearance != ''"> and resident_appearance = #{residentAppearance}</if>
            <if test="idCardFront != null  and idCardFront != ''"> and id_card_front = #{idCardFront}</if>
            <if test="idCardBack != null  and idCardBack != ''"> and id_card_back = #{idCardBack}</if>
            <if test="idCardAddress != null  and idCardAddress != ''"> and id_card_address = #{idCardAddress}</if>
            <if test="facePhoto != null  and facePhoto != ''"> and face_photo = #{facePhoto}</if>
        </where>
        order by create_time DESC
    </select>
    
    <select id="selectResidentInfoByResidentId" parameterType="Long" resultMap="ResidentInfoResult">
        <include refid="selectResidentInfoVo"/>
        where resident_id = #{residentId}
    </select>
    <select id="selectResidentInfoByUserId" parameterType="Long" resultMap="ResidentInfoResult">
        <include refid="selectResidentInfoVo"/>
        where user_id = #{userId}
    </select>
    <select id="selectCount" parameterType="ResidentInfo" resultType="java.lang.Integer">
        select count(1) from resident_info where resident_appearance = #{residentAppearance}
    </select>


    <insert id="insertResidentInfoList">
        insert into resident_info(house_id,
           user_id,
            detailed_address,
            resident_name,
            resident_id_card,
            resident_birthday,
            resident_phone,
            resident_gender,
            resident_relationship,
            is_householder,
            is_tenant,
            resident_employer,
            resident_appearance,
            create_by,
            create_time,
            update_by,
            update_time,
            remark,create_time,update_time)
        <foreach item="residentInfo" index="index" collection="residentInfoList" separator=",">
            (#{residentInfo.houseId},
            #{residentInfo.userId},
            #{residentInfo.detailedAddress},
            #{residentInfo.residentName},
            #{residentInfo.residentIdCard},
            #{residentInfo.residentBirthday},
            #{residentInfo.residentPhone},
            #{residentInfo.residentGender},
            #{residentInfo.residentRelationship},
            #{residentInfo.isHouseholder},
            #{residentInfo.isTenant},
            #{residentInfo.residentEmployer},
            #{residentInfo.residentAppearance},
            #{vcreateBy},
            #{residentInfo.createTime},
            #{residentInfo.updateBy},
            #{residentInfo.updateTime},
            #{residentInforemark},
            sysdate(),sysdate())
        </foreach>
    </insert>

    <insert id="insertResidentInfo" parameterType="ResidentInfo" useGeneratedKeys="true" keyProperty="residentId">
        insert into resident_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="houseId != null">house_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="detailedAddress != null">detailed_address,</if>
            <if test="residentName != null">resident_name,</if>
            <if test="residentIdCard != null">resident_id_card,</if>
            <if test="residentBirthday != null">resident_birthday,</if>
            <if test="residentPhone != null">resident_phone,</if>
            <if test="residentGender != null">resident_gender,</if>
            <if test="residentRelationship != null">resident_relationship,</if>
            <if test="isHouseholder != null">is_householder,</if>
            <if test="isTenant != null">is_tenant,</if>
            <if test="residentEmployer != null">resident_employer,</if>
            <if test="specialType != null">special_type,</if>
            <if test="residentHobby != null">resident_hobby,</if>
            <if test="residentAppearance != null">resident_appearance,</if>
            <if test="idCardFront != null">id_card_front,</if>
            <if test="idCardBack != null">id_card_back,</if>
            <if test="idCardAddress != null">id_card_address,</if>
            <if test="facePhoto != null">face_photo,</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="houseId != null">#{houseId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="detailedAddress != null">#{detailedAddress},</if>
            <if test="residentName != null">#{residentName},</if>
            <if test="residentIdCard != null">#{residentIdCard},</if>
            <if test="residentBirthday != null">#{residentBirthday},</if>
            <if test="residentPhone != null">#{residentPhone},</if>
            <if test="residentGender != null">#{residentGender},</if>
            <if test="residentRelationship != null">#{residentRelationship},</if>
            <if test="isHouseholder != null">#{isHouseholder},</if>
            <if test="isTenant != null">#{isTenant},</if>
            <if test="residentEmployer != null">#{residentEmployer},</if>
            <if test="specialType != null">#{specialType},</if>
            <if test="residentHobby != null">#{residentHobby},</if>
            <if test="residentAppearance != null">#{residentAppearance},</if>
            <if test="idCardFront != null">#{idCardFront},</if>
            <if test="idCardBack != null">#{idCardBack},</if>
            <if test="idCardAddress != null">#{idCardAddress},</if>
            <if test="facePhoto != null">#{facePhoto},</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="updateResidentInfo" parameterType="ResidentInfo">
        update resident_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="houseId != null">house_id = #{houseId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="detailedAddress != null">detailed_address = #{detailedAddress},</if>
            <if test="residentName != null">resident_name = #{residentName},</if>
            <if test="residentIdCard != null">resident_id_card = #{residentIdCard},</if>
            <if test="residentBirthday != null">resident_birthday = #{residentBirthday},</if>
            <if test="residentPhone != null">resident_phone = #{residentPhone},</if>
            <if test="residentGender != null">resident_gender = #{residentGender},</if>
            <if test="residentRelationship != null">resident_relationship = #{residentRelationship},</if>
            <if test="isHouseholder != null">is_householder = #{isHouseholder},</if>
            <if test="isTenant != null">is_tenant = #{isTenant},</if>
            <if test="residentEmployer != null">resident_employer = #{residentEmployer},</if>
            <if test="specialType != null">special_type = #{specialType},</if>
            <if test="residentHobby != null">resident_hobby = #{residentHobby},</if>
            <if test="residentAppearance != null">resident_appearance = #{residentAppearance},</if>
            <if test="idCardFront != null">id_card_front = #{idCardFront},</if>
            <if test="idCardBack != null">id_card_back = #{idCardBack},</if>
            <if test="idCardAddress != null">id_card_address = #{idCardAddress},</if>
            <if test="facePhoto != null">face_photo = #{facePhoto},</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 resident_id = #{residentId}
    </update>

    <delete id="deleteResidentInfoByResidentId" parameterType="Long">
        delete from resident_info where resident_id = #{residentId}
    </delete>

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