package beilv.quartz.task;

import beilv.admissionticket.domain.AdmissionTicket;
import beilv.admissionticket.service.IAdmissionTicketService;
import beilv.common.utils.uuid.IdUtils;
import beilv.session.domain.BeilvSession;
import beilv.session.service.IBeilvSessionService;
import beilv.site.domain.BeilvSite;
import beilv.site.service.IBeilvSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component("siteTask")
public class SiteTask {

    @Autowired
    private IBeilvSessionService sessionService;

    @Autowired
    private IBeilvSiteService siteService;

    @Autowired
    private IAdmissionTicketService ticketService;


    public void abcd(){
        System.out.println("==================================================");
    }

    /**
     * 门票定时任务
     * 定时将日期下的各类型场地+时段写入到数据表中
     *
     * @param day 天数
     */
    public void addAdmissionTicket(Integer day) {

        // 计算写入日期 = 当前日期 + 传入的day天数
        Date writeDate = addDaysToDate(new Date(), day);

        //获取场地信息
        List<BeilvSite> siteList = siteService.getAllToList();

        //获取时段信息
        List<BeilvSession> sessionList = sessionService.getAllToList();

        //创建写入集合, 用于批量新增
        List<AdmissionTicket> ticketList = new ArrayList<>();

        siteList.forEach(site -> {
            sessionList.forEach(session -> {
                //判断场馆相同, 并且场地类型相同
                if (session.getVenueId().equals(site.getVenueId()) && session.getSiteType().equals(site.getSiteType())) {
                    //创建门票实体
                    AdmissionTicket admissionTicket = new AdmissionTicket(IdUtils.fastSimpleUUID(), String.valueOf(session.getVenueId()), site.getName(), session.getSiteType(), session.getSession(), session.getPrice(),writeDate,"0", session.getVenueName());
                    ticketList.add(admissionTicket);
                }
            });
        });

        //批量写入门票信息
        ticketService.insertAdmissionTicketToList(ticketList);
    }

    public void updateTicketStatus(Long millisecond) {
        AdmissionTicket admissionTicket = new AdmissionTicket();
        admissionTicket.setAdmissionTicketStatus("0");

        List<AdmissionTicket> admissionTickets = ticketService.selectBeilvAdmissionTicketList(admissionTicket);

        List<String> ids = new ArrayList<>();

        // 获取当前日期和时间
        Date currentDate = new Date();

        admissionTickets.forEach(ticket -> {
            String session = ticket.getSession();//8:00-10:00
            Date ticketDate = ticket.getTicketDate();//2024-01-07

            try {
                // 解析ticketDate
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                // 解析session的开始时间
                SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

                // 将ticketDate和startTime合并为一个Date对象
                Date ticketDateTime = combineDateAndTime(dateFormat.parse(dateFormat.format(ticketDate)), timeFormat.parse(session.split("-")[0]));

//                long tenMinutesInMillis = 10 * 60 * 1000;
                if (ticketDateTime != null) {
                    Date ticketDateTimeMinusTenMinutes = new Date(ticketDateTime.getTime() - millisecond);
                    // 判断条件
                    if (ticketDateTimeMinusTenMinutes.before(currentDate)) {
                        // 获取ticket的id并放入ids中
                        ids.add(ticket.getId());
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });

        ids.forEach(id->{
            System.out.println("id = " + id);
        });
        if(!ids.isEmpty()){
            ticketService.updateTicketStatus(ids);
        }
    }

    // 将Date类型加上指定天数的方法
    private Date addDaysToDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    // 合并日期和时间的辅助方法
    private static Date combineDateAndTime(Date date, Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

        try {
            Date parsedDate = dateFormat.parse(dateFormat.format(date));
            Date parsedTime = timeFormat.parse(timeFormat.format(time));

            long dateMillis = parsedDate.getTime();
            long timeMillis = parsedTime.getTime() - timeFormat.parse("00:00").getTime();

            return new Date(dateMillis + timeMillis);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

}
