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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yc.ship.framework.mybatis.core.mapper.BaseMapperX;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.product.api.dto.*;
import com.yc.ship.module.trade.api.sms.dto.SmsSendLogDTO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.OrderTotalSpuVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderDetailCountRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.refund.RefundDetailRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.rule.GateCheckDetailRespVO;
import com.yc.ship.module.trade.controller.admin.order.vo.rule.MarketingPageReqVO;
import com.yc.ship.module.trade.controller.admin.smsSendconfig.vo.SmsSendconfigQueryRespVO;
import com.yc.ship.module.trade.dal.dataobject.order.CategoryRespVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeDetailDO;
import com.yc.ship.module.trade.service.order.bo.TradeDetailBO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.cache.annotation.Cacheable;

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


/**
 * 订单明细 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface TradeDetailMapper extends BaseMapperX<TradeDetailDO> {

    // region ---------------------------------------- 查询操作 -----------------------------------------------
    List<TradeDetailBO> selectTradeDetailWithBase(@Param("orderId") Long orderId,
                                                  @Param("tradeDetailId") Long tradeDetailId,
                                                  @Param("voucherList") List<String> voucherList,
                                                  @Param("detailIdList") List<Long> detailIdList);

    Map<String, Object> queryDetailCountByOrderId(@Param("orderId") Long orderId);

    Map<String, Long> selectRefundCount(@Param("orderId") Long orderId);

    List<Map<String, Object>> getDetailInfoByDetailId( Map<String, Object> param);

    TradeDetailDO getOneByCardNo(@Param("orderId") Long orderId, @Param("cardNo") String cardNo);
    TradeDetailDO getOneByCardNoAndChannelId(@Param("channelId") Long channelId, @Param("cardNo") String cardNo);
    TradeDetailDO getOneByVoucherCode(@Param("voucherCode") String voucherCode);
    @TenantIgnore
    int updateImgByVoucherCode(@Param("voucherCode") String voucherCode,@Param("url") String url);
    @TenantIgnore
    GateCheckDetailRespVO getOneCheckGateQuery(@Param("voucherCode") String voucherCode);
    @TenantIgnore
    String getProductTag4(@Param("productId") Long productId);
    TradeDetailDO getOneByVoucherCodePlat(@Param("voucherCode") String voucherCode);
    List<TradeDetailDO> getListByVoucherCodePlat(@Param("voucherCodes") List<String> voucherCodes);
    List<TradeDetailDO> getListByVoucherCodeOtc(@Param("voucherCodes") List<String> voucherCodes);

    List<TradeDetailRespVO> selectDetailAndVisitor(@Param("orderId") Long orderId, @Param("detailVoucherList") List<String> detailVoucherList);
    List<Map<String,Object>> selectOrderProduct(@Param("orderId") Long orderId);
    List<Map<String,Object>> selectOrderProductV2(@Param("orderId") Long orderId,@Param("noContainSpecList") List<String> noContainSpecList);

    @TenantIgnore
    int selectNoCheckCount(@Param("orderId") Long orderId);
    // endregion

    // region ---------------------------------------- 修改操作 -----------------------------------------------
    int updateRefundingBatch(@Param("tradeDetailList") List<TradeDetailDO> tradeDetailDOList,
                             @Param("refundStatus") Integer refundStatus);

    @TenantIgnore
    int updateStatusAfterAllCheck(@Param("orderId") Long orderId);

    int updateVoucherStatus(@Param("tradeDetailId") Long tradeDetailId, @Param("voucherStatus") Integer voucherStatus);
    // endregion

    // region ------------------------------------- 查询产品相关基础信息 -----------------------------------------
    @TenantIgnore
    ProductSpuRespDTO getProductSpuPlat(@Param("productId") Long productId, @Param("tenantId") Long tenantId);

    @TenantIgnore
    ProductSpuRespDTO getProductSpu(@Param("productId") Long productId);

    @TenantIgnore
    List<ProductGroupRespDTO> getProductSupInfo(@Param("productId") Long productId);

    @TenantIgnore
    List<ProductGroupRespDTO> getProductSupInfoByPlanId(@Param("productId") Long productId, @Param("planId") String planId);
    @TenantIgnore
    List<ProductGroupRespDTO> getProductSkuInfo(@Param("productId") Long productId,@Param("skuId") Long skuId,@Param("distributorId") Long distributorId);


    @TenantIgnore
    @Cacheable(value = "selectSpuListByTenantId#10", key = "#p0 + '_' + #p1 + '_' + #p2 + '_' + #p3")
    List<ProductSpuRespDTO> selectSpuListByTenantId(@Param("categoryId") Long categoryId, @Param("tenantId") Long tenantId,@Param("type") String type,@Param("useDate") String useDate);

    List<ProductSpuRespDTO> platProductAll(@Param("productIds") List<Long> productIds);
    @TenantIgnore
    List<ProductTakeRuleRespDTO> getProductTakeRules(Long orderId);

    List<ProductSpuRespDTO> selectSpuOTAListByTenantId(@Param("categoryId") Long categoryId,@Param("type")String type);
    ProductBasicRuleRespDTO getProductBasicRule(@Param("productId") Long productId);
    ProductSaleRuleRespDTO getProductSaleRule(@Param("productId") Long productId);
    ProductRefundRuleRespDTO getProductRefundRule(@Param("productId") Long productId);

    ProductTakeRuleRespDTO getProductTakeRule(@Param("productId") Long productId);

    ProductOtherRuleRespDTO getProductOtherRule(@Param("productId") Long productId);

    List<ProductSkuRespDTO> getProductSukList(@Param("productId") Long productId);
    @TenantIgnore
    List<ProductSkuRespDTO> getProductSkuPriceList(@Param("productId") Long productId,@Param("distributorId") Long distributorId);

    ProductCheckRuleRespDTO getProductCheckRule(@Param("productId") Long productId);

    @TenantIgnore
    List<TradeDetailRespVO> getOrderIdDetail(@Param("orderId") Long orderId);
    @TenantIgnore
    List<TradeDetailRespVO> getOrderIdDetailList(@Param("vo") MarketingPageReqVO vo);

    @TenantIgnore
    List<TradeDetailRespVO> getOrderIdDetailList1(@Param("vo") MarketingPageReqVO vo);

    List<TradeDetailRespVO> getOrderIdDetailList2(@Param("orderIds") List<Long> orderIds);
    List<TradeDetailRespVO> getOrderIdDetailV2(@Param("orderId") Long orderId);
    List<TradeDetailRespVO> getOtcOrderIdDetail(@Param("orderId") Long orderId);

    List<Map<String,Object>> selectInsuranceProductFromOrder(@Param("orderId")Long orderId);

    List<Map<String,Object>> selectContractProductFromOrder(@Param("orderId")Long orderId);

    List<Map<String, Object>> queryNotifyFailedVoucherInfo(Map<String, Object> param);

    List<TradeDetailRespVO> getTradeDetailWithBaseByCodeAndIdcard(@Param("voucherCode") String voucherCode,
                                                  @Param("idCard") String idCard,@Param("dispatchId") Long dispatchId,@Param("channelId") Long channelId);

    List<TradeDetailRespVO> getPlatByVoucherCode1(@Param("voucherCode") String voucherCode);
    List<TradeDetailRespVO> getPlatByVoucherCode2(@Param("voucherCode") String voucherCode);
    // endregion
    @TenantIgnore
    List<TradeDetailRespVO> getOrderDetail(@Param("orderId") Long orderId);
    @TenantIgnore
    List<TradeDetailRespVO> getOrderDetailByOrderId(@Param("orderId") Long orderId);

    Map<String,Object> getProductProject(@Param("projectId") Long projectId);

    List<Map<String, Object>> selectVisitorOrderProductCount(@Param("productId") Long productId,@Param("useDate") LocalDateTime useDate,@Param("credentialNoList") List<String> credentialNoList,@Param("excludeOrderId") Long excludeOrderId);

    TradeDetailRespVO getTradeDetailByCheckId(@Param("checkId") Long checkId);
    List<TradeDetailDO> selectPlatDetailByDetailIds(@Param("detailIdList") List<Long> detailIdList,@Param("orderId") Long orderId);
    List<TradeDetailDO> getPlatDetailByOrderId(@Param("orderId") Long orderId);
    List<TradeDetailDO> selectChangeDetailList(@Param("orderId") Long orderId,@Param("originOrderId") Long originOrderId);

    List<TradeDetailDO> selectToCheckList(@Param("useDate") String useDate);
    List<TradeDetailDO> selectToAllCheckList(@Param("useDate") String useDate);
    List<TradeDetailDO> selectToTravlCheckList(@Param("useDate") String useDate);

    TradeDetailDO selectTravelTradeDetail(@Param("voucherCode") String voucherCode);
    TradeDetailDO selectTradeDetailByVistorId(@Param("visitorId") Long visitorId);

    TradeDetailBaseDO selectPlatTradeBaseDetail(@Param("voucherCode") String voucherCode);
    List<Map<String,Object>> selectDispatchRecord(@Param("dispatchId") String dispatchId, @Param("detailId") long detailId);

    void insertSmsLog(@Param("smsSendLogDTO") SmsSendLogDTO smsSendLogDTO);

    List<CategoryRespVO> getAllUpCategoryById(@Param("id") Long id, @Param("type") Integer type);

    @TenantIgnore
    IPage<TradeDetailDO> selectOverdueOrderDetailForRefund(IPage<TradeDetailDO> page,@Param("overdueDay") String overdueDay);

    BigDecimal selectCheckAmount(@Param("orderId") Long orderId);

    List<Map<String,Object>> selectOrderPackProduct(@Param("orderId") Long orderId);

    Map<String,Object> queryRoutePlanDetailTimeByPlanId(@Param("planId") Long planId);

    List<Map<String,Object>> selectContractByOrderId(@Param("orderId") Long orderId);

    List<SmsSendconfigQueryRespVO> selectOrderSmsInfo(@Param("orderId") Long orderId, @Param("type") Integer type);
    List<SmsSendconfigQueryRespVO> selectCreateOrderSmsInfo(@Param("orderId") Long orderId, @Param("type") Integer type);
    List<RoutePlanRespDTO> getRoutePlanByProductId(@Param("productId") Long productId, @Param("useStatus") Long useStatus);

    List<RefundDetailRespVO> getRefundBatchDetails(@Param("refundId") Long refundId);

    @TenantIgnore
    int getMaxOrderNo(@Param("voyageId") Long voyageId);

    @TenantIgnore
    Integer getNewMaxOrderNo(@Param("voyageId") Long voyageId,@Param("code") String code);

    List<TradeOrderDetailCountRespVO> getOrderIdDetailCount(@Param("orderId") Long orderId);
    @TenantIgnore
    void updateDeleted(@Param("orderId") Long orderId, @Param("deleted")  int deleted);

    /**
     * 获取订单下所有产品
     * @param orderIds
     * @return
     */
    List<OrderTotalSpuVO> selectOrderSpuStatsByOrderIds(@Param("orderIds") List<Long> orderIds);
}
