<?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.liquidnet.service.kylin.mapper.KylinOrderRefundsMapper">

    <select id="getRefundList" resultType="com.liquidnet.service.kylin.entity.KylinOrderRefunds">
        SELECT order_refunds_id, mid
        FROM kylin_order_refunds
        <where>
            order_refund_batches_id=${refundBatchId}
            AND type=${whereType}
            AND mid>${mid}
            AND status IN
            <foreach collection="whereStatus" item="where_status" index="index" open="(" close=")" separator=",">
                ${where_status}
            </foreach>
        </where>
        LIMIT ${limitNum}
    </select>

    <select id="refundedTotalPrice" resultType="java.lang.Double">
        SELECT SUM('price')
        FROM kylin_order_refunds
        <where>
            order_tickets_id=#{orderTicketsId}
            AND status IN (3, 4)
        </where>
        LIMIT #{limitNum}
    </select>
    <resultMap id="misRefundListResult" type="com.liquidnet.service.kylin.dao.OrderRefundDao">
        <result column="order_tickets_id" property="orderTicketsId"/>
        <result column="order_refund_code" property="orderRefundCode"/>
        <result column="status" property="status"/>
        <result column="price" property="price"/>
        <result column="created_at" property="createdAt"/>
    </resultMap>
    <select id="misRefundList" resultType="com.liquidnet.service.kylin.dao.OrderRefundDao" resultMap="misRefundListResult">
        SELECT *
        FROM kylin_order_refunds
        <where>
            <if test="orderTicketsId!=''">
                AND order_tickets_id = #{orderTicketsId}
            </if>
            <if test="orderRefundCode!=''">
                AND order_refund_code = #{orderRefundCode}
            </if>
            <if test="status!=''">
                AND status = #{status}
            </if>
        </where>
    </select>
    <select id="selectRefundingCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM kylin_order_refunds AS a
        JOIN kylin_order_refund_entities AS b ON a.order_refunds_id = b.order_refunds_id
        <where>
            AND a.order_tickets_id = #{orderTicketsId}
            AND a.status != ${orderRefundStatusCancel}
            AND b.order_ticket_entities_id IN
            <foreach collection="ticketEntityIds" item="entityIds" index="index" open="(" close=")" separator=",">
                ${entityIds}
            </foreach>
        </where>
    </select>
    <select id="RefundPriceExpressSum" resultType="java.lang.Double">
        SELECT SUM(price_express)
        FROM kylin_order_refunds
        <where>
            AND order_tickets_id = #{orderTicketsId}
            AND status != ${orderRefundStatusCancel}
        </where>
    </select>
    <select id="selectEntitiesCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM kylin_order_refunds AS a
        JOIN kylin_order_refund_entities AS b ON a.order_refunds_id = b.order_refunds_id
        <where>
            AND a.order_tickets_id = #{orderTicketsId}
            AND a.order_refunds_id != #{orderRefundsId}
            AND a.status NOT IN
            <foreach collection="whereRefundingCount" item="statusWhere" index="index" open="(" close=")" separator=",">
                ${statusWhere}
            </foreach>
            AND b.order_ticket_entities_id = #{entitiesId}
        </where>
    </select>
</mapper>
