package com.xvji.quartz.service.impl;

import java.util.List;
import javax.annotation.PostConstruct;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xvji.common.constant.ScheduleConstants;
import com.xvji.common.exception.job.TaskException;
import com.xvji.quartz.domain.SysJob;
import com.xvji.quartz.mapper.SysJobMapper;
import com.xvji.quartz.service.ISysJobService;
import com.xvji.quartz.util.CronUtils;
import com.xvji.quartz.util.ScheduleUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.lang.reflect.Method;

/**
 * 定时任务调度信息 服务层
 * 
 * @author lt
 */
@Service
public class SysJobServiceImpl implements ISysJobService, ApplicationContextAware
{
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private SysJobMapper jobMapper;

    private static final Logger log = LoggerFactory.getLogger(SysJobServiceImpl.class);


    //Spring上下文（用于获取其他模块的Bean）
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 项目启动时，初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理（注：不能手动修改数据库ID和任务组名，否则会导致脏数据）
     */
    @PostConstruct
    public void init() throws SchedulerException, TaskException
    {
        scheduler.clear();
        List<SysJob> jobList = jobMapper.selectJobAll();
        for (SysJob job : jobList)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
    }

    /**
     * 获取quartz调度器的计划任务列表
     * 
     * @param job 调度信息
     * @return
     */
    @Override
    public List<SysJob> selectJobList(SysJob job)
    {
        return jobMapper.selectJobList(job);
    }

    /**
     * 查询全部定时任务 无分页
     * @return
     */
    @Override
    public List<SysJob> selectJob() {
        return jobMapper.selectJobAll();
    }

    /**
     * 通过调度任务ID查询调度信息
     * 
     * @param jobId 调度任务ID
     * @return 调度任务对象信息
     */
    @Override
    public SysJob selectJobById(Long jobId)
    {
        return jobMapper.selectJobById(jobId);
    }

