<?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.xinelu.manage.mapper.residentinfo.ResidentInfoMapper">

    <resultMap type="com.xinelu.manage.domain.residentinfo.ResidentInfo" id="ResidentInfoResult">
            <result property="id" column="id"/>
            <result property="patientName" column="patient_name"/>
            <result property="patientPhone" column="patient_phone"/>
            <result property="familyMemberPhone" column="family_member_phone"/>
            <result property="birthDate" column="birth_date"/>
            <result property="cardNo" column="card_no"/>
            <result property="sex" column="sex"/>
            <result property="address" column="address"/>
            <result property="openId" column="open_id"/>
            <result property="unionId" column="union_id"/>
            <result property="officialAccountOpenid" column="official_account_openid"/>
            <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="selectResidentInfoVo">
        select id, patient_name, patient_phone, family_member_phone, birth_date, card_no, sex, address, open_id, union_id, official_account_openid, create_by, create_time, update_by, update_time from resident_info
    </sql>

	<select id="selectResidentInfoList" parameterType="com.xinelu.manage.domain.residentinfo.ResidentInfo" resultMap="ResidentInfoResult">
		<include refid="selectResidentInfoVo" />
		<where>
			<if test="patientName != null  and patientName != ''">
				and patient_name like concat('%', #{patientName}, '%')
			</if>
			<if test="patientPhone != null  and patientPhone != ''">
				and patient_phone = #{patientPhone}
			</if>
			<if test="birthDate != null ">
				and birth_date = #{birthDate}
			</if>
			<if test="cardNo != null  and cardNo != ''">
				and card_no = #{cardNo}
			</if>
			<if test="openId != null  and openId != ''">
				and open_id = #{openId}
			</if>
			<if test="unionId != null  and unionId != ''">
				and union_id = #{unionId}
			</if>
			<if test="officialAccountOpenid != null  and officialAccountOpenid != ''">
				and official_account_openid = #{officialAccountOpenid}
			</if>
		</where>
	</select>

    <select id="selectResidentInfoById" parameterType="Long"
            resultMap="ResidentInfoResult">
            <include refid="selectResidentInfoVo"/>
            where id = #{id}
    </select>

    <insert id="insertResidentInfo" parameterType="com.xinelu.manage.domain.residentinfo.ResidentInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into resident_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientName != null">patient_name,
            </if>
            <if test="patientPhone != null">patient_phone,
            </if>
            <if test="familyMemberPhone != null">family_member_phone,
            </if>
            <if test="birthDate != null">birth_date,
            </if>
            <if test="cardNo != null">card_no,
            </if>
            <if test="sex != null">sex,
            </if>
            <if test="address != null">address,
            </if>
            <if test="openId != null">open_id,
            </if>
            <if test="unionId != null">union_id,
            </if>
            <if test="officialAccountOpenid != null">official_account_openid,
            </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="patientName != null">#{patientName},
	        </if>
	        <if test="patientPhone != null">#{patientPhone},
	        </if>
	        <if test="familyMemberPhone != null">#{familyMemberPhone},
	        </if>
	        <if test="birthDate != null">#{birthDate},
	        </if>
	        <if test="cardNo != null">#{cardNo},
	        </if>
	        <if test="sex != null">#{sex},
	        </if>
	        <if test="address != null">#{address},
	        </if>
	        <if test="openId != null">#{openId},
	        </if>
	        <if test="unionId != null">#{unionId},
	        </if>
	        <if test="officialAccountOpenid != null">#{officialAccountOpenid},
	        </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="updateResidentInfo" parameterType="com.xinelu.manage.domain.residentinfo.ResidentInfo">
		update resident_info
		<trim prefix="SET" suffixOverrides=",">
			<if test="patientName != null">patient_name =
				#{patientName},
			</if>
			<if test="patientPhone != null">patient_phone =
				#{patientPhone},
			</if>
			<if test="familyMemberPhone != null">family_member_phone =
				#{familyMemberPhone},
			</if>
			<if test="birthDate != null">birth_date =
				#{birthDate},
			</if>
			<if test="cardNo != null">card_no =
				#{cardNo},
			</if>
			<if test="sex != null">sex =
				#{sex},
			</if>
			<if test="address != null">address =
				#{address},
			</if>
			<if test="openId != null">open_id =
				#{openId},
			</if>
			<if test="unionId != null">union_id =
				#{unionId},
			</if>
			<if test="officialAccountOpenid != null">official_account_openid =
				#{officialAccountOpenid},
			</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>

	<update id="updateResidentInfoOpenid" parameterType="com.xinelu.manage.domain.residentinfo.ResidentInfo">
		UPDATE resident_info SET open_id = #{openId} WHERE id = #{id}
	</update>

	<delete id="deleteResidentInfoById" parameterType="Long">
        delete from resident_info where id = #{id}
    </delete>

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

	<select id="getResidentInfoByPhoneAndOpenId" parameterType="string"
			resultType="com.xinelu.manage.domain.residentinfo.ResidentInfo">
		<include refid="selectResidentInfoVo" />
		<where>
			del_flag = 0
			<if test="phone != null and phone != ''">
				and patient_phone = #{phone}
			</if>
			<if test="openId != null and openId != ''">
				and open_id = #{openId}
			</if>
		</where>
	</select>

	<select id="getResidentInfoByPhoneAndName" resultType="com.xinelu.manage.domain.residentinfo.ResidentInfo">
		<include refid="selectResidentInfoVo" />
		<where>
			del_flag = 0
			<if test="phone != null and phone != ''">
				and patient_phone = #{phone}
			</if>
			<if test="patientName != null and patientName != ''">
				and patient_name = #{patientName}
			</if>
		</where>
	</select>

    <insert id="insertResidentInfoList" useGeneratedKeys="true" keyProperty="id">
        insert into resident_info(
        patient_name,
        patient_phone,
        card_no,
        del_flag,
        create_by,
        create_time
        ) values
        <foreach item="PatientInfoImport" index="index" collection="list" separator=",">
            (
            #{PatientInfoImport.patientName},
            #{PatientInfoImport.patientPhone},
            #{PatientInfoImport.cardNo},
            0,
            #{PatientInfoImport.createBy},
            #{PatientInfoImport.createTime}
            )
        </foreach>
    </insert>

	<select id="selectResidentInfoByPhoneList" resultType="com.xinelu.manage.domain.residentinfo.ResidentInfo">
        select
        patient_name,
        patient_phone
        from resident_info
        <where>
            (patient_name, patient_phone) IN
            <foreach item="list" index="index" collection="list" open="(" separator="," close=")">
                (#{list.patientName}, #{list.patientPhone})
            </foreach>
        </where>
    </select>
</mapper>