package com.ylx.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.massage.domain.CancelOrderApplication;
import com.ylx.order.domain.OrderDispatch;
import com.ylx.order.domain.TOrder;
import com.ylx.order.domain.dto.AdminOrderDispatchMerchantQueryDTO;
import com.ylx.order.domain.dto.AdminOrderQueryDTO;
import com.ylx.order.domain.vo.AdminOrderDetailVO;
import com.ylx.order.domain.vo.AdminOrderDispatchMerchantVO;
import com.ylx.order.domain.vo.AdminOrderDispatchOrderVO;
import com.ylx.order.domain.vo.AdminOrderPageVO;
import com.ylx.order.domain.vo.AdminOrderServiceCategoryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface AdminOrderMapper extends BaseMapper<TOrder> {

    Page<AdminOrderPageVO> selectAdminOrderPage(Page<AdminOrderPageVO> page, @Param("dto") AdminOrderQueryDTO dto);

    Page<AdminOrderPageVO> selectAdminOrderRecyclePage(Page<AdminOrderPageVO> page, @Param("dto") AdminOrderQueryDTO dto);

    List<AdminOrderServiceCategoryVO> selectServiceCategoryOptions();

    int logicDeleteById(@Param("id") Long id);

    int restoreById(@Param("id") Long id);

    CancelOrderApplication selectRefundApplicationOrderById(@Param("id") Long id);

    int countPendingRefundApplicationByOrderNo(@Param("orderNo") String orderNo);

    int insertRefundApplication(@Param("application") CancelOrderApplication application);

    AdminOrderDetailVO.OrderInfoVO selectOrderDetailInfoById(@Param("id") Long id);

    AdminOrderDetailVO.ProjectInfoVO selectOrderProjectDetailById(@Param("id") Long id);

    List<AdminOrderDetailVO.OperationLogVO> selectOrderOperationLogs(@Param("id") Long id);

    AdminOrderDispatchOrderVO selectDispatchOrderById(@Param("id") Long id);

    /**
     * 查询订单派单候选商户
     * @param order
     * @param query
     * @return List<AdminOrderDispatchMerchantVO>
     */
    List<AdminOrderDispatchMerchantVO> selectDispatchMerchantCandidates(@Param("order") AdminOrderDispatchOrderVO order, @Param("query") AdminOrderDispatchMerchantQueryDTO query);

    /**
     *
     * @param order
     * @param merchantId 商户ID
     * @param radiusMeters 查询半径，单位米；为空时默认 10000 米。
     * @return AdminOrderDispatchMerchantVO
     */
    AdminOrderDispatchMerchantVO selectDispatchMerchantById(@Param("order") AdminOrderDispatchOrderVO order,
                                                            @Param("merchantId") Integer merchantId,
                                                            @Param("radiusMeters") java.math.BigDecimal radiusMeters);

    /**
     * 查询订单当前派单状态
     * @param orderId
     * @return int 订单当前派单状态
     */
    int countCurrentDispatchByOrderId(@Param("orderId") Long orderId);

    /**
     * 插入订单派单记录
     *
     * @param dispatch
     * @return int 插入结果
     */
    int insertOrderDispatch(OrderDispatch dispatch);

    /**
     * 更新订单派单状态
     * @param orderId
     * @param merchant
     * @param statusBefore
     * @param statusAfter
     * @return int 更新结果
     */
    int updateOrderDispatch(@Param("orderId") Long orderId, @Param("merchant") AdminOrderDispatchMerchantVO merchant,
                            @Param("statusBefore") Integer statusBefore,
                            @Param("statusAfter") Integer statusAfter);

    /**
     * 插入订单状态流转记录
     *
     * @param orderId
     * @param status
     * @param operator
     * @return int 插入结果
     */
    int insertOrderStatusFlow(@Param("orderId") Long orderId, @Param("status") Integer status, @Param("operator") String operator);
}
