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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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 com.blueland.bpm.application.model.ApplicationEntity;
import com.blueland.bpm.application.service.ApplicationManager;
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.identity.model.User;
/**
 * @author lw
 * @version 1.0
 * @since  
 */
 















import java.util.*;

import com.blueland.bpm.engine.config.model.*;
import com.blueland.bpm.engine.config.service.*;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.manage.util.BaseUser;
import com.blueland.bpm.manage.util.MyContants;

@Controller
@RequestMapping(value = "msgRemind")
public class MsgRemindController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	@Autowired
	private MsgRemindManager msgRemindManager;
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	@Autowired
	private ApplicationManager applicationManager;
	
	private final String LIST_ACTION = "redirect:/msgRemind/list.do";
	
	public MsgRemindController() {
	}
	

	/**
	 * 增加了@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,MsgRemind query) {
		ModelAndView result = new ModelAndView("/msgRemind/list");
		
		try {
			User user = BaseUser.getInstance().getSessionUser(request);
			List<ApplicationEntity> apps = applicationManager.selectByUserId(user.getUserId());
			result.addObject("apps", apps);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Page page = this.msgRemindManager.findPage(query);
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(HttpServletRequest request)  {
		ModelAndView result = new ModelAndView("/msgRemind/show");
		String id = new String(request.getParameter("remindId"));
		MsgRemind msgRemind = (MsgRemind)msgRemindManager.getById(id);
		if(null!=msgRemind){
			if(null!=msgRemind.getRemindProcessDefKey()){
				ProcessDefinitionModel processDefinition =
						processDefinitionService.getProcessDef("DEMO_SYS_SUB:1:033d25ef-d1f1-11e4-9ddb-28d24474b3a8");
				result.addObject("procdefname", processDefinition.getProcDefName());
			}
			if(null!=msgRemind.getRemindAppId()){
//				ApplicationEntity app = applicationManager.getById(msgRemind.getRemindAppId());
//				System.out.println(app.getAppName());
			}
			
		}
		result.addObject("msgRemind",msgRemind);
		return result;
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,MsgRemind msgRemind)  {
		ModelAndView result = new ModelAndView("/msgRemind/create");
		try {
			User user = BaseUser.getInstance().getSessionUser(request);
			List<ApplicationEntity> apps = applicationManager.selectByUserId(user.getUserId());
			result.addObject("apps", apps);
			result.addObject("msgRemind", msgRemind);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	public ModelAndView save(HttpServletRequest request,MsgRemind msgRemind,RedirectAttributes redirectAttributes)  {
		HttpSession session=request.getSession(false);
		User user = (User)session.getAttribute(MyContants.LOGIN_SESSION_USER);
		if(null!=user){
			msgRemind.setRemindUserId(user.getUserId());
		}
		//msgRemind.setRemindAppId(BaseUser.getInstance().getLoginUserAppId(request));
		msgRemindManager.save(msgRemind);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, CREATED_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "saveMsgRemind")
	@ResponseBody
	public String saveMsgRemind(HttpServletRequest request,MsgRemind msgRemind,RedirectAttributes redirectAttributes)  {
		HttpSession session=request.getSession(false);
		User user = (User)session.getAttribute(MyContants.LOGIN_SESSION_USER);
		if(null!=user){
			msgRemind.setRemindUserId(user.getUserId());
		}
		//msgRemind.setRemindAppId(BaseUser.getInstance().getLoginUserAppId(request));
		msgRemindManager.save(msgRemind);
		return ACTION_SUCCESS;
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request)  {
		ModelAndView result = new ModelAndView("/msgRemind/edit");
		String id = new String(request.getParameter("remindId"));
		MsgRemind msgRemind = (MsgRemind)msgRemindManager.getById(id);
		if(null!=msgRemind){
			if(null!=msgRemind.getRemindProcessDefKey()){
				ProcessDefinitionModel processDefinition =
						processDefinitionService.getProcessDef(
						msgRemind.getRemindProcessDefKey());
				result.addObject("procDefName", processDefinition.getProcDefName());
			}
		}
		
		try {
			User user = BaseUser.getInstance().getSessionUser(request);
			List<ApplicationEntity> apps = applicationManager.selectByUserId(user.getUserId());
			result.addObject("apps", apps);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		result.addObject("msgRemind", msgRemind);
		return result;
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	public ModelAndView update(HttpServletRequest request,MsgRemind msgRemind,RedirectAttributes redirectAttributes)  {
		String id = new String(request.getParameter("remindId"));
		msgRemindManager.update(msgRemind);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "updateMsgRemind")
	@ResponseBody
	public String updateMsgRemind(HttpServletRequest request,MsgRemind msgRemind,RedirectAttributes redirectAttributes)  {
		String id = new String(request.getParameter("remindId"));
		msgRemindManager.update(msgRemind);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, UPDATE_SUCCESS);
		return ACTION_SUCCESS;
	}
	
	/**
	 *删除对象
	 **/
	@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("remindId"));
			
			msgRemindManager.removeById(id);
		}
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

