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.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.fa.byzitf.IAssetInfoToByzService;
import nc.itf.fa.byzlog.IbyzLogRcord;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.fa.alter.AlterVO;
import nc.vo.fa.byzLogVO.ByzLogRcordVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import uap.json.JSONObject;

public class ByzSysAmChangeServlet 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);
			JSONObject jsonohead = jsonObject.getJSONObject("head");
			BillInfoQueryUtil.initEnvironment("0001H810000000000HS2", new UFDate(jsonohead.getString("business_date")));
			BillInfoQueryUtil.initSysBilltypeCache(new String[] { "HG", "H0" ,"HG-00","HZ"});
			logvo.setByztonccparam(SendUtil.unicodeToString(jsonObject.toString()));
			JSONObject headjoson=(JSONObject) jsonObject.get("head");
			resultjson.put("guid", headjoson.get("def4"));
			//ARC来源单号唯一校验
			String sql="select count(1) from fa_alter where nvl(dr,0)=0 and def4='"+headjoson.get("def4")+"'";
			int count=(int) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql, new ColumnProcessor());
			if(count>0)
				throw new BusinessException("该ARC单号【"+headjoson.get("def4")+"】，已存在");
			
			AlterVO altervo = NCLocator.getInstance().lookup(IAssetInfoToByzService.class).assetCardRecBYZ(jsonObject);
	
			logvo.setPk_bill(altervo.getPrimaryKey());
			logvo.setVbillcode(altervo.getParentVO().getAttributeValue("bill_code") + "");
			
			resultjson.put("billpk", altervo.getPrimaryKey());
			resultjson.put("billno", altervo.getParentVO().getAttributeValue("bill_code"));
			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();
			}
//			BillInfoQueryUtil.insertlog(logvo);
		}
	}
}
