package nc.bs.pu.wgpt.bp;

import javax.servlet.http.HttpServletRequest;

import nc.bcmanage.bs.IBusiCenterManageService;
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.net.servlet.IReturnStatus;
import nc.pubitf.para.SysInitQuery;
import nc.util.uap.wgpt.WuYeWgptUtil;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;
import uap.json.JSONObject;

public class WuYeWgpt {
	public String writePurchaseinInfo(HttpServletRequest request){
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().writePurchase(jsonobj);
		} catch (Exception e) {
			ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
			ResultVO result = new ResultVO();
			esb.setResultInfo(result);
			result.setCode(IReturnStatus.ERROR);// 异常
			result.setError(WuYeWgptUtil.getExceptionDetail(e));
			WuYeWgptUtil.createResultEsbVO(esb);// 初始化响应对象
			esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_ERROR);
			esb.getEsbInfo().setReturnMsg("异常");
			esb.setResultInfo(result);// 设置响应内容
			String rsdata = com.alibaba.fastjson.JSONObject.toJSONString(esb);
			return rsdata;
		}
	}
	
	private void initInvocationInfo(){
		try {
			IBusiCenterManageService busi = NCLocator.getInstance().lookup(IBusiCenterManageService.class);
			
			InvocationInfoProxy.getInstance().setUserDataSource(busi.getAvailableBusiCenterDsNames()[0]);
			InvocationInfoProxy.getInstance().setGroupId(SysInitQuery.getParaString("GLOBLE00000000000000", "GROUP"));
			InvocationInfoProxy.getInstance().setUserId(SysInitQuery.getParaString("GLOBLE00000000000000", "WYBCUSER"));
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ISecurityTokenCallback tc = NCLocator.getInstance().lookup(ISecurityTokenCallback.class);
		byte[] token = tc.token("__system".getBytes(), "NC".getBytes());
		NetStreamContext.setToken(token);
	}
}
