package cn.axzo.workflow.server.controller.web.form;

import cn.axzo.workflow.client.feign.manage.FormAdminApi;
import cn.axzo.workflow.common.model.dto.BpmnFormRelationSearchDTO;
import cn.axzo.workflow.common.model.request.form.definition.StartFormSearchDTO;
import cn.axzo.workflow.common.model.request.form.instance.FormDetailDTO;
import cn.axzo.workflow.common.model.request.form.instance.FormSearchDTO;
import cn.axzo.workflow.common.model.request.form.instance.FromDataSearchDTO;
import cn.axzo.workflow.common.model.response.form.FormVO;
import cn.axzo.workflow.common.model.response.form.definition.FormDefinitionVO;
import cn.axzo.workflow.common.model.response.form.instance.FormDataVO;
import cn.axzo.workflow.common.model.response.form.instance.FormInstanceVO;
import cn.axzo.workflow.core.service.FormCoreService;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.azxo.framework.common.model.CommonResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.azxo.framework.common.model.CommonResponse.success;

/**
 * 表单实例相关控制器
 *
 * @author wangli
 * @since 2023/7/21 15:23
 */
@Slf4j
@RequestMapping({"/web/v1/api/form/admin", "/api/form/admin"})
@RestController
@ErrorReporter
@Validated
public class FormAdminController implements FormAdminApi {

    @Resource
    private FormCoreService formCoreService;

    @Operation(summary = "表单列表")
    @PostMapping("/form/page")
    public CommonResponse<List<FormVO>> formPage(@Validated @RequestBody FormSearchDTO dto) {
        BpmnFormRelationSearchDTO searchDTO = new BpmnFormRelationSearchDTO();
        searchDTO.setKey(dto.getKey());
        searchDTO.setTenantId(dto.getTenantId());
        return success(formCoreService.pageForm(searchDTO));
    }

    @Operation(summary = "获取指定业务含有表单的定义内容和权限")
    @PostMapping("/start/form")
    public CommonResponse<FormDefinitionVO> getFormDefinition(@Validated @RequestBody StartFormSearchDTO dto) {
        return success(formCoreService.getStartForm(dto));
    }

    @Operation(summary = "获取指定表单审批的实例信息")
    @PostMapping("/instance/render")
    public CommonResponse<FormInstanceVO> getFormInstance(@Validated @RequestBody FormDetailDTO dto) {
        return success(formCoreService.getFormInstance(dto));
    }

    @Operation(summary = "获取指定表单审批的实例数据")
    @PostMapping("/instance/form/data")
    public CommonResponse<List<FormDataVO>> getFormData(@Validated @RequestBody FromDataSearchDTO dto) {
        return success(formCoreService.getFormData(dto));
    }
}
