package nc.util.arap.arc;

import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.bs.hz.utils.SafeObject;
import nc.bs.hz.utils.SendUtil;
import nc.bs.logging.Logger;
import nc.itf.fa.byzlog.IbyzLogRcord;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.util.fa.httputil.BillHttpRequestUtil;
import nc.vo.fa.byzLogVO.ByzLogRcordVO;
import nc.vo.pub.BusinessException;

public class ArapForARCApproveAction {
	private ObjectMapper mapper;

	/**
	 * 
	 * @param pk
	 * @param src_code ARC全局唯一单据号
	 * @param type    3、资产处置应收单  6、调拨应付单
	 * @return
	 * @throws BusinessException
	 */
	public String sendApprove(String pk, String src_code, Integer type) throws BusinessException {
		NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(), "pfxx".getBytes());
		mapper = new ObjectMapper();
		String arcip = null, request = null, response = null;
		try {
			// 审批给ARC
			ObjectNode data = mapper.createObjectNode();
			// 单据主键
			data.put("pkCard", pk);
			// 回退类型
			data.put("type", type);
			// 单据编号(ARC)
			data.put("billno", src_code);
			// 审批结果
			data.put("auditResult", 0);
			// 回退原因
			data.put("backReason", "审批通过");
			// 回退用户
			String user = InvocationInfoProxy.getInstance().getUserId();
			data.put("backUser", user);
			// 回退用户名
			data.put("backUserName", getUserName(user));
			arcip = SendUtil.getSysinittempValue("backtobyz");
			request = SendUtil.getResult(data);
			// 推送给ARC
			response = BillHttpRequestUtil.sendPost(arcip, request);
			JsonNode json = mapper.readTree(response);
			if (json.findValue("code").asInt() != 200) {
				if(json.findValue("msg")==null)
					throw new BusinessException("ARC返回信息：null");
				throw new BusinessException("ARC返回信息："+json.findValue("msg").asText());	
			}
		} catch (IOException e) {
			throw new BusinessException(e.getMessage());
		} finally {
			IbyzLogRcord ibyzLogRcord = (IbyzLogRcord) NCLocator.getInstance().lookup(IbyzLogRcord.class);
			ByzLogRcordVO byzLogRcordVO = new ByzLogRcordVO();
			byzLogRcordVO.setVbillcode(src_code);
			byzLogRcordVO.setItf_type("arap相关单据审批回调ARC");
			byzLogRcordVO.setItfclass(this.getClass().toString());
			byzLogRcordVO.setNcctobyzresult(request);
			byzLogRcordVO.setByztonccparam(response);
			byzLogRcordVO.setItfaddress(arcip);
			try {
				ibyzLogRcord.RcordLogBYZ__RequiresNew(byzLogRcordVO);
			} catch (BusinessException e) {
				Logger.error(e.getMessage(), e);
				Logger.error("日志生成失败！！nc.itf.fa.byzlog.IbyzLogRcord");
			}
		}
		return response;
	}

	private String getUserName(String usercode) throws BusinessException {
		String sql = "SELECT USER_NAME FROM SM_USER WHERE cuserid = ?";
		SQLParameter parameter = new SQLParameter();
		parameter.addParam(usercode);
		IUAPQueryBS bs = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		String result = SafeObject.toString(bs.executeQuery(sql, parameter, new ColumnProcessor()));
		if (result == null) {
			throw new BusinessException("没有找到该用户,请检查数据:" + usercode);
		}
		return result;
	}
}
