linwe
2024-08-02 a1eede2f9f7c2590952ab1bdf34210020cdde7c6
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
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.customTask.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.modules.customTask.dto.CustomTaskDTO;
import org.springblade.modules.customTask.entity.CustomTaskEntity;
import org.springblade.modules.customTask.mapper.CustomTaskMapper;
import org.springblade.modules.customTask.service.ICustomTaskService;
import org.springblade.modules.customTask.vo.CustomTaskVO;
import org.springblade.xxljob.entity.JobInfoEntity;
import org.springblade.xxljob.service.IJobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 自定义任务表 服务实现类
 *
 * @author BladeX
 * @since 2024-07-30
 */
@Service
public class CustomTaskServiceImpl extends ServiceImpl<CustomTaskMapper, CustomTaskEntity> implements ICustomTaskService {
 
    @Override
    public IPage<CustomTaskVO> selectCustomTaskPage(IPage<CustomTaskVO> page, CustomTaskVO customTask) {
        return page.setRecords(baseMapper.selectCustomTaskPage(page, customTask));
    }
 
 
    /**
     * 查询自定义任务表
     *
     * @param id 自定义任务表ID
     * @return 自定义任务表
     */
    @Override
    public CustomTaskDTO selectCustomTaskById(Integer id) {
        return this.baseMapper.selectCustomTaskById(id);
    }
 
    /**
     * 查询自定义任务表列表
     *
     * @param customTaskDTO 自定义任务表
     * @return 自定义任务表集合
     */
    @Override
    public List<CustomTaskDTO> selectCustomTaskList(CustomTaskDTO customTaskDTO) {
        return this.baseMapper.selectCustomTaskList(customTaskDTO);
    }
 
    @Autowired
    private IJobInfoService jobInfoService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCustomTask(CustomTaskEntity customTask) {
        boolean update = updateById(customTask);
        if (!update) {
            return update;
        }
        JobInfoEntity jobInfoEntity = jobInfoService.getById(customTask.getJobId());
        jobInfoEntity.setScheduleConf(customTask.getCron());
        Integer taskStatus = customTask.getTaskStatus() - 1;
        jobInfoEntity.setTriggerStatus(taskStatus.byteValue());
        jobInfoEntity.setExecutorParam(JSON.toJSONString(customTask));
        jobInfoEntity.setJobDesc(customTask.getName());
        boolean update1 = jobInfoService.updateById(jobInfoEntity);
        if (!update1) {
            throw new RuntimeException("更新任务表失败");
        }
        return update;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCustomTask(CustomTaskEntity customTask) {
        // 保存自定义任务表
        save(customTask);
        // 保存任务表
        JobInfoEntity jobInfoEntity = new JobInfoEntity();
        jobInfoEntity.setJobDesc(customTask.getName());
        jobInfoEntity.setScheduleConf(customTask.getCron());
        jobInfoEntity.setJobGroup(1);
        jobInfoEntity.setScheduleType("CRON");
        jobInfoEntity.setExecutorRouteStrategy("FIRST");
        jobInfoEntity.setExecutorHandler("threeColourJobHandler");
        jobInfoEntity.setExecutorParam(JSON.toJSONString(customTask));
        jobInfoEntity.setExecutorBlockStrategy("SERIAL_EXECUTION");
        jobInfoEntity.setGlueType("BEAN");
        jobInfoEntity.setGlueSource("");
        jobInfoEntity.setGlueRemark("GLUE代码初始化");
        Integer taskStatus = customTask.getTaskStatus() - 1;
        jobInfoEntity.setTriggerStatus(taskStatus.byteValue());
        jobInfoEntity.setTriggerLastTime(0L);
        jobInfoEntity.setTriggerNextTime(0L);
        jobInfoEntity.setAuthor("林伟");
        jobInfoEntity.setAlarmEmail("872216696@qq.com");
        jobInfoEntity.setExecutorFailRetryCount(0);
        jobInfoEntity.setGlueUpdatetime(DateUtil.now());
        jobInfoEntity.setAddTime(DateUtil.now());
        jobInfoEntity.setUpdateTime(DateUtil.now());
        jobInfoEntity.setMisfireStrategy("DO_NOTHING");
        jobInfoEntity.setExecutorTimeout(0);
        boolean save1 = jobInfoService.save(jobInfoEntity);
        if (!save1) {
            throw new RuntimeException("保存任务表失败");
        }
        customTask.setJobId(jobInfoEntity.getId());
        boolean update = updateById(customTask);
        if (!update) {
            throw new RuntimeException("更新自定义任务表失败");
        }
        return update;
    }
}