鞍钢鲅鱼圈能源管控系统后端代码
houzhongjian
2024-12-26 07073fa5e1e14b1f9d5d4f3253d9403ab311ae3c
提交 | 用户 | 时间
07073f 1 package com.iailab.module.ansteel.job.service.impl;
H 2
3 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5 import com.iailab.framework.common.constant.Constant;
6 import com.iailab.framework.common.pojo.PageResult;
7 import com.iailab.framework.common.util.object.ConvertUtils;
8 import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
9 import com.iailab.framework.tenant.core.context.TenantContextHolder;
10 import com.iailab.module.ansteel.job.dao.ScheduleJobDao;
11 import com.iailab.module.ansteel.job.dto.ScheduleJobDTO;
12 import com.iailab.module.ansteel.job.entity.ScheduleJobEntity;
13 import com.iailab.module.ansteel.job.service.ScheduleJobService;
14 import com.iailab.module.ansteel.job.utils.ScheduleUtils;
15 import com.iailab.module.ansteel.job.vo.ScheduleJobReqVO;
16 import org.apache.commons.lang3.StringUtils;
17 import org.quartz.Scheduler;
18 import org.springframework.stereotype.Service;
19 import org.springframework.transaction.annotation.Transactional;
20
21 import javax.annotation.Resource;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 @Service
27 public class ScheduleJobServiceImpl extends ServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
28     @Resource
29     private Scheduler scheduler;
30
31     @Resource
32     private ScheduleJobDao scheduleJobDao;
33
34     @Override
35     public PageResult<ScheduleJobEntity> page(ScheduleJobReqVO reqVO) {
36         return scheduleJobDao.selectPage(reqVO);
37     }
38
39     @Override
40     public ScheduleJobDTO get(Long id) {
41         ScheduleJobEntity entity = scheduleJobDao.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) {
58         Long tenantId = TenantContextHolder.getRequiredTenantId();
59         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
60         entity.setId(System.currentTimeMillis());
61         entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
62         entity.setTenantId(tenantId);
63         entity.setCreator(SecurityFrameworkUtils.getLoginUser().getId());
64         entity.setCreateDate(new Date());
65         scheduleJobDao.insert(entity);
66         
67         ScheduleUtils.createScheduleJob(scheduler, entity);
68     }
69     
70     @Override
71     @Transactional(rollbackFor = Exception.class)
72     public void update(ScheduleJobDTO dto) {
73         Long tenantId = TenantContextHolder.getRequiredTenantId();
74         ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
75         entity.setUpdateDate(new Date());
76         entity.setUpdater(SecurityFrameworkUtils.getLoginUser().getId());
77         entity.setTenantId(tenantId);
78         ScheduleUtils.updateScheduleJob(scheduler, entity);
79                 
80         this.updateById(entity);
81     }
82
83     @Override
84     @Transactional(rollbackFor = Exception.class)
85     public void deleteBatch(Long id) {
86         ScheduleUtils.deleteScheduleJob(scheduler, id);
87
88         scheduleJobDao.deleteById(id);
89     }
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 scheduleJobDao.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, scheduleJobDao.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 }