package com.yihu.hos.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.system.model.SystemServiceFlow;
import com.yihu.hos.system.model.SystemServiceFlowClass;
import com.yihu.hos.system.service.FlowManager;
import com.yihu.hos.system.service.intf.IFlowManage;
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.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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Resource(name = FlowManager.BEAN_ID)
    private IFlowManage flowManage;

    /**
     *  流程管理管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String appInitial(Model model) {
        model.addAttribute("contentPage", "system/flow/flow");
        return "partView";
    }

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

            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 = flowManage.getFlowList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 流程管理-新增/修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editorFlow")
    public String editorFlow(Model model, String id) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SystemServiceFlow flow = null;
            List<SystemServiceFlowClass> flowClassList = null;
            if (id != null && id.length() > 0) {
                flow = flowManage.getFlowById(Integer.parseInt(id));
                flowClassList = flowManage.getFlowClassByFlowId(flow.getId());
            }  else {
                flow = new SystemServiceFlow();
                flowClassList = new ArrayList<>();
            }
            flow.setFlowClassArray(flowClassList);
            model.addAttribute("model", flow);
            model.addAttribute("flowClass", objectMapper.writeValueAsString(flowClassList));
            model.addAttribute("contentPage", "/system/flow/editorFlow");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增流程信息
     * @param request
     * @return
     */
    @RequestMapping("addFlow")
    @ResponseBody
    public Result addFlow(HttpServletRequest request) {
        try {
            SystemServiceFlow obj = new SystemServiceFlow();
            BeanUtils.populate(obj, request.getParameterMap());

            List<SystemServiceFlowClass> flowClass = new ArrayList<>();
            BeanUtils.populate(flowClass, request.getParameterMap());
            obj.setFlowClassArray(flowClass);
            return flowManage.addFlow(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除流程信息
     * @param request
     * @return
     */
    @RequestMapping("/deleteFlow")
    @ResponseBody
    public Result deleteFlow(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");
            if (id != null && id.length() > 0) {
                flowManage.deleteFlow(Integer.parseInt(id));
                return Result.success("删除成功！");
            }else {
                return Result.error("删除失败,该流程已不存在！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 修改流程信息
     */
    @RequestMapping("updateFlow")
    @ResponseBody
    public Result updateFlow(HttpServletRequest request,String flowClass) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            SystemServiceFlow flow = objectMapper.readValue(flowClass,SystemServiceFlow.class);
            return flowManage.updateFlow(flow);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }


    /**
     * 文件上传
     * @param file
     * @param session
     * @return
     */
    @RequestMapping(value = "/upload", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(@RequestParam MultipartFile file,HttpSession session
    ) {
        //TODO 文件地址设置
        String basePath = getServletContext().getRealPath("/") + "upload";
        String path = flowManage.uploadFile(file, basePath);
        if (StringUtils.isEmpty(path)){
            return Result.error("上传失败");
        }else {
            return Result.success(path);
        }
    }



}
