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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.admin.domain.CouponTicket;
import com.qs.mp.admin.domain.TicketBox;
import com.qs.mp.admin.service.ICouponTicketService;
import com.qs.mp.common.enums.CouponTypeEnum;
import com.qs.mp.common.enums.CouponUseAreaEnum;
import com.qs.mp.common.enums.TicketTypeEnum;
import com.qs.mp.common.enums.UserCouponStatusEnum;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.user.domain.UserCoupon;
import com.qs.mp.user.domain.vo.UserCouponVO;
import com.qs.mp.user.mapper.UserCouponMapper;
import com.qs.mp.user.service.IUserCouponService;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * 用户的优惠券 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-03-07
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements
    IUserCouponService {

  @Autowired
  private ICouponTicketService couponTicketService;

  @Override
  public List<UserCouponVO> listTicketOrderCouponByIds(List<String> userCouponIds) {
    List<UserCouponVO> userCouponVOList = getBaseMapper().listUserCouponVO(
        new QueryWrapper<UserCoupon>()
            .in("t1.id", userCouponIds));
    return userCouponVOList;
  }

  @Override
  public List<UserCouponVO> listCoupon(Long userId, UserCouponStatusEnum status) {
    QueryWrapper<UserCoupon> queryWrapper = new QueryWrapper<UserCoupon>()
        .eq("t1.user_id", userId)
        .orderByAsc("t1.valid_end");
    if (status == UserCouponStatusEnum.UNUSED) {
      queryWrapper.eq("t1.status", UserCouponStatusEnum.UNUSED)
          .le("t1.valid_start", DateUtils.getToday()).ge("t1.valid_end", DateUtils.getToday());
    } else if (status == UserCouponStatusEnum.OVERDUE) {
      queryWrapper.eq("t1.status", UserCouponStatusEnum.UNUSED)
          .lt("t1.valid_end", DateUtils.getToday());
    } else if (status == UserCouponStatusEnum.USED) {
      queryWrapper.eq("t1.status", UserCouponStatusEnum.USED);
    }
    List<UserCouponVO> userCouponVOList = getBaseMapper().listUserCouponVO(queryWrapper);
    return userCouponVOList;
  }

  @Override
  public List<UserCouponVO> listUserCouponVO(Wrapper<UserCoupon> queryWrapper) {
    return getBaseMapper().listUserCouponVO(queryWrapper);
  }

  @Override
  public List<UserCouponVO> queryUserCouponList(Long userId, Integer orderAmt,
      TicketBox ticketBox) {
    // 系统自动选择优惠券，按照金额从大到小排序
    List<UserCouponVO> userCouponList = listTicketOrderCoupon(userId, orderAmt);
    Iterator<UserCouponVO> iterator = userCouponList.iterator();
    while (iterator.hasNext()) {
      UserCouponVO userCouponVO = iterator.next();
      // 校验使用范围
      if (userCouponVO.getUseArea() == CouponUseAreaEnum.PRE_SCOPE) {
        // 指定盲票
        List<CouponTicket> couponTicketList = couponTicketService.list(
            new LambdaQueryWrapper<CouponTicket>()
                .eq(CouponTicket::getCouponId, userCouponVO.getCouponId())
                .eq(CouponTicket::getBoxId, ticketBox.getBoxId()));
        if (CollectionUtils.isEmpty(couponTicketList)) {
          iterator.remove();
          continue;
        }
      } else if (userCouponVO.getUseArea() == CouponUseAreaEnum.ONLINE_SCOPE) {
        // 线上票
        if (ticketBox.getType() != TicketTypeEnum.ONLINE) {
          iterator.remove();
          continue;
        }
      } else if (userCouponVO.getUseArea() == CouponUseAreaEnum.OFFLINE_SCOPE) {
        // 线下票
        if (ticketBox.getType() != TicketTypeEnum.OFFLINE) {
          iterator.remove();
          continue;
        }
      }
    }
    return userCouponList;
  }

  private List<UserCouponVO> listTicketOrderCoupon(Long userId, Integer orderAmt) {
    List<UserCouponVO> userCouponVOList = getBaseMapper().listUserCouponVO(
        new QueryWrapper<UserCoupon>()
            .eq("t1.user_id", userId).eq("t2.type", CouponTypeEnum.TICKET_ORDER)
            .eq("t1.status", UserCouponStatusEnum.UNUSED)
            .le("t1.valid_start", DateUtils.getToday()).ge("t1.valid_end", DateUtils.getToday())
            .le("t2.min_order_amt", orderAmt)
            .le("t2.discount", orderAmt)
            .orderByDesc("t2.discount","t1.id"));
    return userCouponVOList;
  }


  @Override
  public Integer sumDiscountByQueryWrapper(QueryWrapper<UserCoupon> queryWrapper) {
    return getBaseMapper().sumDiscountByQueryWrapper(queryWrapper);
  }
}
