<?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="org.springblade.modules.finance.mapper.AfterSalesMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="afterSalesResultMap" type="org.springblade.modules.finance.entity.AfterSales">
        <result column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
        <result column="order_id" property="orderId"/>
        <result column="after_sales_no" property="afterSalesNo"/>
        <result column="type" property="type"/>
        <result column="reason" property="reason"/>
        <result column="status" property="status"/>
        <result column="refund_state" property="refundState"/>
    </resultMap>


    <select id="selectAfterSalesPage" resultMap="afterSalesResultMap">
        select * from t_after_sales where is_delete = 0
    </select>
    <select id="selectUserAfterSalesList" resultType="org.springblade.modules.finance.vo.AfterSalesVO">
        SELECT
            s.*
        FROM
            t_after_sales s
            WHERE s.user_id = #{userId}
            <if test="state != null">
                <if test="state == 1">
                    and s.`status` = 3
                </if>
                <if test="state == 2">
                    and s.refund_state = 1
                </if>
            </if>
            ORDER BY s.create_time desc
    </select>
    <select id="getByGrouponIds" resultType="org.springblade.modules.finance.vo.AfterSalesVO">
        SELECT
            s.*,
            g.self_take_id
        FROM
            t_after_sales s
            JOIN t_order_groupon g on s.order_id = g.order_id
            and s.groupon_id in
        <foreach item="item" index="index" collection="gIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="getByGrouponId" resultType="org.springblade.modules.finance.vo.AfterSalesVO">
        SELECT
        s.*,
        g.self_take_id
        FROM
        t_after_sales s
        JOIN t_order_groupon g on s.order_id = g.order_id
        and s.groupon_id = #{grouponId}
        <if test="selfTakeId != null">
            and g.self_take_id = #{selfTakeId}
        </if>
        order by s.create_time desc
    </select>

</mapper>
