package com.kingdee.eas.hr.project.handler;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.project.IProjectManagement;
import com.kingdee.eas.hr.project.ProjectManagementFactory;
import com.kingdee.eas.hr.project.ProjectManagementInfo;
import com.kingdee.eas.hr.project.projectState;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.common.util.StringUtils;
import com.kingdee.util.DateTimeUtils;

public class ProjectManagementEditHandler extends EditHandler{
	private static final Logger logger = LoggerFactory.getLogger(ProjectManagementEditHandler.class);
	
	/**
	 * 初始化
	 */
	protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response,
			CoreBaseInfo coreBaseInfo) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		Date nowDate = DateTimeUtils.truncateDate(new Date());
		coreBaseInfo.put("applyDate", nowDate);
		coreBaseInfo.put("HrOrgUnit", ContextUtil.getCurrentHRUnit(ctx));
		super.afterCreateNewModel(request, response, coreBaseInfo);
	}
	
	
	/**
	 * 启用
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws SHRWebException
	 */
	public void enablePorjectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billId = request.getParameter("billId");
		Map<String, Object> result = new HashMap();
		if (!StringUtils.isEmpty(billId)) {
			try {
				IProjectManagement iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
				ProjectManagementInfo getProject = iProjectManagement
						.getProjectManagementInfo("where id = '" + billId + "'");
				if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 0
						&& getProject.getProjectState().getValue() != 3) {
					result.put("state", "error");
					result.put("errorMsg", "只能启用“未启用”、“禁用”的项目！");
				} else {
					getProject.setProjectState(projectState.INPROGRESS);
					iProjectManagement.update(new ObjectUuidPK(billId), getProject);
					result.put("state", "success");
				}
			} catch (Exception var9) {
				logger.error(var9.getMessage(), var9);
				throw new ShrWebBizException("启用项目失败！", var9);
			}
		} else {
			result.put("state", "error");
			result.put("errorMsg", "数据异常，billId为空！");
		}

		JSONUtils.writeJson(response, result);
	}

	@Override
	public String deleteAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		// TODO 自动生成的方法存根
		return super.deleteAction(request, response, modelMap);
	}
	/**
	 * 禁用
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws SHRWebException
	 */
	public void disablePorjectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billId = request.getParameter("billId");
		Map<String, Object> result = new HashMap();
		if (!StringUtils.isEmpty(billId)) {
			try {
				IProjectManagement iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
				ProjectManagementInfo getProject = iProjectManagement
						.getProjectManagementInfo("where id = '" + billId + "'");
				if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 1) {
					result.put("state", "error");
					result.put("errorMsg", "只能禁用“已启用的”的项目！");
				} else {
					getProject.setProjectState(projectState.DISABLES);
					iProjectManagement.update(new ObjectUuidPK(billId), getProject);
					result.put("state", "success");
				}
			} catch (Exception var9) {
				logger.error(var9.getMessage(), var9);
				throw new ShrWebBizException("禁用项目失败！", var9);
			}
		} else {
			result.put("state", "error");
			result.put("errorMsg", "数据异常，billId为空！");
		}

		JSONUtils.writeJson(response, result);
	}
}
