package com.lanyuanxiaoyao.service.scheduler.quartz.compaction;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lanyuanxiaoyao.service.forest.service.HudiService;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import com.lanyuanxiaoyao.service.scheduler.utils.ScheduleHelper;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.eclipse.collections.api.factory.Maps;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * 日常普通调度
 *
 * @author ZhangJiacheng
 * @date 2023-05-11
 */
@DisallowConcurrentExecution
public class DailyScheduleJob extends BaseScheduleJob {
    private static final Logger logger = LoggerFactory.getLogger(DailyScheduleJob.class);

    private final DiscoveryClient discoveryClient;
    private final InfoService infoService;
    private final HudiService hudiService;
    private final ObjectMapper mapper;

    public DailyScheduleJob(DiscoveryClient discoveryClient, InfoService infoService, HudiService hudiService, Jackson2ObjectMapperBuilder builder) {
        this.discoveryClient = discoveryClient;
        this.infoService = infoService;
        this.hudiService = hudiService;

        this.mapper = builder.build();
    }

    public static void schedule(DiscoveryClient discoveryClient, InfoService infoService, HudiService hudiService, ObjectMapper mapper, String comment) {
        logger.info("Daily schedule");
        ScheduleHelper.schedule(
                discoveryClient,
                infoService,
                hudiService,
                mapper,
                meta -> true,
                comment,
                Maps.immutable.empty()
        );
    }

    @Override
    protected void executeInternal(JobExecutionContext context) {
        String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        schedule(discoveryClient, infoService, hudiService, mapper, "Daily schedule for " + now);
    }
}
