package com.ningdatech.pmapi.sys.controller;

import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.projectdeclared.manage.DefaultDeclaredProjectManage;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import com.wflow.bean.dto.WflowModelHistorysDto;
import com.wflow.bean.vo.WflowModelVo;
import com.wflow.service.ModelGroupService;
import com.wflow.workflow.service.ProcessModelService;
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 SysProcdefController
 * @Description
 * @Date 2023/1/18 14:28
 * @Author PoffyZhang
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/sys/procdef")
@Api(value = "SysProcdef", tags = "系统管理-流程配置")
@RequiredArgsConstructor
public class SysProcDefController {

    private final ModelGroupService modelGroupService;

    private final ProcessModelService processModelService;

    private final DefaultDeclaredProjectManage defaultDeclaredProjectManage;

    @ApiOperation(value = "系统流程配置列表", notes = "系统流程配置列表")
    @GetMapping("/list")
    public PageVo<WflowModelVo> list(@RequestParam String regionCode, @RequestParam(defaultValue = "1") Integer pageNumber,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return modelGroupService.getModelsPage(regionCode,pageNumber,pageSize);
    }

    @ApiOperation(value = "系统流程配置详情", notes = "系统流程配置详情")
    @GetMapping("/detail/{formId}")
    public Object list(@PathVariable String formId) {
        return modelGroupService.getModelById(formId);
    }

    @ApiOperation(value = "系统流程配置保存", notes = "系统流程配置保存")
    @PostMapping("/save")
    public String save(@Validated @RequestBody WflowModelHistorysDto dto) {
        return processModelService.saveProcess(dto);
    }

    @ApiOperation(value = "系统流程配置启用(停用)", notes = "系统流程配置启用(停用)")
    @PutMapping("/{formId}/active/{state}")
    public String enOrDisModel(@PathVariable String formId,
                               @PathVariable Boolean state) {
        modelGroupService.enOrDisModel(formId, !state);
        return Boolean.TRUE.equals(state) ? "启用流程成功":"停用流程成功";
    }

    @ApiOperation(value = "系统流程配置发布", notes = "系统流程配置发布")
    @PostMapping("/deploy/{formId}")
    public String deploy(@Validated @PathVariable String formId) {
         return processModelService.deployProcess(formId,null);
    }
}
