<?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.gxwebsoft.clinic.mapper.ClinicPrescriptionMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, b.real_name, b.age, b.sex, b.height, b.weight, c.real_name as doctorName, c.qualification, d.order_status as orderStatus, d.pay_status as payStatus
        FROM clinic_prescription a
        LEFT JOIN clinic_patient_user b ON a.user_id = b.user_id
        LEFT JOIN clinic_doctor_user c ON a.doctor_id = c.user_id
        LEFT JOIN shop_order d ON a.order_no = d.order_no
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.clinicId != null">
                AND a.clinic_id = #{param.clinicId}
            </if>
            <if test="param.userId != null and param.withDoctor == null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.userId != null and param.withDoctor != null">
                AND (a.user_id = #{param.userId} OR a.doctor_id = #{param.userId})
            </if>

            <if test="param.doctorId != null">
                AND a.doctor_id = #{param.doctorId}
            </if>
            <if test="param.orderNo != null">
                AND a.order_no LIKE CONCAT('%', #{param.orderNo}, '%')
            </if>
            <if test="param.visitRecordId != null">
                AND a.visit_record_id = #{param.visitRecordId}
            </if>
            <if test="param.prescriptionType != null">
                AND a.prescription_type = #{param.prescriptionType}
            </if>
            <if test="param.diagnosis != null">
                AND a.diagnosis LIKE CONCAT('%', #{param.diagnosis}, '%')
            </if>
            <if test="param.treatmentPlan != null">
                AND a.treatment_plan LIKE CONCAT('%', #{param.treatmentPlan}, '%')
            </if>
            <if test="param.decoctionInstructions != null">
                AND a.decoction_instructions LIKE CONCAT('%', #{param.decoctionInstructions}, '%')
            </if>
            <if test="param.orderPrice != null">
                AND a.order_price = #{param.orderPrice}
            </if>
            <if test="param.price != null">
                AND a.price = #{param.price}
            </if>
            <if test="param.payPrice != null">
                AND a.pay_price = #{param.payPrice}
            </if>
            <if test="param.isInvalid != null">
                AND a.is_invalid = #{param.isInvalid}
            </if>
            <if test="param.isSettled != null">
                AND a.is_settled = #{param.isSettled}
            </if>
            <if test="param.ids != null">
              AND a.id IN
              <foreach collection="param.ids" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.settleTime != null">
                AND a.settle_time LIKE CONCAT('%', #{param.settleTime}, '%')
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                )
              </if>
          <!-- 订单状态筛选：-1全部，0待支付，1待发货，2待核销，3待收货，4待评价，5已完成，6已退款，7已删除 -->
          <if test="param.statusFilter != null and param.statusFilter != -1">
            <if test="param.statusFilter == 0">
              <!-- 0待支付：未付款 -->
              AND d.pay_status = 0 AND d.order_status = 0
            </if>
            <if test="param.statusFilter == 1">
              <!-- 1待发货：已付款但未发货 -->
              AND d.pay_status = 1 AND d.delivery_status = 10 AND d.order_status = 0
            </if>
            <if test="param.statusFilter == 2">
              <!-- 2待核销：已付款但订单状态为未使用 -->
              AND d.pay_status = 1 AND d.order_status = 0
            </if>
            <if test="param.statusFilter == 3">
              <!-- 3待收货：已发货但订单状态不是已完成 -->
              AND d.delivery_status = 20 AND d.order_status != 1
            </if>
            <if test="param.statusFilter == 4">
              <!-- 4待评价：订单已完成但可能需要评价 -->
              AND d.order_status = 1 AND d.evaluate_status = 0
            </if>
            <if test="param.statusFilter == 5">
              <!-- 5已完成：订单状态为已完成 -->
              AND d.order_status = 1
            </if>
            <if test="param.statusFilter == 6">
              <!-- 6退款/售后：订单状态为退款成功 -->
              AND (d.order_status = 4 OR d.order_status = 5 OR d.order_status = 6 OR d.order_status = 7)
            </if>
            <if test="param.statusFilter == 7">
              <!-- 7已删除：订单被删除 -->
              AND d.deleted = 1
            </if>
            <if test="param.statusFilter == 8">
              <!-- 8已取消：订单已取消 -->
              AND a.order_status = 2
            </if>
          </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.clinic.entity.ClinicPrescription">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.clinic.entity.ClinicPrescription">
        <include refid="selectSql"></include>
    </select>

</mapper>
