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

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.trade.controller.admin.orderjzdetail.vo.OrderJzDetailPageReqVO;
import com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderJzDetailDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 订单接站人员名单 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface OrderJzDetailMapper extends BaseMapperX<OrderJzDetailDO> {

  default PageResult<OrderJzDetailDO> selectPage(OrderJzDetailPageReqVO reqVO) {
    return selectPage(reqVO, new LambdaQueryWrapperX<OrderJzDetailDO>()
      .eqIfPresent(OrderJzDetailDO::getJzId, reqVO.getJzId())
      .eqIfPresent(OrderJzDetailDO::getArriveTime, reqVO.getArriveTime())
      .eqIfPresent(OrderJzDetailDO::getAddress, reqVO.getAddress())
      .likeIfPresent(OrderJzDetailDO::getName, reqVO.getName())
      .eqIfPresent(OrderJzDetailDO::getPhone, reqVO.getPhone())
      .eqIfPresent(OrderJzDetailDO::getRemark, reqVO.getRemark())
      .eqIfPresent(OrderJzDetailDO::getStatus, reqVO.getStatus())
      .betweenIfPresent(OrderJzDetailDO::getCreateTime, reqVO.getCreateTime())
      .orderByDesc(OrderJzDetailDO::getId));
  }

  default List<OrderJzDetailDO> selectList(OrderJzDetailPageReqVO reqVO) {
    return selectList(new LambdaQueryWrapperX<OrderJzDetailDO>()
            .eqIfPresent(OrderJzDetailDO::getJzId, reqVO.getJzId())
            .eqIfPresent(OrderJzDetailDO::getArriveTime, reqVO.getArriveTime())
            .eqIfPresent(OrderJzDetailDO::getAddress, reqVO.getAddress())
            .likeIfPresent(OrderJzDetailDO::getName, reqVO.getName())
            .eqIfPresent(OrderJzDetailDO::getPhone, reqVO.getPhone())
            .eqIfPresent(OrderJzDetailDO::getRemark, reqVO.getRemark())
            .eqIfPresent(OrderJzDetailDO::getStatus, reqVO.getStatus())
            .betweenIfPresent(OrderJzDetailDO::getCreateTime, reqVO.getCreateTime())
            .orderByDesc(OrderJzDetailDO::getId));
  }

  @Select("select d.* from trade_order_jz_detail d " +
    " inner join trade_order_jz oz on d.jz_id = oz.id " +
    " inner join trade_order o on o.deleted = 0 and o.id = oz.order_id " +
    " where d.deleted = 0 and o.voyage_id = #{voyageId}" +
    " order by d.address asc, d.arrive_time asc, d.batch_no asc")
  List<OrderJzDetailDO> selectPageByVoyageId(String voyageId);


  IPage<OrderJzDetailDO> selectPage2(IPage<OrderJzDetailDO> page, @Param("vo") OrderJzDetailPageReqVO vo);

  List<OrderJzDetailDO> selectVisitorExportList(@Param("vo") OrderJzDetailPageReqVO vo);

  IPage<Map> selectPage3(IPage<Map> page, @Param("vo") OrderJzDetailPageReqVO vo);

  Long cleanSign(@Param("id") String id);

    /**
     * 根据接站单ID列表软删除接站人员名单
     * 修改订单时，需要软删除旧接站头表关联的所有接站人员名单
     *
     * @param jzIdList 接站单ID列表
     * @param deleted  删除标记（1=已删除）
     */
    @Update("<script> UPDATE trade_order_jz_detail SET deleted = #{deleted}, update_time = now() WHERE jz_id IN " +
            "<foreach collection='jzIdList' item='jzId' open='(' separator=',' close=')'> #{jzId} </foreach>" +
            "</script>")
    @TenantIgnore
    void updateDeletedByJzIds(@Param("jzIdList") List<Long> jzIdList, @Param("deleted") int deleted);

  // ==================== 赠送行程接站相关 ====================

  IPage<OrderJzDetailDO> selectGiftPage(IPage<OrderJzDetailDO> page, @Param("vo") OrderJzDetailPageReqVO vo);

  List<OrderJzDetailDO> selectGiftExportList(@Param("vo") OrderJzDetailPageReqVO vo);

  IPage<Map> selectGiftOrderPage(IPage<Map> page, @Param("vo") OrderJzDetailPageReqVO vo);
}