package com.yc.ship.module.trade.dal.mysql.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorPageReqVO;
import com.yc.ship.module.otc.api.agency.dto.AgencyUserLoginInfoRespDTO;
import com.yc.ship.module.product.api.dto.CategoryRespDTO;
import com.yc.ship.module.trade.controller.admin.business.vo.DailyBusinessStatisticsReqVO;
import com.yc.ship.module.trade.controller.admin.business.vo.DailyBusinessStatisticsRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.check.CheckOrderPageVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.*;
import com.yc.ship.module.trade.controller.admin.order.vo.otc.PrintRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundPageReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundRecordPageReqVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundRecordRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.rule.MarketingPageReqVO;
import com.yc.ship.module.trade.controller.app.otc.vo.*;
import com.yc.ship.module.trade.controller.app.pda.vo.SupplierCheckOrderPageVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.service.order.bo.TradeBindOrderQueryBO;
import org.apache.ibatis.annotations.Mapper;
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 管理员
 */
@Mapper
public interface TradeOrderMapper extends BaseMapperX<TradeOrderDO> {

    // region --------------------------------------- 查询操作 -----------------------------------------
    @TenantIgnore
    List<CategoryRespDTO> getCategoryTreeList(@Param("tenantId") Long tenantId);

    List<CategoryRespDTO> getCategoryOTATreeList();

    IPage<TradeOrderRespVO> getTradeOrderUserPage(IPage<TradeOrderRespVO> page, @Param("vo") TradeOrderPageReqVO vo);

    TradeOrderCountRespVO getTradeOrderUserCount(@Param("vo") TradeOrderPageReqVO vo);

    IPage<TradeOrderRespVO> getPaidOrderPage(IPage<TradeOrderRespVO> page, @Param("vo") TradeOrderPageReqVO vo);

    IPage<TradeDetailRespVO> getRefundDetailPage(IPage<TradeDetailRespVO> page, @Param("vo") TradeDetailPageReqVO vo);

    IPage<RefundRespVO> getRefundRecordPage(IPage<RefundRespVO> page, @Param("vo") RefundPageReqVO vo);

    TradeOrderRespVO getOrderInfo(Long id);

    TradeOrderPdaRespVO getPdaOrderInfo(Long id);

    @TenantIgnore
    IPage<TradeOrderRespVO> getMarketingApplyOrderPage(IPage<TradeOrderRespVO> page, @Param("vo") MarketingPageReqVO vo);

    @TenantIgnore
    IPage<TradeMarketOrderRespVO> getStoresMarketingOrderPage(IPage<TradeMarketOrderRespVO> page, @Param("vo") MarketingPageReqVO vo);

    List<Map<String, Object>> getStoresMarketingOrderDesc(IPage<TradeOrderRespVO> page, @Param("vo") MarketingPageReqVO vo);

    List<Long> selectBindMarketing(@Param("orderId") Long orderId);

    List<Long> selectBindUseMarketing(@Param("orderId") Long orderId);

    List<Long> selectOrderIdByOrderNo(@Param("orderNo") String orderNo);

    TradeOrderRespVO getRefundOrderInfo(Long id);

    List<TradeOrderDO> getTradeOrderByExpire(@Param("expiretime") String expiretime);


    IPage<RefundRecordRespVO> getPageRefundRecord(IPage<RefundRecordRespVO> page, @Param("vo") RefundRecordPageReqVO vo);

    @TenantIgnore
    PrintRespVO getPrintTemp(Integer printTemp);

    TradeOrderDO getOrderByVoucherCode(@Param("voucherCode") String voucherCode);

    TradeOrderDO getOrderByExternalOrderNo(@Param("tradeNo") String tradeNo);

    List<Map<String, Object>> getOtcOrderByExternalOrderNo(@Param("tradeNo") String tradeNo);

    TradeOrderDO getTradeOrderByVoucherCode(@Param("voucherCode") String voucherCode);

    List<TradeOrderDO> getOrderByCardNo(@Param("cardNo") String cardNo);

    @TenantIgnore
    IPage<TradeOrderRespVO> queryCanInvoicedOrderPage(IPage<TradeOrderRespVO> page, @Param("vo") TradeOrderPageReqVO pageReqVO, @Param("canInvoicedDay") Integer canInvoicedDay);
    // endregion

    // region ---------------------------------------- 修改操作 -----------------------------------------------
    int updateOrderStatus(@Param("orderId") Long orderId, @Param("orderStatus") Integer orderStatus);

    int updateOrderIsAccount(@Param("orderId") Long orderId, @Param("isAccount") Integer isAccount);

    @TenantIgnore
    int updateOrderDeleted(@Param("orderId") Long orderId, @Param("deleted") Integer deleted);

    // endregion

    IPage<TradeOrderRespVO> getAccountOrderPage(IPage<TradeOrderRespVO> page, @Param("vo") TradeOrderPageReqVO vo);

    List<TradeOrderPdaRespVO> getOrderAndDetailInfo(@Param("orderNo") String orderNo, @Param("cardNo") String cardNo, @Param("phone") String phone, @Param("dispatchId") Long dispatchId, @Param("channelId") Long channelId);

    String getPlatOrderNo(@Param("orderNo") String orderNo);

    @TenantIgnore
    BigDecimal getTotalPayByOrderNo(@Param("orderNo") String orderNo);

    IPage<TradeOrderRespVO> getCheckOrder(IPage<TradeOrderRespVO> page, @Param("vo") CheckOrderPageVO reqVO);

    int countCheckOrder(@Param("vo") CheckOrderPageVO reqVO);

    TradeOrderDO getOrderByDetailBase(@Param("baseId") String baseId);

