package com.sonic.frog.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.frog.domain.input.ClassificationListInput;
import com.sonic.frog.service.HomeClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 分类默认条件缓存定时任务
 * 角色-选择所有
 * 性格-选择感性
 * 标签-选择感性下所有标签
 *
 * @author mzc
 */
@Slf4j
@Component
public class HomeClassificationDefaultConditionCacheJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private HomeClassificationService homeClassificationService;

    /**
     * 每5分钟执行一次
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void homeClassificationDefaultConditionCacheSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("homeClassificationDefaultConditionCache", TimeUnit.MINUTES.toSeconds(2), this::homeClassificationDefaultConditionCache);
    }

    protected JobmanClient.JobResult homeClassificationDefaultConditionCache(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> homeClassificationDefaultConditionCache start !!!");
            //默认ALL,没有任何筛选条件
            ClassificationListInput input = ClassificationListInput.builder()
                    .build();
            homeClassificationService.homeClassificationUpdateCache(input);
            log.info("===> homeClassificationDefaultConditionCache end !!!");
            return JobmanClient.JobResult.success(0);
        } finally {
            LogUtils.removeTraceId();
        }
    }
}