/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yfd.platform.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.modules.basedata.domain.SubstationPatroldevice;
import com.yfd.platform.modules.basedata.service.impl.SubstationPatroldeviceServiceImpl;
import com.yfd.platform.modules.patroltask.domain.TaskTodo;
import com.yfd.platform.modules.patroltask.mapper.TaskTodoMapper;
import com.yfd.platform.modules.patroltask.service.impl.QuartzMultiTaskManage;
import com.yfd.platform.modules.robotapi.domain.PlatformParentSystem;
import com.yfd.platform.modules.robotapi.service.IPlatformParentSystemService;
import com.yfd.platform.system.domain.QuartzJob;
import com.yfd.platform.system.mapper.QuartzJobMapper;
import com.yfd.platform.utils.HttpRESTfulUtils;
import com.yfd.platform.utils.QuartzManage;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @date 2019-01-07
 */
@Component
@RequiredArgsConstructor
public class JobRunner implements ApplicationRunner {

    private static final Logger log = LoggerFactory.getLogger(JobRunner.class);
    private final QuartzJobMapper quartzJobMapper;
    private final TaskTodoMapper taskTodoMapper;
    private final QuartzManage quartzManage;
    private final QuartzMultiTaskManage quartzTaskManage;
    private final SubstationPatroldeviceServiceImpl patroldeviceService;
    private final RedisTemplate<String, String> redisTemplate;
    private final HttpRESTfulUtils httpUtil;
    private final HttpServerConfig config;
    private final IPlatformParentSystemService platformParentSystemService;

    /**
     * 项目启动时重新激活启用的定时任务
     *
     * @param applicationArguments /
     */
    @Override
    public void run(ApplicationArguments applicationArguments) {

        log.info("--------------------注入系统定时任务---------------------");
        List<QuartzJob> quartzJobs =
                quartzJobMapper.selectList(new LambdaQueryWrapper<QuartzJob>().eq(QuartzJob::getStatus, "1"));
        quartzJobs.forEach(quartzManage::addJob);
        log.info("--------------------系统任务注入完成---------------------");

        log.info("--------------------注入变电站定时巡视任务---------------------");
        List<TaskTodo> todoTaskJobs = taskTodoMapper.selectTaskTodoList();
        todoTaskJobs.forEach(quartzTaskManage::addJob);
        log.info("--------------------变电站定时巡视任务注入完成---------------------");

        log.info("--------------------注入变电站摄像机静默巡视任务---------------------");
        //        patroldeviceService.addDailyMonitorTask();
        log.info("--------------------变电站摄像机静默巡视任务注入完成---------------------");

        log.info("--------------------自启动上级系统注册---------------------");
        List<PlatformParentSystem> list =
                platformParentSystemService.list(new LambdaQueryWrapper<PlatformParentSystem>().eq(PlatformParentSystem::getIsEnable, "1"));
        if (list != null && list.size() > 0) {
            PlatformParentSystem platformParentSystem = list.get(0);
            platformParentSystemService.sendRegisterToSuper(platformParentSystem.getId());
        }

        log.info("--------------------自启动上级系统注册完成---------------------");
    }
}
