/* * 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.threeColorTask.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.threeColorTask.dto.CustomTaskDTO; import org.springblade.modules.threeColorTask.entity.CustomTaskEntity; import org.springblade.modules.threeColorTask.mapper.CustomTaskMapper; import org.springblade.modules.threeColorTask.service.ICustomTaskService; import org.springblade.modules.threeColorTask.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 implements ICustomTaskService { @Override public IPage selectCustomTaskPage(IPage 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 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; } }