package com.yihu.hos.system.controller;

import com.yihu.hos.system.model.SystemServiceFlowProcessor;
import com.yihu.hos.system.service.ProcessorManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

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

    @Autowired
    private ProcessorManager processorManager;

    @RequestMapping("/initial")
    public String processInitial(Model model) {
        model.addAttribute("contentPage", "system/processor/processor");
        return "partView";
    }

    @RequestMapping("/getProcessorList")
    @ResponseBody
    public Result getProcessorList(HttpServletRequest request,String name) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);

            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            Result result = processorManager.getProcessorList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }

    }

    @RequestMapping("/editorProcessor")
    public String editorAppServicePage(Model model, String id) {
        try {
            SystemServiceFlowProcessor serviceFlowProcessor = null;
            if (id != null && id.length() > 0) {
                serviceFlowProcessor = processorManager.getProcessorById(id);
            }  else {
                serviceFlowProcessor = new SystemServiceFlowProcessor();
            }
            model.addAttribute("model", serviceFlowProcessor);
            model.addAttribute("contentPage", "/system/processor/editorProcessor");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("/processorDetail")
    public String appServiceDetail(Model model, String id) {
        try {
            SystemServiceFlowProcessor processor = null;
            if (id != null && id.length() > 0) {
                processor = processorManager.getProcessorById(id);
            }  else {
                processor = new SystemServiceFlowProcessor();
            }
            model.addAttribute("model", processor);
            model.addAttribute("contentPage", "/system/processor/processorDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("addProcessor")
    @ResponseBody
    public Result addProcessor(HttpServletRequest request) {
        try {
            SystemServiceFlowProcessor obj = new SystemServiceFlowProcessor();
            BeanUtils.populate(obj, request.getParameterMap());
            return processorManager.addProcessor(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    @RequestMapping("/deleteProcessor")
    @ResponseBody
    public Result deleteProcessor(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");
            processorManager.deleteProcessor(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    @RequestMapping("updateProcessor")
    @ResponseBody
    public Result updateProcessor(HttpServletRequest request) {
        try {
            SystemServiceFlowProcessor obj = new SystemServiceFlowProcessor();
            BeanUtils.populate(obj, request.getParameterMap());

            return processorManager.updateProcessor(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }
}
