package com.casic.missiles.controller.flowable;

import cn.hutool.core.lang.Assert;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.flowable.*;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.flowable.ApprovalOperateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description: 审批操作按钮功能
 * @Author: wangpeng
 * @Date: 2022/12/23 11:49
 */
@Api(tags = "审批操作按钮功能接口")
@Controller
@RequestMapping("/approval/operate")
public class ApprovalOperateController {
    @Autowired
    private ApprovalOperateService approvalOperateService;

    /**
     * 查看-审批记录
     *      查看每个节点的名称、人员、提交时间、审批留言
     * 步骤（钉钉中是获取后形成执行过程的流程图，ruoyi的是审批记录的表格形式，暂使用若依的符合prd）：
     *      1、跟据流程实例id查询历史活动（HistoricActivityInstance查询每个流程实例processInstance经历的所有活动，即走过的审批节点历程。包括任务执行人、网关、开始事件和结束事件等）
     *      2、获取表单信息
     */
    @ApiOperation("审批操作-查看-审批记录")
    @GetMapping("/approvalLog")
    @ResponseBody
    public ReturnDTO<List<ApprovalLogResponse>> approvalLog(String processId) {
        Assert.isFalse(StringUtils.isEmpty(processId), () -> {
            throw new BusinessException(BusinessExceptionEnum.PROCESS_ID_NULL);
        });
        List<List<ApprovalLogResponse>> resultList = approvalOperateService.approvalLog(processId);
        return ReturnUtil.success(resultList);
    }

    /**
     * 查看-流程定义图，复用流程定义详情接口
     */

    /**
     * 同意
     */
    @ApiOperation("审批操作-同意")
    @PostMapping("/agree")
    @ResponseBody
    public ReturnDTO agree(@RequestBody @Valid ApprovalAgreeRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return approvalOperateService.agree(request);
    }

    /**
     * 驳回
     */
    @ApiOperation("审批操作-驳回")
    @PostMapping("/reject")
    @ResponseBody
    public ReturnDTO reject(@RequestBody @Valid ApprovalRejectRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return approvalOperateService.reject(request);
    }

    /**
     * 拒绝
     */
    @ApiOperation("审批操作-拒绝")
    @PostMapping("/refuse")
    @ResponseBody
    public ReturnDTO refuse(@RequestBody @Valid ApprovalRefuseRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return approvalOperateService.refuse(request);
    }

    /**
     * 取消（发起者撤回，表单状态更改为已取消，仅审批中状态可撤回）
     */
    @ApiOperation("审批操作-取消")
    @PostMapping("/revoke")
    @ResponseBody
    public ReturnDTO revoke(@RequestBody @Valid ApprovalRevokeRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return approvalOperateService.revoke(request);
    }

    /**
     * 审批操作-删除
     * 该功能需要具体业务的删除，放在各业务controller中处理，提供公共flowable删除处理service:approvalOperateService
     */
}
