package org.jeecg.modules.approval.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.approval.model.dto.ApprovalOpinionDTO;
import org.jeecg.modules.approval.model.dto.DynamicFormConf;
import org.jeecg.modules.approval.model.vo.ApprovalOpinionVO;
import org.jeecg.modules.approval.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 申请管理
 *
 * @author yao.sun
 * @data 2018/06/04
 */
@RestController
@Slf4j
@RequestMapping("/approval")
public class ApprovalController {

    @Autowired
    private ApprovalService approvalService;

    /**
     * 流程环节审批信息
     */
    @GetMapping("/detail")
    public Result detail(@RequestParam String processId) {
        List<ApprovalOpinionDTO> list = approvalService.listApprovalDetail(processId);
        return Result.OK(list);
    }

    /**
     * 流程审批
     */
    @PostMapping("/handle")
    public Result handleApproval(@RequestBody ApprovalOpinionVO approvalOpinionVO) {
        boolean success = approvalService.handleApproval(approvalOpinionVO);
        return success ? Result.OK() : Result.error("审批失败");
    }

    /**
     * 获取流程状态(能否撤销,能否修改)
     * pid 实例id
     * tid 任务id
     */
    @GetMapping("/getProcessStatus")
    public Result getProcessStatus(@RequestParam String processInstanceId) {
        Map map = approvalService.getProcessStatus(processInstanceId);
        return Result.OK(map);
    }

    /**
     * 撤销申请
     *
     * @param processId
     * @return
     */
    @GetMapping("/withdraw")
    public Result withdraw(@RequestParam String processId) {
        boolean flag = approvalService.withdrawProcess(processId);
        return Result.OK(true);
    }

    /**
     * 根据任务id获取动态表单属性配置
     *
     * @param taskId
     * @return
     */
    @GetMapping("/taskDynamicConf")
    public Result taskDynamicParams(@RequestParam String taskId) {
        DynamicFormConf dynamicFormConf = approvalService.taskDynamicParams(taskId);
        return Result.OK(dynamicFormConf);
    }

    @GetMapping("/getRunNodes")
    public Result getRunNodes(@RequestParam String processInstanceId) {
        Map<String,String> dynamicFormConf = approvalService.getRunNodes(processInstanceId);
        return Result.OK(dynamicFormConf);
    }




}
