package nc.bs.uapbd.mdg.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.comn.NetStreamContext;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.itf.uapbd.mdg.IMDGSupplierInfoService;
import nc.net.servlet.IReturnStatus;
import nc.net.servlet.SapToNcEsbJsonServlet;
import nc.pub.sap.constant.SapConstants;
import nc.pubitf.para.SysInitQuery;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;

public class MDGToNcSupplierServelet extends SapToNcEsbJsonServlet{

	@Override
	public void execFunction(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jsonobj = null;
		
		try {
			initInvocationInfo();
			jsonobj = findPostJsonValue(request);// 获取Post请求的值
			if (jsonobj == null || "".equals(jsonobj)) {// 无Post提交值
				ResultVO result = new ResultVO();
				result.setCode(IReturnStatus.FAILING);// 失败
				result.setMessage("Post所提交请求Json格式错误或者无值!");
				ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
				createResultEsbVO(esb);
				esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_FAILING);
				esb.getEsbInfo().setReturnMsg("失败");
				esb.setResultInfo(result);
				sendResponse(response, esb);
			}else {
				IMDGSupplierInfoService service=NCLocator.getInstance().lookup(IMDGSupplierInfoService.class);
				String resString =service.mdgSynSupToNCC(jsonobj);
				ResultEsbVO<ResultVO> esb =JSON.parseObject(resString,new TypeReference<ResultEsbVO<ResultVO>>() {});
				sendResponse(response, esb);
			}
		} catch (Exception e) {
			ResultVO result = new ResultVO();
			result.setCode(IReturnStatus.ERROR);// 异常
			result.setError(getExceptionDetail(e));
			ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();// 创建响应对象
			createResultEsbVO(esb);// 初始化响应对象
			esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_ERROR);
			esb.getEsbInfo().setReturnMsg("异常");
			esb.setResultInfo(result);// 设置响应内容
			sendResponse(response, esb);
		}
	}
	
	/**
	 * 加载Post提交上来的Json参数
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	private JSONObject findPostJsonValue(final HttpServletRequest request)
			throws Exception {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					(ServletInputStream) request.getInputStream(), "utf-8"));
			StringBuffer sb = new StringBuffer();
			String temp = null;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			return JSONObject.parseObject(sb.toString());
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void initInvocationInfo() throws BusinessException {
		if(MMValueCheck.isEmpty(InvocationInfoProxy.getInstance().getUserDataSource())) {
			InvocationInfoProxy.getInstance().setUserDataSource("NCCDATA");
		}
		InvocationInfoProxy.getInstance().setGroupId(SapConstants.GROUPID);
		InvocationInfoProxy.getInstance().setUserId(SysInitQuery.getParaString("GLOBLE00000000000000", "ITFUSER"));
		ISecurityTokenCallback tc = NCLocator.getInstance().lookup(
				ISecurityTokenCallback.class);
		byte[] token = tc.token("__system".getBytes(), "NC".getBytes());
		NetStreamContext.setToken(token);
	}
}
