package com.ruoyi.platform.controller.workflow;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.Workflow;
import com.ruoyi.platform.service.WorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * DAG workflow(Workflow)表控制层
 *
 * @author makejava
 * @since 2023-10-18 14:23:56
 */
@RestController
@RequestMapping("workflow")
@Api("流水线管理")
public class WorkflowController extends BaseController {





    /**
     * 服务对象
     */
    @Resource
    private WorkflowService workflowService;

    /**
     * 分页查询
     *
     * @param workflow    筛选条件
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<Workflow>> queryByPage(Workflow workflow, int page, int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        return  genericsSuccess(this.workflowService.queryByPage(workflow, pageRequest));
    }


    /**
     * 按流水线名字模糊查询
     *
     * @param name 筛选条件
     * @return 查询结果
     */
    @GetMapping("name/{name}")
    @ApiOperation("按流水线名字模糊查询流水线")
    public GenericsAjaxResult<Page<Workflow>> queryByName(@PathVariable("name") String name) {
        return  genericsSuccess(this.workflowService.queryByName(name));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询单条数据")
    public GenericsAjaxResult<Workflow> queryById(@PathVariable("id") Long id) {
        return  genericsSuccess(this.workflowService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param workflow 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增流水线")
    public GenericsAjaxResult<Workflow> add(@RequestBody Workflow workflow) throws Exception {
        return  genericsSuccess(this.workflowService.insert(workflow));
    }


    /**
     * 复制流水线记录重新插入数据库
     *
     * @param id 需要复制的流水线id
     * @return 复制结果
     */
    @ApiOperation("复制流水线记录")
    @PostMapping("duplicate/{id}")
    public GenericsAjaxResult<Workflow> duplicateWorkflow(@PathVariable("id") Long id) {
        return  genericsSuccess(this.workflowService.duplicateWorkflow(id));
    }

    /**
     * 流水线编辑页面保存接口
     *
     * @return 保存结果
     */
//    @ApiOperation("流水线保存")
//    @PutMapping("/saveWorkflow/{id}")
//    public ResponseEntity<Workflow> saveWorkflow(@PathVariable("id") Long id) {
//        return  genericsSuccess(this.workflowService.saveWorkflow(id));
//    }


    /*
      编辑数据

      @param workflow 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑流水线")
    public GenericsAjaxResult<Workflow> edit(@RequestBody Workflow workflow) throws Exception {
        return  genericsSuccess(this.workflowService.update(workflow));
    }

    /**
     *
     *
     *
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除流水线")
    public GenericsAjaxResult<String> deleteById(@PathVariable("id") Long id) throws Exception {
        return  genericsSuccess(this.workflowService.removeById(id));
    }

}

