package cn.iocoder.yudao.module.bpm.controller.admin.definition;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionExtDO;
import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流程定义")
@RestController
@RequestMapping("/bpm/process-definition")
@Validated
public class BpmProcessDefinitionController {

    @Resource
    private BpmProcessDefinitionService bpmDefinitionService;

    @GetMapping("/page")
    @Operation(summary = "获得流程定义分页")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<PageResult<BpmProcessDefinitionPageItemRespVO>> getProcessDefinitionPage(
            BpmProcessDefinitionPageReqVO pageReqVO) {
        return success(bpmDefinitionService.getProcessDefinitionPage(pageReqVO));
    }

    @GetMapping ("/list")
    @Operation(summary = "获得流程定义列表")
//    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<List<BpmProcessDefinitionRespVO>> getProcessDefinitionList(
            BpmProcessDefinitionListReqVO listReqVO) {

        List<BpmProcessDefinitionRespVO> respVOS = bpmDefinitionService.getProcessDefinitionList(listReqVO);
        respVOS = respVOS.stream().filter(data -> !data.getId().contains("work_task")).collect(Collectors.toList());
        return success(respVOS);
    }

    @GetMapping ("/list-simple")
    @Operation(summary = "获得流程定义列表-前端下拉框使用")
    public CommonResult<List<BpmProcessDefinitionSimpleVO>> getProcessDefinitionSimpleList(BpmProcessDefinitionListReqVO reqVO) {

        List<BpmProcessDefinitionSimpleVO> respVOS = bpmDefinitionService.getProcessDefinitionSimpleList(reqVO);
        return success(respVOS);
    }

    @GetMapping ("/get-bpmn-xml")
    @Operation(summary = "获得流程定义的 BPMN XML")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<String> getProcessDefinitionBpmnXML(@RequestParam("id") String id) {
        String bpmnXML = bpmDefinitionService.getProcessDefinitionBpmnXML(id);
        return success(bpmnXML);
    }

    @PutMapping ("/update-scope")
    @Operation(summary = "修改流程定义的 Scope")
    public  CommonResult<Boolean> updateProcessDefinitionScope(@Valid @RequestBody DefinitionScopeVO scopeVO) {
        bpmDefinitionService.updateProcessDefinitionScope(scopeVO);
        return success(true);
    }

    @GetMapping ("/get")
    @Operation(summary = "获得流程定义")
    @Parameter(name = "processDefinitionId", description = "流程定义编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<DefinitionScopeVO> getProcessDefinition(@RequestParam("processDefinitionId") String processDefinitionId) {

        BpmProcessDefinitionExtDO definitionExtDO = bpmDefinitionService.getProcessDefinitionExt(processDefinitionId);
        return success(BeanUtils.toBean(definitionExtDO, DefinitionScopeVO.class));
    }
}