    /**
     * 暂停任务
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int pauseJob(SysJob job) throws SchedulerException
    {
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = jobMapper.updateJob(job);
        if (rows > 0)
        {
            scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 恢复任务
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int resumeJob(SysJob job) throws SchedulerException
    {
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
        int rows = jobMapper.updateJob(job);
        if (rows > 0)
        {
            scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 删除任务后，所对应的trigger也将被删除
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteJob(SysJob job) throws SchedulerException
    {
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        int rows = jobMapper.deleteJobById(jobId);
        if (rows > 0)
        {
            scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
        }
        return rows;
    }

    /**
     * 批量删除调度信息
     * 
     * @param jobIds 需要删除的任务ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteJobByIds(Long[] jobIds) throws SchedulerException
    {
        for (Long jobId : jobIds)
        {
            SysJob job = jobMapper.selectJobById(jobId);
            deleteJob(job);
        }
    }

    /**
     * 任务调度状态修改
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeStatus(SysJob job) throws SchedulerException {
        int rows = 0;
        String cronStatus = job.getStatus(); // 定时任务状态：0=启用，1=暂停
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();

        // 处理定时任务自身状态变更
        if (ScheduleConstants.Status.NORMAL.getValue().equals(cronStatus)) {
            rows = resumeJob(job); // 启用：cronStatus=0
        } else if (ScheduleConstants.Status.PAUSE.getValue().equals(cronStatus)) {
            rows = pauseJob(job); // 暂停：cronStatus=1
        }

        // 同步业务任务状态
        if (rows > 0 && ("PREDICT_TASK".equals(jobGroup) || "TRAIN_TASK".equals(jobGroup))) {
            try {
                // 解析业务任务ID
                Class<?> invokeTargetUtilsClass = Class.forName("com.xvji.utils.InvokeTargetUtils");
                Method parseMethod = invokeTargetUtilsClass.getMethod("parseTaskIdFromInvokeTarget", String.class);
                Long taskId = (Long) parseMethod.invoke(null, job.getInvokeTarget());
                if (taskId == null) {
                    log.error("定时任务[ID:{}]状态变更，无法解析业务任务ID", jobId);
                    return rows;
                }

                // 按你的需求映射状态：
                Integer bizStatus;
                if ("1".equals(cronStatus)) {
                    // 定时任务暂停（1） 业务任务状态=2（未启用）
                    bizStatus = 2;
                } else {
                    // 定时任务启用（0） 保留上次执行结果（不修改）
                    log.info("定时任务[ID:{}]启用，业务任务[ID:{}]保留上次状态（0失败/1成功）", jobId, taskId);
                    return rows;
                }

                // 同步到预测任务
                if ("PREDICT_TASK".equals(jobGroup)) {
                    Object predictTaskService = applicationContext.getBean("predictTaskServiceImpl");
                    Class<?> predictTaskClass = Class.forName("com.xvji.domain.PredictTask");
                    Object predictTask = predictTaskClass.newInstance();
                    // 只更新ID和状态
                    Method setPTaskId = predictTaskClass.getMethod("setPTaskId", Long.class);
                    //Method setPTaskStatus = predictTaskClass.getMethod("setPTaskStatus", Integer.class);
                    setPTaskId.invoke(predictTask, taskId);
                    //setPTaskStatus.invoke(predictTask, bizStatus);
                    // 调用更新
                    Method updateMethod = predictTaskService.getClass().getMethod("updateById", Object.class);
                    updateMethod.invoke(predictTaskService, predictTask);
                    log.info("定时任务[ID:{}]暂停，同步预测任务[ID:{}]状态为2（未启用）", jobId, taskId);
                }
                // 同步到训练任务
                else if ("TRAIN_TASK".equals(jobGroup)) {
                    Object trainTaskService = applicationContext.getBean("trainTaskServiceImpl");
                    Class<?> trainTaskClass = Class.forName("com.xvji.domain.TrainTask");
                    Object trainTask = trainTaskClass.newInstance();
                    Method setTTaskId = trainTaskClass.getMethod("setTTaskId", Long.class);
                    //Method setTTaskStatus = trainTaskClass.getMethod("setTTaskStatus", Integer.class);
                    setTTaskId.invoke(trainTask, taskId);
                    //setTTaskStatus.invoke(trainTask, bizStatus);
                    Method updateMethod = trainTaskService.getClass().getMethod("updateById", Object.class);
                    updateMethod.invoke(trainTaskService, trainTask);
                    log.info("定时任务[ID:{}]暂停，同步训练任务[ID:{}]状态为2（未启用）", jobId, taskId);
                }

            } catch (Exception e) {
                log.error("定时任务[ID:{}]状态变更，同步业务任务失败", jobId, e);
            }
        }

        return rows;
    }

    /**
     * 立即运行任务
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(SysJob job) throws SchedulerException
    {
        Long jobId = job.getJobId();
        String jobGroup = job.getJobGroup();
        SysJob properties = selectJobById(job.getJobId());
        // 参数
        JobDataMap dataMap = new JobDataMap();
        dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
        scheduler.triggerJob(ScheduleUtils.getJobKey(jobId, jobGroup), dataMap);
    }

    /**
     * 新增任务
     * 
     * @param job 调度信息 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertJob(SysJob job) throws SchedulerException, TaskException
    {
        job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
        int rows = jobMapper.insertJob(job);
        if (rows > 0)
        {
            ScheduleUtils.createScheduleJob(scheduler, job);
        }
        return rows;
    }

    /**
     * 更新任务的时间表达式
     * 
     * @param job 调度信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateJob(SysJob job) throws SchedulerException, TaskException {
        SysJob properties = selectJobById(job.getJobId());
        int rows = jobMapper.updateJob(job);
        if (rows > 0) {
            updateSchedulerJob(job, properties.getJobGroup());

            log.info("定时任务[ID:{}]更新后，开始同步业务任务", job.getJobId());
            if ("PREDICT_TASK".equals(job.getJobGroup())) {
                try {
                    Class<?> invokeTargetUtilsClass = Class.forName("com.xvji.utils.InvokeTargetUtils");
                    Method parseMethod = invokeTargetUtilsClass.getMethod("parseTaskIdFromInvokeTarget", String.class);
                    Long taskId = (Long) parseMethod.invoke(null, job.getInvokeTarget());

                    if (taskId == null) {
                        log.error("无法解析预测任务ID");
                        return rows;
                    }

                    Integer taskStatus = ScheduleConstants.Status.NORMAL.getValue().equals(job.getStatus()) ? 1 : 0;
                    Object predictTaskService = applicationContext.getBean("predictTaskServiceImpl");
                    if (predictTaskService == null) {
                        log.error("未找到predictTaskService");
                        return rows;
                    }

                    Class<?> predictTaskClass = Class.forName("com.xvji.domain.PredictTask");
                    Object predictTask = predictTaskClass.newInstance();
                    Method setPTaskId = predictTaskClass.getMethod("setPTaskId", Long.class);
                    Method setPCronExpression = predictTaskClass.getMethod("setPCronExpression", String.class);
                    Method setPTaskName = predictTaskClass.getMethod("setPTaskName", String.class);
                    //Method setPTaskStatus = predictTaskClass.getMethod("setPTaskStatus", Integer.class);

                    setPTaskId.invoke(predictTask, taskId);
                    setPCronExpression.invoke(predictTask, job.getCronExpression());
                    setPTaskName.invoke(predictTask, job.getJobName() + "[SYNCED]");
                    //setPTaskStatus.invoke(predictTask, taskStatus);

                    Method updateMethod = predictTaskService.getClass().getMethod("updateById", Object.class);
                    updateMethod.invoke(predictTaskService, predictTask);
                    log.info("同步预测任务[ID:{}]成功", taskId);

                } catch (Exception e) {
                    log.error("同步预测任务失败", e);
                }
            }
            else if ("TRAIN_TASK".equals(job.getJobGroup())) {
                try {
                    // 解析训练任务ID
                    Class<?> invokeTargetUtilsClass = Class.forName("com.xvji.utils.InvokeTargetUtils");
                    Method parseMethod = invokeTargetUtilsClass.getMethod("parseTaskIdFromInvokeTarget", String.class);
                    Long taskId = (Long) parseMethod.invoke(null, job.getInvokeTarget());

                    if (taskId == null) {
                        log.error("无法解析训练任务ID");
                        return rows;
                    }

                    // 状态转换
                    Integer taskStatus = ScheduleConstants.Status.NORMAL.getValue().equals(job.getStatus()) ? 1 : 0;

                    // 获取训练任务Service
                    Object trainTaskService = applicationContext.getBean("trainTaskServiceImpl");
                    if (trainTaskService == null) {
                        log.error("未找到trainTaskService");
                        return rows;
                    }

                    // 反射设置训练任务属性
                    Class<?> trainTaskClass = Class.forName("com.xvji.domain.TrainTask");
                    Object trainTask = trainTaskClass.newInstance();
                    Method setTTaskId = trainTaskClass.getMethod("setTTaskId", Long.class);
                    Method setTCronExpression = trainTaskClass.getMethod("setTCronExpression", String.class);
                    Method setTTaskName = trainTaskClass.getMethod("setTTaskName", String.class);
                    //Method setTTaskStatus = trainTaskClass.getMethod("setTTaskStatus", Integer.class);

                    setTTaskId.invoke(trainTask, taskId);
                    setTCronExpression.invoke(trainTask, job.getCronExpression());
                    setTTaskName.invoke(trainTask, job.getJobName() + "[SYNCED]"); // 防循环标记
                    //setTTaskStatus.invoke(trainTask, taskStatus);

                    // 调用更新
                    Method updateMethod = trainTaskService.getClass().getMethod("updateById", Object.class);
                    updateMethod.invoke(trainTaskService, trainTask);
                    log.info("同步训练任务[ID:{}]成功", taskId);

                } catch (Exception e) {
                    log.error("同步训练任务失败", e);
                }
            }
        }
        return rows;
    }



    /**
     * 更新任务
     * 
     * @param job 任务对象
     * @param jobGroup 任务组名
     */
    public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException
    {
        Long jobId = job.getJobId();
        // 判断是否存在
        JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
        if (scheduler.checkExists(jobKey))
        {
            // 防止创建时存在数据问题 先移除，然后在执行创建操作
            scheduler.deleteJob(jobKey);
        }
        ScheduleUtils.createScheduleJob(scheduler, job);
    }

