<?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.familydoctor.applet.mapper.ResidentPatientInfoMapper">

    <resultMap type="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo" id="PatientInfoResult">
        <result property="id" column="id"/>
        <result property="areaCode" column="area_code"/>
        <result property="communityCode" column="community_code"/>
        <result property="patientCode" column="patient_code"/>
        <result property="patientName" column="patient_name"/>
        <result property="cardNo" column="card_no"/>
        <result property="userId" column="user_id"/>
        <result property="unionid" column="unionid"/>
        <result property="openid" column="openid"/>
        <result property="bindOpenid" column="bind_openid"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="urgentContactName" column="urgent_contact_name"/>
        <result property="urgentContactPhone" column="urgent_contact_phone"/>
        <result property="communityAliasName" column="community_alias_name"/>
        <result property="homeLongitude" column="home_longitude"/>
        <result property="homeLatitude" column="home_latitude"/>
        <result property="headPictureUrl" column="head_picture_url"/>
        <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="password" column="password"/>
        <result property="integral" column="integral"/>
        <result property="loginFlag" column="login_flag"/>
        <result property="primaryAccountFlag" column="primary_account_flag"/>
        <result property="serviceIds" column="service_ids"/>
        <result property="locationName" column="location_name"/>
        <result property="source" column="source"/>
        <result property="invitationPatientId" column="invitation_patient_id"/>
        <result property="totalSignInDays" column="total_sign_in_days"/>
        <result property="sex" column="sex"/>
        <result property="birthDate" column="birth_date"/>
        <result property="delFlag" column="del_flag"/>
        <result property="personalWechatCodeUrl" column="personal_wechat_code_url"/>
        <result property="disablingCondition" column="disabling_condition"/>
        <result property="disablingReason" column="disabling_reason"/>
        <result property="disease" column="disease"/>
        <result property="cityCode" column="city_code"/>
        <result property="bindingTime" column="binding_time"/>
        <result property="isChecked" column="is_checked"/>
        <result property="householdRelationship" column="household_relationship"/>
        <result property="householdCardNo" column="household_card_no"/>
        <result property="signNo" column="sign_no"/>
    </resultMap>

    <sql id="selectPatientInfoVo">
        select id,
               area_code,
               community_code,
               patient_code,
               patient_name,
               card_no,
               user_id,
               unionid,
               openid,
               bind_openid,
               phone,
               address,
               urgent_contact_name,
               urgent_contact_phone,
               community_alias_name,
               home_longitude,
               home_latitude,
               head_picture_url,
               create_by,
               create_time,
               update_by,
               update_time,
               password,
               integral,
               login_flag,
               primary_account_flag,
               service_ids,
               location_name,
               source,
               invitation_patient_id,
               total_sign_in_days,
               sex,
               birth_date,
               del_flag,
               personal_wechat_code_url,
               disabling_condition,
               disabling_reason,
               disease,
               city_code,
               binding_time,
               is_checked,
               household_relationship,
               household_card_no,
               sign_no
        from patient_info
    </sql>

    <select id="selectPatientInfoList" parameterType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo"
            resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        <where>
            <if test="patientName != null  and patientName != ''">
                and patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and card_no = #{cardNo}
            </if>
            <if test="openid != null  and openid != ''">
                and openid = #{openid}
            </if>
            <if test="bindOpenid != null and bindOpenid != ''">
                and bind_openid = #{bindOpenid}
            </if>
            <if test="phone != null  and phone != ''">
                and phone = #{phone}
            </if>
            <if test="address != null  and address != ''">
                and address = #{address}
            </if>
            <if test="cityCode != null  and cityCode != ''">
                and city_code = #{cityCode}
            </if>
            <if test="bindingTime != null ">
                and binding_time = #{bindingTime}
            </if>
            <if test="isChecked != null  and isChecked != ''">
                and is_checked = #{isChecked}
            </if>
            <if test="householdCardNo">
                and household_card_no = #{householdCardNo}
            </if>
        </where>
        order by binding_time desc
    </select>

    <insert id="insertPatientInfo" parameterType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">area_code,
            </if>
            <if test="communityCode != null">community_code,
            </if>
            <if test="patientCode != null">patient_code,
            </if>
            <if test="patientName != null">patient_name,
            </if>
            <if test="cardNo != null">card_no,
            </if>
            <if test="userId != null">user_id,
            </if>
            <if test="unionid != null">unionid,
            </if>
            <if test="openid != null">openid,
            </if>
            <if test="bindOpenid != null">
               bind_openid,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="address != null">address,
            </if>
            <if test="urgentContactName != null">urgent_contact_name,
            </if>
            <if test="urgentContactPhone != null">urgent_contact_phone,
            </if>
            <if test="communityAliasName != null">community_alias_name,
            </if>
            <if test="homeLongitude != null">home_longitude,
            </if>
            <if test="homeLatitude != null">home_latitude,
            </if>
            <if test="headPictureUrl != null">head_picture_url,
            </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="password != null">password,
            </if>
            <if test="integral != null">integral,
            </if>
            <if test="loginFlag != null">login_flag,
            </if>
            <if test="primaryAccountFlag != null">primary_account_flag,
            </if>
            <if test="serviceIds != null">service_ids,
            </if>
            <if test="locationName != null">location_name,
            </if>
            <if test="source != null">source,
            </if>
            <if test="invitationPatientId != null">invitation_patient_id,
            </if>
            <if test="totalSignInDays != null">total_sign_in_days,
            </if>
            <if test="sex != null">sex,
            </if>
            <if test="birthDate != null">birth_date,
            </if>
            <if test="delFlag != null">del_flag,
            </if>
            <if test="personalWechatCodeUrl != null">personal_wechat_code_url,
            </if>
            <if test="disablingCondition != null">disabling_condition,
            </if>
            <if test="disablingReason != null">disabling_reason,
            </if>
            <if test="disease != null">
                disease,
            </if>
            <if test="cityCode != null and cityCode != ''">city_code,
            </if>
            <if test="bindingTime != null">binding_time,
            </if>
            <if test="isChecked != null">is_checked,
            </if>
            <if test="householdRelationship != null">
                household_relationship,
            </if>
            <if test="householdCardNo != null">
                household_card_no,
            </if>
            <if test="signNo != null">
                sign_no,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null">#{areaCode},
            </if>
            <if test="communityCode != null">#{communityCode},
            </if>
            <if test="patientCode != null">#{patientCode},
            </if>
            <if test="patientName != null">#{patientName},
            </if>
            <if test="cardNo != null">#{cardNo},
            </if>
            <if test="userId != null">#{userId},
            </if>
            <if test="unionid != null">#{unionid},
            </if>
            <if test="openid != null">#{openid},
            </if>
            <if test="bindOpenid != null">
                #{bindOpenid},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="address != null">#{address},
            </if>
            <if test="urgentContactName != null">#{urgentContactName},
            </if>
            <if test="urgentContactPhone != null">#{urgentContactPhone},
            </if>
            <if test="communityAliasName != null">#{communityAliasName},
            </if>
            <if test="homeLongitude != null">#{homeLongitude},
            </if>
            <if test="homeLatitude != null">#{homeLatitude},
            </if>
            <if test="headPictureUrl != null">#{headPictureUrl},
            </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="password != null">#{password},
            </if>
            <if test="integral != null">#{integral},
            </if>
            <if test="loginFlag != null">#{loginFlag},
            </if>
            <if test="primaryAccountFlag != null">#{primaryAccountFlag},
            </if>
            <if test="serviceIds != null">#{serviceIds},
            </if>
            <if test="locationName != null">#{locationName},
            </if>
            <if test="source != null">#{source},
            </if>
            <if test="invitationPatientId != null">#{invitationPatientId},
            </if>
            <if test="totalSignInDays != null">#{totalSignInDays},
            </if>
            <if test="sex != null">#{sex},
            </if>
            <if test="birthDate != null">#{birthDate},
            </if>
            <if test="delFlag != null">#{delFlag},
            </if>
            <if test="personalWechatCodeUrl != null">#{personalWechatCodeUrl},
            </if>
            <if test="disablingCondition != null">#{disablingCondition},
            </if>
            <if test="disablingReason != null">#{disablingReason},
            </if>
            <if test="disease != null">#{disease},</if>
            <if test="cityCode != null and cityCode != ''">#{cityCode},
            </if>
            <if test="bindingTime != null">#{bindingTime},
            </if>
            <if test="isChecked != null">#{isChecked},
            </if>
            <if test="householdRelationship != null">
                #{householdRelationship},
            </if>
            <if test="householdCardNo != null">
                #{householdCardNo},
            </if>
            <if test="signNo != null">
                #{signNo},
            </if>
        </trim>
    </insert>

    <update id="updatePatientInfo" parameterType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo">
        update patient_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null">
                area_code = #{areaCode},
            </if>
            <if test="communityCode != null">community_code =
                #{communityCode},
            </if>
            <if test="patientName != null">patient_name =
                #{patientName},
            </if>
            <if test="cardNo != null">card_no =
                #{cardNo},
            </if>
            <if test="userId != null">user_id =
                #{userId},
            </if>
            <if test="unionid != null">unionid =
                #{unionid},
            </if>
            <if test="openid != null">openid =
                #{openid},
            </if>
            <if test="bindOpenid != null">
                bind_openid = #{bindOpenid},
            </if>
            <if test="phone != null">phone =
                #{phone},
            </if>
            <if test="address != null">address =
                #{address},
            </if>
            <if test="urgentContactName != null">urgent_contact_name =
                #{urgentContactName},
            </if>
            <if test="urgentContactPhone != null">urgent_contact_phone =
                #{urgentContactPhone},
            </if>
            <if test="communityAliasName != null">community_alias_name =
                #{communityAliasName},
            </if>
            <if test="homeLongitude != null">home_longitude =
                #{homeLongitude},
            </if>
            <if test="homeLatitude != null">home_latitude =
                #{homeLatitude},
            </if>
            <if test="headPictureUrl != null">head_picture_url =
                #{headPictureUrl},
            </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="password != null">password =
                #{password},
            </if>
            <if test="integral != null">integral =
                #{integral},
            </if>
            <if test="loginFlag != null">login_flag =
                #{loginFlag},
            </if>
            <if test="primaryAccountFlag != null">primary_account_flag =
                #{primaryAccountFlag},
            </if>
            <if test="serviceIds != null">service_ids =
                #{serviceIds},
            </if>
            <if test="locationName != null">location_name =
                #{locationName},
            </if>
            <if test="source != null">source =
                #{source},
            </if>
            <if test="invitationPatientId != null">invitation_patient_id =
                #{invitationPatientId},
            </if>
            <if test="totalSignInDays != null">total_sign_in_days =
                #{totalSignInDays},
            </if>
            <if test="sex != null">sex =
                #{sex},
            </if>
            <if test="birthDate != null">birth_date =
                #{birthDate},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </if>
            <if test="personalWechatCodeUrl != null">personal_wechat_code_url =
                #{personalWechatCodeUrl},
            </if>
            <if test="disablingCondition != null">disabling_condition =
                #{disablingCondition},
            </if>
            <if test="disablingReason != null">disabling_reason =
                #{disablingReason},
            </if>
            <if test="disease != null">
                disease = #{disease},
            </if>
            <if test="cityCode != null and cityCode != ''">city_code =
                #{cityCode},
            </if>
            <if test="bindingTime != null">binding_time =
                #{bindingTime},
            </if>
            <if test="isChecked != null">is_checked =
                #{isChecked},
            </if>
            <if test="householdRelationship != null">
                household_relationship = #{householdRelationship},
            </if>
            <if test="householdCardNo != null">
                household_card_no = #{householdCardNo},
            </if>
            <if test="signNo != null">
                sign_no = #{signNo},
            </if>
        </trim>
        where patient_code = #{patientCode}
    </update>

    <!-- 获取注册详细信息 -->
    <select id="selectPatientInfoByCode" parameterType="String"
            resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        where patient_code = #{patientCode} limit 1
    </select>

    <!--  身份证号是否已注册  -->
    <select id="isRegisterByCardNo" resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        where card_no = #{cardNo} limit 1
    </select>

    <!-- 获取已注册列表 -->
    <select id="getList" resultType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo">
        <include refid="selectPatientInfoVo"/>
        where bind_openid = #{openid} and del_flag = 0
        <if test="cityCode != null and cityCode != '' and cityCode != 'null'">
            and city_code = #{cityCode}
        </if>
        order by binding_time desc
    </select>

    <!-- 获取注册信息-根据身份证号 -->
    <select id="getByCardNo" resultType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo">
        <include refid="selectPatientInfoVo"/>
        where card_no = #{cardNo} limit 1
    </select>

    <!-- 切换账号 -->
    <update id="switchResident">
        update patient_info
        set is_checked = (case when patient_code = #{patientCode} then '1' else '0' end)
        where bind_openid = #{openid} and del_flag = 0
    </update>

    <!-- 更新选中标识 -->
    <update id="updateChecked">
        update patient_info set is_checked = #{isChecked}
        where patient_code = #{patientCode}
    </update>

    <!-- 获取注册详细信息 -->
    <select id="selectPatientInfoByPhone" parameterType="String" resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        where phone = #{pnone}
        and login_flag = '1' and del_flag = 0
    </select>

    <select id="getPatientInfoByPatientCode" resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        where patient_code = #{patientCode}
        and del_flag = 0
    </select>

    <!-- 查询已登录的居民信息 -->
    <select id="getLoginByOpenId" resultType="com.xinelu.familydoctor.applet.pojo.entity.PatientInfo">
        <include refid="selectPatientInfoVo"/>
        where openid = #{openid} limit 1
    </select>
</mapper>
