package com.cnooc.expert.controller.workflow;

import com.cnooc.expert.common.response.ApiResult;
import com.cnooc.expert.common.response.BasePageResp;
import com.cnooc.expert.controller.common.AbstractBaseController;
import com.cnooc.expert.controller.workflow.model.request.ApprovePathSettingByConditionGetReq;
import com.cnooc.expert.controller.workflow.model.request.ApprovePathSettingPageReq;
import com.cnooc.expert.external.workflow.model.request.ApprovePathSettingByConditionGetApiReq;
import com.cnooc.expert.external.workflow.model.request.ApprovePathSettingSaveOrUpdateApiReq;
import com.cnooc.expert.external.workflow.model.request.InstCalculateApiReq;
import com.cnooc.expert.external.workflow.model.request.InstRevokeApiReq;
import com.cnooc.expert.external.workflow.model.request.InstStartApiReq;
import com.cnooc.expert.external.workflow.model.response.ApprovePathSettingByConditionGetApiResp;
import com.cnooc.expert.external.workflow.model.response.InstCalculateApiResp;
import com.cnooc.expert.external.workflow.model.response.InstStartApiResp;
import com.cnooc.expert.external.workflow.model.response.ProcDefPageApiResp;
import com.cnooc.expert.external.workflow.model.response.ReceiveSettingByConditionFindApiResp;
import com.cnooc.expert.service.WorkflowService;
import lombok.AllArgsConstructor;
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 java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/workflow")
public class WorkflowController extends AbstractBaseController {

    private final WorkflowService workflowService;

    // 流程实例启动
    @PostMapping("/start")
    public ApiResult<InstStartApiResp> instStart(@RequestBody @Validated InstStartApiReq req) {
        InstStartApiResp apiResp = workflowService.startInst(req);
        return ApiResult.successWithResult(apiResp);
    }

    // 流程实例回撤
    @PostMapping("/revoke")
    public ApiResult<Boolean> instRevoke(@RequestBody @Validated InstRevokeApiReq req) {
        Boolean apiResp = workflowService.revokeInst(req);
        return ApiResult.successWithResult(apiResp);
    }

    // 获取审批路径
    @PostMapping("/findDefInfoListByCondition")
    public ApiResult<BasePageResp<ProcDefPageApiResp>> pageApprovePathSetting(@RequestBody @Validated ApprovePathSettingPageReq req) {
        BasePageResp<ProcDefPageApiResp> apiResp = workflowService.pageApprovePathSetting(req);
        return ApiResult.successWithResult(apiResp);
    }

    // 计算是否需要审批, 需要审批返回能使用的模板信息
    @PostMapping("/inst/calculate")
    public ApiResult<InstCalculateApiResp> instCalculate(@RequestBody @Validated InstCalculateApiReq req) {
        InstCalculateApiResp apiResp = workflowService.calculateInst(req);
        return ApiResult.successWithResult(apiResp);
    }

    // 保存、编辑审批路径配置
    @PostMapping("/saveOrUpdateApprovePathSetting")
    public ApiResult<Object> saveOrUpdateApprovePathSetting(@RequestBody @Validated ApprovePathSettingSaveOrUpdateApiReq req) {
        workflowService.saveOrUpdateApprovePathSetting(req);
        return ApiResult.success();
    }

    // 获取审批模版
    @PostMapping("/getApprovePathSettingByCondition")
    public ApiResult<ApprovePathSettingByConditionGetApiResp> saveOrUpdateApprovePathSetting(@RequestBody @Validated ApprovePathSettingByConditionGetApiReq req) {
        ApprovePathSettingByConditionGetApiResp apiResp = workflowService.getApprovePathSettingByCondition(req);
        return ApiResult.successWithResult(apiResp);
    }

    // 查询限上岗配置 - 没数据表示不需要配置限上岗"
    @PostMapping("/fingReceiveSettingByCondition")
    public ApiResult<List<ReceiveSettingByConditionFindApiResp>> fingReceiveSettingByCondition(@RequestBody @Validated ApprovePathSettingByConditionGetReq req) {
        List<ReceiveSettingByConditionFindApiResp> apiResp = workflowService.findReceiveSettingByCondition(req);
        return ApiResult.successWithResult(apiResp);
    }
}

