package cn.fateverse.workflow.controller;

import cn.fateverse.common.excel.utils.ExcelUtil;
import cn.hutool.core.util.StrUtil;
import cn.fateverse.common.core.entity.Option;
import cn.fateverse.workflow.entity.dto.ProcessListenerDto;
import cn.fateverse.workflow.entity.query.ProcessListenerQuery;
import cn.fateverse.workflow.entity.vo.ProcessListenerVo;
import cn.fateverse.workflow.service.ProcessListenerService;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.common.log.annotation.Log;
import cn.fateverse.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统内置监听器 Controller
 *
 * @author clay
 * @date 2023-03-27
 */
@Api(value = "系统内置监听器管理",tags = "系统内置监听器管理")
@RestController
@RequestMapping("/process/listener")
public class ProcessListenerController {

    private final ProcessListenerService processListenerService;

    public ProcessListenerController(ProcessListenerService processListenerService) {
        this.processListenerService = processListenerService;
    }


    @ApiOperation("获取系统内置监听器列表")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('flowable:listener:list')")
    public Result<TableDataInfo<ProcessListenerVo>> list(ProcessListenerQuery query) {
        TableDataInfo<ProcessListenerVo> dataInfo = processListenerService.searchList(query);
        return Result.ok(dataInfo);
    }

    @ApiOperation("获取系统内置监听器选项")
    @GetMapping("option/{type}")
    public Result<List<Option>> option(@PathVariable String type) {
        if (StrUtil.isEmpty(type)){
            return Result.error("请求参数不能为空!");
        }
        List<Option> optionList = processListenerService.searchOptionList(type);
        return Result.ok(optionList);
    }

    @ApiOperation("导出excel数据")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermission('flowable:listener:export')")
    public void export(ProcessListenerQuery query){
        List<ProcessListenerVo> list = processListenerService.exportList(query);
        ExcelUtil.exportExcel(list,ProcessListenerVo.class);
    }

    @ApiOperation("获取系统内置监听器详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('flowable:listener:info')")
    public Result<ProcessListenerVo> info(@PathVariable String id) {
        ObjectUtils.checkPk(id);
        ProcessListenerVo processListener = processListenerService.searchById(id);
        return Result.ok(processListener);
    }


    @ApiOperation("新增系统内置监听器")
    @PostMapping
    @Log(title = "新增系统内置监听器",businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermission('flowable:listener:add')")
    public Result<Void> add(@RequestBody @Validated ProcessListenerDto processListener){
        processListenerService.save(processListener);
        return Result.ok();
    }

    @ApiOperation("修改系统内置监听器")
    @PutMapping
    @Log(title = "修改系统内置监听器",businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('flowable:listener:edit')")
    public Result<Void> edit(@RequestBody @Validated ProcessListenerDto processListener){
        ObjectUtils.checkPk(processListener.getId());
        processListenerService.edit(processListener);
        return Result.ok();
    }

    @ApiOperation("删除系统内置监听器")
    @DeleteMapping("/{idList}")
    @Log(title = "删除系统内置监听器",businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermission('flowable:listener:del')")
    public Result<Void> batchDel(@PathVariable List<String> idList){
        ObjectUtils.checkPkList(idList);
        processListenerService.removeBatch(idList);
        return Result.ok();
    }

}