package com.blueland.bpm.manage.config.web;

import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.engine.config.model.SysGlType;
import com.blueland.bpm.engine.config.service.SysGlTypeManager;
import com.blueland.bpm.manage.util.MyContants;
import com.blueland.bpm.manage.util.TreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lw
 * @version 1.0
 * @since  
 */


@Controller
@RequestMapping(value = "sysGlType")
public class SysGlTypeController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	@Autowired
	private SysGlTypeManager sysGlTypeManager;
	@Autowired
	SysGlTypeTreeManager sysGlTypeTreeManager;
	private final String LIST_ACTION = "redirect:/sysGlType/list.do";
	
	public SysGlTypeController() {
	}
	

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		//model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "list")
	public ModelAndView list(HttpServletRequest request,SysGlType query) {
		Page page = this.sysGlTypeManager.findPage(query);
		ModelAndView result = new ModelAndView("/sysGlType/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,SysGlType sysGlType)  {
		SysGlType parentSysGlType =null;
		if (StringUtils.isNotEmpty(sysGlType.getTypeParentId())){
			SysGlType param = new SysGlType();
			param.setTypeId(sysGlType.getTypeParentId());;
			parentSysGlType = sysGlTypeManager.getByEntity(param);
		}
		if(parentSysGlType==null){
			parentSysGlType=sysGlType;
		}
		return new ModelAndView("/sysGlType/create","sysGlType",parentSysGlType);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(HttpServletRequest request,SysGlType sysGlType,RedirectAttributes redirectAttributes)  {
		if(null!=sysGlType && null!=sysGlType.getTypeId() && !"".equals(sysGlType.getTypeId())){
		SysGlType result = sysGlTypeManager.getById(sysGlType.getTypeId());
		   if(result != null){
			  return "["+sysGlType.getAppKey()+"]"+"应用系统已存在分类ID为["+sysGlType.getTypeId()+"]的分类，请您重新输入分类ID!";			   
		   }
		}
		sysGlTypeManager.save(sysGlType);
		return this.ACTION_SUCCESS;
		
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request,String typeId)  {
		SysGlType sysGlType =null;
		if (StringUtils.isEmpty(typeId)){
			SysGlType param = new SysGlType();
			param.setTypeParentId("NONE");
			sysGlType = sysGlTypeManager.getByEntity(param);
		}else{
			sysGlType = sysGlTypeManager.getById(typeId);
		}
		return new ModelAndView("/sysGlType/edit","sysGlType",sysGlType);
	}
	
	/**
	 * 进入详细页面
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(HttpServletRequest request,String typeId)  {
		SysGlType sysGlType =null;
		if (StringUtils.isEmpty(typeId)){
			SysGlType param = new SysGlType();
			param.setTypeParentId("NONE");
			sysGlType = sysGlTypeManager.getByEntity(param);
		}else{
			sysGlType = sysGlTypeManager.getById(typeId);
		}
		return new ModelAndView("/sysGlType/show","sysGlType",sysGlType);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	@ResponseBody
	public String update(HttpServletRequest request,SysGlType sysGlType,RedirectAttributes redirectAttributes)  {
		String id = new String(request.getParameter("typeId"));
		sysGlTypeManager.update(sysGlType);
		return this.ACTION_SUCCESS;
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(HttpServletRequest request,String typeId) {
		
		sysGlTypeManager.removeById(typeId);
		MyContants.saveUserOperate(request, "typeId", true, "删除分类成功",
				MyContants.LOGIN_SESSION_USER,MyContants.APPLICATION);
		return this.ACTION_SUCCESS;
	}
	/**
	 *获取流程类型树
	 * @throws Exception 
	 **/
	@RequestMapping(value="geWfTypeTree",method=RequestMethod.POST)
	@ResponseBody 
	public List<TreeNode> geWfTypeTree(HttpServletRequest request,String filterNodeIdsStr) throws Exception {
		String typeId=request.getParameter("id");
		System.out.println(typeId);
		List<TreeNode> treeNodes=sysGlTypeTreeManager.getTypeTree(typeId, (String)request.getSession(false).getAttribute("app_id"));
		String[] filterNodeIds=null;
		if(StringUtils.isNotEmpty(filterNodeIdsStr)){
			if(filterNodeIdsStr.indexOf(",")!=-1){
				filterNodeIds=filterNodeIdsStr.split(",");
			}else{
				filterNodeIds=new String[]{filterNodeIdsStr};
			}
			for (String filterNodeId : filterNodeIds) {
				TreeNode treeNode=new TreeNode();
				treeNode.setId(filterNodeId);
				treeNodes.remove(treeNode);
			}
		}
		return treeNodes;
	}
	
}

