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

import java.util.List;

import javax.annotation.Resource;
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.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.common.base.BaseSpringController;
import com.blueland.bpm.engine.config.model.ActivityInfoConfig;
import com.blueland.bpm.engine.config.model.NoticeRemindConfig;
import com.blueland.bpm.engine.config.model.ProcessInfoConfig;
import com.blueland.bpm.engine.config.service.ActivityInfoConfigManager;
import com.blueland.bpm.engine.config.service.NoticeRemindConfigService;
import com.blueland.bpm.engine.config.service.ProcessInfoConfigManager;
import com.blueland.bpm.identity.model.User;
import com.blueland.bpm.manage.util.BaseUser;

/**
 * @author ly
 *
 */
@Controller
@RequestMapping(value = "noticeRemindConfig")
public class NoticeRemindConfigController extends BaseSpringController {

	@Resource(name = "noticeRemindConfigService")
	private NoticeRemindConfigService noticeRemindConfigService;
	@Autowired
	private ProcessInfoConfigManager processInfoConfigManager;
	@Autowired
	private ActivityInfoConfigManager activityInfoConfigManager;

	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = "edit")
	public ModelAndView edit(HttpServletRequest request, String procDefId) {
		NoticeRemindConfig noticeRemindConfig = null;
		if (StringUtils.isNotEmpty(procDefId)) {
			ProcessInfoConfig processInfoConfig = (ProcessInfoConfig) processInfoConfigManager.getById(procDefId);
			noticeRemindConfig = noticeRemindConfigService.getById(procDefId);
			if (null == noticeRemindConfig) {
				noticeRemindConfig = new NoticeRemindConfig();
				noticeRemindConfig.setProcDefId(procDefId);
			}
			noticeRemindConfig.setProcDefKey(processInfoConfig.getProcessDefKey());
		}
		return new ModelAndView("/procDefMgr/noticeRemindConfig/edit", "noticeRemindConfig", noticeRemindConfig);
	}

	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "update")
	@ResponseBody
	public String update(HttpServletRequest request, NoticeRemindConfig noticeRemindConfig,
			RedirectAttributes redirectAttributes) {
		if (null == noticeRemindConfig || StringUtils.isEmpty(noticeRemindConfig.getProcDefId())) {
			return ACTION_ERROR;
		}
		User user = null;
		try {
			user = BaseUser.getInstance().getSessionUser(request);
		} catch (Exception e) {
			return ACTION_ERROR;
		}
		noticeRemindConfig.setCretaeUserId(user.getUserId());
		noticeRemindConfig.setUpdateUserId(user.getUserId());
		NoticeRemindConfig old = noticeRemindConfigService.getById(noticeRemindConfig.getProcDefId());
		if (old == null) {
			noticeRemindConfigService.save(noticeRemindConfig);
		} else {
			noticeRemindConfigService.update(noticeRemindConfig);
		}
		return ACTION_SUCCESS;
	}

	/**
	 * 获取节点列表
	 * 
	 * @param processDefId
	 * @return
	 */
	@RequestMapping(value = "selectActInfoConfigs")
	public ModelAndView selectActInfoConfigs(String processDefId) {
		ModelAndView modelAndView = new ModelAndView("/procDefMgr/noticeRemindConfig/selectActInfoConfigs");
		List<ActivityInfoConfig> actInfoConfigs = activityInfoConfigManager.findActivityInfoConfigs(processDefId);
		modelAndView.addObject("actInfoConfigs", actInfoConfigs);
		return modelAndView;
	}

	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(HttpServletRequest request, String procDefId, RedirectAttributes redirectAttributes) {
		NoticeRemindConfig noticeRemindConfig = noticeRemindConfigService.getById(procDefId);
		if(null == noticeRemindConfig){
			return ACTION_SUCCESS;
		}
		try {
			noticeRemindConfigService.removeById(procDefId);
		} catch (Exception e) {
			return ACTION_ERROR;
		}
		return ACTION_SUCCESS;
	}

}
