package com.zksy.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.common.domain.Result;
import com.zksy.data.domain.dto.TimeTaskDto;
import com.zksy.data.domain.po.XhTimeTask;
import com.zksy.data.service.IBaseSaveDataInterface;
import com.zksy.data.service.XhTimeTaskService;
import com.zksy.data.mapper.XhTimeTaskMapper;
import com.zksy.data.utils.TaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author hxb
* @description 针对表【xh_time_task】的数据库操作Service实现
* @createDate 2024-11-07 11:03:48
*/
@Service
public class XhTimeTaskServiceImpl extends ServiceImpl<XhTimeTaskMapper, XhTimeTask>
    implements XhTimeTaskService{
    @Autowired
    private TaskManager taskManager;
    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public Result<String> saveTimeTask(TimeTaskDto timeTaskDto) {
        boolean b = applicationContext.containsBean(timeTaskDto.getServiceName());
        if(!b){
            return Result.error("你想添加的服务不存在");
        }
        Object bean = applicationContext.getBean(timeTaskDto.getServiceName());
        if(!(bean instanceof IBaseSaveDataInterface)){
            return Result.error("该服务不是可被定时任务执行的服务");
        }
        LambdaQueryWrapper<XhTimeTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XhTimeTask::getServiceName, timeTaskDto.getServiceName());
        if(this.count(wrapper)>0){
            return Result.error("该服务已存在");
        }
        XhTimeTask task = new XhTimeTask();
        task.setCreatedAt(new Date());
        task.setUpdatedAt(new Date());
        task.setTaskName(timeTaskDto.getTaskName());
        task.setServiceName(timeTaskDto.getServiceName());
        task.setCronExpression(timeTaskDto.getCronExpression());
        task.setScheduleType(timeTaskDto.getScheduleType());
        task.setEnabled(1);
        this.save(task);
        return Result.ok("添加定时任务成功");
    }

    @Override
    public Result<String> removeTimeTask(String taskName) {
        taskManager.removeTask(taskName);
        LambdaQueryWrapper<XhTimeTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XhTimeTask::getTaskName, taskName);
        this.remove(wrapper);
        return Result.ok("删除定时任务成功");
    }

    @Override
    public Result<String> stopAllTasks() {
        taskManager.stopAllTasks();
        LambdaUpdateWrapper<XhTimeTask> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(XhTimeTask::getEnabled, 1);
        wrapper.set(XhTimeTask::getEnabled, 0);
        this.update(wrapper);
        return Result.ok("停止所有定时任务成功");
    }

    @Override
    public Result<List<XhTimeTask>> getAllTask() {
        List<XhTimeTask> list = this.list();
        return Result.ok(list);
    }
}




