<?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.nursingorder.NursingOrderMapper">
    <!-- 预约服务订单详情映射关系 -->
    <resultMap id="getAppointmentDetailsResultMap"
               type="com.xinelu.applet.vo.specialdisease.AppointmentOrderDetailsInfoVO">
        <result property="patientId" column="patient_id"/>
        <result property="appointmentOrderId" column="appointmentOrderId"/>
        <result property="patientName" column="patient_name"/>
        <result property="patientPhone" column="patientPhone"/>
        <result property="orderNo" column="order_no"/>
        <result property="totalPrice" column="total_price"/>
        <result property="orderStatus" column="order_status"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="nurseStationItemId" column="nurse_station_item_id"/>
        <result property="serviceAddress" column="service_address"/>
        <result property="serviceDate" column="service_date"/>
        <result property="serviceStartTime" column="service_start_time"/>
        <result property="serviceEndTime" column="service_end_time"/>
        <result property="nurseItemName" column="nurse_item_name"/>
        <result property="nurseItemPrice" column="nurse_item_price"/>
        <result property="itemServeDurationUnit" column="item_serve_duration_unit"/>
        <result property="consumableTotalPrice" column="consumable_total_price"/>
        <result property="disablingCondition" column="disabling_condition"/>
        <result property="disablingReason" column="disabling_reason"/>
        <result property="orderType" column="order_type"/>
        <result property="companionStartDate" column="companion_start_date"/>
        <result property="companionEndDate" column="companion_end_date"/>
        <result property="companionDays" column="companion_days"/>
        <result property="orderWriteName" column="order_write_name"/>
        <result property="caregiverName" column="caregiver_name"/>
        <result property="caregiverPhone" column="caregiver_phone"/>
        <result property="hospitalName" column="hospital_name"/>
        <result property="departmentName" column="department_name"/>
        <result property="hospitalBedNumber" column="hospital_bed_number"/>
        <collection property="itemConsumableList" javaType="java.util.List"
                    resultMap="getAppointmentOrderConsumableResult"/>
    </resultMap>

    <!-- 预约服务耗材映射关系 -->
    <resultMap type="com.xinelu.manage.domain.appointmentorderconsumable.AppointmentOrderConsumable"
               id="getAppointmentOrderConsumableResult">
        <result property="id" column="id"/>
        <result property="appointOrderDetailsId" column="appoint_order_details_id"/>
        <result property="orderConsumableName" column="order_consumable_name"/>
        <result property="orderConsumablePrice" column="order_consumable_price"/>
        <result property="orderConsumableCount" column="order_consumable_count"/>
        <result property="consumableUnit" column="consumable_unit"/>
    </resultMap>

    <select id="getNursingOrderVOByPatientId" parameterType="long"
            resultType="com.xinelu.applet.vo.nursingorder.NursingOrderInfoVO">
        SELECT
        aod.order_no,
        aod.service_date,
        aod.service_start_time,
        aod.service_end_time,
        aod.appointment_status,
        aod.total_price,
        aod.item_serve_duration_unit,
        aod.nurse_item_name,
        aod.order_status,
        nsi.advance_appoint_duration,
        nsi.item_picture_url,
        ns.nurse_station_name
        FROM
        appointment_order_details aod
        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
        LEFT JOIN appointment_order ao ON ao.order_no = aod.order_no
        WHERE
        aod.del_flag = 0 and ao.del_flag = 0
        <if test="patientId != null">
            AND ao.patient_id = #{patientId}
        </if>
        ORDER BY
        aod.service_date DESC,
        aod.service_start_time DESC
    </select>


    <select id="getGoodsOrder" parameterType="com.xinelu.applet.vo.nursingorder.AppletGoodsOrderVO"
            resultType="com.xinelu.applet.vo.nursingorder.AppletGoodsOrderVO">
        SELECT
        gr.id goodsOrderId,
        gr.patient_id,
        gr.order_no goOrderNo,
        gr.order_status,
        gr.total_price,
        gr.receiver,
        gr.receive_address,
        gr.phone,
        gr.order_time,
        gr.refund_type,
        gr.refund_reason_dict_id,
        gr.refund_reason_remark,
        gr.confirm_refund_status,
        gr.express_no,
        gr.buy_source,
        gr.order_type,
        gr.original_total_price,
        gr.order_channel,
        gr.hospital_person_id,
        gr.health_consultation_content,
        gr.health_appoint_date,
        gr.remark,
        god.integral_deduction_count,
        god.id goodsOrderDetailsId,
        god.goods_name,
        god.goods_count,
        god.goods_attribute_name,
        god.goods_attribute_content,
        god.goods_price,
        god.total_price godTotalPrice,
        god.discount_price,
        god.transport_price,
        god.give_integral,
        gad.attribute_piture_url,
        gad.integral_exchange_flag,
        god.integral_exchange_sill,
        god.integral_exchange_count,
        god.coupon_id,
        god.coupon_title,
        sdd.dict_label,
        gr.update_time appleTime,
        (select rf.success_time from refund_info rf where rf.order_no = gr.order_no) update_time,
        IF( gr.order_type = 'HEALTH_CONSULTATION', ( SELECT person_picture_url FROM hospital_person_info WHERE id =
        gr.hospital_person_id ), NULL ) personPictureUrl,
        IF( gr.order_type = 'HEALTH_CONSULTATION', ( SELECT person_phone FROM hospital_person_info WHERE id =
        gr.hospital_person_id ), NULL ) personPhone,
        IF( gr.order_type = 'HEALTH_CONSULTATION', ( SELECT academic_title FROM hospital_person_info WHERE id =
        gr.hospital_person_id ), NULL ) academicTitle,
        gr.hospital_person_name
        FROM
        goods_order gr
        LEFT JOIN goods_order_details god ON gr.id = god.goods_order_id
        LEFT JOIN goods_attribute_details gad ON god.goods_attribute_details_id = gad.id
        LEFT JOIN sys_dict_data sdd ON sdd.dict_code = gr.refund_reason_dict_id
        LEFT JOIN hospital_person_info hpi on hpi.id = gr.hospital_person_id
        <where>
            <if test="patientId != null ">
                and gr.patient_id = #{patientId}
            </if>
            <if test="orderStatus != null  and orderStatus != ''">
                and gr.order_status = #{orderStatus}
            </if>
            <if test="goodsName != null  and goodsName != ''">
                and god.goods_name like concat('%', #{goodsName}, '%')
            </if>
            <if test="goodsOrderId != null ">
                and gr.id = #{goodsOrderId}
            </if>
            <if test="goOrderNo != null ">
                and gr.order_no = #{goOrderNo}
            </if>
            <if test="orderType != null  and orderType != ''">
                and gr.order_type = #{orderType}
            </if>
            <if test="orderTypeList != null and orderTypeList.size > 0">
                and gr.order_type in
                <foreach collection="orderTypeList" item="orderTypeList" open="(" separator="," close=")">
                    #{orderTypeList}
                </foreach>
            </if>
            <if test="hospitalPersonName != null  and hospitalPersonName != ''">
                and hpi.person_name like concat('%', #{hospitalPersonName}, '%')
            </if>
            and gr.del_flag = 0
            and god.del_flag = 0
        </where>
        ORDER BY gr.order_time DESC
    </select>
    <select id="getConsultationOrder" parameterType="string"
            resultType="com.xinelu.applet.vo.nursingorder.ConsultationGoodVO">
        SELECT
            gr.order_no,
            gr.buy_source,
            gr.total_price,
            gr.order_time,
            gr.order_status,
            IF( gr.order_type = 'HEALTH_CONSULTATION',
            (SELECT person_picture_url FROM hospital_person_info WHERE id =gr.hospital_person_id ), NULL ) personPictureUrl,
            IF( gr.order_type = 'HEALTH_CONSULTATION', ( SELECT person_phone FROM hospital_person_info WHERE id =gr.hospital_person_id ), NULL ) personPhone,
            IF( gr.order_type = 'HEALTH_CONSULTATION', ( SELECT academic_title FROM hospital_person_info WHERE id = gr.hospital_person_id ), NULL ) academicTitle,
            gr.hospital_person_name,
            ci.problem_description,
            ci.situation_description,
            ci.problem_statement,
            ci.medical_record,
            ci.appointment_date,
            ci.appointment_start_time,
            ci.appointment_end_time
        FROM
            goods_order gr
                LEFT JOIN hospital_person_info hpi on hpi.id = gr.hospital_person_id
                LEFT JOIN consultation_info ci ON ci.id =gr.consultation_info_id
        <where>
            <if test="orderNo != null  and orderNo != ''">
                and gr.order_no = #{orderNo}
            </if>
        </where>
        and gr.del_flag = 0
    </select>

    <select id="getRefundReason" parameterType="com.xinelu.common.core.domain.entity.SysDictData"
            resultType="com.xinelu.common.core.domain.entity.SysDictData">
        SELECT
        dict_code,
        dict_sort,
        dict_label,
        dict_value,
        dict_type
        FROM
        sys_dict_data
        <where>
            <if test="dictType != null and dictType != ''">
                AND dict_type = #{dictType}
            </if>
        </where>
    </select>


    <update id="updateGoodsOrder" parameterType="com.xinelu.manage.domain.goodsOrder.GoodsOrder">
        update goods_order
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationId != null">
                nurse_station_id =
                #{nurseStationId},
            </if>
            <if test="patientId != null">
                patient_id =
                #{patientId},
            </if>
            <if test="orderNo != null">
                order_no =
                #{orderNo},
            </if>
            <if test="orderStatus != null">
                order_status =
                #{orderStatus},
            </if>
            <if test="totalPrice != null">
                total_price =
                #{totalPrice},
            </if>
            <if test="receiver != null">
                receiver =
                #{receiver},
            </if>
            <if test="receiveAddress != null">
                receive_address =
                #{receiveAddress},
            </if>
            <if test="phone != null">
                phone =
                #{phone},
            </if>
            <if test="orderTime != null">
                order_time =
                #{orderTime},
            </if>
            <if test="orderChannel != null">
                order_channel =
                #{orderChannel},
            </if>
            <if test="buySource != null">
                buy_source =
                #{buySource},
            </if>
            <if test="remark != null">
                remark =
                #{remark},
            </if>
            <if test="delFlag != null">
                del_flag =
                #{delFlag},
            </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="refundType != null">
                refund_type =
                #{refundType},
            </if>
            <if test="refundReasonDictId != null">
                refund_reason_dict_id =
                #{refundReasonDictId},
            </if>
            <if test="refundReasonRemark != null">
                refund_reason_remark =
                #{refundReasonRemark},
            </if>
            <if test="confirmRefundStatus != null">
                confirm_refund_status =
                #{confirmRefundStatus},
            </if>
        </trim>
        where id = #{id}
    </update>

    <select id="getAppointmentOrderDetailsInfo" resultMap="getAppointmentDetailsResultMap">


                        SELECT pi.patient_name,
                               pi.phone patientPhone,
                               aor.id   appointmentOrderId,
                               aor.patient_id,
                               aor.order_no,
                               aor.total_price,
                               aor.order_status,
                               aor.remark,
                               aor.create_time,
                               aor.order_type,
                               aor.companion_start_date,
                               aor.companion_end_date,
                               aor.companion_days,
                               aor.order_write_name,
                               aod.nurse_station_item_id,
                               aod.service_address,
                               aod.service_date,
                               aod.service_start_time,
                               aod.service_end_time,
                               aod.nurse_item_name,
                               aod.nurse_item_price,
                               aod.item_serve_duration_unit,
                               aod.consumable_total_price,
                               aoc.id,
                               aoc.appoint_order_details_id,
                               aoc.order_consumable_name,
                               aoc.order_consumable_price,
                               aoc.order_consumable_count,
                               aoc.consumable_unit,
                               aod.disabling_condition,
                               aod.disabling_reason,
                               aod.caregiver_name,
                               aod.caregiver_phone,
                               aod.hospital_name,
                               aod.department_name,
                               aod.hospital_bed_number
                        FROM appointment_order aor
                                 LEFT JOIN appointment_order_details aod ON aor.order_no = aod.order_no
                                 LEFT JOIN appointment_order_consumable aoc ON aod.id = aoc.appoint_order_details_id
                                 LEFT JOIN patient_info pi ON pi.id = aor.patient_id
                        where aor.order_no = #{ordeNo}
                          AND aor.del_flag = 0
                          and aod.del_flag = 0


    </select>

    <select id="getNurseStationByItemId"
            resultType="com.xinelu.applet.vo.specialdisease.AppointmentOrderDetailsInfoVO">


                        SELECT ns.nurse_station_name nurseStationName,
                               ns.phone              stationPhone,
                               ns.address,
                               ns.station_picture_url,
                               nsi.item_picture_url
                        FROM nurse_station_item nsi
                                 LEFT JOIN nurse_station ns ON nsi.nurse_station_id = ns.id
                        WHERE nsi.id = #{nurseStationItemId}


    </select>

    <select id="getAppointmentOrderListByParentId" resultType="com.xinelu.applet.vo.nursingorder.PatientOrder">
        SELECT
            ao.patient_id,
            ao.order_no,
            ao.order_status,
            ao.create_time,
            aod.nurse_item_name orderName,
            nsi.item_picture_url pictureUrl,
            'APPOINTMENT_ORDER' AS orderType,
            nei.evaluate_satisfaction,
            'SPRING_DOCTOR' AS orderSource
        FROM
            appointment_order ao
            LEFT JOIN appointment_order_details aod ON ao.order_no = aod.order_no
            LEFT JOIN nurse_station_item nsi ON nsi.id = aod.nurse_station_item_id
            LEFT JOIN order_evaluate_info nei ON nei.order_no = ao.order_no
        where
            ao.order_status = #{orderStatus}
        and ao.patient_id = #{patientId}
        and ao.del_flag = 0
    </select>

    <select id="getGoodsOrderAndConsultationOrder"
            resultType="com.xinelu.applet.vo.nursingorder.PatientOrder">
        SELECT
            gor.patient_id,
            gor.order_no,
            gor.order_status,
            gor.order_time createTime,
            gor.order_type,
            god.goods_name orderName,
            gad.attribute_piture_url pictureUrl,
            hpi.person_picture_url personPictureUrl,
            nei.composite_score,
            'SPRING_DOCTOR' AS orderSource
        FROM
            goods_order gor
            LEFT JOIN goods_order_details god ON god.order_no = gor.order_no
            LEFT JOIN goods_attribute_details gad ON gad.id = god.goods_attribute_details_id
            LEFT JOIN hospital_person_info hpi ON hpi.id = gor.hospital_person_id
            LEFT JOIN order_evaluate_info nei ON nei.order_no = gor.order_no
        where
        <if test="orderStatusList != null and orderStatusList.size > 0">
            gor.order_status in
            <foreach collection="orderStatusList" item="orderStatusList" open="(" separator="," close=")">
                #{orderStatusList}
            </foreach>
        </if>
        and gor.patient_id = #{patientId}
        and gor.del_flag = 0
    </select>

    <select id="getOrderEvaluateByPatientId" resultType="com.xinelu.applet.vo.nursingorder.PatientOrder">
       SELECT order_no
       FROM order_evaluate_info
       where patient_id = #{patientId}
    </select>
</mapper>
