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