<?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.leromro.core.mapper.OrderRefundMapper">
    
    <resultMap type="OrderRefund" id="LOrderRefundResult">
        <result property="orderRefundId"    column="order_refund_id"    />
        <result property="mainOrderId"    column="main_order_id"    />
        <result property="userId"    column="user_id"    />
        <result property="volunteerId"    column="volunteer_id"    />
        <result property="refundApplyAmount"    column="refund_apply_amount"    />
        <result property="refundAmount"    column="refund_amount"    />
        <result property="refundReason"    column="refund_reason"    />
        <result property="refundPicture"    column="refund_picture"    />
        <result property="rejectionReason"    column="rejection_reason"    />
        <result property="rejectionPicture"    column="rejection_picture"    />
        <result property="status"    column="status"    />
        <result property="refundType"    column="refund_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectLOrderRefundVo">
        select order_refund_id,
               main_order_id,
               user_id,
               volunteer_id,
               refund_apply_amount,
               refund_amount,
               refund_reason,
               refund_picture,
               rejection_reason,
               rejection_picture,
               status,
               refund_type,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from l_order_refund
    </sql>

    <select id="selectLOrderRefundList" parameterType="OrderRefund" resultMap="LOrderRefundResult">
        <include refid="selectLOrderRefundVo"/>
        <where>  
            <if test="orderRefundId != null "> and order_refund_id = #{orderRefundId}</if>
            <if test="mainOrderId != null  and mianOrderId != ''"> and main_order_id = #{mainOrderId}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="volunteerId != null  and volunteerId != ''"> and volunteer_id = #{volunteerId}</if>
            <if test="refundApplyAmount != null "> and refund_apply_amount = #{refundApplyAmount}</if>
            <if test="refundAmount != null "> and refund_amount = #{refundAmount}</if>
            <if test="refundReason != null  and refundReason != ''"> and refund_reason = #{refundReason}</if>
            <if test="refundPicture != null  and refundPicture != ''"> and refund_picture = #{refundPicture}</if>
            <if test="rejectionReason != null  and rejectionReason != ''"> and rejection_reason = #{rejectionReason}</if>
            <if test="rejectionPicture != null  and rejectionPicture != ''"> and rejection_picture = #{rejectionPicture}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="refundType != null "> and refund_type = #{refundType}</if>
        </where>
    </select>
    
    <select id="selectLOrderRefundByOrderRefundId" parameterType="Long" resultMap="LOrderRefundResult">
        <include refid="selectLOrderRefundVo"/>
        where order_refund_id = #{orderRefundId}
    </select>
    <select id="selectOrderRefundInfoByMainOrderId" resultType="com.leromro.core.domain.vo.OrderRefundVO"
            parameterType="java.lang.Long">
        select lmo.*,
               lvi.volunteer_picture as volunteerPicture,
               lvi.name as volunteerName,
               (select business_tier_name from l_business_management lbm where lmo.business_management_id = lbm.business_management_id) as  businessTierName
        from l_main_orders lmo
                 left join l_volunteer_info lvi
                           on lmo.volunteer_id = lvi.volunteer_id and lmo.service_category = lvi.service_category
        where main_order_id = #{mainOrderId}
    </select>
    <select id="selectWebOrderRefundList" resultType="com.leromro.core.domain.vo.WebOrderRefundVO"
            parameterType="com.leromro.core.domain.MainOrders">
        select lmo.*, lor.refund_apply_amount,lor.status,lor.refund_amount, lor.refund_reason,lor.order_refund_id,lor.status as orderRefundStatus ,
        lor.refund_picture,(select business_tier_name from l_business_management lbm where lmo.business_management_id = lbm.business_management_id) as  businessTierName,
        (select name from l_volunteer_info lvi where lmo.volunteer_info_id = lvi.volunteer_info_id) as volunteerName
        from l_main_orders lmo
        left join l_order_refund lor on lmo.main_order_id = lor.main_order_id
        <where>
            lmo.order_status = '5' or lmo.order_status ='6' or lmo.order_status ='7'
            <if test="clientName != null and clientName != ''">and lmo.client_name = #{clientName}</if>
            <if test="clientTelephone != null and clientTelephone != ''">and lmo.clien_telephone = #{clientTelephone}</if>
            <if test="businessManagementId != null and businessManagementId != ''">and lmo.business_management_id = #{businessManagementId}</if>
        </where>

    </select>

    <insert id="insertLOrderRefund" parameterType="OrderRefund">
        insert into l_order_refund
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderRefundId != null">order_refund_id,</if>
            <if test="mainOrderId != null">main_order_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="volunteerId != null">volunteer_id,</if>
            <if test="refundApplyAmount != null">refund_apply_amount,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="refundReason != null">refund_reason,</if>
            <if test="refundPicture != null">refund_picture,</if>
            <if test="rejectionReason != null">rejection_reason,</if>
            <if test="rejectionPicture != null">rejection_picture,</if>
            <if test="status != null">status,</if>
            <if test="refundType != null">refund_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderRefundId != null">#{orderRefundId},</if>
            <if test="mainOrderId != null">#{mainOrderId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="volunteerId != null">#{volunteerId},</if>
            <if test="refundApplyAmount != null">#{refundApplyAmount},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="refundReason != null">#{refundReason},</if>
            <if test="refundPicture != null">#{refundPicture},</if>
            <if test="rejectionReason != null">#{rejectionReason},</if>
            <if test="rejectionPicture != null">#{rejectionPicture},</if>
            <if test="status != null">#{status},</if>
            <if test="refundType != null">#{refundType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateLOrderRefund" parameterType="OrderRefund">
        update l_order_refund
        <trim prefix="SET" suffixOverrides=",">
            <if test="mainOrderId != null">main_order_id = #{mainOrderId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="volunteerId != null">volunteer_id = #{volunteerId},</if>
            <if test="refundApplyAmount != null">refund_apply_amount = #{refundApplyAmount},</if>
            <if test="refundAmount != null">refund_amount = #{refundAmount},</if>
            <if test="refundReason != null">refund_reason = #{refundReason},</if>
            <if test="refundPicture != null">refund_picture = #{refundPicture},</if>
            <if test="rejectionReason != null">rejection_reason = #{rejectionReason},</if>
            <if test="rejectionPicture != null">rejection_picture = #{rejectionPicture},</if>
            <if test="status != null">status = #{status},</if>
            <if test="refundType != null">refund_type = #{refundType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where order_refund_id = #{orderRefundId}
    </update>
    <update id="updateList" parameterType="java.util.List">
        update l_order_refund
        <trim prefix="SET" suffixOverrides=",">
            refund_amount =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.refundAmount}
            </foreach>
            rejection_reason =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.rejectionReason}
            </foreach>
            rejection_picture =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.rejectionPicture}
            </foreach>
            status =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.status}
            </foreach>
            update_time =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.updateTime}
            </foreach>
            update_by =
            <foreach collection="list" item="item" open="case order_refund_id" close="end,">
                when #{item.orderRefundId} then #{item.updateBy}
            </foreach>
        </trim>
        where order_refund_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.orderRefundId}
        </foreach>
    </update>

    <delete id="deleteLOrderRefundByOrderRefundId" parameterType="Long">
        delete from l_order_refund where order_refund_id = #{orderRefundId}
    </delete>

    <delete id="deleteLOrderRefundByOrderRefundIds" parameterType="String">
        delete from l_order_refund where order_refund_id in 
        <foreach item="orderRefundId" collection="array" open="(" separator="," close=")">
            #{orderRefundId}
        </foreach>
    </delete>
</mapper>