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

package com.yami.shop.dao;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.MyOrderDto;
import com.yami.shop.bean.app.dto.MyOrderItemDto;
import com.yami.shop.bean.app.dto.OrderCountData;
import com.yami.shop.bean.distribution.UserShoppingDataDto;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.param.*;
import com.yami.shop.bean.vo.OrderCountVo;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.common.util.PageParam;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface OrderMapper extends BaseMapper<Order> {

    Order getOrderByOrderNumber(@Param("orderNumber") String orderNumber);

    List<Order> listUnRefundOrderAndOrderItems(@Param("orderStatus") Integer orderStatus, @Param("lessThanUpdateTime") DateTime lessThanUpdateTime);
    List<Order> selectCancelOrders(@Param("orderStatus") Integer orderStatus, @Param("lessThanUpdateTime") DateTime lessThanUpdateTime);

    /**
     * 定时任务取消订单
     * @param orders
     */
    void cancelOrders(@Param("orders") List<Order> orders, @Param("cancelReason") String cancelReason);

    /**
     * 确认收货
     * @param orders
     */
    void receiptOrder(@Param("orders") List<Order> orders);

    void updateByToPaySuccess(@Param("orderNumbers") List<String> orderNumbers, @Param("payType") Integer payType);

    List<Order> listOrdersDetialByOrder(@Param("order") Order order, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<Order> listOrdersDetialByOrderParam(@Param("adapter") PageAdapter adapter, @Param("orderParam") OrderParam orderParam);

    IPage<Order> deliverList(@Param("pageParam") PageParam pageParam, @Param("orderParam") BackendOrderParam orderParam);


    Long countOrderDetial(@Param("orderParam") OrderParam orderParam);

    UserShoppingDataDto calculateUserInShopData(@Param("shopId") Long shopId, @Param("userId") String userId);

    List<MyOrderDto> listMyOrderByUserIdAndStatus(@Param("adapter") PageAdapter adapter, @Param("userId") String userId, @Param("status") Integer status,@Param("dvyType") Integer dvyType);

    Long countMyOrderByUserIdAndStatus(@Param("userId") String userId, @Param("status") Integer status);

    void deleteOrders(@Param("orders") List<Order> orders);

    OrderCountData getOrderCount(String userId);


    OrderPayParam getOrderCountByShopId(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getActualTotalByHour(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getActualTotalByDay(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<OrderPayParam> getPayOrderByTime(@Param("shopId") Long shopId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);


    void cancelSeckillOrderByTime(@Param("now") Date now);

    /**
     * 更新订单为待成团
     */
    int updateToWaitGroup(@Param("orderNumber") String orderNumber);

    /**
     * 通过拼团团队id更新订单为已发货状态
     */
    int updateToWaitDelivery(@Param("groupTeamId") Long groupTeamId);

    /**
     * 通过用户id和订单编号获取订单
     */
    Order getOrderByOrderNumberAndUserId(@Param("orderNumber") String orderNumber, @Param("userId") String userId);

    /**
     * 根据订单号获取订单及订单项
     *
     * @param orderNumber
     * @return
     */
    Order getOrderAndOrderItemByOrderNumber(@Param("orderNumber") String orderNumber);

    Integer hasBuySuccessProd(@Param("prodId") Long prodId, @Param("userId") String userId);

    /**
     * 模糊查询订单列表中的某一项订单
     */
    List<MyOrderDto> listMyOrderByParams(@Param("adapter") PageAdapter adapter, @Param("userId") String userId, @Param("status") Integer status,
                                         @Param("orderName") String orderName, @Param("orderTimeStatus") Integer orderTimeStatus,
                                         @Param("preTime") String preTime, @Param("orderType") Integer orderType, @Param("orderNumber") String orderNumber);

    /**
     * 统计模糊查询订单列表中的某一项订单
     */
    Long countMyOrderByParams(@Param("userId") String userId, @Param("status") Integer status, @Param("orderName") String orderName,
                              @Param("orderTimeStatus") Integer orderTimeStatus, @Param("preTime") String preTime,
                              @Param("orderType") Integer orderType, @Param("orderNumber") String orderNumber);

    List<Order> listOrdersDetialByOrderInfo(@Param("orderParam") OrderParam orderParam);

    List<Order> getOrderPayInfoByOrderNumber(@Param("orderNumberList") List<String> orderNumberList);

    Order getOrderDetailByOrderNumberAndShopId(@Param("orderNumber") String orderNumber, @Param("shopId") Long shopId);

    List<MyOrderDto> OrderCommentByUserIdAndStatus(@Param("adapter") PageAdapter pageAdapter, @Param("userId") String userId, @Param("isComm") Integer isComm);

    List<MyOrderItemDto> orderItemCommentByUserIdAndStatus(@Param("adapter") PageAdapter pageAdapter, @Param("userId") String userId,
                                                           @Param("isComm") Integer isComm);
    long countOrderItemComment(@Param("userId") String userId, @Param("isComm") Integer isComm);

    void updateByToPaySuccessDelivery(@Param("orderNumbers") List<String> orderNumbers, @Param("payType") Integer payType);

    Integer statusCount(@Param("status") List<Integer> status, @Param("orderParam")BackendOrderParam orderParam);

    /**
     * 统计订单数量
     */
    OrderCountParam selectOrderCount(@Param("orderParam") OrderParam orderParam);

    /**
     * 后管端分页获取订单列表
     */
    IPage<Order> deliveryOrder(@Param("page") PageParam<Order> page, @Param("orderParam") OrderStatisticsParam orderParam);

    List<Order> findList( @Param("orderParam")BackendOrderParam orderParam);

    OrderCountVo orderCount(@Param("orderParam") BackendOrderParam orderParam);

    String getRiderInfo(String orderNumber);

    void addOrderPushLog(@Param("orderNumber") String orderNumber, @Param("isSuccess") Integer isSuccess, @Param("result") String result);

    List<String> syncOrderList();

    Integer getOrderPushLog(String orderNumber);

    void updateOrderPushLog(@Param("orderNumber") String orderNumber, @Param("isSuccess") Integer isSuccess, @Param("result") String result);
}
