<?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="misRefundList1" resultType="com.liquidnet.service.kylin.dao.OrderRefundDao" resultMap="misRefundListResult">
        SELECT a.*, b.order_code, b.performance_title, d.mailno
        FROM kylin_order_refunds AS a
        JOIN kylin_order_tickets AS b ON a.order_tickets_id = b.order_tickets_id
        JOIN kylin_order_ticket_relations AS c ON a.order_tickets_id = c.order_id
        LEFT JOIN kylin_order_express AS d ON d.order_refunds_id = a.order_refunds_id
        <where>
            <if test="orderTicketsId!=''">
                AND a.order_tickets_id = #{orderTicketsId}
            </if>
            <if test="performancesId!=''">
                AND c.performance_id = #{performancesId}
            </if>
            <if test="orderCode!=''">
                AND b.order_code = #{orderCode}
            </if>
            <choose>
                <when test="roleType!=''">
                    <if test="roleType == 1">
                        AND a.type = 3 AND a.status = 1
                    </if>
                    <if test="roleType == 2">
                        <if test="statusStr == '9'">
                            AND a.status = 9
                        </if>
                        <if test="statusStr 1= '1'">
                            AND a.status = 1 AND a.type != 3
                        </if>
                        <if test="statusStr != '1' and statusStr != '9'">
                            AND (a.status = 9 OR (a.status = 1 AND a.type != 3)
                        </if>
                    </if>
                </when>
                <otherwise>
                    <if test="status!=''">
                        AND a.status IN
                        <foreach collection="statusStr" item="status" index="index" open="(" close=")" separator=",">
                            ${status}
                        </foreach>
                    </if>
                </otherwise>
            </choose>
        </where>
        ORDER BY a.mid DESC
    </select>

    <select id="misRefundList" resultType="com.liquidnet.service.kylin.dao.OrderRefundDao" resultMap="misRefundListResult">
        select t.*, b.order_code, b.performance_title from
            (select a.*, d.mailno
            from kylin_order_refunds a
            LEFT JOIN kylin_order_express AS d ON d.order_refunds_id = a.order_refunds_id
            <where>
                <if test="orderTicketsId!=''">
                    AND a.order_tickets_id = #{orderTicketsId}
                </if>
                <choose>
                    <when test="roleType!=''">
                        <if test="roleType == 1">
                            AND a.type = 3 AND a.status = 1
                        </if>
                        <if test="roleType == 2">
                            <if test="statusStr == '9'">
                                AND a.status = 9
                            </if>
                            <if test="statusStr 1= '1'">
                                AND a.status = 1 AND a.type != 3
                            </if>
                            <if test="statusStr != '1' and statusStr != '9'">
                                AND (a.status = 9 OR (a.status = 1 AND a.type != 3)
                            </if>
                        </if>
                    </when>
                    <otherwise>
                        <if test="status!=''">
                            AND a.status IN
                            <foreach collection="statusStr" item="status" index="index" open="(" close=")" separator=",">
                                ${status}
                            </foreach>
                        </if>
                    </otherwise>
                </choose>
            </where>
            ORDER BY a.mid DESC
            ) t
        JOIN kylin_order_tickets AS b ON t.order_tickets_id = b.order_tickets_id
        JOIN kylin_order_ticket_relations AS c ON t.order_tickets_id = c.order_id
        <where>
            <if test="performancesId!=''">
                AND c.performance_id = #{performancesId}
            </if>
            <if test="orderCode!=''">
                AND b.order_code = #{orderCode}
            </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.math.BigDecimal">
        SELECT SUM(price_express)
        FROM kylin_order_refunds
        <where>
            AND order_tickets_id = #{orderTicketsId}
            AND status != ${orderRefundStatusCancel}
            AND status != 5
        </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>
