package nc.itf.fa.servlet;

import java.io.IOException;

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

import nc.bs.dao.BaseDAO;
import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.comn.NetStreamContext;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.bs.hz.utils.BillInfoQueryUtil;
import nc.bs.hz.utils.SendUtil;
import nc.itf.arap.byzitf.IArapToByzService;
import nc.itf.fa.byzlog.IbyzLogRcord;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.fa.byzLogVO.ByzLogRcordVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import uap.json.JSONObject;

public class ByzSysAmPaymentServlet implements IHttpServletAdaptor {

	@Override
	public void doAction(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 后台模拟用户登录
		ISecurityTokenCallback tc = NCLocator.getInstance().lookup(ISecurityTokenCallback.class);
		  byte[] token = tc.token("__system".getBytes(), "NC".getBytes());
		  NetStreamContext.setToken(token);
//		NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(), "pfxx".getBytes());
		JSONObject resultjson = new JSONObject();
		ByzLogRcordVO logvo = new ByzLogRcordVO();
		logvo.setItf_type("ARC同步资产调拨应付单");
		logvo.setItfclass("nc.itf.fa.byzitf.IAssetInfoToByzService");
		try {
			String requestJson = BillInfoQueryUtil.requestJson(request);
			JSONObject jsonObject = new JSONObject(requestJson);
			logvo.setByztonccparam(SendUtil.unicodeToString(jsonObject.toString()));
			JSONObject headjson = jsonObject.getJSONObject("data").getJSONObject("head");
			//初始化
			BillInfoQueryUtil.initEnvironment("0001H810000000000HS2", new UFDate(headjson.getString("billdate")));
			BillInfoQueryUtil.initSysBilltypeCache(new String[] { "F1" });
			
			resultjson.put("guid", headjson.getString("def57"));
			//add by djy 20220318唯一性校验
			selectOnlyID(headjson.getString("def57"));
			
			AggPayableBillVO paybillvo = NCLocator.getInstance().lookup(IArapToByzService.class)
					.payableRecBYZ(jsonObject);
			logvo.setPk_bill(paybillvo.getPrimaryKey());
			logvo.setVbillcode(paybillvo.getParentVO().getAttributeValue("billno") + "");
			
			resultjson.put("billpk", paybillvo.getPrimaryKey());
			resultjson.put("billno", paybillvo.getParentVO().getAttributeValue("billno"));
			resultjson.put("status", 1);
			resultjson.put("message", "资产采购应付单新增成功");
			logvo.setNcctobyzresult(SendUtil.unicodeToString(resultjson.toString()));
		} catch (Exception e) {
			resultjson.put("status", 0);
			resultjson.put("message", e.getMessage());
			logvo.setNcctobyzresult(e.getMessage());
		} finally {
			BillInfoQueryUtil.responseWriter(response, resultjson.toString());
			try {
				NCLocator.getInstance().lookup(IbyzLogRcord.class).RcordLogBYZ__RequiresNew(logvo);
			} catch (BusinessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 判断单据接收唯一
	 * 
	 * @param src_id
	 * @throws BusinessException
	 */
	private void selectOnlyID(String src_id) throws BusinessException {
		BaseDAO dao= new BaseDAO();
		String sql = "SELECT DEF57 FROM ap_payablebill WHERE DEF57 = '"+src_id+"' AND NVL(DR,0) = 0";
		String result=(String) dao.executeQuery(sql,new ColumnProcessor());
		if (result != null) {
			throw new BusinessException("来源单据号(def57)已存在,请检查数据:" + src_id);
		}
	}
}
