<?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.patientinfo.PatientInfoMapper">

    <resultMap type="com.xinelu.manage.vo.patientinfo.PatientInfoVO" id="PatientInfoResult">
        <result property="id" column="id"/>
        <result property="communityCode" column="community_code"/>
        <result property="areaCode" column="area_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="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="password" column="password"/>
        <result property="integral" column="integral"/>
        <result property="loginFlag" column="login_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="areaName" column="area_name"/>
        <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="bindOpenid" column="bind_openid"/>
    </resultMap>

    <sql id="selectPatientInfoVo">
        select id,
               community_code,
               area_code,
               patient_code,
               patient_name,
               card_no,
               user_id,
               unionid,
               openid,
               phone,
               address,
               urgent_contact_name,
               urgent_contact_phone,
               community_alias_name,
               home_longitude,
               home_latitude,
               head_picture_url,
               password,
               integral,
               login_flag,
               create_by,
               create_time,
               primary_account_flag,
               service_ids,
               location_name,
               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,
               bind_openid
        from patient_info
    </sql>

    <select id="selectPatientInfoByPatientId" parameterType="Long"
            resultMap="PatientInfoResult">
        <include refid="selectPatientInfoVo"/>
        where id = #{id}
    </select>

    <select id="selectPatientInfoList" parameterType="com.xinelu.manage.vo.patientinfo.PatientInfoVO"
            resultMap="PatientInfoResult">
        select
        pi.id,
        pi.community_code,
        pi.area_code,
        pi.patient_code,
        pi.patient_name,
        pi.card_no,
        pi.user_id,
        pi.unionid,
        pi.openid,
        pi.phone,
        pi.address,
        pi.urgent_contact_name,
        pi.urgent_contact_phone,
        pi.community_alias_name,
        pi.home_longitude,
        pi.home_latitude,
        pi.head_picture_url,
        pi.password,
        pi.integral,
        pi.login_flag,
        pi.create_by,
        pi.create_time,
        pi.disabling_condition,
        pi.disabling_reason,
        pi.bind_openid,
        sa.area_name
        from patient_info pi
        left join sys_area sa on sa.area_code = pi.area_code
        <where>
            pi.del_flag = 0
            <if test="communityCode != null  and communityCode != ''">
                and pi.community_code = #{communityCode}
            </if>
            <if test="areaCode != null  and areaCode != ''">
                and pi.area_code = #{areaCode}
            </if>
            <if test="patientCode != null  and patientCode != ''">
                and pi.patient_code = #{patientCode}
            </if>
            <if test="patientName != null  and patientName != ''">
                and pi.patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and pi.card_no = #{cardNo}
            </if>
            <if test="userId != null ">
                and pi.user_id = #{userId}
            </if>
            <if test="unionid != null  and unionid != ''">
                and pi.unionid = #{unionid}
            </if>
            <if test="openid != null  and openid != ''">
                and pi.openid = #{openid}
            </if>
            <if test="phone != null  and phone != ''">
                and pi.phone = #{phone}
            </if>
            <if test="address != null  and address != ''">
                and pi.address = #{address}
            </if>
            <if test="urgentContactName != null  and urgentContactName != ''">
                and pi.urgent_contact_name like concat('%', #{urgentContactName}, '%')
            </if>
            <if test="urgentContactPhone != null  and urgentContactPhone != ''">
                and pi.urgent_contact_phone = #{urgentContactPhone}
            </if>
            <if test="communityAliasName != null  and communityAliasName != ''">
                and pi.community_alias_name = #{communityAliasName}
            </if>
            <if test="homeLongitude != null  and homeLongitude != ''">
                and pi.home_longitude = #{homeLongitude}
            </if>
            <if test="homeLatitude != null  and homeLatitude != ''">
                and pi.home_latitude = #{homeLatitude}
            </if>
            <if test="headPictureUrl != null  and headPictureUrl != ''">
                and pi.head_picture_url = #{headPictureUrl}
            </if>
            <if test="password != null  and password != ''">
                and pi.password = #{password}
            </if>
            <if test="integral != null  and integral != ''">
                and pi.integral = #{integral}
            </if>
            <if test="loginFlag != null  and loginFlag != ''">
                and pi.login_flag = #{loginFlag}
            </if>
            <if test="personalWechatCodeUrl != null  and personalWechatCodeUrl != ''">
                and pi.personal_wechat_code_url = #{personalWechatCodeUrl}
            </if>
        </where>
        ORDER BY pi.create_time DESC
    </select>

    <select id="selectPatientInfoById" parameterType="Long"
            resultMap="PatientInfoResult">
        select pi.id,
               pi.community_code,
               pi.area_code,
               pi.patient_code,
               pi.patient_name,
               pi.card_no,
               pi.user_id,
               pi.unionid,
               pi.openid,
               pi.phone,
               pi.address,
               pi.urgent_contact_name,
               pi.urgent_contact_phone,
               pi.community_alias_name,
               pi.home_longitude,
               pi.home_latitude,
               pi.head_picture_url,
               pi.password,
               pi.integral,
               pi.login_flag,
               pi.create_by,
               pi.create_time,
               pi.disabling_condition,
               pi.disabling_reason,
               pi.bind_openid,
               sa.area_name
        from patient_info pi
                 INNER join sys_area sa on sa.area_code = pi.area_code
        where pi.id = #{id}
    </select>

    <insert id="insertPatientInfo" parameterType="PatientInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="communityCode != null and communityCode != ''">community_code,
            </if>
            <if test="areaCode != null and areaCode != ''">area_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="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="password != null">password,
            </if>
            <if test="integral != null">integral,
            </if>
            <if test="loginFlag != null">login_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="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">city_code,
            </if>
            <if test="bindingTime != null">binding_time,
            </if>
            <if test="isChecked != null">is_checked,
            </if>
            <if test="bindOpenid != null">bind_openid,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="communityCode != null and communityCode != ''">#{communityCode},
            </if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},
            </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="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="password != null">#{password},
            </if>
            <if test="integral != null">#{integral},
            </if>
            <if test="loginFlag != null">#{loginFlag},
            </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="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">#{cityCode},
            </if>
            <if test="bindingTime != null">#{bindingTime},
            </if>
            <if test="isChecked != null">#{isChecked},
            </if>
            <if test="bindOpenid != null">#{bindOpenid},</if>
        </trim>
    </insert>

    <update id="updatePatientInfo" parameterType="PatientInfo">
        update patient_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="communityCode != null and communityCode != ''">community_code =
                #{communityCode},
            </if>
            <if test="areaCode != null and areaCode != ''">area_code =
                #{areaCode},
            </if>
            <if test="patientCode != null">patient_code =
                #{patientCode},
            </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="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="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="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="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">city_code =
                #{cityCode},
            </if>
            <if test="bindingTime != null">binding_time =
                #{bindingTime},
            </if>
            <if test="isChecked != null">is_checked =
                #{isChecked},
            </if>
            <if test="bindOpenid != null">
                bind_openid = #{bindOpenid},
            </if>
        </trim>
        where id = #{id}
    </update>

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

    <update id="updateHeadPatientHead" parameterType="PatientInfo">
        update patient_info
        set head_picture_url = #{headPictureUrl}
        where id = #{id}
    </update>

    <update id="updateHeadPatientHeadPortrait" parameterType="PatientInfo">
        update patient_info
        set head_picture_url = #{headPictureUrl}
        where id = #{id}
    </update>

    <select id="getPatientInfoById" resultType="com.xinelu.manage.vo.patientinfo.PatientInfoVO">
        SELECT id,
               community_code,
               area_code,
               patient_code,
               patient_name,
               card_no,
               user_id,
               unionid,
               openid,
               phone,
               address,
               urgent_contact_name,
               urgent_contact_phone,
               community_alias_name,
               home_longitude,
               home_latitude,
               head_picture_url,
               password,
               integral,
               login_flag,
               create_by,
               create_time,
               service_ids,
               primary_account_flag,
               del_flag,
               invitation_patient_id,
               total_sign_in_days,
               sex,
               birth_date,
               personal_wechat_code_url,
               disabling_condition,
               disabling_reason,
               city_code,
               binding_time,
               bind_openid
        FROM patient_info
        WHERE id = #{id}
          and del_flag = 0
    </select>

	<select id="getPatientInfoByCardNo" resultType="com.xinelu.manage.vo.patientinfo.PatientInfoVO">
		SELECT id,
		       community_code,
		       area_code,
		       patient_code,
		       patient_name,
		       card_no,
		       user_id,
		       unionid,
		       openid,
		       phone,
		       address,
		       urgent_contact_name,
		       urgent_contact_phone,
		       community_alias_name,
		       home_longitude,
		       home_latitude,
		       head_picture_url,
		       password,
		       integral,
		       login_flag,
		       create_by,
		       create_time,
		       service_ids,
		       primary_account_flag,
		       del_flag,
		       invitation_patient_id,
		       total_sign_in_days,
		       sex,
		       birth_date,
		       personal_wechat_code_url,
		       disabling_condition,
		       disabling_reason,
               bind_openid
		FROM patient_info
		WHERE card_no = #{cardNo}
		  and del_flag = 0 limit 1
	</select>

    <insert id="insertAppletLoginPatientInfo" parameterType="PatientInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into patient_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openid != null">openid,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="areaCode != null and areaCode != ''">area_code,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openid != null">#{openid},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="areaCode != null and areaCode != ''">#{areaCode},
            </if>
        </trim>
    </insert>

    <update id="addPatientIntegralCount">
        update patient_info
        set integral    = integral + #{integral},
            update_time = now()
        where id = #{id}
    </update>

    <update id="reducePatientIntegralCount">
        update patient_info
        set integral    = integral - #{integral},
            update_time = now()
        where id = #{id}
          and integral &gt; 0
    </update>

    <update id="updatePatientDisabling">
        update patient_info
        set disabling_condition = #{disablingCondition},
            disabling_reason    = #{disablingReason}
        where id = #{id}
    </update>

    <update id="updatePersonalWeChatCodeUrl">
        update patient_info
        set personal_wechat_code_url = #{personalWeChatCodeUrl},
            update_time              = now()
        where id = #{id}
    </update>

    <!-- 更新签约标识 -->
    <update id="updateSignNo">
        update patient_info set sign_no = #{signNo} where id = #{patientId}
    </update>

</mapper>
