package com.bonus.material.approval.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.approval.domain.ApprovalInstance;
import com.bonus.material.approval.service.IApprovalEngineService;
import com.bonus.material.approval.service.IApprovalInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批任务Controller
 * 
 * @author bonus
 * @date 2024-11-18
 */
@Api(tags = "审批任务")
@RestController
@RequestMapping("/approval/task")
public class ApprovalTaskController extends BaseController {

    @Resource
    private IApprovalInstanceService instanceService;

    @Resource
    private IApprovalEngineService approvalEngineService;

    /**
     * 查询我的待办审批列表
     */
    @ApiOperation("查询我的待办审批列表")
    //@RequiresPermissions("material:approval:task:todo")
    @GetMapping("/todo")
    public TableDataInfo todoList() {
        startPage();
        List<ApprovalInstance> list = instanceService.selectMyTodoList();
        return getDataTable(list);
    }

    /**
     * 查询我的已办审批列表
     */
    @ApiOperation("查询我的已办审批列表")
    //@RequiresPermissions("material:approval:task:done")
    @GetMapping("/done")
    public TableDataInfo doneList() {
        startPage();
        List<ApprovalInstance> list = instanceService.selectMyDoneList();
        return getDataTable(list);
    }

    /**
     * 获取审批详情（包含审批记录）
     */
    @ApiOperation("获取审批详情")
    //@RequiresPermissions("material:approval:task:query")
    @GetMapping("/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id) {
        return success(approvalEngineService.getApprovalDetail(id));
    }

    /**
     * 执行审批操作
     */
    @ApiOperation("执行审批操作")
    //@RequiresPermissions("material:approval:task:approve")
    @PostMapping("/approve")
    public AjaxResult approve(
            @ApiParam(value = "实例ID", required = true) @RequestParam Long instanceId,
            @ApiParam(value = "审批结果（1通过 2驳回）", required = true) @RequestParam String approveResult,
            @ApiParam(value = "审批意见") @RequestParam(required = false) String approveOpinion) {
        return approvalEngineService.doApprove(instanceId, approveResult, approveOpinion);
    }
}

