package com.blueland.bpm.manage.web;

import javax.servlet.http.HttpServletRequest;

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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.blueland.bpm.common.base.BaseSpringController;
import com.blueland.bpm.common.page.Page;
import com.blueland.bpm.common.util.HttpUtils;
/**
 * @author lw
 * @version 1.0
 * @since  
 */
 
import java.util.*;

import com.blueland.bpm.engine.config.model.SmsConfigLog;
import com.blueland.bpm.engine.config.service.SmsConfigLogService;
import com.blueland.bpm.manage.util.BaseUser;

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

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		//model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value = "list")
	public ModelAndView list(SmsConfigLog query,HttpServletRequest request) {
		try {
			if(!BaseUser.isEbpmAdmin(BaseUser.getInstance().getSessionUser(request))||StringUtils.isEmpty(query.getAppId())){
				String loginUserAppId = BaseUser.getInstance().getLoginUserAppId(request);
				query.setAppId(loginUserAppId);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(query.getStartTime() == null ) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);  
		    cal.set(Calendar.SECOND, 0);  
		    cal.set(Calendar.MINUTE, 0);  
		    cal.set(Calendar.MILLISECOND, 0);
		    query.setEndTime(cal.getTime());
		    cal.add(Calendar.DATE, -1);
			query.setStartTime(cal.getTime());
		}
		query.setSortColumns("CREATE_TIME desc");
		Page page = this.smsConfigLogService.findPage(query);
		ModelAndView result = new ModelAndView("/smsConfigLog/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(String logid,HttpServletRequest request)  {
		SmsConfigLog smsConfigLog = (SmsConfigLog)smsConfigLogService.getById(logid);
		return new ModelAndView("/smsConfigLog/show","smsConfigLog",smsConfigLog);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(SmsConfigLog smsConfigLog,HttpServletRequest request)  {
		return new ModelAndView("/smsConfigLog/create","smsConfigLog",smsConfigLog);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	public ModelAndView save(SmsConfigLog smsConfigLog,HttpServletRequest request,RedirectAttributes redirectAttributes)  {
		smsConfigLogService.save(smsConfigLog);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, CREATED_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(String logid,HttpServletRequest request)  {
		SmsConfigLog smsConfigLog = (SmsConfigLog)smsConfigLogService.getById(logid);
		return new ModelAndView("/smsConfigLog/edit","smsConfigLog",smsConfigLog);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	public ModelAndView update(SmsConfigLog smsConfigLog,HttpServletRequest request,RedirectAttributes redirectAttributes)  {
		String id = request.getParameter("logid");
		smsConfigLogService.update(smsConfigLog);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@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("logid"));
			
			smsConfigLogService.removeById(id);
		}
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

