package com.yihu.hos.system.controller;

import com.yihu.hos.system.service.ProcessManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *  流程管理
 * @author HZY
 * @vsrsion 1.0
 * Created at 2016/8/12.
 */
@RequestMapping("/process")
@Controller
public class ProcessController  extends BaseController {

    @Autowired
    private ProcessManager processManager;
    /**
     *  流程管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String processInitial(Model model) {
        model.addAttribute("contentPage", "system/process/process");
        return "partView";
    }

    @RequestMapping(value = "/getAllApp", method = RequestMethod.GET)
    public Result getAllApp() {
        try {
            return processManager.getAllApp();
        } catch (Exception e) {
            return Result.error("查找所有应用失败");
        }
    }

    @RequestMapping(value = "/getAllAppService", method = RequestMethod.GET)
    public Result getAllAppService() {
        try {
            return processManager.getAllAppService();
        } catch (Exception e) {
            return Result.error("查找所有服务失败");
        }
    }

    @RequestMapping(value = "/getAppService", method = RequestMethod.GET)
    public Result getAppService(String appId) {
        try {
            return processManager.getAppServiceByAppId(appId);
        } catch (Exception e) {
            return Result.error("查找服务失败");
        }
    }

    @RequestMapping(value = "/json", method = RequestMethod.POST)
    public Result formatJson(String code, String name, String positionJson, String flowJson) {
        try {
            String fileName = processManager.formatJson(flowJson);
            processManager.saveProcess(code, name, fileName, positionJson);

            return Result.error("生成模板成功");
        } catch (Exception e) {
            return Result.error("生成模板失败");
        }
    }
}
