<?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.yonge.cooleshow.biz.dal.dao.UserOrderRefundDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserOrderRefund">
        <result column="id_" property="id" />
        <result column="user_id_" property="userId" />
        <result column="order_id_" property="orderId" />
        <result column="order_no_" property="orderNo" />
        <result column="oredr_detil_ids_" property="oredrDetilIds" />
        <result column="status_" property="status" />
        <result column="apply_amount_" property="applyAmount" />
        <result column="actual_amount_" property="actualAmount" />
        <result column="reason_" property="reason" />
        <result column="operate_id_" property="operateId" />
        <result column="operate_reason_" property="operateReason" />
        <result column="create_time_" property="createTime" />
        <result column="update_time_" property="updateTime" />
    </resultMap>

    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.order_id_ as orderId
        , t.order_no_ as orderNo
        , t.oredr_detil_ids_ as oredrDetilIds
        , t.status_ as status
        , t.apply_amount_ as applyAmount
        , t.actual_amount_ as actualAmount
        , t.reason_ as reason
        , t.operate_id_ as operateId
        , t.operate_reason_ as operateReason
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        </sql>

    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderRefundVo">
        SELECT
        <include refid="baseColumns"/>
        FROM user_order_refund t
        where t.id_ = #{id}
        <if test="userId != null">
            and t.user_id_ = #{userId}
        </if>
    </select>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.UserOrderRefundVo">
        SELECT
            <include refid="baseColumns" />,
            o.order_name_ as orderName,
            o.order_type_ as orderType
        FROM user_order_refund t
        left join user_order o on t.order_no_ = o.order_no_
        <where>
            <if test="param.userId !=null">
                AND t.user_id_ = #{param.userId}
            </if>
            <if test="param.startTime !=null">
                <![CDATA[AND t.create_time_ >= #{param.startTime} ]]>
            </if>
            <if test="param.endTime !=null">
                <![CDATA[AND t.create_time_ < #{param.endTime} ]]>
            </if>
        </where>
        order by t.create_time_ desc
    </select>

    <select id="selectOrderRefundDetils" resultType="com.yonge.cooleshow.biz.dal.entity.UserOrderDetail">
        select d.* from user_order o
        left join user_order_detail d on o.order_no_ = d.order_no_
        where o.id_ = #{orderId}
          and o.status_ = 'PAID'
          and not exists (
                select 1 from user_order_refund r
                left join user_order_refund_bill bi on r.id_ = bi.refund_id_
                where r.order_no_ = o.order_no_ and (bi.status_ in ('pending','succeeded') or r.status_ in ('DOING','PASS'))
                  and FIND_IN_SET(d.id_,r.oredr_detil_ids_)
          )
    </select>
</mapper>