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.core.util.ObjectCreator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.impl.wgpt.process.WuYeWgptBusiImpl;
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 nccloud.framework.core.exception.ExceptionUtils;
import uap.json.JSONObject;

/**
 * @Description: 
 * 
 * @author 谢文如 【碧桂园-物业板块-网购平台接口】
 * @date 2019-3-6
 * @version with NC V6.3
 */
@SuppressWarnings("restriction")
public class WuYeWgptBP {
	/**
	 * 【网购平台->NC】新增采购订单+付款计划
	 * @param jsonobj
	 * @return
	 */
	public String writeOrderInfo(final HttpServletRequest request) {
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			WuYeWgptBusiImpl wgptBusiImpl=(WuYeWgptBusiImpl) ObjectCreator.newInstance("nc.impl.wgpt.process.WuYeWgptBusiImpl");
			return wgptBusiImpl.writeOrderInfo(jsonobj);
//			return WuYeWgptUtil.getIWuYeWgptBusiService().writeOrderInfo(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;
		}
	}
	/**
	 * 【网购平台->NC】整单关闭采购订单
	 * @param key
	 * @param billdata
	 * @return
	 */
	public String closeOrder(final HttpServletRequest request) {
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().closeOrder(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;
		}
	}
	/**
	 * 【网购平台->NC】新增/修改采购发票
	 * @param key
	 * @param billdata
	 * @return
	 */
	public String writeInvoiceInfo(final HttpServletRequest request) {
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().writeInvoiceInfo(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;
		}
	}
	/**
	 * 【网购平台->NC】新加的方法，采购平台查询库存量
	 * @author 符耀铭
	 * @time 2019-4-29
	 * @param request
	 * @return
	 */
	public String queryInventoryByCondition(HttpServletRequest request){
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().queryInventoryByCondition(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;
		}
	}
	
	/**【网购平台->NC】新加的方法，采购平台协同库存采购入库/退库
	 * @author 符耀铭
	 * @time 2019-7-15
	 * @param request
	 * @return
	 */
	public String writePurchaseinInfo(HttpServletRequest request){
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().writePurchaseinInfo(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;
		}
	}
	
	/**【网购平台->NC】项目档案查询接口
	 * @author 周蓝萍
	 * @time 2020-11-05
	 * @param request
	 * @return
	 */
	public String queryProjectInfo(HttpServletRequest request){
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().queryProjectInfo(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;
		}
	}
	
	/**碧彩京东接口改造，预付款单红字付款单
	 * @author 符耀铭
	 * @date 2020年10月14日
	 * @param request
	 * @return
	 */
	public String writePaybillRedInfo(HttpServletRequest request) {
		try {
			initInvocationInfo();
			JSONObject jsonobj = WuYeWgptUtil.findPostJsonValue(request);// 获取Post请求的值
			return WuYeWgptUtil.getIWuYeWgptBusiService().writePaybillRedInfo(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 busiCenterManageService=NCLocator.getInstance().lookup(IBusiCenterManageService.class);
			InvocationInfoProxy.getInstance().setUserDataSource(busiCenterManageService.getAvailableBusiCenterDsNames()[0]);
			InvocationInfoProxy.getInstance().setGroupId(SysInitQuery.getParaString("GLOBLE00000000000000", "GROUP"));
			InvocationInfoProxy.getInstance().setUserId(SysInitQuery.getParaString("GLOBLE00000000000000", "WYBCUSER"));
		} catch (BusinessException e) {
			ExceptionUtils.wrapBusinessException(e.getMessage());
		}
		ISecurityTokenCallback tc = NCLocator.getInstance().lookup(ISecurityTokenCallback.class);
		byte[] token = tc.token("__system".getBytes(), "NC".getBytes());
		NetStreamContext.setToken(token);
	}
	
}
