package com.pty.pub.service.task;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pty.pub.api.IPaAutomaticTaskService;
import com.pty.pub.common.bean.PageResult;
import com.pty.pub.common.util.YmlUtils;
import com.pty.pub.dao.PaAutomaticTaskDao;
import com.pty.pub.entity.PaAutomaticTask;
import com.pty.pub.entity.PaAutomaticTaskStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 自动任务
*/
@Service
@Transactional
public class PaAutomaticTaskService implements IPaAutomaticTaskService, ApplicationRunner {
@Autowired
private PaAutomaticTaskDao automaticTaskDao;
@Autowired
private PaAutomaticTasksStatusService automaticTasksStatusService;
@Autowired
private QuartzScheduler quartzScheduler;
private String autoTaskStatus = "run";//定时任务开启状态 run开启 stop不开启
@PostConstruct
private void init() {
try {
String configPath = Paths.get(System.getProperty("user.dir"), "config", "application-app.yaml").toString();
File yamlFile = new File(configPath);
Object ptyObject = YmlUtils.getFromYml(yamlFile, "pty");
if (ptyObject != null) {
Map<String, Object> ptyMap = (Map<String, Object>) ptyObject;
Object pubObject = ptyMap.get("pub");
if (pubObject != null) {
Map<String, Object> pubMap = (Map<String, Object>) pubObject;
Object autotaskObject = pubMap.get("autotask");
if (autotaskObject != null) {
Map<String, Object> autotaskMap = (Map<String, Object>) autotaskObject;
Object statusObject = autotaskMap.get("status");
if (statusObject != null) {
autoTaskStatus = (String) statusObject;
}
}
}
}
} catch (IOException e) {
}
}
/**
* 根据单位查询自动任务列表
*/
@Override
public PageResult<PaAutomaticTask> getAutoTasksByAgyCode(PaAutomaticTask automaticTasks) {
PageInfo<PaAutomaticTask> pageInfo = null;
pageInfo = PageHelper.startPage(automaticTasks.getPageIndex(), automaticTasks.getPageSize())
.doSelectPageInfo(() -> {
automaticTaskDao.getAutoTasksByAgyCode(automaticTasks);
});
return new PageResult().setTotal(pageInfo.getTotal()).setResult(pageInfo.getList());
}
/**
* 根据task_id获取任务调度信息
*/
@Override
public PaAutomaticTask selectPATById(PaAutomaticTask automaticTasks) {
List<PaAutomaticTask> paAutomaticTasks = automaticTaskDao.selectPATById(automaticTasks);
if (paAutomaticTasks.size() != 0) {
return paAutomaticTasks.get(0);
}
return null;
}
/**
* 根据id更新自动任务信息
*/
@Override
public void updatePATById(PaAutomaticTask automaticTasks) {
if ("run".equalsIgnoreCase(autoTaskStatus)) {
automaticTaskDao.deleteByTaskId(automaticTasks);
automaticTaskDao.insert(automaticTasks);
}
}
/**
* 开启一个定时任务
*/
@Override
public Map<String, String> startQuartz(PaAutomaticTask automaticTasks) {
Map<String, String> resultMap = null;
if ("run".equalsIgnoreCase(autoTaskStatus)) {
PaAutomaticTask paAutomaticTask = selectPATById(automaticTasks);
paAutomaticTask.setIsEnabled(1);
int updatePaAutomaticTask = automaticTaskDao.updatePaAutomaticTask(paAutomaticTask);
if (updatePaAutomaticTask == 1) {
resultMap = quartzScheduler.startJob(paAutomaticTask);
PaAutomaticTaskStatus paAutomaticTaskStatus = new PaAutomaticTaskStatus();
paAutomaticTaskStatus.setTaskId(paAutomaticTask.getTaskId());
paAutomaticTaskStatus.setAgyCode(paAutomaticTask.getAgyCode());
if (automaticTasksStatusService.getPaTaskStatusByTaskId(paAutomaticTaskStatus).size() != 0) {
automaticTasksStatusService.deleteByTaskId(paAutomaticTaskStatus);
}
paAutomaticTaskStatus.setStatus(2);
automaticTasksStatusService.insert(paAutomaticTaskStatus);
}
}
return resultMap;
}
/**
* 删除一个定时任务
*/
@Override
public Map<String, String> deleteQuartz(PaAutomaticTask automaticTasks) {
Map<String, String> resultMap = null;
if ("run".equalsIgnoreCase(autoTaskStatus)) {
PaAutomaticTask paAutomaticTask = selectPATById(automaticTasks);
paAutomaticTask.setIsEnabled(0);
int updatePaAutomaticTask = automaticTaskDao.updatePaAutomaticTask(paAutomaticTask);
if (updatePaAutomaticTask == 1) {
resultMap = quartzScheduler.deleteJob(paAutomaticTask);
PaAutomaticTaskStatus paAutomaticTaskStatus = new PaAutomaticTaskStatus();
paAutomaticTaskStatus.setTaskId(paAutomaticTask.getTaskId());
if (automaticTasksStatusService.getPaTaskStatusByTaskId(paAutomaticTaskStatus).size() != 0) {
automaticTasksStatusService.deleteByTaskId(paAutomaticTaskStatus);
}
}
}
return resultMap;
}
/**
* 立即执行定时任务
*/
@Override
public Map<String, String> immediateQuartz(PaAutomaticTask automaticTasks) {
Map<String, String> resultMap = null;
if ("run".equalsIgnoreCase(autoTaskStatus)) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
PaAutomaticTask paAutomaticTask = selectPATById(automaticTasks);
long startTime = System.currentTimeMillis();
resultMap = quartzScheduler.doJob(paAutomaticTask);
long endTime = System.currentTimeMillis();
if (resultMap.get("flag").equals("true")) {
PaAutomaticTaskStatus paAutomaticTaskStatus = new PaAutomaticTaskStatus();
paAutomaticTaskStatus.setTaskId(paAutomaticTask.getTaskId());
List<PaAutomaticTaskStatus> PaAutomaticTaskStatusList = automaticTasksStatusService
.getPaTaskStatusByTaskId(paAutomaticTaskStatus);
if (PaAutomaticTaskStatusList.size() != 0) {
PaAutomaticTaskStatus ts = PaAutomaticTaskStatusList.get(0);
if (ts.getStartTime() == null) {
paAutomaticTaskStatus.setStartTime(df.format(new Date()));
}
paAutomaticTaskStatus.setLastExeTime(df.format(new Date()));
Long lastTotalTime = endTime - startTime;
paAutomaticTaskStatus.setLastTotalTime(lastTotalTime.intValue());
if (ts.getTotalCount() == null) {
paAutomaticTaskStatus.setTotalCount(1);
} else {
paAutomaticTaskStatus.setTotalCount(ts.getTotalCount() + 1);
}
if (ts.getOkCount() == null) {
paAutomaticTaskStatus.setOkCount(1);
} else {
paAutomaticTaskStatus.setOkCount(ts.getOkCount() + 1);
}
automaticTasksStatusService.update(paAutomaticTaskStatus);
}
}
}
return resultMap;
}
@Override
public void run(ApplicationArguments args) throws Exception {
if ("run".equalsIgnoreCase(autoTaskStatus)) {
// 获取所有定时任务
List<PaAutomaticTask> patList = automaticTaskDao.getPaAllTasks();
ArrayList<PaAutomaticTaskStatus> taskStatusList = new ArrayList<>();
if (patList != null) {
for (int i = 0; i < patList.size(); i++) {
PaAutomaticTask ptyAutomaticTasks = patList.get(i);
// 1是启用
if (ptyAutomaticTasks.getIsEnabled() == 1) {
try {
quartzScheduler.startJob(ptyAutomaticTasks);
} catch (Exception e) {
e.printStackTrace();
}
PaAutomaticTaskStatus paAutomaticTaskStatus = new PaAutomaticTaskStatus();
paAutomaticTaskStatus.setTaskId(ptyAutomaticTasks.getTaskId());
paAutomaticTaskStatus.setAgyCode(ptyAutomaticTasks.getAgyCode());
paAutomaticTaskStatus.setStatus(2);
taskStatusList.add(paAutomaticTaskStatus);
}
}
// 清空明细表数据
automaticTasksStatusService.delete();
if (taskStatusList.size() != 0) {
// 批量插入运行中定时任务明细表
automaticTasksStatusService.insertBatch(taskStatusList);
}
}
}
}
/**
*
* 暂停一个定时任务
*
*/
@Override
public void pauseQuartz(PaAutomaticTask automaticTasks) {
int updatePaAutomaticTask = automaticTaskDao.updatePaAutomaticTask(automaticTasks);
if (updatePaAutomaticTask == 1) {
quartzScheduler.pauseJob(automaticTasks);
}
}
}
获取项目中得系统路径,获取项目得配置文件
最新推荐文章于 2022-10-13 18:41:05 发布