<?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.mall.mapper.MallOrderBackMapper">

    <resultMap type="com.ruoyi.mall.domain.MallOrderBack" id="MallOrderBackResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="reason"    column="reason"    />
        <result property="status"    column="status"    />
        <result property="refuse"    column="refuse"    />
        <result property="refundNo"    column="refund_no"    />
        <result property="price"    column="price"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="orginalStatus"    column="orginal_status"    />
        <result property="userId"    column="user_id"    />
    </resultMap>
    <resultMap type="com.ruoyi.mall.domain.MallOrderBackDto" id="MallOrderBackDtoResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="reason"    column="reason"    />
        <result property="status"    column="status"    />
        <result property="refuse"    column="refuse"    />
        <result property="refundNo"    column="refund_no"    />
        <result property="price"    column="price"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="orginalStatus"    column="orginal_status"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <resultMap type="MallOrderOrder" id="MallOrderOrderResult">
        <result property="id"    column="id"    />
        <result property="orderNo"    column="order_no"    />
        <result property="userId"    column="user_id"    />
        <result property="postFee"    column="post_fee"    />
        <result property="orderPrice"    column="order_price"    />
        <result property="payment"    column="payment"    />
        <result property="status"    column="status"    />
        <result property="createAt"    column="create_at"    />
        <result property="payAt"    column="pay_at"    />
        <result property="payType"    column="pay_type"    />
        <result property="consignAt"    column="consign_at"    />
        <result property="enterAt"    column="enter_at"    />
        <result property="returnAt"    column="return_at"    />
        <result property="completeAt"    column="complete_at"    />
        <result property="shippingMark"    column="shipping_mark"    />
        <result property="shippingName"    column="shipping_name"    />
        <result property="shippingCode"    column="shipping_code"    />
        <result property="buyerMsg"    column="buyer_msg"    />
        <result property="isRefund"    column="is_refund"    />
        <result property="receiverName"    column="receiver_name"    />
        <result property="receiverPhone"    column="receiver_phone"    />
        <result property="receiverProvince"    column="receiver_province"    />
        <result property="receiverCity"    column="receiver_city"    />
        <result property="receiverArea"    column="receiver_area"    />
        <result property="receiverAddress"    column="receiver_address"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
        <result property="day"    column="day"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectMallOrderBackVo">
        select id, order_id,user_id, reason, status, refuse, refund_no, price,orginal_status, create_at, update_at, del_flag from mall_order_back
    </sql>

    <select id="selectMallOrderBackList" parameterType="MallOrderBack" resultMap="MallOrderBackResult">
        select mob.id, mob.order_id,mob.user_id,moo.order_no, mob.reason, mob.status, mob.refuse, mob.refund_no, mob.price
        ,mob.orginal_status, mob.create_at, mob.update_at, mob.del_flag, mmu.name memberUserName
        from mall_order_back mob
        left join mall_order_order moo on mob.order_id = moo.id
        left join mall_member_user mmu on moo.user_id = mmu.id
        <where>
            <if test="orderNo != null  and orderNo != ''"> and moo.order_no like concat('%', #{orderNo}, '%')</if>
            <if test="status != null  and status != ''"> and mob.status = #{status}</if>
            <if test="userId != null  and userId != ''"> and mob.user_id = #{userId}</if>
            <if test="shopId != null">and moo.shop_id = #{shopId}</if>
        </where>
        order by mob.create_at desc,mob.status asc
    </select>

    <select id="selectMallOrderBackById" parameterType="Long" resultMap="MallOrderBackResult">
        <include refid="selectMallOrderBackVo"/>
        where id = #{id}
    </select>
    <select id="selectMallOrderBackByOrderId" parameterType="Long" resultMap="MallOrderBackResult">
        <include refid="selectMallOrderBackVo"/>
        where order_id = #{orderId}
    </select>

    <insert id="insertMallOrderBack" parameterType="MallOrderBack" useGeneratedKeys="true" keyProperty="id">
        insert into mall_order_back
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null">order_id,</if>
            <if test="reason != null">reason,</if>
            <if test="status != null">status,</if>
            <if test="refuse != null">refuse,</if>
            <if test="refundNo != null">refund_no,</if>
            <if test="price != null">price,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="orginalStatus != null">orginal_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null">#{orderId},</if>
            <if test="reason != null">#{reason},</if>
            <if test="status != null">#{status},</if>
            <if test="refuse != null">#{refuse},</if>
            <if test="refundNo != null">#{refundNo},</if>
            <if test="price != null">#{price},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="orginalStatus != null">#{orginalStatus},</if>
         </trim>
    </insert>

    <update id="updateMallOrderBack" parameterType="MallOrderBack">
        update mall_order_back
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="status != null">status = #{status},</if>
            <if test="refuse != null">refuse = #{refuse},</if>
            <if test="refundNo != null">refund_no = #{refundNo},</if>
            <if test="price != null">price = #{price},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="orginalStatus != null">orginal_status = #{orginalStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallOrderBackById" parameterType="Long">
        delete from mall_order_back where id = #{id}
    </delete>

    <delete id="deleteMallOrderBackByIds" parameterType="String">
        delete from mall_order_back where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getReturnPrice" resultType="decimal">
        SELECT
            IFNULL(price, 0)
        FROM
            mall_order_back
        WHERE del_flag = '0'
          AND order_id = #{orderId}
--           AND `status` IN ('3', '4')
        order by create_at desc
        limit 1
    </select>

    <select id="selectMallOrderBackByOrderIdDto" parameterType="Long" resultMap="MallOrderBackDtoResult">
        <include refid="selectMallOrderBackVo"/>
        where order_id = #{orderId} and refund_no !=''
    </select>
</mapper>
