package com.ics.quartz.task;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ics.admin.domain.meeting.CustomerTicket;
import com.ics.admin.domain.meeting.Reservation;
import com.ics.admin.domain.meeting.Ticket;
import com.ics.admin.service.meeting.ICustomerTicketService;
import com.ics.admin.service.meeting.IReservationService;
import com.ics.admin.service.meeting.ITicketService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component("payTimeOutTask")
public class PayTimeOutTask {

    @Autowired
    private IReservationService iReservationService;

    @Autowired
    private ICustomerTicketService customerTicketService;

    @Autowired
    private ITicketService ticketService;



    // 支付超时处理
    public void payTimeOut() {
        log.info("执行支付超时处理任务");
        // 处理逻辑
        Reservation reservation = new Reservation();
        reservation.setStatusValue(0);
        List<Reservation> reservations = iReservationService.selectReservationList(reservation);
        for (Reservation reservation1 : reservations) {
            Date endDate = reservation1.getEndDate();
            DateUtil.offset(endDate, DateField.MINUTE, 5);
            int compare = DateUtil.compare(DateUtil.date(), endDate);
            if (compare < 0) {
                reservation1.setStauts(Reservation.Status.CANCELED);
                reservation1.setCancelResaon("管理员取消原因为:支付超时");
                iReservationService.updateReservation(reservation1);


                Long ticketId = reservation.getTicketId();
                if (ticketId !=null){
                    Ticket ticket = ticketService.selectTicketById(ticketId);
                    if (ticket != null){
                        Long customerId = reservation.getCustomerId();
                        CustomerTicket customerTicket = new CustomerTicket();
                        customerTicket.setTicketId(ticketId);
                        customerTicket.setCustomerId(customerId);
                        customerTicket.setType(ticket.getType());
                        customerTicket.setIsVerification(0);
                        int i1 = customerTicketService.insertCustomerTicket(customerTicket);
                    }

                }

                log.info("支付超时处理任务执行成功");
            }
        }
    }


    /**
     * 开会开始状态
     */

    public void meetingStartStatus() {
        log.info("执行开会开始状态任务");
        Reservation reservation = new Reservation();

        reservation.setStatusValue(1);
        List<Reservation> reservations = iReservationService.selectReservationList(reservation);
        for (Reservation reservation1 : reservations) {
            Date startTime = reservation1.getStartTime();
            Date endDate = reservation1.getEndDate();
            boolean in = DateUtil.isIn(DateUtil.date(), startTime, endDate);
            if (in) {
                reservation1.setStauts(Reservation.Status.ONGOING);
                iReservationService.updateReservation(reservation1);
                log.info("执行开会开始状态任务");
            }
        }
    }

}
