/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yami.shop.bean.app.dto.OrderCountData;
import com.yami.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.*;
import com.yami.shop.bean.po.SMOrderPo;
import com.yami.shop.bean.vo.OrderCountVo;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lgh on 2018/09/15.
 */
public interface OrderService extends IService<Order> {


    Order getOrderByOrderNumber(String orderNumber);

    /**
     * 根据订单号和用户id获取订单
     *
     * @param orderNumber
     * @param userId
     * @return
     */
    Order getOrderByOrderNumberAndUserId(String orderNumber, String userId, boolean valid);

    /**
     * 根据店铺id获取支付数据
     *
     * @param shopId
     */
    OrderPayParam getPayUserCountByshopId(Long shopId, Date startTime, Date endTime);

    /**
     * 根据时间和店铺id获取分段时间数据
     */
    OrderPayParam getActualTotalByHour(Long shopId, Date startTime, Date endTime);

    /**
     * 根据天数和店铺id获取分段时间数据
     */
    List<OrderPayParam> getActualTotalByDay(Long shopId, Date startTime, Date endTime);

    /**
     * 根据店铺id和订单号获取订单
     *
     * @param orderNumber
     * @param shopId
     * @return
     */
    Order getOrderByOrderNumberAndShopId(String orderNumber, Long shopId, boolean valid);


    ShopCartOrderMergerDto putConfirmOrderCache(String userId, ShopCartOrderMergerDto shopCartOrderMergerDto);

    ShopCartOrderMergerDto getConfirmOrderCache(String userId);

    void removeConfirmOrderCache(String userId);


    List<Order> submit(String userId, ShopCartOrderMergerDto mergerOrder);

    void delivery(Order order);

    /**
     * 获取送达的订单项
     * @param orderStatus 订单状态
     * @param lessThanUpdateTime 时间
     * @return
     */
    List<Order> listUnRefundOrderAndOrderItems(Integer orderStatus, DateTime lessThanUpdateTime);

    /**
     * @param orderStatus 订单状态
     * @param lessThanUpdateTime 时间
     */
    List<Order> selectCancelOrders(Integer orderStatus, DateTime lessThanUpdateTime);

    void cancelOrders(List<Order> orders, String reason);

    void receiptOrder(List<Order> orders);

//    List<Order> listOrdersDetialByOrder(Order order, Date startTime, Date endTime);

    IPage<Order> pageOrdersDetialByOrderParam(Page<Order> page, OrderParam orderParam);

    IPage<Order> deliverList(PageParam<Order> page, BackendOrderParam orderParam);

    void deleteOrders(List<Order> orders);

    OrderCountData getOrderCount(String userId);

    double sumTotalDistributionAmountByOrderItem(List<OrderItem> orderItems);

    void changeAmount(Order order);

    void soldExcel(OrderParam orderParam, HttpServletResponse response);

    List<Order> getOrderPayInfoByOrderNumber(List<String> orderNumberList);

    Order getOrderDetailByOrderNumberAndShopId(String orderNumber, Long shopId);

    /**
     * 获取退款的订单项的id和退款状态
     *
     * @param orderId
     * @return
     */
    Map<Long, Integer> getOrderItemMap(Long orderId);

    String cancelOrder(CancelOrderParam cancelOrderParam);


    /**
     * 订单申请退款
     *
     * @param orderRefundParam 订单申请退款参数
     * @param rReturnMoneySts  退款状态
     * @return 订单申请退款结果
     */
    OrderRefund applyAgain(OrderRefundParam orderRefundParam, Integer rReturnMoneySts);

    /**
     * 订单申请退款
     *
     * @param orderNumber 订单单号
     * @return 订单申请退款结果
     */
    void returnRefund(String orderNumber);


    /**
     * 计算退单金额和积分
     *
     * @param orderNumber 订单编号
     * @param suk  {@link OrderRefundAmountParam}
     * @return {@link OrderRefundAmountDTO}
     */
    OrderRefundAmountDTO computeOrderRefundAmount(String orderNumber, List<OrderRefundAmountParam> suk);

    Map<String,Integer> deliverListStatusNum(BackendOrderParam orderParam);

    /**
     * 订单统计数据
     *
     * @param orderParam 订单参数
     * @return 订单统计数据
     */
    OrderCountParam selectOrderCount(OrderParam orderParam);

    /**
     * 订单发货列表
     *
     * @param page 分页参数
     * @param orderParam 订单参数
     * @return 订单发货列表
     */
    IPage<Order> deliveryOrder(PageParam<Order> page, OrderStatisticsParam orderParam);

    R<String> export(BackendOrderParam orderParam, Long userId);


    OrderCountVo orderCount(BackendOrderParam orderParam);

    /**
     * 订单拆单处理
     * @param c
     * @param channel
     * @param objects
     */
    void cd(Order c, Channel channel, List<Order> objects);

    /**
     * 手动拆单处理
     * 创建临时子订单（splitStatus=0），父订单进入拆单中状态（splitStatus=3）
     *
     * @param param 手动拆单参数
     * @return 子订单信息
     */
    Order manualSplitOrder(ManualSplitOrderParam param);

    /**
     * 确定拆单
     * 将父订单状态改为已拆单（splitStatus=4），子订单改为不拆单（splitStatus=1）
     *
     * @param param 确认拆单参数
     */
    void confirmSplitOrder(ConfirmSplitOrderParam param);

    /**
     * 删除临时子订单
     * 删除临时子订单，并减少父订单项的已拆单数量
     *
     * @param subOrderNumber 删除临时子订单参数
     */
    void deleteTempSubOrder(String subOrderNumber);

    void addSMOrder(SMOrderPo orderPo);
}