    /**
     * 校验cron表达式是否有效
     * 
     * @param cronExpression 表达式
     * @return 结果
     */
    @Override
    public boolean checkCronExpressionIsValid(String cronExpression)
    {
        return CronUtils.isValid(cronExpression);
    }

    @Override
    public SysJob selectJobByParam(String jobParam) {
        // 完整匹配 invoke_target
        return jobMapper.selectJobByField("invoke_target", jobParam);
    }

    @Override
    public void deleteJobByParam(String jobParam) {
        // 完整匹配 invoke_target
        jobMapper.deleteJobByField("invoke_target", jobParam);
    }

    @Override
    public SysJob getSysJobByCondition(String conditionValue) {
        // 模糊匹配 invoke_target
        SysJob queryJob = new SysJob();
        queryJob.setInvokeTarget("%" + conditionValue + "%");
        List<SysJob> jobList = jobMapper.selectJobList(queryJob);
        return jobList != null && !jobList.isEmpty() ? jobList.get(0) : null;
    }

    @Override
    public void deleteSysJobByCondition(String field, String value) {
        // 按指定字段删除
        jobMapper.deleteJobByField(field, value);
    }

    @Override
    public boolean updateSysJob(SysJob sysJob) {
        try {
            int rows = updateJob(sysJob);
            return rows > 0;
        } catch (SchedulerException | TaskException e) {
            e.printStackTrace();
            return false;
        }
    }
}
