<?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.applet.mapper.nurseapplogin.NurseAppLoginMapper">

    <!-- 被护理人基本信息以及其疾病信息结果映射 -->
    <resultMap type="com.xinelu.applet.vo.nurseapplogin.PatientAndDiseaseVO"
               id="PatientAndDiseaseVO">
        <result property="patientName" column="patient_name"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="areaCode" column="area_code"/>
        <result property="cardNo" column="card_no"/>
        <result property="homeLongitude" column="home_longitude"/>
        <result property="homeLatitude" column="home_latitude"/>
        <result property="patientId" column="patientId"/>
        <result property="integral" column="integral"/>
        <result property="headPictureUrl" column="head_picture_url"/>
        <result property="locationName" column="location_name"/>
        <result property="sex" column="sex"/>
        <result property="patientCouponCount" column="patientCouponCount"/>
        <result property="birthDate" column="birth_date"/>
        <result property="disablingCondition" column="disabling_condition"/>
        <result property="disablingReason" column="disabling_reason"/>
        <result property="patientCode" column="patient_code"/>
        <result property="disease" column="disease"/>
        <result property="signNo" column="sign_no"/>
        <result property="cityCode" column="city_code"/>
        <collection property="patientDiseaseInfoList" javaType="java.util.List" resultMap="PatientDiseaseInfoResult"/>
    </resultMap>

    <!-- 护理人疾病信息结果映射 -->
    <resultMap type="com.xinelu.applet.vo.nurseapplogin.PatientDiseaseVO" id="PatientDiseaseInfoResult">
        <result property="patientDiseaseId" column="patientDiseaseId"/>
        <result property="diseaseId" column="disease_id"/>
        <result property="diseaseName" column="disease_name"/>
    </resultMap>

    <select id="selectPatientDiseaseInfo" resultType="int">
        select
        count(1)
        from patient_disease_info
        <where>
            <if test="patientId != null">
                patient_id = #{patientId}
            </if>
        </where>
    </select>

    <delete id="deletePatientDiseaseInfo" parameterType="long">
        delete
        from patient_disease_info
        where patient_id = #{patientId}
    </delete>

    <select id="getCardNoCount" resultType="java.lang.Integer">
        select
        count(1)
        from patient_info
        <where>
            del_flag = 0
            <if test="cardNo != null">
                and card_no = #{cardNo}
            </if>
        </where>
    </select>

    <select id="getPatientDiseaseByPatientId" resultMap="PatientAndDiseaseVO">
        SELECT
        pi.id patientId,
        pi.patient_name,
        pi.phone,
        pi.area_code,
        pi.card_no,
        pi.address,
        pi.integral,
        pi.home_longitude,
        pi.home_latitude,
        pi.head_picture_url,
        pi.birth_date,
        pi.sex,
        pi.location_name,
        pi.disabling_condition,
        pi.disabling_reason,
        pi.patient_code,
        pi.disease,
        pi.sign_no,
        pi.city_code,
        pdi.id patientDiseaseId,
        pdi.disease_id,
        pdi.disease_name,
        (SELECT count(1) patientCouponCount FROM patient_coupon_receive WHERE patient_id = pi.id and use_status &lt;&gt;
        'WAIT_RECEIVE') patientCouponCount
        FROM patient_info pi
        LEFT JOIN patient_disease_info pdi ON pdi.patient_id = pi.id
        <where>
            pi.del_flag = 0
            <if test="patientId != null and patientId != ''">
                and pi.id = #{patientId}
            </if>
        </where>
    </select>

    <select id="selectAppointmentOrderDetailsByPatientId"
            resultType="com.xinelu.applet.vo.nursepersonapplogin.OrderAndItemVO">
        SELECT
        aod.id,
        aod. nurse_station_item_id stationItemId,
        aod.nurse_station_item_price_id stationItemPriceId,
        aod.order_no,
        aod.order_count,
        aod.order_status,
        aod.appointment_status,
        aod.total_price,
        aod.nurse_item_name,
        aod.item_serve_duration_unit,
        aod.create_time,
        nsi.item_picture_url,
        ns.id stationId,
        ao.order_type
        FROM
        appointment_order ao
        LEFT JOIN appointment_order_details aod ON aod.order_no = ao.order_no
        LEFT JOIN nurse_station_item nsi ON nsi.id = aod.nurse_station_item_id
        LEFT JOIN nurse_station ns ON ns.id = nsi.nurse_station_id
        <where>
            ao.del_flag = 0
            <if test="patientId != null and patientId != ''">
                and ao.patient_id = #{patientId}
            </if>
            <if test="orderStatus != null and orderStatus != ''">
                AND ao.order_status = #{orderStatus}
            </if>
        </where>
        ORDER BY aod.create_time DESC
    </select>

    <select id="getLoginFlagByPatientId" resultType="integer">
        select login_flag
        from patient_info
        <where>
            del_flag = 0
            <if test="patientId != null">
                and id = #{patientId}
            </if>
        </where>
    </select>

    <select id="selectGoodsOrderListByPatient" resultType="com.xinelu.manage.domain.goodsOrder.GoodsOrder">
        select id,
        order_status
        from goods_order
        <where>
            <if test="patientId != null">
                patient_id = #{patientId}
            </if>
            <if test="orderType != null and orderType!= ''">
                AND order_type &lt;&gt; #{orderType}
            </if>
        </where>
    </select>

    <update id="updateAppointmentByOrderId">
        UPDATE appointment_order a,
            appointment_order_details b
        SET a.order_status = #{orderStatus},
            a.cancel_appointment_reason = #{cancelAppointmentReason},
            a.confirm_refund_status = #{confirmRefundStatus},
            b.order_status = #{orderStatus},
            a.update_time = now(),
            b.update_time = now()
        WHERE
            a.order_no = b.order_no
          AND a.del_flag = 0
          AND a.order_status &lt;&gt; #{orderStatus}
          AND b.order_status &lt;&gt; #{orderStatus}
          AND a.id = #{id}
    </update>
</mapper>
