package com.blueland.bpm.manage.config.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.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.engine.config.model.ActivityInfoConfig;
import com.blueland.bpm.engine.config.model.ActivityOperatePower;
import com.blueland.bpm.engine.config.service.ActivityOperatePowerManager;
import com.blueland.bpm.manage.util.MyContants;
/**
 * @author lw
 * @version 1.0
 * @since  
 */

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

	/**
	 * 增加了@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,ActivityOperatePower query,ActivityInfoConfig params) {
		query.setTenantId((String)request.getSession().getAttribute(MyContants.APP_ID_KEY));
		Page page = this.activityOperatePowerManager.findTenantOperatePower(query);
		ModelAndView result = new ModelAndView("/actInfo/actOperatePower/list");
		result.addAllObjects(toModelMap(page, query));
		result.addObject("activityInfoConfig",params);
		this.saveQueryParams(request, query);
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "show")
	public ModelAndView show(HttpServletRequest request)  {
		String id = new String(request.getParameter("opId"));
		ActivityOperatePower activityOperatePower = (ActivityOperatePower)activityOperatePowerManager.getById(id);
		return new ModelAndView("/actInfo/actOperatePower/show","activityOperatePower",activityOperatePower);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = "create")
	public ModelAndView create(HttpServletRequest request,ActivityOperatePower activityOperatePower)  {
		return new ModelAndView("/actInfo/actOperatePower/create","activityOperatePower",activityOperatePower);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(HttpServletRequest request,ActivityOperatePower activityOperatePower,RedirectAttributes redirectAttributes)  {
		ActivityOperatePower power = activityOperatePowerManager.getById(activityOperatePower.getOpId());				
		if(power != null) {
			ApplicationEntity application = (ApplicationEntity)applicationManager.getById(power.getTenantId());//通过从power里面得到的系统ID得到系统对象
			String appName = application.getAppName();//获取系统名称
			return "环节权限ID已被"+appName+"系统占用，请您重新输入！";
		}else {
			activityOperatePower.setTenantId((String)request.getSession().getAttribute(MyContants.APP_ID_KEY));
			activityOperatePowerManager.save(activityOperatePower);
			return CREATED_SUCCESS;
		}
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request)  {
		String id = new String(request.getParameter("opId"));
		ActivityOperatePower activityOperatePower = (ActivityOperatePower)activityOperatePowerManager.getById(id);
		return new ModelAndView("/actInfo/actOperatePower/edit","activityOperatePower",activityOperatePower);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	public ModelAndView update(HttpServletRequest request,ActivityOperatePower activityOperatePower,RedirectAttributes redirectAttributes)  {
		String id = new String(request.getParameter("opId"));
		activityOperatePower.setTenantId((String)request.getSession().getAttribute(MyContants.APP_ID_KEY));
		activityOperatePowerManager.update(activityOperatePower);
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = "delete")
	public ModelAndView delete(HttpServletRequest request,RedirectAttributes redirectAttributes) {
		String[] opIds = request.getParameterValues("opIds");
		for(int i = 0; i < opIds.length; i++) {
			if(StringUtils.isNotEmpty(opIds[i])){
				activityOperatePowerManager.removeById(opIds[i]);
			}
		}
		redirectAttributes.addFlashAttribute(ACTION_MESSAGE, DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

