<?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.web.modules.order.mapper.BizOrderAftersaleMapper">

    <resultMap id="BizOrderAftersaleResult" type="com.ruoyi.web.modules.order.domain.BizOrderAftersale">
        <id property="aftersaleId" column="aftersale_id"/>
        <result property="aftersaleNo" column="aftersale_no"/>
        <result property="orderId" column="order_id"/>
        <result property="orderItemId" column="order_item_id"/>
        <result property="memberId" column="member_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="applyType" column="apply_type"/>
        <result property="applyReason" column="apply_reason"/>
        <result property="returnQuantity" column="return_quantity"/>
        <result property="applyAmount" column="apply_amount"/>
        <result property="description" column="description"/>
        <result property="evidencePics" column="evidence_pics"/>
        <result property="goodsId" column="goods_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="goodsImage" column="goods_image"/>
        <result property="orderNo" column="order_no"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="memberNickName" column="member_nick_name"/>
        <result property="consigneeName" column="consignee_name"/>
        <result property="consigneeMobile" column="consignee_mobile"/>
        <result property="consigneeAddress" column="consignee_address"/>
        <result property="aftersaleStatus" column="aftersale_status"/>
        <result property="processResult" column="process_result"/>
        <result property="finishTime" column="finish_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="aftersaleId">
        insert into biz_order_aftersale (aftersale_no, order_id, order_item_id, member_id, shop_id,
                                         apply_type, apply_reason, return_quantity, apply_amount, description,
                                         evidence_pics, goods_id, goods_name, goods_spec, goods_image, order_no,
                                         pay_amount, member_nick_name, consignee_name, consignee_mobile,
                                         consignee_address, aftersale_status, create_time, update_time)
        values (#{aftersaleNo}, #{orderId}, #{orderItemId}, #{memberId}, #{shopId},
                #{applyType}, #{applyReason}, #{returnQuantity}, #{applyAmount}, #{description},
                #{evidencePics}, #{goodsId}, #{goodsName}, #{goodsSpec}, #{goodsImage}, #{orderNo},
                #{payAmount}, #{memberNickName}, #{consigneeName}, #{consigneeMobile},
                #{consigneeAddress}, #{aftersaleStatus}, #{createTime}, #{updateTime})
    </insert>

    <select id="selectByIdAndMember" resultMap="BizOrderAftersaleResult">
        select * from biz_order_aftersale
        where aftersale_id = #{aftersaleId} and member_id = #{memberId}
    </select>

    <select id="countInProgressByMember" resultType="int">
        select count(1) from biz_order_aftersale
        where member_id = #{memberId} and aftersale_status = '1'
    </select>

    <select id="countInProgressByOrderItem" resultType="int">
        select count(1) from biz_order_aftersale
        where order_item_id = #{orderItemId} and aftersale_status = '1'
    </select>

    <select id="countInProgressByOrderId" resultType="int">
        select count(1) from biz_order_aftersale
        where order_id = #{orderId} and aftersale_status = '1'
    </select>

    <select id="selectAftersaleStatusByOrderIds"
            resultType="com.ruoyi.web.modules.order.dto.OrderAftersaleStatusRow">
        select order_id as orderId,
               if(sum(case when aftersale_status = '1' then 1 else 0 end) &gt; 0, '1', '2') as aftersaleStatus
        from biz_order_aftersale
        where order_id in
        <foreach collection="orderIds" item="orderId" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        group by order_id
    </select>

    <select id="selectMemberList" resultMap="BizOrderAftersaleResult">
        select * from biz_order_aftersale
        where member_id = #{memberId}
        <if test="tab != null and tab == 'IN_PROGRESS'">
            and aftersale_status = '1'
        </if>
        <if test="tab != null and tab == 'FINISHED'">
            and aftersale_status = '2'
        </if>
        order by create_time desc
    </select>

    <select id="selectMaxAftersaleNoByPrefix" resultType="String">
        select max(aftersale_no) from biz_order_aftersale where aftersale_no like concat(#{prefix}, '%')
    </select>

    <sql id="sellerListWhere">
        where a.shop_id = #{shopId}
        <if test="query != null and query.aftersaleNo != null and query.aftersaleNo != ''">
            and a.aftersale_no like concat('%', #{query.aftersaleNo}, '%')
        </if>
        <if test="query != null and query.orderNo != null and query.orderNo != ''">
            and a.order_no like concat('%', #{query.orderNo}, '%')
        </if>
        <if test="query != null and query.applyType != null and query.applyType != ''">
            and a.apply_type = #{query.applyType}
        </if>
        <if test="query != null and query.aftersaleStatus != null and query.aftersaleStatus != ''">
            and a.aftersale_status = #{query.aftersaleStatus}
        </if>
    </sql>

    <select id="selectByIdAndShop" resultMap="BizOrderAftersaleResult">
        select * from biz_order_aftersale
        where aftersale_id = #{aftersaleId} and shop_id = #{shopId}
        limit 1
    </select>

    <select id="selectSellerList" resultType="com.ruoyi.web.modules.order.vo.SellerAftersaleListRowVO">
        select a.aftersale_id as aftersaleId,
               a.aftersale_no as aftersaleNo,
               a.apply_type as applyType,
               a.order_no as orderNo,
               a.goods_image as goodsImage,
               a.goods_name as goodsName,
               a.goods_spec as goodsSpec,
               coalesce(a.return_quantity,
                   (select sum(oi2.quantity) from biz_order_item oi2 where oi2.order_id = a.order_id)) as quantity,
               a.pay_amount as payAmount,
               a.member_nick_name as memberNickName,
               a.consignee_name as consigneeName,
               a.consignee_mobile as consigneeMobile,
               a.consignee_address as consigneeAddress,
               a.apply_reason as applyReason,
               a.return_quantity as returnQuantity,
               a.apply_amount as applyAmount,
               a.create_time as createTime,
               a.aftersale_status as aftersaleStatus,
               a.finish_time as finishTime
        from biz_order_aftersale a
        left join biz_order_item oi on a.order_item_id = oi.item_id
        <include refid="sellerListWhere"/>
        order by a.create_time desc, a.aftersale_id desc
    </select>

    <select id="selectSellerDetail" resultType="com.ruoyi.web.modules.order.vo.SellerAftersaleDetailVO">
        select a.aftersale_id as aftersaleId,
               a.aftersale_no as aftersaleNo,
               a.apply_type as applyType,
               a.apply_reason as applyReason,
               a.return_quantity as returnQuantity,
               a.apply_amount as applyAmount,
               a.description as description,
               a.evidence_pics as evidencePicsRaw,
               a.goods_id as goodsId,
               a.goods_image as goodsImage,
               a.goods_name as goodsName,
               a.goods_spec as goodsSpec,
               coalesce(a.return_quantity,
                   (select sum(oi2.quantity) from biz_order_item oi2 where oi2.order_id = a.order_id)) as quantity,
               a.order_id as orderId,
               a.order_no as orderNo,
               a.pay_amount as payAmount,
               a.member_nick_name as memberNickName,
               a.consignee_name as consigneeName,
               a.consignee_mobile as consigneeMobile,
               a.consignee_address as consigneeAddress,
               a.aftersale_status as aftersaleStatus,
               a.process_result as processResult,
               a.create_time as createTime,
               a.finish_time as finishTime
        from biz_order_aftersale a
        left join biz_order_item oi on a.order_item_id = oi.item_id
        where a.aftersale_id = #{aftersaleId}
          and a.shop_id = #{shopId}
        limit 1
    </select>

    <update id="updateForFinish" parameterType="com.ruoyi.web.modules.order.domain.BizOrderAftersale">
        update biz_order_aftersale
        set process_result = #{processResult},
            aftersale_status = '2',
            finish_time = #{finishTime},
            update_time = #{updateTime}
        where aftersale_id = #{aftersaleId}
          and shop_id = #{shopId}
          and aftersale_status = '1'
    </update>

</mapper>
