<?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.ruoyi.info.order.mapper.OrderMapper">

    <resultMap type="com.ruoyi.info.order.domain.Order" id="OrderResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="userId" column="user_id"/>
        <result property="status" column="status"/>
        <result property="hospitalId" column="hospital_id"/>
        <result property="hospitalInfo" column="hospital_info"/>
        <result property="hospitalDepartmentId" column="hospital_department_id"/>
        <result property="hospitalDepartmentInfo" column="hospital_department_info"/>
        <result property="roomNumber" column="room_number"/>
        <result property="remarks" column="remarks"/>
        <result property="patientId" column="patient_id"/>
        <result property="patientInfo" column="patient_info"/>
        <result property="serviceId" column="service_id"/>
        <result property="serviceInfo" column="service_info"/>
        <result property="serviceUnit" column="service_unit"/>
        <result property="serviceUnitDuration" column="service_unit_duration"/>
        <result property="expectedTime" column="expected_time"/>
        <result property="orderNum" column="order_num"/>
        <result property="sellingPrice" column="selling_price"/>
        <result property="totalPrice" column="total_price"/>
        <result property="workerId" column="worker_id"/>
        <result property="cancellationMethod" column="cancellation_method"/>
        <result property="cancellationTime" column="cancellation_time"/>
        <result property="cancellationReason" column="cancellation_reason"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="queryAllPageList" resultType="com.ruoyi.info.order.domain.vo.OrderVo">
        <include refid="queryList"/>
    </select>

    <select id="queryAllList" resultType="com.ruoyi.info.order.domain.vo.OrderVo">
        <include refid="queryList"/>
    </select>

    <select id="queryHospitalOrderStatistics"
            resultType="com.ruoyi.info.hospital.domain.vo.HospitalStatisticsVo">
        select o.hospital_id, count(o.id) as orderNum from tb_order o
        group by o.hospital_id
        order by orderNum desc
        limit 10
    </select>

    <sql id="queryList">
        select o.*, u.nickname as userName, u.mobile as userMobile from tb_order o
        left join tb_user u on u.id = o.user_id
        <where>
            <if test="bo.orderNo != null and bo.orderNo != ''">
                and o.order_no like concat('%', #{bo.orderNo}, '%')
            </if>
            <if test="bo.userId != null">
                and o.user_id = #{bo.userId}
            </if>
            <if test="bo.status != null">
                and o.status = #{bo.status.code}
            </if>
            <if test="bo.hospitalId != null">
                and o.hospital_id = #{bo.hospitalId}
            </if>
            <if test="bo.userMobile != null and bo.userMobile != ''">
                and u.mobile like concat('%', #{bo.userMobile}, '%')
            </if>
            <if test="bo.serviceName != null and bo.serviceName != ''">
                and JSON_UNQUOTE(JSON_EXTRACT(o.service_info, '$.name')) like concat('%', #{bo.serviceName}, '%')
            </if>
            <if test="bo.startTime != null">
                and o.create_time >= #{bo.startTime}
            </if>
            <if test="bo.endTime != null">
                and o.create_time <![CDATA[ <= ]]> #{bo.endTime}
            </if>
        </where>
        order by o.create_time desc
    </sql>

</mapper>
