<?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.appointmentorderdetails.AppointmentOrderDetailsMapper">

    <resultMap type="AppointmentOrderDetails" id="AppointmentOrderDetailsResult">
        <result property="id" column="id"/>
        <result property="nurseStationItemId" column="nurse_station_item_id"/>
        <result property="nurseStationItemPriceId" column="nurse_station_item_price_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="orderCount" column="order_count"/>
        <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="orderStatus" column="order_status"/>
        <result property="appointmentStatus" column="appointment_status"/>
        <result property="totalPrice" column="total_price"/>
        <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="onDutyPictureUrl" column="on_duty_picture_url"/>
        <result property="serviceProgressVideoUrl" column="service_progress_video_url"/>
        <result property="serviceEndPictureUrl" column="service_end_picture_url"/>
        <result property="userSignaturePictureUrl" column="user_signature_picture_url"/>
        <result property="serveLocationLatitude" column="serve_location_latitude"/>
        <result property="serveLocationLongitude" column="serve_location_longitude"/>
        <result property="serviceLocationName" column="service_location_name"/>
        <result property="delFlag" column="del_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="disablingCondition" column="disabling_condition"/>
        <result property="disablingReason" column="disabling_reason"/>
        <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"/>
    </resultMap>

    <sql id="selectAppointmentOrderDetailsVo">
        SELECT id,
               nurse_station_item_id,
               nurse_station_item_price_id,
               order_no,
               order_count,
               service_address,
               service_date,
               service_start_time,
               service_end_time,
               order_status,
               appointment_status,
               total_price,
               nurse_item_name,
               nurse_item_price,
               item_serve_duration_unit,
               consumable_total_price,
               on_duty_picture_url,
               service_progress_video_url,
               service_end_picture_url,
               user_signature_picture_url,
               serve_location_longitude,
               serve_location_latitude,
               service_location_name,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               disabling_condition,
               disabling_reason,
               caregiver_name,
               caregiver_phone,
               hospital_name,
               department_name,
               hospital_bed_number
        FROM appointment_order_details
    </sql>

    <select id="selectAppointmentOrderDetailsList" parameterType="AppointmentOrderDetails"
            resultMap="AppointmentOrderDetailsResult">
        <include refid="selectAppointmentOrderDetailsVo"/>
        <where>
            <if test="nurseStationItemId != null ">
                and nurse_station_item_id = #{nurseStationItemId}
            </if>
            <if test="nurseStationItemPriceId != null ">
                and nurse_station_item_price_id = #{nurseStationItemPriceId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="orderCount != null ">
                and order_count = #{orderCount}
            </if>
            <if test="serviceAddress != null  and serviceAddress != ''">
                and service_address = #{serviceAddress}
            </if>
            <if test="serviceDate != null ">
                and service_date = #{serviceDate}
            </if>
            <if test="serviceStartTime != null ">
                and service_start_time = #{serviceStartTime}
            </if>
            <if test="serviceEndTime != null ">
                and service_end_time = #{serviceEndTime}
            </if>
            <if test="orderStatus != null  and orderStatus != ''">
                and order_status = #{orderStatus}
            </if>
            <if test="appointmentStatus != null ">
                and appointment_status = #{appointmentStatus}
            </if>
            <if test="totalPrice != null ">
                and total_price = #{totalPrice}
            </if>
            <if test="nurseItemName != null  and nurseItemName != ''">
                and nurse_item_name like concat('%', #{nurseItemName}, '%')
            </if>
            <if test="nurseItemPrice != null ">
                and nurse_item_price = #{nurseItemPrice}
            </if>
            <if test="consumableTotalPrice != null ">
                and consumable_total_price = #{consumableTotalPrice}
            </if>
        </where>
    </select>

    <select id="selectAppointmentOrderDetailsById" parameterType="Long"
            resultMap="AppointmentOrderDetailsResult">
        <include refid="selectAppointmentOrderDetailsVo"/>
        where id = #{id}
    </select>

    <insert id="insertAppointmentOrderDetails" parameterType="AppointmentOrderDetails" useGeneratedKeys="true"
            keyProperty="id">
        insert into appointment_order_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,
            </if>
            <if test="nurseStationItemId != null">nurse_station_item_id,
            </if>
            <if test="nurseStationItemPriceId != null">nurse_station_item_price_id,
            </if>
            <if test="orderNo != null">order_no,
            </if>
            <if test="orderCount != null">order_count,
            </if>
            <if test="serviceAddress != null">service_address,
            </if>
            <if test="serviceDate != null">service_date,
            </if>
            <if test="serviceStartTime != null">service_start_time,
            </if>
            <if test="serviceEndTime != null">service_end_time,
            </if>
            <if test="orderStatus != null">order_status,
            </if>
            <if test="appointmentStatus != null">appointment_status,
            </if>
            <if test="totalPrice != null">total_price,
            </if>
            <if test="nurseItemName != null">nurse_item_name,
            </if>
            <if test="nurseItemPrice != null">nurse_item_price,
            </if>
            <if test="itemServeDurationUnit != null">item_serve_duration_unit,
            </if>
            <if test="consumableTotalPrice != null">consumable_total_price,
            </if>
            <if test="delFlag != null">del_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="disablingCondition != null">disabling_condition,
            </if>
            <if test="disablingReason != null">disabling_reason,
            </if>
            <if test="caregiverName != null">caregiver_name,
            </if>
            <if test="caregiverPhone != null">caregiver_phone,
            </if>
            <if test="hospitalName != null">hospital_name,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="hospitalBedNumber != null">hospital_bed_number,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},
            </if>
            <if test="nurseStationItemId != null">#{nurseStationItemId},
            </if>
            <if test="nurseStationItemPriceId != null">#{nurseStationItemPriceId},
            </if>
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="orderCount != null">#{orderCount},
            </if>
            <if test="serviceAddress != null">#{serviceAddress},
            </if>
            <if test="serviceDate != null">#{serviceDate},
            </if>
            <if test="serviceStartTime != null">#{serviceStartTime},
            </if>
            <if test="serviceEndTime != null">#{serviceEndTime},
            </if>
            <if test="orderStatus != null">#{orderStatus},
            </if>
            <if test="appointmentStatus != null">#{appointmentStatus},
            </if>
            <if test="totalPrice != null">#{totalPrice},
            </if>
            <if test="nurseItemName != null">#{nurseItemName},
            </if>
            <if test="nurseItemPrice != null">#{nurseItemPrice},
            </if>
            <if test="itemServeDurationUnit != null">#{itemServeDurationUnit},
            </if>
            <if test="consumableTotalPrice != null">#{consumableTotalPrice},
            </if>
            <if test="delFlag != null">#{delFlag},
            </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="disablingCondition != null">#{disablingCondition},
            </if>
            <if test="disablingReason != null">#{disablingReason},
            </if>
            <if test="caregiverName != null">#{caregiverName},
            </if>
            <if test="caregiverPhone != null">#{caregiverPhone},
            </if>
            <if test="hospitalName != null">#{hospitalName},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="hospitalBedNumber != null">#{hospitalBedNumber},
            </if>
        </trim>
    </insert>

    <update id="updateAppointmentOrderDetails" parameterType="AppointmentOrderDetails">
        update appointment_order_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationItemId != null">nurse_station_item_id =
                #{nurseStationItemId},
            </if>
            <if test="nurseStationItemPriceId != null">nurse_station_item_price_id =
                #{nurseStationItemPriceId},
            </if>
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="orderCount != null">order_count =
                #{orderCount},
            </if>
            <if test="serviceAddress != null">service_address =
                #{serviceAddress},
            </if>
            <if test="serviceDate != null">service_date =
                #{serviceDate},
            </if>
            <if test="serviceStartTime != null">service_start_time =
                #{serviceStartTime},
            </if>
            <if test="serviceEndTime != null">service_end_time =
                #{serviceEndTime},
            </if>
            <if test="orderStatus != null">order_status =
                #{orderStatus},
            </if>
            <if test="appointmentStatus != null">appointment_status =
                #{appointmentStatus},
            </if>
            <if test="totalPrice != null">total_price =
                #{totalPrice},
            </if>
            <if test="nurseItemName != null">nurse_item_name =
                #{nurseItemName},
            </if>
            <if test="nurseItemPrice != null">nurse_item_price =
                #{nurseItemPrice},
            </if>
            <if test="itemServeDurationUnit != null">item_serve_duration_unit =
                #{itemServeDurationUnit},
            </if>
            <if test="consumableTotalPrice != null">consumable_total_price =
                #{consumableTotalPrice},
            </if>
            <if test="onDutyPictureUrl != null">on_duty_picture_url =
                #{onDutyPictureUrl},
            </if>
            <if test="serviceProgressVideoUrl != null">service_progress_video_url =
                #{serviceProgressVideoUrl},
            </if>
            <if test="serviceEndPictureUrl != null">service_end_picture_url =
                #{serviceEndPictureUrl},
            </if>
            <if test="userSignaturePictureUrl != null">user_signature_picture_url =
                #{userSignaturePictureUrl},
            </if>
            <if test="serveLocationLatitude != null">serve_location_longitude =
                #{serveLocationLatitude},
            </if>
            <if test="serveLocationLongitude != null">serve_location_latitude =
                #{serveLocationLongitude},
            </if>
            <if test="serviceLocationName != null">service_location_name =
                #{serviceLocationName},
            </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="disablingCondition != null">disabling_condition =
                #{disablingCondition},
            </if>
            <if test="disablingReason != null">disabling_reason =
                #{disablingReason},
            </if>
            <if test="caregiverName != null">caregiver_name =
                #{caregiverName},
            </if>
            <if test="caregiverPhone != null">caregiver_phone =
                #{caregiverPhone},
            </if>
            <if test="hospitalName != null">hospital_name =
                #{hospitalName},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="hospitalBedNumber != null">hospital_bed_number =
                #{hospitalBedNumber},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppointmentOrderDetailsById" parameterType="Long">
        delete
        from appointment_order_details
        where id = #{id}
    </delete>

    <update id="updateAppointmentOrderDetailsByIds" parameterType="String">
        update appointment_order_details set del_flag = 1 ,update_time = now() where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="updateAppointmentOrderDetailsOrderNoByIds" parameterType="String">
        update appointment_order_details set del_flag = 1 ,update_time = now()
        <where>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
        </where>
    </update>

    <select id="getAppointmentOrderDetailsOrderNoByIds" parameterType="AppointmentOrderDetails"
            resultMap="AppointmentOrderDetailsResult">
        SELECT id,
        nurse_station_item_id,
        nurse_station_item_price_id,
        order_no,
        order_count,
        service_address,
        service_date,
        service_start_time,
        service_end_time,
        order_status,
        appointment_status,
        total_price,
        nurse_item_name,
        nurse_item_price,
        item_serve_duration_unit,
        consumable_total_price,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        disabling_condition,
        disabling_reason
        FROM
        appointment_order_details
        <where>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            and del_flag=0
        </where>
        ORDER BY create_time DESC
    </select>


    <select id="getAppointmentOrderDetailsByDetailed"
            parameterType="com.xinelu.manage.vo.appointmentorderdetails.AppointmentOrderDetailsVO"
            resultType="com.xinelu.manage.vo.appointmentorderdetails.AppointmentOrderDetailsVO">
        SELECT
        ns.nurse_station_name,
        nsi.nurse_item_name,
        ao.id appointOrderId,
        ao.order_type,
        ao.order_commission_amount,
        ao.companion_start_date,
        ao.companion_end_date,
        ao.companion_days,
        ao.order_write_name,
        aod.id appointOrderDetailsId,
        aod.order_count,
        aod.service_date,
        aod.service_start_time,
        aod.service_end_time,
        aod.service_address,
        aod.order_no,
        aod.order_status,
        aod.nurse_item_price,
        aod.item_serve_duration_unit,
        aod.total_price,
        aod.on_duty_picture_url,
        aod.service_progress_video_url,
        aod.service_end_picture_url,
        aod.user_signature_picture_url,
        aod.serve_location_longitude,
        aod.serve_location_latitude,
        aod.service_location_name,
        oei.evaluate_satisfaction,
        oei.composite_score,
        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_details aod
        LEFT JOIN order_evaluate_info oei ON oei.order_no = aod.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
        LEFT JOIN appointment_order ao ON ao.order_no = aod.order_no
        <where>
            <if test="orderNo != null  and orderNo != ''">
                and aod.order_no = #{orderNo}
            </if>
            AND aod.del_flag = 0
        </where>
    </select>

    <select id="getOrderNoList" parameterType="AppointmentOrderDetails"
            resultMap="AppointmentOrderDetailsResult">
        SELECT id,
        nurse_station_item_id,
        nurse_station_item_price_id,
        order_no,
        order_count,
        service_address,
        service_date,
        service_start_time,
        service_end_time,
        order_status,
        appointment_status,
        total_price,
        nurse_item_name,
        nurse_item_price,
        item_serve_duration_unit,
        consumable_total_price,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        disabling_condition,
        disabling_reason
        FROM appointment_order_details
        <where>
            <if test="orderNo != null  and orderNo != ''">
                and order_no = #{orderNo}
            </if>
        </where>
    </select>

    <select id="getCancelAppointmentOrderInfo" parameterType="string"
            resultType="com.xinelu.manage.vo.CancelAppointmentOrderVO">
        SELECT ao.order_no,
               ao.total_price,
               ao.cancel_appointment_reason,
               aor.nurse_item_name,
               pi.patient_name
        FROM appointment_order ao
                 INNER JOIN appointment_order_details aor ON ao.order_no = aor.order_no
                 INNER JOIN patient_info pi ON ao.patient_id = pi.id
        WHERE ao.del_flag = 0
          AND aor.del_flag = 0
          AND ao.order_no = #{orderNo}
    </select>

    <update id="updateOrderDetailsById">
        UPDATE appointment_order a,
            appointment_order_details b
        SET a.order_status = #{orderStatus},
            b.order_status = #{orderStatus},
            b.on_duty_picture_url = #{onDutyPictureUrl},
            b.service_end_picture_url = #{serviceEndPictureUrl},
            b.user_signature_picture_url = #{userSignaturePictureUrl},
            b.serve_location_longitude = #{serveLocationLongitude},
            b.serve_location_latitude = #{serveLocationLatitude},
            b.service_location_name = #{serviceLocationName},
            a.update_time = now(),
            b.update_time = now()
        WHERE
            a.order_no = b.order_no
          AND a.del_flag = 0
          AND b.del_flag = 0
          AND a.order_status &lt;&gt; #{orderStatus}
          AND b.order_status &lt;&gt; #{orderStatus}
          AND a.order_no = #{orderNo}
          AND b.order_no = #{orderNo}
    </update>

    <select id="selectAppointmentOrderById" parameterType="Long"
            resultType="com.xinelu.manage.vo.appointmentorder.AppointmentOrderRecordVO">
        SELECT ao.id,
        ao.patient_id,
        ao.nurse_station_person_id,
        ao.order_commission_amount,
        ao.total_price,
        aod.order_no,
        aod.order_status,
        aod.nurse_item_name,
        aod.service_address,
        nsp.openid,
        nsp.nurse_station_id,
        nsp.nurse_person_name,
        nsp.phone
        FROM appointment_order ao
        LEFT JOIN appointment_order_details aod ON ao.order_no = aod.order_no
        LEFT JOIN nurse_station_person nsp ON nsp.id = ao.nurse_station_person_id
        <where>
            <if test="orderNo != null  and orderNo != ''">
                AND ao.order_no = #{orderNo}
            </if>
            AND ao.del_flag = 0
            AND aod.del_flag = 0
        </where>
    </select>

    <select id="getAppointmentReceivingOrder" parameterType="string"
            resultType="com.xinelu.manage.vo.appointmentorder.AppointmentReceivingOrderVO">
        SELECT
        nsp.nurse_station_id,
        nsp.nurse_person_name,
        nsp.openid,
        aod.order_no,
        aod.nurse_item_name,
        ao.task_return_reason,
        aod.service_address,
        aod.service_date,
        aod.service_start_time,
        pi.patient_name,
        pi.phone
        FROM
        appointment_order ao
        LEFT JOIN appointment_order_details aod ON ao.order_no = aod.order_no
        LEFT JOIN nurse_station_person nsp ON nsp.id = ao.nurse_station_person_id
        LEFT JOIN patient_info pi ON pi.id = ao.patient_id
        <where>
            <if test="orderNo != null  and orderNo != ''">
                AND ao.order_no = #{orderNo}
            </if>
            AND ao.del_flag = 0
            AND aod.del_flag = 0
        </where>
    </select>
</mapper>
