package com.qs.mp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.mp.admin.domain.vo.TicketListVO;
import com.qs.mp.admin.domain.vo.UserTicketOrderItemVO;
import com.qs.mp.admin.domain.vo.UserTicketOrderTicketDetailVO;
import com.qs.mp.common.enums.ErrorCodeEnum;
import com.qs.mp.common.enums.TicketStatusEnum;
import com.qs.mp.common.enums.UserTicketOrderStatusEnum;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.user.domain.UserTicketOrderItem;
import com.qs.mp.user.mapper.UserTicketOrderItemMapper;
import com.qs.mp.user.service.IUserTicketOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 用户盲票订单明细 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-07
 */
@Service
public class UserTicketOrderItemServiceImpl extends ServiceImpl<UserTicketOrderItemMapper, UserTicketOrderItem> implements IUserTicketOrderItemService {

  @Override
  public int countUserTicketOrderItem(QueryWrapper<UserTicketOrderItem> queryWrapper) {
    return getBaseMapper().countUserTicketOrderItem(queryWrapper);
  }

  @Override
  public List<UserTicketOrderItemVO> listUserTicketOrderItem(QueryWrapper<UserTicketOrderItem> queryWrapper) {
    return getBaseMapper().listUserTicketOrderItem(queryWrapper);
  }

  @Override
  public UserTicketOrderItem queryFinishedOrderItem(Long userId, String ticketId) {
    List<UserTicketOrderItem> itemList = getBaseMapper().listTicketOrderItemVO(new QueryWrapper<UserTicketOrderItem>().eq("t1.ticket_id", ticketId)
        .eq("t1.user_id", userId).eq("t2.status", UserTicketOrderStatusEnum.FINISHED));
    if (CollectionUtils.isEmpty(itemList)) {
      return null;
    }
    return itemList.get(0);
  }

  @Override
  public List<TicketListVO> listMyTicketVO(Long userId, TicketStatusEnum status) {

    return getBaseMapper().listMyTicketVO(new QueryWrapper<UserTicketOrderItem>()
        .eq("t1.user_id", userId).eq("t2.status", UserTicketOrderStatusEnum.FINISHED)
        .eq("t3.status", status).orderByDesc("t1.created_time"));
  }

  @Override
  public int countMyTicket(Long userId, TicketStatusEnum status) {
    return getBaseMapper().countMyTicket(new QueryWrapper<UserTicketOrderItem>()
        .eq("t1.user_id", userId).eq("t2.status", UserTicketOrderStatusEnum.FINISHED)
        .eq(null != status, "t3.status", status));
  }

  @Override
  public TicketListVO queryTicketVO(Long userId, String orderId) {
    List<TicketListVO> ticketListVOS = getBaseMapper().listMyTicketVO(new QueryWrapper<UserTicketOrderItem>()
        .eq("t1.user_id", userId).eq("t1.order_id", orderId).eq("t2.status", UserTicketOrderStatusEnum.FINISHED));
    if (CollectionUtils.isEmpty(ticketListVOS)) {
      throw new ServiceException(ErrorCodeEnum.ERROR_CODE_1022);
    }
    return ticketListVOS.get(0);
  }

  @Override
  public List<UserTicketOrderTicketDetailVO> listTicketDetailByOrderId(String orderId) {
    QueryWrapper<UserTicketOrderTicketDetailVO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("t1.order_id", orderId);
    return getBaseMapper().listTicketDetailByOrderId(queryWrapper);
  }
}
