package com.ningdatech.pmapi.organization.controller;

import com.ningdatech.basic.model.PageVo;
import com.wflow.bean.dto.WflowOrgProcdefDto;
import com.wflow.bean.vo.OrgProcdefVo;
import com.wflow.service.OrgProcdefService;
import com.wflow.workflow.service.OrgProcessModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname OrganizationProcessController
 * @Description
 * @Date 2023/1/13 11:13
 * @Author PoffyZhang
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/organization/procdef")
@Api(value = "OrganizationProcdef", tags = "单位配置-流程配置")
@RequiredArgsConstructor
public class ProcDefController {

    private final OrgProcdefService orgProcdefService;

    private final OrgProcessModelService orgProcessModelService;

    @ApiOperation(value = "单位流程配置列表", notes = "单位流程配置列表")
    @GetMapping("/list")
    public PageVo<OrgProcdefVo> list(@RequestParam String orgCode, @RequestParam(defaultValue = "1") Integer pageNumber,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return orgProcdefService.getOrgProcessByOrgCode(orgCode, pageNumber, pageSize);
    }

    @ApiOperation(value = "单位流程配置详情", notes = "单位流程配置详情")
    @GetMapping("/detail/{processDefId}")
    public OrgProcdefVo detail(@PathVariable String processDefId) {
        return orgProcessModelService.getProcess(processDefId);
    }

    @ApiOperation(value = "单位流程配置保存", notes = "单位流程配置保存")
    @PostMapping("/save")
    public String save(@Validated @RequestBody WflowOrgProcdefDto wflowOrgProcdefDto) {
        return orgProcessModelService.saveProcess(wflowOrgProcdefDto);
    }

    @ApiOperation(value = "单位流程配置启用", notes = "单位流程配置启用")
    @PutMapping("/enable/{processDefId}")
    public String enableProcess(@PathVariable String processDefId) {
        orgProcessModelService.enableProcess(processDefId, Boolean.FALSE);
        return "启用成功";
    }

    @ApiOperation(value = "单位流程配置禁用", notes = "单位流程配置禁用")
    @PutMapping("/disable/{processDefId}")
    public String disableProcess(@PathVariable String processDefId) {
        orgProcessModelService.enableProcess(processDefId, Boolean.TRUE);
        return "禁用成功";
    }

    @ApiOperation(value = "单位流程配置部署", notes = "单位流程配置部署")
    @PostMapping("/deploy/{processDefId}")
    public String deploy(@Validated @PathVariable String processDefId) {
        return orgProcessModelService.deployProcess(processDefId, null);
    }

    @ApiOperation(value = "单位流程配置删除", notes = "单位流程配置删除")
    @PostMapping("/delete/{processDefId}")
    public Boolean delete(@PathVariable String processDefId) {
        return orgProcessModelService.delProcess(processDefId);
    }
}
