package com.kingdee.customer.util.handler;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

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

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

import com.aspose.pdf.DocSaveOptions;
import com.aspose.pdf.Document;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.base.param.IParamControl;
import com.kingdee.eas.base.param.ParamControlFactory;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
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.ListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

/**
 * 配置项工具类
 * @author issuser
 *
 */
public class ConfigurationHandler extends ListHandler{

	public void getCustomerConfigAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException {
		String fnumber = request.getParameter("fnumber");
		System.out.print("配置项工具类参数编码：" + fnumber);
		if(StringUtils.isBlank("fnumber")) {
			throw new ShrWebBizException("The parameter cannot be null !"); 
		}
		Map<String,String> resutlMap = getConfig(fnumber);
		System.out.print("配置项工具类参数结果：" + resutlMap);
	    JSONUtils.writeJson(response, resutlMap);
	}
	
	public Map<String,String> getConfig(String fnumber) throws ShrWebBizException{
		Context ctx = SHRContext.getInstance().getContext();
		String selectSql = "select top 1 * from T_HR_SHRRsvItem0 where fnumber = '" + fnumber + "' and FState = '1'";
		Map<String,String> resutlMap = new HashMap<String,String>();
		try {
			IRowSet rs = DbUtil.executeQuery(ctx, selectSql);	
			IParamControl ipc = ParamControlFactory.getLocalInstance(ctx);
			String shrEnvironmentIP = ipc.getParamValue(null, "shrEnvironmentIP");
	        while(rs.next()){
	        	resutlMap.put("name", rs.getString("FName_l2")); 
	        	resutlMap.put("ip", shrEnvironmentIP); 
	        }
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		if(StringUtils.isBlank(resutlMap.get("name"))) { 
			throw new ShrWebBizException("No configuration item with code " + fnumber + " found !"); 
		}
		return resutlMap;
	}
	
}
