潘志宝
2024-08-20 92d87eda945d68346475e6766673a3cdccf3d41c
提交 | 用户 | 时间
a6de49 1 /**
H 2  * Copyright (c) 2018 人人开源 All rights reserved.
3  *
4  * https://www.renren.io
5  *
6  * 版权所有,侵权必究!
7  */
8
9 package com.iailab.module.job.service.impl;
10
11 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
12 import com.baomidou.mybatisplus.core.metadata.IPage;
13 import com.iailab.framework.common.constant.Constant;
14 import com.iailab.framework.common.page.PageData;
15 import com.iailab.framework.common.service.impl.BaseServiceImpl;
16 import com.iailab.framework.common.util.object.ConvertUtils;
17 import com.iailab.module.job.dao.ScheduleJobDao;
18 import com.iailab.module.job.dto.ScheduleJobDTO;
19 import com.iailab.module.job.entity.ScheduleJobEntity;
20 import com.iailab.module.job.service.ScheduleJobService;
21 import com.iailab.module.job.utils.ScheduleUtils;
22 import org.apache.commons.lang3.StringUtils;
23 import org.quartz.Scheduler;
24 import javax.annotation.Resource;
25 import org.springframework.stereotype.Service;
26 import org.springframework.transaction.annotation.Transactional;
27
28 import java.util.Arrays;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 @Service
34 public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
35     @Resource
36     private Scheduler scheduler;
37
38     @Override
39     public PageData<ScheduleJobDTO> page(Map<String, Object> params) {
40         IPage<ScheduleJobEntity> page = baseDao.selectPage(
41             getPage(params, Constant.CREATE_DATE, false),
42             getWrapper(params)
43         );
44         return getPageData(page, ScheduleJobDTO.class);
45     }
46
47     @Override
48     public ScheduleJobDTO get(Long id) {
49         ScheduleJobEntity entity = baseDao.selectById(id);
50
51         return ConvertUtils.sourceToTarget(entity, ScheduleJobDTO.class);
52     }
53
54     private QueryWrapper<ScheduleJobEntity> getWrapper(Map<String, Object> params){
55         String beanName = (String)params.get("beanName");
56
57         QueryWrapper<ScheduleJobEntity> wrapper = new QueryWrapper<>();
58         wrapper.like(StringUtils.isNotBlank(beanName), "bean_name", beanName);
59
60         return wrapper;
61     }
62
63     @Override
64     @Transactional(rollbackFor = Exception.class)
65     public void save(ScheduleJobDTO dto) {
66         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
67         entity.setCreateDate(new Date());
68         entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
69         this.insert(entity);
70         
71         ScheduleUtils.createScheduleJob(scheduler, entity);
72     }
73     
74     @Override
75     @Transactional(rollbackFor = Exception.class)
76     public void update(ScheduleJobDTO dto) {
77         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
78         entity.setUpdateDate(new Date());
79         ScheduleUtils.updateScheduleJob(scheduler, entity);
80                 
81         this.updateById(entity);
82     }
83
84     @Override
85     @Transactional(rollbackFor = Exception.class)
86     public void deleteBatch(Long[] ids) {
87         for(Long id : ids){
88             ScheduleUtils.deleteScheduleJob(scheduler, id);
89         }
90         
91         //删除数据
92         this.deleteBatchIds(Arrays.asList(ids));
93     }
94
95     @Override
96     public int updateBatch(Long[] ids, int status){
97         Map<String, Object> map = new HashMap<>(2);
98         map.put("ids", ids);
99         map.put("status", status);
100         return baseDao.updateBatch(map);
101     }
102     
103     @Override
104     @Transactional(rollbackFor = Exception.class)
105     public void run(Long[] ids) {
106         for(Long id : ids){
107             ScheduleUtils.run(scheduler, this.selectById(id));
108         }
109     }
110
111     @Override
112     @Transactional(rollbackFor = Exception.class)
113     public void pause(Long[] ids) {
114         for(Long id : ids){
115             ScheduleUtils.pauseJob(scheduler, id);
116         }
117         
118         updateBatch(ids, Constant.ScheduleStatus.PAUSE.getValue());
119     }
120
121     @Override
122     @Transactional(rollbackFor = Exception.class)
123     public void resume(Long[] ids) {
124         for(Long id : ids){
125             ScheduleUtils.resumeJob(scheduler, id);
126         }
127
128         updateBatch(ids, Constant.ScheduleStatus.NORMAL.getValue());
129     }
130     
131 }