package com.bocloud.ims.service.scheduler;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.ims.interfaces.scheduler.ServerTimedTaskService;
import com.bocloud.ims.repository.resource.ServerConfigRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import com.bocloud.ims.service.config.DefaultSchedulingConfigurer;
import com.bocloud.ims.service.internal.worker.WorkerService;
import com.bocloud.ims.service.scheduler.thread.ServerTriggerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

/**
 * x86服务器 定时同步
 *
 * @author 作者
 * @version 创建时间：2019年4月15日 下午6:04:20 类说明 定时规则修改
 */
@Service
public class ServerTimedTaskServiceImpl implements ServerTimedTaskService {

    private static Logger logger = LoggerFactory.getLogger(ServerTimedTaskServiceImpl.class);

    @Autowired
    private DefaultSchedulingConfigurer defaultSchedulingConfigurer;
    @Autowired
    private ServerRepository serverRepository;
    @Autowired
    private ServerConfigRepository serverConfigRepository;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private WorkerService workerService;

    @Override
    public BocloudResult create(String assetCategory, String cron, RequestContext context) {
        try {
            // 等待任务调度初始化完成
            while (!defaultSchedulingConfigurer.inited()) {
                Thread.sleep(5000);
            }
            ServerTriggerTask serverTriggerTask = new ServerTriggerTask(assetCategory, serverRepository,
                    serverConfigRepository, workerService);
            defaultSchedulingConfigurer.resetTriggerTask(assetCategory, new TriggerTask(serverTriggerTask,
                    new CronTrigger(cron.trim())), cron.trim());
            logger.info(assetCategory + " --> 定时同步任务【重置】成功！定时策略 ： " + cron.trim());
            // 将服务器的定时表达式存储到redis中
            redisTemplate.opsForValue().set(assetCategory, cron);
            return new BocloudResult<>(true, "定时任务添加成功！");
        } catch (Exception e) {
            logger.error("添加平台定时任务失败 ： " + e.getMessage());
            return new BocloudResult<>(false, e.getMessage());
        }
    }

    @Override
    public BocloudResult remove(String params, RequestContext context) {
        return null;
    }

    @Override
    public BocloudResult modify(String assetCategory, String cron, RequestContext context) {
        try {
            // 等待任务调度初始化完成
            while (!defaultSchedulingConfigurer.inited()) {
                Thread.sleep(5000);
            }
            ServerTriggerTask serverTriggerTask = new ServerTriggerTask(assetCategory, serverRepository,
                    serverConfigRepository, workerService);
            defaultSchedulingConfigurer.resetTriggerTask(assetCategory, new TriggerTask(serverTriggerTask,
                    new CronTrigger(cron.trim())), cron.trim());
            logger.info(assetCategory + " --> 定时同步任务【编辑】成功！定时策略 ： " + cron);
            redisTemplate.opsForValue().set(assetCategory, cron);
            return new BocloudResult<>(true, "定时任务编辑成功！");
        } catch (Exception e) {
            logger.error("添加平台定时任务失败 ： " + e.getMessage());
            return new BocloudResult<>(false, e.getMessage());
        }
    }

    @Override
    public BocloudResult serverCron(String assetCategory, RequestContext context) {
        try {
            String cron = redisTemplate.opsForValue().get(assetCategory);
            return new BocloudResult<>(true, cron, "获取cron成功！");
        } catch (Exception e) {
            logger.error("获取定时表达式异常 ： " + e.getMessage());
            return new BocloudResult<>(false, "获取定时表达式异常 ： " + e.getMessage());
        }
    }

}
