package com.jy.flow.controller;

import com.jy.flow.model.dto.FlowDefinitionDto;
import jakarta.annotation.Resource;
import org.dromara.warm.flow.core.entity.Definition;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.utils.page.Page;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Collections;

@RestController
@RequestMapping("/flow/definition")
public class DefController {

    @Resource
    private DefService defService;

    /**
     * 分页查询流程定义列表
     */
    @GetMapping("/list")
    public Page<Definition> list(FlowDefinitionDto flowDefinition) {
        return defService.orderByCreateTime().desc()
                .page(flowDefinition, Page.pageOf(flowDefinition.getPageNum(), flowDefinition.getPageSize()));
    }

    /**
     * 获取流程定义详细信息
     */
    @GetMapping(value = "/{id}")
    public Definition getInfo(@PathVariable("id") Long id) {
        return defService.getById(id);
    }

    /**
     * 新增流程定义
     */
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(@RequestBody FlowDefinition flowDefinition) {
        return defService.checkAndSave(flowDefinition);
    }

    /**
     * 修改流程定义
     */
    @PutMapping
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(@RequestBody FlowDefinition flowDefinition) {
        return defService.updateById(flowDefinition);
    }

    /**
     * 删除流程定义
     */
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(@PathVariable(name = "id") Long id) {
        return defService.removeDef(Collections.singletonList(id));
    }

    /**
     * 发布流程定义
     */
    @GetMapping("/publish/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Boolean publish(@PathVariable("id") Long id) {
        return defService.publish(id);
    }

    /**
     * 取消发布流程定义
     */
    @GetMapping("/unPublish/{id}")
    @Transactional(rollbackFor = Exception.class)
    public void unPublish(@PathVariable("id") Long id) {
        defService.unPublish(id);
    }

    /**
     * 激活流程
     */
    @GetMapping("/active/{definitionId}")
    public Boolean active(@PathVariable("definitionId") Long definitionId) {
        return defService.active(definitionId);
    }

    /**
     * 挂起流程
     */
    @GetMapping("/unActive/{definitionId}")
    public Boolean unActive(@PathVariable("definitionId") Long definitionId) {
        return defService.unActive(definitionId);
    }

    /**
     * 保存流程定义
     */
    @PostMapping("/saveXml")
    @Transactional(rollbackFor = Exception.class)
    public void saveXml(@RequestBody FlowDefinition def) throws Exception {
        defService.saveXml(def);
    }

    /**
     * 复制流程定义
     */
    @GetMapping("/copyDef/{id}")
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyDef(@PathVariable("id") Long id) {
        return defService.copyDef(id);
    }

    /**
     * 获取流程xml定义字符串
     */
    @GetMapping("/xmlString/{id}")
    public String xmlString(@PathVariable("id") Long id) {
        return defService.xmlString(id);
    }

    /**
     * 查询流程图
     */
    @GetMapping("/flowChart/{instanceId}")
    public String flowChart(@PathVariable("instanceId") Long instanceId) throws IOException {
        return defService.flowChart(instanceId);
    }

    /**
     * 查询流程图
     */
    @GetMapping("/flowChartNoColor/{definitionId}")
    public String flowChartNoColor(@PathVariable("definitionId") Long definitionId) throws IOException {
        return defService.flowChartNoColor(definitionId);
    }

}
