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


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

import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.project.IProjectManagement;
import com.kingdee.eas.hr.project.ProjectManagementCollection;
import com.kingdee.eas.hr.project.ProjectManagementFactory;
import com.kingdee.eas.hr.project.ProjectManagementInfo;
import com.kingdee.eas.hr.project.projectState;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsBody;
import com.kingdee.shr.base.syssetting.api.bean.BatchMessageTipsHeader;
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.util.WebConvertUtils;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.shr.recuritment.CustomMenuDetailFactory;
import com.kingdee.util.StringUtils;

public class ProjectManagementListHandler  extends ListHandler {
	

	private static Logger logger = Logger.getLogger(ProjectManagementListHandler.class);
	
	public void enablePorjectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billIds = request.getParameter("billId");
		BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
		int success = 0;
		int failure = 0;
		String billId = "";
		if (!StringUtils.isEmpty(billIds)) {
			IProjectManagement iProjectManagement;
			ProjectManagementCollection coll;
			try {
				iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
				coll = iProjectManagement.getProjectManagementCollection("billIds");
			} catch (BOSException var17) {
				logger.error(var17.getMessage(), var17);
				throw new ShrWebBizException("数据异常！获取项目异常");
			}

			int i = 0;

			for (int length = coll.size(); i < length; ++i) {
				ProjectManagementInfo getProject = coll.get(i);
				billId = getProject.getId().toString();
				BatchMessageTipsBody body = new BatchMessageTipsBody();
				body.setId(billId);

				try {
					if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 0
							&& getProject.getProjectState().getValue() != 3) {
						++failure;
						body.setMuitTipsState(Boolean.FALSE);
						body.setMuitTipsMessage("只能启用“未启用”、“禁用”的项目！");
						batchMessageTipsHeader.addResult(body);
					} else {
						getProject.setProjectState(projectState.INPROGRESS);
						iProjectManagement.update(new ObjectUuidPK(billId), getProject);
						++success;
						body.setMuitTipsState(Boolean.TRUE);
						body.setMuitTipsMessage("启用成功！");
						batchMessageTipsHeader.addResult(body);
					}
				} catch (EASBizException var18) {
					logger.error(var18.getMessage(), var18);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("启用失败！" + var18.getMessage());
					batchMessageTipsHeader.addResult(body);
				} catch (BOSException var19) {
					logger.error(var19.getMessage(), var19);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("启用失败！" + var19.getMessage());
					batchMessageTipsHeader.addResult(body);
				}
			}
		}

		batchMessageTipsHeader.setBillId(billIds);
		batchMessageTipsHeader.setFailureCount(failure);
		batchMessageTipsHeader.setSuccessCount(success);
		this.writeSuccessData(batchMessageTipsHeader);
	}

	public void disableProjectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billIds = request.getParameter("billId");
		BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
		int success = 0;
		int failure = 0;
		String billId = "";
		if (!StringUtils.isEmpty(billIds)) {
			IProjectManagement iProjectManagement;
			ProjectManagementCollection coll;
			try {
				iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
				coll = this.getProject(ctx, billIds, iProjectManagement);
			} catch (BOSException var17) {
				logger.error(var17.getMessage(), var17);
				throw new ShrWebBizException("数据异常！获取项目异常");
			}

			int i = 0;

			for (int length = coll.size(); i < length; ++i) {
				ProjectManagementInfo getProject = coll.get(i);
				billId = getProject.getId().toString();
				BatchMessageTipsBody body = new BatchMessageTipsBody();
				body.setId(billId);

				try {
					if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 1) {
						++failure;
						body.setMuitTipsState(Boolean.FALSE);
						body.setMuitTipsMessage("只能禁用“进行中”的项目！");
						batchMessageTipsHeader.addResult(body);
					} else {
						getProject.setProjectState(projectState.DISABLES);
						iProjectManagement.update(new ObjectUuidPK(billId), getProject);
						++success;
						body.setMuitTipsState(Boolean.TRUE);
						body.setMuitTipsMessage("禁用成功！");
						batchMessageTipsHeader.addResult(body);
					}
				} catch (EASBizException var18) {
					logger.error(var18.getMessage(), var18);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("禁用失败！" + var18.getMessage());
					batchMessageTipsHeader.addResult(body);
				} catch (BOSException var19) {
					logger.error(var19.getMessage(), var19);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("禁用失败！" + var19.getMessage());
					batchMessageTipsHeader.addResult(body);
				}
			}
		}

		batchMessageTipsHeader.setBillId(billIds);
		batchMessageTipsHeader.setFailureCount(failure);
		batchMessageTipsHeader.setSuccessCount(success);
		this.writeSuccessData(batchMessageTipsHeader);
	}

