package com.kingdee.shr.recuritment.web.handler;


import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.base.permission.util.ToolUtils;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.recuritment.bizEnum.ExecuteResultEnum;
import com.kingdee.eas.custom.recuritment.task.IRecuritmentFacade;
import com.kingdee.eas.custom.recuritment.task.RecuritmentFacadeFactory;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.recuritment.IRecuritmentDemand;
import com.kingdee.shr.recuritment.RecuritmentDemandCollection;
import com.kingdee.shr.recuritment.RecuritmentDemandFactory;
import com.kingdee.shr.recuritment.RecuritmentDemandInfo;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 

public class RecuritmentDemandListHandlerEx extends RecuritmentDemandListHandler{

	
	
	/**
	 * 北森手工同步
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws SHRWebException 
	 */
	public void synBeisenAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		String billIds = request.getParameter("selectedId");
		String[] billIdArray = billIds.split(",");
		List<String>  exbills = new ArrayList();
		int exeCount = 0;
		int unCount = 0;
		try {
			IRecuritmentDemand deIns = RecuritmentDemandFactory.getLocalInstance(ctx);
			RecuritmentDemandCollection recuritmentDemandCollection = deIns.getRecuritmentDemandCollection
			( "select  syncStatus  where  id in ("+ToolUtils.aryToStr(billIdArray) +")" ); 
			for(int i = 0; i<recuritmentDemandCollection.size() ;i ++) {
				RecuritmentDemandInfo recuritmentDemandInfo = recuritmentDemandCollection.get(i);
				Object object = recuritmentDemandInfo.get("syncStatus");
				//为空或失败时再执行;
				if( !ObjectUtils.isEmpty(object)  ) {
					if(!StringUtils.equals( String.valueOf(object), ExecuteResultEnum.SUCCESS_VALUE) ) {
						exbills.add(recuritmentDemandInfo.getId().toString());
					}
				}else {
					exbills.add(recuritmentDemandInfo.getId().toString());
				}
			}
			IRecuritmentFacade localInstance = RecuritmentFacadeFactory.getLocalInstance(ctx);
			
			for(int i = 0;i<exbills.size();i++) {
				String string = exbills.get(i);
				
				try {
					localInstance.syncRecuritmentToBeisen(string);
					exeCount++;
				} catch (EASBizException e) {
					unCount++;
					e.printStackTrace();
				}
			}
		} catch (BOSException  e) {
			unCount++;
			e.printStackTrace();
		}
		Map hashMap = new HashMap();
		hashMap.put("allCount", billIdArray.length);
		hashMap.put("exeCount", exeCount);
		hashMap.put("unCount", unCount);
		JSONUtils.writeJson(response, hashMap);
	}


	@Override
	public void stopBillAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws EASBizException, BOSException, SHRWebException {
		super.stopBillAction(request, response, modelMap);
		extracted(request);
	}

	@Override
	public void preStartAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		super.preStartAction(request, response, modelMap);
		try {
			extracted(request);
		} catch (BOSException | EASBizException e) {
			throw new SHRWebException(e);
		}
	}


	/**
	 * 北森同步
	 * @param request
	 * @throws BOSException
	 * @throws EASBizException
	 */
	private static void extracted(HttpServletRequest request) throws BOSException, EASBizException {
		Context ctx = SHRContext.getInstance().getContext();
		String billId = request.getParameter("billId");
		IRecuritmentFacade localInstance = RecuritmentFacadeFactory.getLocalInstance(ctx);
		localInstance.syncRecuritmentToBeisen(billId);
	}


}