content: package com.platform.admin.controller;
  
  
  import com.platform.admin.base.BaseController;
  import com.platform.admin.dto.FlinkXBatchJsonBuildDto;
  import com.platform.admin.dto.TriggerJobDto;
  import com.platform.admin.service.JobService;
  import com.platform.core.biz.model.ReturnT;
  import com.platform.core.util.DateUtil;
  import com.platform.admin.core.cron.CronExpression;
  import com.platform.admin.core.thread.JobTriggerPoolHelper;
  import com.platform.admin.core.trigger.TriggerTypeEnum;
  import com.platform.admin.core.util.I18nUtil;
  import com.platform.admin.entity.JobInfo;
  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
  import org.springframework.web.bind.annotation.*;
  
  import javax.annotation.Resource;
  import javax.servlet.http.HttpServletRequest;
  import java.io.IOException;
  import java.text.ParseException;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  import java.util.Map;
  
  /**
   *
   * @author AllDataDC
   * @date 2023/3/26 11:14
   * 任务配置接口
   **/
  @Api(tags = "任务配置接口")
  @RestController
  @RequestMapping("/job")
  public class JobInfoController extends BaseController {
  
      @Resource
      private JobService jobService;
  
  
      @GetMapping("/pageList")
      @ApiOperation("任务列表")
      public ReturnT<Map<String, Object>> pageList(@RequestParam(value = "current", required = false, defaultValue = "0") int current,
                                          @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                                                   @RequestParam("jobGroup") int jobGroup, @RequestParam("triggerStatus") int triggerStatus,
                                                   @RequestParam("jobDesc") String jobDesc, @RequestParam("glueType") String glueType,
                                                   @RequestParam("projectIds") Integer[] projectIds) {
  
          return new ReturnT<>(jobService.pageList((current-1)*size, size, jobGroup, triggerStatus, jobDesc, glueType, 0, projectIds));
      }
  
      @GetMapping("/list")
      @ApiOperation("全部任务列表")
      public ReturnT<List<JobInfo>> list(){
          return new ReturnT<>(jobService.list());
      }
  
      @PostMapping("/add")
      @ApiOperation("添加任务")
      public ReturnT<String> add(HttpServletRequest request, @RequestBody JobInfo jobInfo) {
          jobInfo.setUserId(getCurrentUserId(request));
          return jobService.add(jobInfo);
      }
  
      @PostMapping("/update")
      @ApiOperation("更新任务")
      public ReturnT<String> update(HttpServletRequest request,@RequestBody JobInfo jobInfo) {
          jobInfo.setUserId(getCurrentUserId(request));
          return jobService.update(jobInfo);
      }
  
      @PostMapping(value = "/remove/{id}")
      @ApiOperation("移除任务")
      public ReturnT<String> remove(@PathVariable(value = "id") int id) {
          return jobService.remove(id);
      }
  
      @RequestMapping(value = "/stop",method = RequestMethod.POST)
      @ApiOperation("停止任务")
      public ReturnT<String> pause(int id) {
          return jobService.stop(id);
      }
  
      @RequestMapping(value = "/start",method = RequestMethod.POST)
      @ApiOperation("开启任务")
      public ReturnT<String> start(int id) {
          return jobService.start(id);
      }
  
      @PostMapping(value = "/trigger")
      @ApiOperation("触发任务")
      public ReturnT<String> triggerJob(@RequestBody TriggerJobDto dto) {
  		try {
  			String executorParam=dto.getExecutorParam();
  			if (executorParam == null) {
  				executorParam = "";
  			}
  			JobTriggerPoolHelper jobTriggerPoolHelper =  new JobTriggerPoolHelper();
  			jobTriggerPoolHelper.runJob(dto.getJobId());
  		} catch (Exception e) {
  			return ReturnT.FAIL;
  		}
  		return ReturnT.SUCCESS;
      }
  
      @GetMapping("/nextTriggerTime")
      @ApiOperation("获取近5次触发时间")
      public ReturnT<List<String>> nextTriggerTime(String cron) {
          List<String> result = new ArrayList<>();
          try {
              CronExpression cronExpression = new CronExpression(cron);
              Date lastTime = new Date();
              for (int i = 0; i < 5; i++) {
                  lastTime = cronExpression.getNextValidTimeAfter(lastTime);
                  if (lastTime != null) {
                      result.add(DateUtil.formatDateTime(lastTime));
                  } else {
                      break;
                  }
              }
          } catch (ParseException e) {
              return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
          }
          return new ReturnT<>(result);
      }
  
      @PostMapping("/batchAdd")
      @ApiOperation("批量创建任务")
      public ReturnT<String> batchAdd(@RequestBody FlinkXBatchJsonBuildDto dto) throws IOException {
          if (dto.getTemplateId() ==0) {
              return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_choose") + I18nUtil.getString("jobinfo_field_temp")));
          }
          return jobService.batchAdd(dto);
      }
  }
  