//	public void endProjectAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
//			throws SHRWebException {
//		Context ctx = SHRContext.getInstance().getContext();
//		String billIds = request.getParameter("billId");
//		BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
//		int success = 0;
//		int failure = 0;
//		String billId = "";
//		if (!StringUtils.isEmpty(billIds)) {
//			Date now = new Date();
//
//			IProjectManagement iProjectManagement;
//			ProjectManagementCollection coll;
//			try {
//				iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
//				coll = this.getProject(ctx, billIds, iProjectManagement);
//			} catch (BOSException var18) {
//				logger.error(var18.getMessage(), var18);
//				throw new ShrWebBizException("数据异常！获取项目异常");
//			}
//
//			int i = 0;
//
//			for (int length = coll.size(); i < length; ++i) {
//				ProjectManagementInfo getProject = coll.get(i);
//				billId = getProject.getId().toString();
//				BatchMessageTipsBody body = new BatchMessageTipsBody();
//				body.setId(billId);
//
//				try {
//					if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 1
//							&& getProject.getProjectState().getValue() != 3) {
//						++failure;
//						body.setMuitTipsState(Boolean.FALSE);
//						body.setMuitTipsMessage("只能结束“进行中”、“禁用”的项目！");
//						batchMessageTipsHeader.addResult(body);
//					} else {
//						getProject.setProjectState(projectState.ENDED);
//						getProject.setEndDate(now);
//						iProjectManagement.update(new ObjectUuidPK(billId), getProject);
//						++success;
//						body.setMuitTipsState(Boolean.TRUE);
//						body.setMuitTipsMessage("结束成功！");
//						batchMessageTipsHeader.addResult(body);
//					}
//				} catch (EASBizException var19) {
//					logger.error(var19.getMessage(), var19);
//					++failure;
//					body.setMuitTipsState(Boolean.FALSE);
//					body.setMuitTipsMessage("结束失败！" + var19.getMessage());
//					batchMessageTipsHeader.addResult(body);
//				} catch (BOSException var20) {
//					logger.error(var20.getMessage(), var20);
//					++failure;
//					body.setMuitTipsState(Boolean.FALSE);
//					body.setMuitTipsMessage("结束失败！" + var20.getMessage());
//					batchMessageTipsHeader.addResult(body);
//				}
//			}
//		}
//
//		batchMessageTipsHeader.setBillId(billIds);
//		batchMessageTipsHeader.setFailureCount(failure);
//		batchMessageTipsHeader.setSuccessCount(success);
//		this.writeSuccessData(batchMessageTipsHeader);
//	}

	public void deleteOneAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billIds = request.getParameter("billId");
		BatchMessageTipsHeader batchMessageTipsHeader = new BatchMessageTipsHeader();
		int success = 0;
		int failure = 0;
		String billId = "";
		if (!StringUtils.isEmpty(billIds)) {
			IProjectManagement iProjectManagement;
			ProjectManagementCollection coll;
			try {
				iProjectManagement = ProjectManagementFactory.getLocalInstance(ctx);
				coll = this.getProject(ctx, billIds, iProjectManagement);
			} catch (BOSException var17) {
				logger.error(var17.getMessage(), var17);
				throw new ShrWebBizException("数据异常！获取项目异常");
			}

			int i = 0;

			for (int length = coll.size(); i < length; ++i) {
				ProjectManagementInfo getProject = coll.get(i);
				billId = getProject.getId().toString();
				BatchMessageTipsBody body = new BatchMessageTipsBody();
				body.setId(billId);

				try {
					if (getProject.getProjectState() != null && getProject.getProjectState().getValue() != 0) {
						++failure;
						body.setMuitTipsState(Boolean.FALSE);
						body.setMuitTipsMessage("只能删除“未启用”的项目！");
						batchMessageTipsHeader.addResult(body);
					} else {
						iProjectManagement.delete(new ObjectUuidPK(billId));
						CustomMenuDetailFactory.getLocalInstance(ctx).delete("where businessId ='" + billId + "'");
						++success;
						body.setMuitTipsState(Boolean.TRUE);
						body.setMuitTipsMessage("删除成功！");
						batchMessageTipsHeader.addResult(body);
					}
				} catch (EASBizException var18) {
					logger.error(var18.getMessage(), var18);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("删除失败！" + var18.getMessage());
					batchMessageTipsHeader.addResult(body);
				} catch (BOSException var19) {
					logger.error(var19.getMessage(), var19);
					++failure;
					body.setMuitTipsState(Boolean.FALSE);
					body.setMuitTipsMessage("删除失败！" + var19.getMessage());
					batchMessageTipsHeader.addResult(body);
				}
			}
		}

		batchMessageTipsHeader.setBillId(billIds);
		batchMessageTipsHeader.setFailureCount(failure);
		batchMessageTipsHeader.setSuccessCount(success);
		this.writeSuccessData(batchMessageTipsHeader);
	}
	
	
	private ProjectManagementCollection getProject(Context ctx, String billIds,
			IProjectManagement biz) throws BOSException {
		EntityViewInfo evi = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems()
				.add(new FilterItemInfo("id", WebConvertUtils.getStringSet(billIds.split(",")), CompareType.INCLUDE));
		evi.setFilter(filter);
		SelectorItemCollection sic = evi.getSelector();
		sic.add(new SelectorItemInfo("projectState"));
		ProjectManagementCollection coll = biz.getProjectManagementCollection(evi);
		return coll;
	}
}
