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.mybatis.core.query.LambdaQueryWrapperX;
import com.yc.ship.framework.tenant.core.aop.TenantIgnore;
import com.yc.ship.module.product.api.dto.OrderRoomUseDTO;
import com.yc.ship.module.trade.controller.admin.order.vo.order.*;
import com.yc.ship.module.trade.controller.admin.visitor.vo.VisitorPageReqVO;
import com.yc.ship.module.trade.controller.admin.visitor.vo.VisitorRespVO;
import com.yc.ship.module.trade.controller.app.otc.vo.AppItineraryPersonVO;
import com.yc.ship.module.trade.controller.app.otc.vo.AppPersonDescVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO;
import com.yc.ship.module.trade.enums.InsuranceStatusEnum;
import com.yc.ship.module.trade.service.order.bo.TradeVisitorBO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 游客 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface TradeVisitorMapper extends BaseMapperX<TradeVisitorDO> {

    List<TradeVisitorDO> selectVisitorListBySupplierOrder(@Param("platSupplierOrderId") Long platSupplierOrderId);

    List<TradeVisitorDO> selectVisitorListBySpecType(@Param("platSupplierOrderId") Long platSupplierOrderId, @Param("specType") String specType, @Param("baseList") List<Long> baseList);

    List<TradeVisitorBO> selectVisitorVoucherListByOrderId(@Param("orderId") Long orderId);

    List<TradeVisitorBO> selectInsureVisitor(@Param("orderId") Long orderId,@Param("productId") Long productId);

    List<TradeVisitorBO> selectContractVisitor(@Param("orderId") Long orderId,@Param("productId") Long productId);

    List<TradeVisitorBO> selectSupplierVisitor(@Param("platSupplierOrderId") Long platSupplierOrderId,@Param("visitorIdList") List<Long> visitorIdList);

    List<TradeVisitorDO> queryRelatedVisitor(@Param("visitorIds") List<Long> visitorIds);
    List<TradeVisitorRespVO> queryVisitorByOrderId(@Param("orderId") Long orderId);
    IPage<TradeVisitorBO> selectVisitorListByDate(IPage<TradeVisitorBO> page,@Param("orderId") Long orderId, @Param("useDate") Date useDate,@Param("tenantId") Long tenantId);
    List<TradeVisitorDO> selectBoatVisitorListByRefund(@Param("refundId") Long refundId, @Param("useDate") Date useDate);
    Integer selectOrderVisitorCount(@Param("orderId") Long orderId);


    List<TradeVisitorDO> selectShipVisitor(@Param("orderId") Long orderId);
    List<OrderRoomUseDTO> selectRoomShipVisitor(@Param("orderId") Long orderId);
    List<OrderRoomUseDTO> selectRefundRoomShipVisitor(@Param("refundId") Long refundId);

    @TenantIgnore
    List<OrderRoomUseDTO> selectRoomShipVisitor2(@Param("orderId") Long orderId);

    /**
     * 获取游轮的游客
     * @param orderIds
     * @return
     */
    List<TradeVisitorDO> selectShipByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 获取当前线路当前证件是否已经购买
     */
    Integer queryVisitorByVovageId(@Param("voyageId") Long voyageId,@Param("credentialNo") String credentialNo,@Param("orderNo") String orderNo);


    /**
     * 查询该航次所有游客信息
     * @param voyageId
     * @return
     */
    List<TradeVisitorDO> selectListByVoyageId(@Param("voyageId") String voyageId);

    @Update("UPDATE trade_visitor SET deleted = #{deleted},update_time = now(), sync_status = '0' WHERE order_id = #{orderId}")
    @TenantIgnore
    void updateDeleted(@Param("orderId") Long orderId, @Param("deleted")  int deleted);


    List<TradeVisitorDO> selectListByVoyageIdAndRoomId(@Param("voyageId")Long voyageId, @Param("roomId")Long roomId);

    List<TradeOrderRespExcelVO> getExportVisitorList(@Param("vo") TradeOrderPageReqVO reqVO);

    /**
     * 查询游客名单导出游客列表
     */
    List<TouristExportVisitorVO> selectTouristExportVisitor(@Param("vo") TradeOrderPageReqVO reqVO);

    List<AppPersonDescVO> selectPersonDesc(@Param("orderIds") List<Long> orderIds, @Param("keyword") String keyword);
    /**
     * 获取订单陪同和领队男女统计
     */
    List<OrderWithLeaderStatVO> selectWithLeaderStatsByOrderIds(@Param("orderIds") List<Long> orderIds);
    /**
     * 获取订单国家统计
     */
    List<OrderTotalCountryVO> selectOrderCountryStatsByOrderIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 获取小程序行程详情中的同行人员
     */
    List<AppItineraryPersonVO> selectPersonListByOrderId(@Param("orderId") Long orderId, @Param("keyword") String keyword);

    default List<TradeVisitorDO> selectListByOrderId(Long orderId) {
        return selectList(new LambdaQueryWrapper<TradeVisitorDO>().eq(TradeVisitorDO::getOrderId, orderId));
    }

    default void updateInsuranceById(Long visitorId, Integer insuranceStatus, String resMsg) {
        update(new TradeVisitorDO().setIsInsure(insuranceStatus).setInsureMsg(resMsg), new LambdaQueryWrapper<TradeVisitorDO>().eq(TradeVisitorDO::getId, visitorId));
    }

    /**
     * 查询未投保的游客Id
     * @param orderIdList
     * @return
     */
    default List<Long> selectNotInsureByOrderIds(List<Long> orderIdList) {
        return selectList(new LambdaQueryWrapper<TradeVisitorDO>().eq(TradeVisitorDO::getIsInsure, InsuranceStatusEnum.NOT_INSURE).in(TradeVisitorDO::getOrderId, orderIdList))
                .stream().map(TradeVisitorDO::getId).collect(Collectors.toList());
    }

    IPage<VisitorRespVO> selectVisitorPage(IPage<VisitorRespVO> page, @Param("vo") VisitorPageReqVO vo);
}