package com.ruoyi.web.controller.bpm;

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

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.entity.vo.NodeVo;
import io.swagger.annotations.Api;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.entity.BpmProcessConfiguration;
import com.ruoyi.system.service.IBpmProcessConfigurationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流程配置Controller
 *
 * @author hzh
 * @date 2023-10-20
 */
@Anonymous
@RestController
@RequestMapping("/system/configuration")
@Api(value = "/system/configuration", description = "流程配置-接口")
public class BpmProcessConfigurationController extends BaseController {
    @Autowired
    private IBpmProcessConfigurationService bpmProcessConfigurationService;

    /**
     * 查询流程配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询流程配置列表")
    public TableDataInfo list(BpmProcessConfiguration bpmProcessConfiguration) {
        startPage();
        List<BpmProcessConfiguration> list = bpmProcessConfigurationService.selectBpmProcessConfigurationList(bpmProcessConfiguration);
        return getDataTable(list);
    }

    /**
     * 导出流程配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:export')")
    @Log(title = "流程配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出流程配置列表")
    public void export(HttpServletResponse response, BpmProcessConfiguration bpmProcessConfiguration) {
        List<BpmProcessConfiguration> list = bpmProcessConfigurationService.selectBpmProcessConfigurationList(bpmProcessConfiguration);
        ExcelUtil<BpmProcessConfiguration> util = new ExcelUtil<BpmProcessConfiguration>(BpmProcessConfiguration.class);
        util.exportExcel(response, list, "流程配置数据");
    }

    /**
     * 获取流程配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取流程配置详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bpmProcessConfigurationService.selectBpmProcessConfigurationById(id));
    }

    /**
     * 新增流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:add')")
    @Log(title = "流程配置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增流程配置")
    public AjaxResult add(@RequestBody List<BpmProcessConfiguration> bpmProcessConfigurationList) {
        return toAjax(bpmProcessConfigurationService.insertBpmProcessConfiguration(bpmProcessConfigurationList));
    }

    /**
     * 修改流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:edit')")
    @Log(title = "流程配置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改流程配置")
    public AjaxResult edit(@RequestBody BpmProcessConfiguration bpmProcessConfigurationList) {
        return toAjax(bpmProcessConfigurationService.updateBpmProcessConfiguration(bpmProcessConfigurationList));
    }

    /**
     * 删除流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:configuration:remove')")
    @Log(title = "流程配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除流程配置")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bpmProcessConfigurationService.deleteBpmProcessConfigurationByIds(ids));
    }

    /**
     * 新增流程配置信息
     */
    @Log(title = "流程节点配置", businessType = BusinessType.INSERT)
    @PostMapping("/addProcessNodeConfiguration")
    @ApiOperation(value = "新增节点配置")
    public AjaxResult insert(@RequestBody NodeVo nodeVo) {
        System.out.println(SecurityUtils.getUserId());
        return toAjax(bpmProcessConfigurationService.insertBpmProcessNodeConfig(nodeVo));
    }

    /**
     * 修改流程配置信息
     */
    @Log(title = "流程节点配置", businessType = BusinessType.INSERT)
    @PutMapping("/updateProcessNodeConfiguration")
    @ApiOperation(value = "修改节点配置")
    public AjaxResult update(@RequestBody NodeVo nodeVo) {
        return toAjax(bpmProcessConfigurationService.updateBpmProcessNodeConfig(nodeVo));
    }

    @Log(title = "流程节点配置")
    @GetMapping("/selectByProcessByKey/{nodeProcessKey}")
    @ApiOperation(value = "当前流程节点详细数据")
    public AjaxResult selectByProcessByKey(@PathVariable("nodeProcessKey") String nodeProcessKey) {
        return success(bpmProcessConfigurationService.selectByProcessByKey(nodeProcessKey));
    }
}
