package nc.bs.zt.wuye;

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

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

import com.alibaba.fastjson.JSONObject;

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.ztbossxt.process.IWuYeZtBossxtBusiService;
import nc.net.servlet.IReturnStatus;
import nc.net.servlet.SapToNcEsbJsonServlet;
import nc.pubitf.para.SysInitQuery;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;

public class GatherDelServelet extends SapToNcEsbJsonServlet{

	@Override
	public void execFunction(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		JSONObject jsonobj = new JSONObject();
		try {
			initInvocationInfo();
			String id= request.getParameter("id");
			jsonobj.put("id",id);
			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 {
				String jsondata =NCLocator.getInstance().lookup(IWuYeZtBossxtBusiService.class).gatherDelApplyInfo(jsonobj);
				sendResponse(response,jsondata);
			}
		} 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);
		}
	}
		
	public void sendResponse(final HttpServletResponse response, String jsondata) {
		PrintWriter out = null;
		try {
			response.setHeader("Content-type", "application/json;charset=UTF-8");
			out = new PrintWriter(new OutputStreamWriter(
					response.getOutputStream(), "UTF-8"));
			out.write(jsondata);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}


	private void initInvocationInfo() throws BusinessException {
		InvocationInfoProxy.getInstance().setGroupId(SysInitQuery.getParaString("GLOBLE00000000000000", "GROUP"));
		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);
	}

}
