package com.YuyeTech.HeartRate.game.food.scheduled;

import cn.hutool.core.date.DateUtil;
import com.YuyeTech.HeartRate.game.food.service.FoodService;
import com.YuyeTech.HeartRate.utils.RedisData;
import com.YuyeTech.HeartRate.utils.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

@Slf4j
@EnableScheduling
public class FoodDayTask extends QuartzJobBean {

    @Autowired
    private FoodService foodService;

    @Autowired
    private RedisLock redisLock;

    /**
     * 执行搜索数据任务
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        long time = System.currentTimeMillis() + RedisData.getKeywordTimeout();
        String _redisKey = "LOCK:FOOD:DAY:"+ DateUtil.format(new Date(),"yyyyMMdd");

        //加线程锁住任务
        if(!redisLock.lock(_redisKey,String.valueOf(time))){
            log.info("触发搜索字段加锁失败！");
            return;
        }
//        log.info(_redisKey);
        //取用户搜索的字段数据，来进行数据更新到本地
//        foodService.getKeywordsFromRedis();

        //按照keyword查询对应字段，把结果记录到数据库


        redisLock.unlock(_redisKey,String.valueOf(time));
    }
}
