<?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.OrderRefundMapper">

    <resultMap type="com.ruoyi.info.order.domain.vo.OrderRefundVo" id="OrderRefundVoMap">
        <result property="id" column="id"/>
        <result property="orderRefundNo" column="order_refund_no"/>
        <result property="orderId" column="order_id"/>
        <result property="refundReason" column="refund_reason"/>
        <result property="refundQuantity" column="refund_quantity"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="refundWay" column="refund_way"/>
        <result property="refundDesc" column="refund_desc"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="actualRefundAmount" column="actual_refund_amount"/>
        <result property="operationReason" column="operation_reason"/>
        <result property="operationTime" column="operation_time"/>
        <result property="operatorType" column="operator_type"/>
        <result property="operatorId" column="operator_id"/>
        <result property="refundTradeNo" column="refund_trade_no"/>
        <result property="completeTime" column="complete_time"/>
        <result property="createTime" column="create_time"/>
        <association property="orderVo" javaType="com.ruoyi.info.order.domain.vo.OrderVo">
            <result property="hospitalInfo" column="hospital_info"/>
            <result property="hospitalDepartmentInfo" column="hospital_department_info"/>
            <result property="patientInfo" column="patient_info"/>
            <result property="serviceInfo" column="service_info"/>
            <result property="orderNum" column="order_num"/>
            <result property="sellingPrice" column="selling_price"/>
            <result property="totalPrice" column="total_price"/>
            <result property="payPrice" column="pay_price"/>
            <result property="userMobile" column="userMobile"/>
        </association>
    </resultMap>

    <sql id="queryByWhere">
        select tor.*, tod.hospital_info, tod.hospital_department_info, tod.patient_info,
               tod.service_info, tod.order_num, tod.selling_price, tod.total_price, tod.pay_price, u.mobile as userMobile
        from tb_order_refund tor
        left join tb_order tod on tor.order_id = tod.id
        left join tb_user u on u.id = tod.user_id
        <where>
            <if test="bo.orderRefundNo != null and bo.orderRefundNo != ''">
                tor.order_refund_no like concat('%',#{bo.orderRefundNo},'%')
            </if>
            <if test="bo.orderId != null">
                and tor.order_id = #{bo.orderId}
            </if>
            <if test="bo.userId != null">
                and tod.user_id = #{bo.userId}
            </if>
            <if test="bo.hospitalDepartmentIds != null and bo.hospitalDepartmentIds.size() > 0">
                and tod.hospital_department_id in
                <foreach item="item" collection="bo.hospitalDepartmentIds" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="bo.hospitalId != null">
                and tod.hospital_id = #{bo.hospitalId}
            </if>
            <if test="bo.serviceName != null and bo.serviceName != ''">
                and JSON_UNQUOTE(JSON_EXTRACT(tod.service_info, '$.name')) like concat('%', #{bo.serviceName}, '%')
            </if>
            <if test="bo.startTime != null">
                and tor.create_time >= #{bo.startTime}
            </if>
            <if test="bo.endTime != null">
                and tor.create_time <![CDATA[ <= ]]> #{bo.endTime}
            </if>
            <if test="bo.userMobile != null and bo.userMobile != ''">
                and u.mobile like concat('%', #{bo.userMobile}, '%')
            </if>
        </where>
        order by tor.create_time desc
    </sql>

    <select id="selectListPage" resultMap="OrderRefundVoMap">
        <include refid="queryByWhere"/>
    </select>

    <select id="selectOrderRefundList" resultMap="OrderRefundVoMap">
        <include refid="queryByWhere"/>
    </select>

    <select id="selectOrderRefundCount" resultType="java.lang.Integer">
        select count(0) from tb_order_refund tor left join tb_order o on tor.order_id = o.id
        <where>
            <if test="bo.departmentIds != null and bo.departmentIds.size > 0">
                and o.hospital_department_id in
                <foreach item="item" collection="bo.departmentIds" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="bo.status != null">
                and tor.refund_status = #{bo.status}
            </if>
        </where>
    </select>
</mapper>
