/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.workflow.controller;

import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.workflow.entity.WorkFlowForm;
import com.codvision.workflow.entity.vo.WorkFlowFormVO;
import com.codvision.workflow.service.WorkFlowFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hxl
 */
@Slf4j
@Api(tags = "工作流表单权限管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowForm")
public class WorkFlowFormController {

    private final WorkFlowFormService service;

    @PostMapping("/elementFormList")
    @ApiOperation("表单权限列表")
    public ResponseEntity<List<WorkFlowForm>> elementFormList(@RequestParam(value = "definitionId") String definitionId) {
        return new ResponseEntity<List<WorkFlowForm>>().data(service.elementFormList(definitionId));
    }

    @PostMapping("/getAllFormInfo")
    @ApiOperation("获取表单全部信息")
    public ResponseEntity<WorkFlowFormVO> getAllFormInfo(@RequestParam(value = "definitionId") String definitionId,
                                                         @RequestParam(value = "instanceId") String instanceId) {
        ResponseEntity<WorkFlowFormVO> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(definitionId) || StringUtils.isBlank(instanceId)) {
            return responseEntity.error(30002);
        }

        return responseEntity.data(service.getAllFormInfo(definitionId, instanceId));
    }

    @PostMapping("/getStartFormAuth")
    @ApiOperation("获取发起表单权限信息")
    public ResponseEntity<WorkFlowFormVO> getStartFormAuth(@RequestParam(value = "definitionId") String definitionId,
                                                           @RequestParam(required = false) String instanceId) {
        ResponseEntity<WorkFlowFormVO> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(definitionId)) {
            throw new BusinessException(30002);
        }

        return responseEntity.data(service.getStartFormAuth(definitionId, instanceId));
    }

    @PostMapping("/getTaskFormAuth")
    @ApiOperation("获取任务表单权限信息")
    public ResponseEntity<WorkFlowFormVO> getTaskFormAuth(@RequestParam(value = "definitionId") String definitionId,
                                                          @RequestParam(value = "instanceId") String instanceId,
                                                          @RequestParam(required = false) String taskId,
                                                          @RequestParam(value = "elementKey") String elementKey) {
        ResponseEntity<WorkFlowFormVO> responseEntity = new ResponseEntity<>();
        if (StringUtils.isBlank(definitionId) || StringUtils.isBlank(instanceId) || StringUtils.isBlank(elementKey)) {
            return responseEntity.error(30002);
        }

        return responseEntity.data(service.getTaskFormAuth(definitionId, elementKey, instanceId, taskId));
    }
}