    //通过登录用户和设备获取绑定的产品
    List<Long> selectStoreMachineProductList(@Param("vo") AgencyUserLoginInfoRespDTO vo);

    @Select("SELECT product_id FROM otc_agency_store_product where store_id = #{storeId} and deleted = 0")
    List<Long> selectStoreProductList(Long storeId);

    @TenantIgnore
    List<TradeOrderDO> orderListByOta(@Param("tenantId") Long tenantId, @Param("travelDate") String travelDate, @Param("credentialNo") String credentialNo, @Param("mobile") String mobile);

    @TenantIgnore
    List<TradeOrderDO> orderList(@Param("tenantId") Long tenantId, @Param("orderStatus") List<Integer> orderStatus, @Param("travelDate") String travelDate, @Param("name") String name, @Param("mobile") String mobile, @Param("credentialNo") String credentialNo);


    @TenantIgnore
    TradeOrderDO queryOneByOrderNo(@Param("orderNo") String orderNo);

    @TenantIgnore
    void updateAppletProductSellNum(@Param("productId") Long productId, @Param("sellNum") int sellNum);

    List<TradeOrderDO> selectCanBindOrderList(@Param("vo") TradeBindOrderQueryBO vo);

    void deleteOrderById(@Param("orderId") Long orderId);
    void deleteOrderByOrderNo(@Param("orderNo") String orderNo);

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

    Map<String, Object> selectPlatPayAmountByOriginOrderNo(@Param("originOrderNo") String originOrderNo);

    /**
     * 查询订单出行状态为null的订单总数
     *
     * @return 总数
     */
    @TenantIgnore
    @Select("SELECT COUNT(1) FROM trade_order WHERE travel_status is null and order_status > 5")
    Integer queryTravelStatusIsNullTotal();

    /**
     * 查询订单出行状态为null的订单id
     *
     * @param pageNo   页数*数量
     * @param pageSize 每次查询数量
     * @return list
     */
    @TenantIgnore
    @Select("SELECT id FROM trade_order WHERE travel_status is null and order_status > 5 ORDER BY create_time limit #{pageNo}, #{pageSize}")
    List<Long> queryTravelStatusIsNull(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize);


    Integer selectChangeCountByOrderId(@Param("orderId") Long orderId);


    IPage<TradeOrderRespVO> getSupplierCheckOrder(IPage<TradeOrderRespVO> page, @Param("vo") SupplierCheckOrderPageVO reqVO);

    int updateOrderFinishStatus(@Param("sellMethodList") List<Integer> sellMethodList, @Param("endDate") String endDate);

    @TenantIgnore
    IPage<TradeDetailRespVO> getOtaTradeOrderPage(IPage<TradeDetailRespVO> page, @Param("vo") DistributorPageReqVO vo, @Param("distributorId") Long distributorId);

    List<Map<String, Object>> regionStatic(@Param("vo") TradeStaticReqVO regionStatic);

    List<TradeOrderDO> queryOrderByLast(@Param("orderNo")  String orderNo);
    List<TradeOrderDO> queryOrderById(@Param("orderId")  Long orderId);
    /**
     * 查询订单确认单中入住的房型国家信息
     */
    List<TradeOrderConfirmationCountryRespVO> selectConfirmationCountry(@Param("orderId") Long orderId);

    Map<String, Object> getOrderCount(@Param("vo") TradeOrderPageReqVO pageReqVO);

    OrderTotalRespVO selectTotalByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 查询游客名单导出基础信息（船名、航期、航向）
     */
    Map<String, Object> selectTouristExportBase(@Param("vo") TradeOrderPageReqVO reqVO);

    /**
     * 查询游客名单导出表头信息
     */
    @TenantIgnore
    Map<String, Object> selectTouristExportHead(@Param("vo") TradeOrderPageReqVO reqVO);

    /**
     * 小程序订单分页
     * @param iPage
     * @param pageReqVO
     * @return
     */
    IPage<AppTradeOrderRespNewVO> selectAppPage(IPage<AppTradeOrderRespNewVO> iPage, @Param("pageReqVO") AppTradeOrderPageReqNewVO pageReqVO);
    /**
     * 我的行程
     * @param memberId
     * @param status
     * @return
     */
    List<AppMyItineraryVO> selectMyItinerary(@Param("memberId") Long memberId, @Param("status") Integer status, @Param("keyword") String keyword);
    /**
     * 统计另外的订单合计
     * @param orderIds
     * @return
     */
    OrderTotalRespVO selectTotal1ByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 获取营业统计日报
     * @param
     * @return
     */
    IPage<DailyBusinessStatisticsRespVO> getDailyBusinessStatisticsPage(IPage<DailyBusinessStatisticsRespVO> page,@Param("vo")  DailyBusinessStatisticsReqVO pageReqVO);
    /**
     * 获取行程详情
     * @param orderId
     * @return
     */
    AppItineraryDetailVO selectItineraryDetail(@Param("orderId") Long orderId);
    /**
     * 统计另外的订单合计
     * @param orderIds
     * @return
     */
    OrderTotalRespVO selectTotal2ByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 查询可以购买保险的订单ID
     * @return
     */
    List<Long> selectCanBuyInsuranceOrder();

    List<Map<String, Object>> queryOrderLink(@Param("day") int day,@Param("direction")int direction);

    List<Map<String, Object>> queryOrderJzPhone(@Param("day") int day,@Param("direction")int direction);

    List<Map<String, Object>> queryOrderVisitorMobile(@Param("day") int day,@Param("direction") int direction);

    List<Map<String, Object>> getTradeJz();

    default TradeOrderDO selectByOrderNo(String orderNo) {
        return selectOne(new LambdaQueryWrapper<TradeOrderDO>().eq(TradeOrderDO::getOrderNo, orderNo).last("LIMIT 1"));
    }
}
