package com.gxwebsoft.shop.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.param.ShopOrderParam;
import com.gxwebsoft.shop.vo.ShopOrderGoodsVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单Mapper
 *
 * @author 科技小王子
 * @since 2025-01-11 10:45:12
 */
public interface ShopOrderMapper extends BaseMapper<ShopOrder> {

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param param 查询参数
     * @return List<ShopOrder>
     */
    List<ShopOrder> selectPageRel(@Param("page") IPage<ShopOrder> page,
                             @Param("param") ShopOrderParam param);

    /**
     * 查询全部
     *
     * @param param 查询参数
     * @return List<User>
     */
    List<ShopOrder> selectListRel(@Param("param") ShopOrderParam param);

    @InterceptorIgnore(tenantLine = "true")
    ShopOrder getByOutTradeNo(@Param("outTradeNo") String outTradeNo);

    @InterceptorIgnore(tenantLine = "true")
    void updateByOutTradeNo(@Param("param") ShopOrder order);

    /**
     * 统计订单总金额
     * 只统计已支付的订单（pay_status = 1）且未删除的订单（deleted = 0）
     *
     * @return 订单总金额
     */
    @Select("SELECT COALESCE(SUM(pay_price), 0) FROM shop_order WHERE pay_status = 1 AND deleted = 0 AND pay_price IS NOT NULL")
    BigDecimal selectTotalAmount();

    /**
     * 获取用户订单各状态数量（用于前端订单页徽标/统计，一次查询返回多项聚合结果）
     */
    Map<String, Object> selectUserOrderStats(@Param("userId") Integer userId,
                                            @Param("tenantId") Integer tenantId,
                                            @Param("type") Integer type);

    /**
     * 通过订单号查询订单所有商品分润信息
     * @param orderNo   订单号
     * @return
     */
    List<ShopOrderGoodsVO> getOrderGoodsInfo(@Param("orderNo") String orderNo);
}
