package org.jeecg.modules.workFlow.flowplus.workflow.controller;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.workFlow.flowplus.workflow.controller.req.ProcessTemplateReq;
import org.jeecg.modules.workFlow.flowplus.workflow.controller.res.TemplateGroupRes;
import org.jeecg.modules.workFlow.flowplus.workflow.dao.InputDao;
import org.jeecg.modules.workFlow.flowplus.workflow.service.SettingService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程模版设置
 *

 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/workflow")
@Api(tags = {"流程模版设置"})
public class SettingController {

    private final SettingService settingService;

    /**
     * 查询所有模版分组
     */
    @GetMapping("/group")
    public Object getTemplateGroups() {
        return settingService.getTemplateGroups();
    }

    /**
     * 查询分组选项
     */
    @GetMapping("/group/options")
    public Result<?> getGroupOptions() {
        return Result.ok(settingService.getGroupOptions());
    }

    /**
     * 模版分组排序
     */
    @PutMapping("/group/sort")
    public Result<?> templateGroupsSort(@RequestBody List<TemplateGroupRes> groups) {
        return Result.ok(settingService.templateGroupsSort(groups));
    }

    /**
     * 修改分组
     */
    @PutMapping("/group")
    public Object updateTemplateGroupName(@RequestBody InputDao inputDao) {
        return settingService.updateTemplateGroupName(inputDao.getId(), inputDao.getName());
    }

    /**
     * 新增分组
     */
    @PostMapping("/group")
    public Object createTemplateGroup(@RequestBody InputDao inputDao) {
        return settingService.createTemplateGroup(inputDao.getName());
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/group")
    public Object deleteTemplateGroup(@RequestParam Long id) {
        return settingService.deleteTemplateGroup(id);
    }

    /**
     * 创建流程模版
     */
    @PostMapping("/template")
    public Result<?> createTemplate(@RequestBody ProcessTemplateReq processTemplateReq) {
        try {
            settingService.createTemplate(processTemplateReq);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 查询流程模版信息
     */
    @GetMapping("/template/detail")
    public Result<?> getTemplateDetail(@RequestParam("templateId") String templateId) {
        try {
            return Result.ok(settingService.getTemplateDetail(templateId));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新流程模版信息
     */
    @PutMapping("/template/update")
    public Result<?> updateTemplateDetail(@RequestBody ProcessTemplateReq processTemplateReq) {
        try {
            settingService.updateTemplate(processTemplateReq);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.ok();
    }
}
