package com.fzm.mall.server.front.order.mapper;

import com.fzm.mall.server.front.asset.model.po.qo.OrderQO;
import com.fzm.mall.server.front.asset.model.vo.order.OrderVO;
import com.fzm.mall.server.front.asset.sql.OrderSqlFactory;
import com.fzm.mall.server.front.order.model.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fzm.mall.server.front.order.model.vo.ReturnMoneyDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 用户购物订单表 Mapper 接口
 * </p>
 *
 * @author fzm
 * @since 2021-03-02
 */
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 分页查询订单
     */
    @SelectProvider(type = OrderSqlFactory.class, method = "listOrder")
    List<OrderVO> listOrder(OrderQO orderQO);

    /**
     * 分页查询订单
     */
    @SelectProvider(type = OrderSqlFactory.class, method = "countOrder")
    Integer countOrder(OrderQO orderQO);

    /**
     * 分页查询售后订单
     */
    @SelectProvider(type = OrderSqlFactory.class, method = "listAfterSaleOrder")
    List<OrderVO> listAfterSaleOrder(OrderQO orderQO);

    /**
     * 分页查询售后订单
     */
    @SelectProvider(type = OrderSqlFactory.class, method = "countAfterSaleOrder")
    Integer countAfterSaleOrder(OrderQO orderQO);

    /**
     * 订单删除
     */
    @Update("update order set is_delete = 1 where oid = #{oid} and is_delete = 0 and is_delete = 0 and order_state in (1,2,5)")
    Integer orderDelete(String oid);

    /**
     * 订单撤销
     */
    @Update("update order set order_state = 2, update_time = #{timestamp} where oid = #{oid} and is_delete = 0 and order_state in (0,3)")
    Integer orderUndo(@Param("oid") String oid, @Param("timestamp") long timestamp);

    /**
     * 订单超时
     */
    @Update("update order set order_state = 2, update_time = #{timestamp} where oid = #{oid} and order_state = 0")
    Integer orderTimeout(@Param("oid") String oid, @Param("timestamp") long timestamp);


    @Select("select * from order_info where oid = #{oid}")
    Order getByOid(@Param("oid") String toString);

    @Update("update order_info set order_state = 1, update_time = #{timestamp} where oid = #{oid} and order_state in (3,6,7)")
    void paySuccess(@Param("oid") String oid, @Param("timestamp") long timestamp);

    @Update("update order_info set order_state = 4, update_time = #{timestamp} where uid = #{uid} and oid = #{oid} and order_state = 1")
    int applyReturn(@Param("uid") String uid, @Param("oid") String oid, @Param("timestamp") Long timestamp);

    @Update("update order_info set order_state = 1, update_time = #{timestamp} where uid = #{uid} and oid = #{oid} and order_state = 4")
    int revokeReturn(@Param("uid") String uid, @Param("oid") String oid, @Param("timestamp") Long timestamp);

    @Select("select a.*,b.amount as amount from order_refund a left join order_info b on a.oid = b.oid where a.oid = #{oid}")
    ReturnMoneyDetail getReturnMoneyDetail(String oid);
}
