package com.ruoyi.project.business.controller.flow;

import com.ruoyi.common.exception.ServiceErrorException;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.flow.FlowAuditParam;
import com.ruoyi.project.business.domain.flow.FlowConfig;
import com.ruoyi.project.business.domain.flow.FlowTodoListParam;
import com.ruoyi.project.business.enums.AuditFlowEnum;
import com.ruoyi.project.business.enums.CategoryEnum;
import com.ruoyi.project.business.enums.FunctionEnum;
import com.ruoyi.project.business.enums.ModuleEnum;
import com.ruoyi.project.business.service.flow.IFlowBusinessService;
import com.ruoyi.project.business.service.flow.IFlowService;
import org.springframework.web.bind.annotation.*;

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


/**
 * 【请填写功能名称】Controller
 *
 * @author generator
 * @date 2021-09-28
 */
@RestController
@RequestMapping("/system/flow")
public class FlowController extends BaseController {

    @Resource
    private IFlowService flowService;

    @Resource
    private IFlowBusinessService flowBusinessService;

    /************************************ ↓流程配置接口↓ ***********************************/

    @GetMapping(value = "getConfig")
    public AjaxResult getConfig(FlowConfig flowConfig) {
        return AjaxResult.success(flowService.getConfig(flowConfig));
    }

    @PostMapping(value = "saveConfig")
    public AjaxResult saveConfig(@RequestBody FlowConfig flowConfig) {
        return toAjax(flowService.saveConfig(flowConfig));
    }

    @PostMapping(value = "removeConfig")
    public AjaxResult removeConfig(@RequestBody FlowConfig flowConfig) {
        int result = flowService.removeConfig(flowConfig);
        if (result == 0) {
            throw new ServiceErrorException("清除失败，自定义审批流程已不存在");
        }
        return toAjax(result);
    }


    /************************************ ↓流程审批接口↓ ***********************************/

    @PostMapping(value = "audit")
    public AjaxResult audit(@RequestBody FlowAuditParam auditParam) {
        return AjaxResult.success(flowService.audit(auditParam));
    }

    @PostMapping(value = "auditBatch")
    public AjaxResult auditBatch(@RequestBody FlowAuditParam auditParam) {
        return AjaxResult.success(flowService.auditBatch(auditParam));
    }

    @PostMapping(value = "rollback")
    public AjaxResult rollback(@RequestBody FlowAuditParam auditParam) {
        return AjaxResult.success(flowService.rollback(auditParam));
    }

    @GetMapping(value = "getAuditHistory")
    public TableDataInfo getAuditHistory(@RequestParam("auditFlows") List<Integer> auditFlows, @RequestParam("businessId") Integer businessId) {
        return getDataTable(flowService.getAuditHistory(auditFlows.stream().map(AuditFlowEnum::from).collect(Collectors.toList()), businessId));
    }

    @PutMapping(value = "alertAudit")
    public AjaxResult alertAudit(@RequestParam("taskId") Long taskId) {
        return AjaxResult.success(flowService.alertAudit(taskId));
    }

    @PutMapping("deleteAttachment/{businessId}/{auditFlow}")
    public AjaxResult deleteAttachment(@PathVariable Long businessId, @PathVariable Integer auditFlow) {
        return toAjax(flowBusinessService.deleteAttachment(businessId, AuditFlowEnum.from(auditFlow)));
    }



    /************************************ ↓待办信息查询接口↓ ***********************************/

    @GetMapping(value = "getTodoList")
    public TableDataInfo getTodoList(FlowTodoListParam params) {
        if (!params.getIndexQuery()) {
            startPage();
        }
        return getDataTable(flowService.getTodoList(params));
    }

}
