package com.ruoyi.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.entity.BpmProcess;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBpmProcessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流程定义Controller
 *
 * @author hzh
 * @date 2023-10-10
 */
@RestController
@RequestMapping("/system/process")
@Api(value = "/system/process", description = "流程定义接口")
public class BpmProcessController extends BaseController {
    @Autowired
    private IBpmProcessService bpmProcessService;

    /**
     * 查询流程定义列表
     */
//    @PreAuthorize("@ss.hasPermi('system:process:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询流程列表")
    public TableDataInfo list(BpmProcess bpmProcess) {
        startPage();
        List<BpmProcess> list = bpmProcessService.selectBpmProcessList(bpmProcess);
        return getDataTable(list);
    }

    /**
     * 导出流程定义列表
     */
//    @PreAuthorize("@ss.hasPermi('system:process:export')")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出流程excel")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BpmProcess bpmProcess) {
        List<BpmProcess> list = bpmProcessService.selectBpmProcessList(bpmProcess);
        ExcelUtil<BpmProcess> util = new ExcelUtil<BpmProcess>(BpmProcess.class);
        util.exportExcel(response, list, "流程定义数据");
    }

    /**
     * 获取流程定义详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:process:query')")
    @GetMapping(value = "/{processId}")
    @ApiOperation(value = "获取流程详细信息")
    public AjaxResult getInfo(@PathVariable("processId") Long processId) {
        return success(bpmProcessService.selectBpmProcessByProcessId(processId));
    }

    /**
     * 新增流程定义
     */
//    @PreAuthorize("@ss.hasPermi('system:process:add')")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增流程定义")
    public AjaxResult add(@RequestBody BpmProcess bpmProcess) {
        return toAjax(bpmProcessService.insertBpmProcess(bpmProcess));
    }

    /**
     * 修改流程定义
     */
//    @PreAuthorize("@ss.hasPermi('system:process:edit')")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改流程定义")
    public AjaxResult edit(@RequestBody BpmProcess bpmProcess) {
        return toAjax(bpmProcessService.updateBpmProcess(bpmProcess));
    }

    /**
     * 删除流程定义
     */
//    @PreAuthorize("@ss.hasPermi('system:process:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{processIds}")
    @ApiOperation(value = "批量删除流程定义")
    public AjaxResult remove(@PathVariable Long[] processIds) {
        return toAjax(bpmProcessService.deleteBpmProcessByProcessIds(processIds));
    }

    /**
     * 流程备份
     */
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping("/bpmBackups")
    @ApiOperation(value = "备份流程")
    public AjaxResult bpmBackups(BpmProcess bpmProcess) {
        return toAjax(bpmProcessService.bpmBackups(bpmProcess));
    }

    /**
     * 导出流程文件文件xml/bpmn格式
     */
    @ApiOperation(value = "导出流程文件文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processId",value = "编号",dataType = "Long"),
            @ApiImplicitParam(name = "fileType",value = "文件类型",dataType = "String")
    })
    @GetMapping("/export")
    public void export(@RequestParam("processId") Long processId,@RequestParam("fileType") String fileType, HttpServletRequest request, HttpServletResponse response){
        bpmProcessService.export(processId,fileType,request,response);
    }


}
