package com.blueland.bpm.manage.web;

import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.common.util.HttpUtils;
import com.blueland.bpm.engine.config.model.Depute;
import com.blueland.bpm.engine.config.service.DeputeManager;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.model.User;
import com.blueland.bpm.manage.util.BaseUser;
import com.blueland.bpm.manage.util.MyContants;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

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

@Controller
@RequestMapping(value = "depute")
public class DeputeController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	@Autowired
	private DeputeManager deputeManager;
	@Autowired
	private UserService	userService;
	private final String LIST_ACTION = "redirect:/depute/list.do";
	
	public DeputeController() {
	}
	

	/**
	 * 增加了@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,Depute query) {
		query.setSortColumns("START_TIME DESC");		
		Page page = this.deputeManager.findPage(query);
		ModelAndView result = new ModelAndView("/depute/list");
		result.addAllObjects(toModelMap(page, query));		
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(HttpServletRequest request)  {
		String id = new String(request.getParameter("deputeId"));
		Depute depute = (Depute)deputeManager.getById(id);
		return new ModelAndView("/depute/show","depute",depute);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,Depute depute,String userId)  {
		User user=BaseUser.getInstance().backSSO(request,response, userId);
		depute.setDeputeUserId(user.getUserCode());
		ModelAndView result = new ModelAndView("/depute/create");
		Page page = this.deputeManager.findPage(depute);
		//展示委托记录
//		String deputeStr = "";
		//委托信息，一个委托人只会有一个委托人
		Depute existDepute = null ;
		for(int i = 0; i<page.getResult().size(); i++){
			existDepute = (Depute)page.getResult().get(i);
//			if(existDepute.getDeputeType().equals("golal")){
//				deputeStr = deputeStr+"所有流程"; 
//			}
//			else{
//				if(page.getResult().size()>1 && i < page.getResult().size()-1){
//					deputeStr = deputeStr + existDepute.getProcessDefName() +"、";
//				}else{
//					deputeStr = deputeStr + existDepute.getProcessDefName() ;
//				}
//				
//			}
		} 
		result.addObject("user",user);
		result.addObject("existDepute",existDepute);
//		result.addObject("deputeStr",deputeStr);
		return result;
	}
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "backCreate")
	public ModelAndView backCreate(HttpServletRequest request,Depute depute,String userId)  {
		User user = userService.getUserById(userId);
		depute.setDeputeUserId(userId);
		ModelAndView result = new ModelAndView("/depute/createBack");
		Page page = this.deputeManager.findPage(depute);
		//展示委托记录
//		String deputeStr = "";
		//委托信息，一个委托人只会有一个委托人
		Depute existDepute = null ;
		for(int i = 0; i<page.getResult().size(); i++){
			existDepute = (Depute)page.getResult().get(i);
//			if(existDepute.getDeputeType().equals("golal")){
//				deputeStr = deputeStr+"所有流程"; 
//			}
//			else{
//				if(page.getResult().size()>1 && i < page.getResult().size()-1){
//					deputeStr = deputeStr + existDepute.getProcessDefName() +"、";
//				}else{
//					deputeStr = deputeStr + existDepute.getProcessDefName() ;
//				}
//				
//			}
		} 
		result.addObject("user",user);
		result.addObject("existDepute",existDepute);
//		result.addObject("deputeStr",deputeStr);
		return result;
	}
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	@ResponseBody
	public Map save(HttpServletRequest request,Depute depute,String processAllCheck,RedirectAttributes redirectAttributes)  {
		Map result = new HashMap();
		result.put("success",true);		
		if(StringUtils.isNotEmpty(processAllCheck)){
			depute.setDeputeType(processAllCheck);
		}
		try {
			deputeManager.delAndsave(depute);
		} catch (Exception e) {
			result.put("success",false);
		}
		return result;
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request)  {
		String id = new String(request.getParameter("deputeId"));
		Depute depute = (Depute)deputeManager.getById(id);
		User loginUser = (User)request.getSession().getAttribute(MyContants.LOGIN_SESSION_USER);
		String userId = loginUser.getOrgId();
		ModelAndView result = new ModelAndView("/depute/edit");
		result.addObject("userId",userId);
		result.addObject("depute",depute);
		return result;		
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	@ResponseBody
	public Map update(HttpServletRequest request,Depute depute,RedirectAttributes redirectAttributes)  {
		Map result = new HashMap();
		result.put("success",true);
		try {
			String id = new String(request.getParameter("deputeId"));
			deputeManager.update(depute);
		} catch (Exception e) {
			result.put("success",false);
		}
		return result;
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = "delete")
	public ModelAndView delete(HttpServletRequest request,RedirectAttributes redirectAttributes) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			String id = new String((String)params.get("deputeId"));
			
			deputeManager.removeById(id);
		}
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	/**
	 *重置委托
	 **/
	@RequestMapping(value = "reSetDepute")
	@ResponseBody
	public Map reSetDepute(HttpServletRequest request,RedirectAttributes redirectAttributes) {
		Map result = new HashMap();
		result.put("success",true);
		try {
			User loginUser = (User)request.getSession().getAttribute(MyContants.LOGIN_SESSION_USER);
			Depute depute = new Depute();
			depute.setDeputeUserId(loginUser.getUserId());
			//重置该委托人的全部委托信息
			List<Depute> existDeputeList = deputeManager.findByExample(depute);
			for(Depute existDepute: existDeputeList){
				if(null != existDepute){
					deputeManager.removeById(existDepute.getDeputeId());				
				}
			}
		}catch (Exception e) {
			result.put("success",false);
		}
		return result;
	}
	
	/**
	 *重置委托
	 **/
	@RequestMapping(value = "reSetDeputeBack")
	@ResponseBody
	public Map reSetDeputeBack(HttpServletRequest request,String deputeUserId) {
		Map result = new HashMap();
		result.put("success",true);
		try {
			Depute depute = new Depute();
			depute.setDeputeUserId(deputeUserId);
			//重置该委托人的全部委托信息
			List<Depute> existDeputeList = deputeManager.findByExample(depute);
			for(Depute existDepute: existDeputeList){
				if(null != existDepute){
					deputeManager.removeById(existDepute.getDeputeId());				
				}
			}
		}catch (Exception e) {
			result.put("success",false);
		}
		return result;
	}
}

