houzhongjian
2024-10-30 a28ca3f36d0ace05966a8c0fac1e4b5fe921f882
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.job.service.impl;
H 2
3 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 import com.baomidou.mybatisplus.core.metadata.IPage;
5 import com.iailab.framework.common.constant.Constant;
6 import com.iailab.framework.common.page.PageData;
cd5f85 7 import com.iailab.framework.common.pojo.PageResult;
a6de49 8 import com.iailab.framework.common.service.impl.BaseServiceImpl;
H 9 import com.iailab.framework.common.util.object.ConvertUtils;
cd5f85 10 import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
11 import com.iailab.framework.tenant.core.context.TenantContextHolder;
a6de49 12 import com.iailab.module.data.job.entity.ScheduleJobEntity;
H 13 import com.iailab.module.data.job.dao.ScheduleJobDao;
14 import com.iailab.module.data.job.dto.ScheduleJobDTO;
15 import com.iailab.module.data.job.service.ScheduleJobService;
16 import com.iailab.module.data.job.utils.ScheduleUtils;
cd5f85 17 import com.iailab.module.data.job.vo.ScheduleJobReqVO;
a6de49 18 import org.apache.commons.lang3.StringUtils;
H 19 import org.quartz.Scheduler;
20 import javax.annotation.Resource;
21 import org.springframework.stereotype.Service;
22 import org.springframework.transaction.annotation.Transactional;
23
24 import java.util.Arrays;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 @Service
30 public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
31     @Resource
32     private Scheduler scheduler;
33
34     @Override
cd5f85 35     public PageResult<ScheduleJobEntity> page(ScheduleJobReqVO reqVO) {
36         return baseDao.selectPage(reqVO);
a6de49 37     }
H 38
39     @Override
40     public ScheduleJobDTO get(Long id) {
41         ScheduleJobEntity entity = baseDao.selectById(id);
42
43         return ConvertUtils.sourceToTarget(entity, ScheduleJobDTO.class);
44     }
45
46     private QueryWrapper<ScheduleJobEntity> getWrapper(Map<String, Object> params){
47         String beanName = (String)params.get("beanName");
48
49         QueryWrapper<ScheduleJobEntity> wrapper = new QueryWrapper<>();
50         wrapper.like(StringUtils.isNotBlank(beanName), "bean_name", beanName);
51
52         return wrapper;
53     }
54
55     @Override
56     @Transactional(rollbackFor = Exception.class)
57     public void save(ScheduleJobDTO dto) {
cd5f85 58         Long tenantId = TenantContextHolder.getRequiredTenantId();
a6de49 59         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
cd5f85 60         entity.setId(System.currentTimeMillis());
a6de49 61         entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
cd5f85 62         entity.setTenantId(tenantId);
63         entity.setCreator(SecurityFrameworkUtils.getLoginUser().getId());
64         entity.setCreateDate(new Date());
a6de49 65         this.insert(entity);
H 66         
67         ScheduleUtils.createScheduleJob(scheduler, entity);
68     }
69     
70     @Override
71     @Transactional(rollbackFor = Exception.class)
72     public void update(ScheduleJobDTO dto) {
cd5f85 73         Long tenantId = TenantContextHolder.getRequiredTenantId();
a6de49 74         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
H 75         entity.setUpdateDate(new Date());
cd5f85 76         entity.setUpdater(SecurityFrameworkUtils.getLoginUser().getId());
77         entity.setTenantId(tenantId);
a6de49 78         ScheduleUtils.updateScheduleJob(scheduler, entity);
H 79                 
80         this.updateById(entity);
81     }
82
83     @Override
84     @Transactional(rollbackFor = Exception.class)
cd5f85 85     public void deleteBatch(Long id) {
86         ScheduleUtils.deleteScheduleJob(scheduler, id);
87
88         baseDao.deleteById(id);
a6de49 89     }
H 90
91     @Override
92     public int updateBatch(Long[] ids, int status){
93         Map<String, Object> map = new HashMap<>(2);
94         map.put("ids", ids);
95         map.put("status", status);
96         return baseDao.updateBatch(map);
97     }
98     
99     @Override
100     @Transactional(rollbackFor = Exception.class)
101     public void run(Long[] ids) {
102         for(Long id : ids){
103             ScheduleUtils.run(scheduler, this.selectById(id));
104         }
105     }
106
107     @Override
108     @Transactional(rollbackFor = Exception.class)
109     public void pause(Long[] ids) {
110         for(Long id : ids){
111             ScheduleUtils.pauseJob(scheduler, id);
112         }
113         
114         updateBatch(ids, Constant.ScheduleStatus.PAUSE.getValue());
115     }
116
117     @Override
118     @Transactional(rollbackFor = Exception.class)
119     public void resume(Long[] ids) {
120         for(Long id : ids){
121             ScheduleUtils.resumeJob(scheduler, id);
122         }
123
124         updateBatch(ids, Constant.ScheduleStatus.NORMAL.getValue());
125     }
126     
127 }