package com.qs.mp.quartz.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qs.mp.admin.domain.Ticket;
import com.qs.mp.admin.domain.TicketBox;
import com.qs.mp.admin.domain.TicketPackage;
import com.qs.mp.admin.service.ITicketBoxService;
import com.qs.mp.admin.service.ITicketPackageService;
import com.qs.mp.admin.service.ITicketService;
import com.qs.mp.channel.domain.ChannelOrder;
import com.qs.mp.channel.service.IChannelOrderService;
import com.qs.mp.common.enums.ChannelOrderStatusEnum;
import com.qs.mp.common.enums.TicketPkgSaleStatusEnum;
import com.qs.mp.common.enums.TicketStatusEnum;
import com.qs.mp.common.enums.TicketTypeEnum;
import com.qs.mp.common.utils.DateUtils;
import com.qs.mp.common.utils.LogUtil;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 票包销售数量更新
 * @author zhongcp
 * @Date 2022/3/17
 */
@Component("ticketPkgDataTask")
public class TicketPkgDataTask {

  protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

  @Autowired
  private ITicketPackageService ticketPackageService;

  @Autowired
  private ITicketBoxService ticketBoxService;

  @Autowired
  private ITicketService ticketService;


  /**
   * 刷新票包的销量数据
   */
  public void refresh() {
    LogUtil.info(logger, "...票包销售数量更新任务开始...");

    List<TicketPackage> ticketPackageList = ticketPackageService.list(new LambdaQueryWrapper<TicketPackage>());

    for (TicketPackage ticketPackage : ticketPackageList) {
      TicketBox ticketBox = ticketBoxService.getById(ticketPackage.getBoxId());
      int saleQty = ticketService.count(new LambdaQueryWrapper<Ticket>()
          .eq(Ticket::getPkgId, ticketPackage.getPkgId())
          .gt(Ticket::getStatus, TicketStatusEnum.NOT_PAY));
      TicketPkgSaleStatusEnum saleStatusEnum;
      if (saleQty == 0) {
        saleStatusEnum = TicketPkgSaleStatusEnum.WAIT_SALE;
      } else if (saleQty < ticketBox.getPkgUnit()) {
        saleStatusEnum = TicketPkgSaleStatusEnum.ON_SALE;
      } else {
        saleStatusEnum = TicketPkgSaleStatusEnum.SALE_OUT;
      }
      ticketPackageService.update(new LambdaUpdateWrapper<TicketPackage>()
          .set(TicketPackage::getPkgUnit, ticketBox.getPkgUnit())
          .set(TicketPackage::getSaleQty, saleQty)
          .set(TicketPackage::getSaleStatus, saleStatusEnum)
          .eq(TicketPackage::getPkgId, ticketPackage.getPkgId()));
    }

    List<TicketBox> ticketBoxList = ticketBoxService.list(new LambdaQueryWrapper<TicketBox>()
        .eq(TicketBox::getType, TicketTypeEnum.OFFLINE));

    for (TicketBox ticketBox : ticketBoxList) {
      int saleQty = ticketService.count(new LambdaQueryWrapper<Ticket>()
          .eq(Ticket::getBoxId, ticketBox.getBoxId())
          .gt(Ticket::getStatus, TicketStatusEnum.NOT_PAY));
      ticketBoxService.update(new LambdaUpdateWrapper<TicketBox>()
          .set(TicketBox::getSaleQty, saleQty)
          .eq(TicketBox::getBoxId, ticketBox.getBoxId()));
    }
    LogUtil.info(logger, "...票包销售数量更新任务结束...");
  }
}
