package nc.bs.uap.ztxt.process;

import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.net.servlet.IReturnStatus;
import nc.pubitf.para.SysInitQuery;
import nc.util.uap.twxt.WYTwxtUtil;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.arap.payable.PayableBillItemVO;
import nc.vo.arap.payable.PayableBillVO;
import nc.vo.baseapp.jsontool.JsonTransfVOUtil;
import nc.vo.baseapp.jsontool.ParamVO;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.twxt.log.WYTwxtLogVO;


/**
 * @Description: 【中台系统->NC】中台应付单同步到NC
 * 
 * @author tsy 【碧桂园-物业板块-中台系统接口】
 * @date 2031-04-13
 * @version with NCC
 */	
public class WriteZtPayableApplyInfoProcess {


	public String writeZtPayableApplyInfo(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		Object pk_bill=null;
		Long ztid=null;
		String billno = null;
		String pk_tradetype = null;
		ResultVO result = new ResultVO();
		result.setResult("");
		ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
		esb.setResultInfo(result);
		try {
			if (jsonobj == null || "".equals(jsonobj.toString())) {// 无Post提交值

				result.setCode(IReturnStatus.FAILING);// 失败
				result.setMessage("Post所提交请求Json格式错误或者无值!");
				WYTwxtUtil.createResultEsbVO(esb);
				esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_FAILING);
				esb.getEsbInfo().setReturnMsg("失败");
				esb.setResultInfo(result);
			} else {
				JSONObject requestInfo = (JSONObject) jsonobj.get("requestInfo");
				if(requestInfo == null){
					throw new Exception("应付单信息不能为空！");
				}
				InvocationInfoProxy.getInstance().setGroupId(SysInitQuery.getParaString("GLOBLE00000000000000", "GROUP"));
			    InvocationInfoProxy.getInstance().setUserId(SysInitQuery.getParaString("GLOBLE00000000000000", "ITFZTUSER"));
				JSONObject headerJsonObject =requestInfo.getJSONObject("Header");
				ztid =headerJsonObject.getLong("ztId");
				billno = headerJsonObject.getString("def19");
				String sql="select count(def2) from ap_payablebill where def2 ='"+ztid+"'  and nvl(dr, 0) = 0";

				BaseDAO baseDAO=new BaseDAO();
				Object count=baseDAO.executeQuery(sql, new ColumnProcessor());
				
				if(ValueUtils.getInt(count)>0){
					result.setCode(IReturnStatus.SUCCEED);// 成功
					com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
					resJsonObject.put("billno", "");
					result.setResult(resJsonObject);
					result.setMessage("中台系统同步应付单成功");
					WYTwxtUtil.createResultEsbVO(esb);
					esb.setResultInfo(result);
					rsdata=com.alibaba.fastjson.JSONObject.toJSONString(esb);
					return rsdata;
				}

				ParamVO paramVO=new ParamVO();
				paramVO.setAggVOClass(AggPayableBillVO.class);
				paramVO.setHeadVOClass(PayableBillVO.class);
				paramVO.setBodyVOClass(PayableBillItemVO.class);
				paramVO.setBilldata(requestInfo.toString());
				paramVO.sethCfgCode("ZTF1_HEAD");
				paramVO.setBfgCode("ZTF1_BODY");
				
				AggPayableBillVO aggPayableBillVO=JsonTransfVOUtil.json2AggVO(paramVO);
				
				// 转换前非空校验、执行公式、翻译后各项非空校验
				JsonTransfVOUtil.notNullCheckAndExFormula("ZTF1_HEAD", "ZTF1_BODY", aggPayableBillVO);
				pk_tradetype=aggPayableBillVO.getHeadVO().getPk_tradetype();
				aggPayableBillVO.getHeadVO().setDef2(null == ztid ? null : ztid.toString());
				
				WYTwxtUtil.getIPFBusiAction().processAction("SAVE", aggPayableBillVO.getHeadVO().getPk_tradetype(), null, aggPayableBillVO, null, null);
				pk_bill=aggPayableBillVO.getPrimaryKey();
				WYTwxtUtil.getIPFBusiAction().processAction("APPROVE", aggPayableBillVO.getHeadVO().getPk_tradetype(), null, aggPayableBillVO, null, null);

				result.setCode(IReturnStatus.SUCCEED);// 成功
				com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
				resJsonObject.put("billno", aggPayableBillVO.getHeadVO().getBillno());
				result.setResult(resJsonObject);
				result.setMessage("中台系统同步应付单成功");
				WYTwxtUtil.createResultEsbVO(esb);
				esb.setResultInfo(result);
			}
		} catch (Exception e) {
			result.setCode(IReturnStatus.ERROR);// 异常
			result.setError(WYTwxtUtil.getExceptionDetail(e));
			result.setMessage( billno + " :"+e.getMessage());
			WYTwxtUtil.createResultEsbVO(esb);// 初始化响应对象
			esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_ERROR);
			esb.getEsbInfo().setReturnMsg("异常");
			esb.setResultInfo(result);// 设置响应内容
		} finally {
			rsdata=com.alibaba.fastjson.JSONObject.toJSONString(esb);
			WYTwxtLogVO logVO=new WYTwxtLogVO();
			
			logVO.setNc_id(pk_bill+"");
			logVO.setOther_id(ztid.toString() +"");
			logVO.setBillno("");
			logVO.setTranstypecode(pk_tradetype+"");
			logVO.setItfcode("ZT-NC-FI-003");
			logVO.setItfname("【物业中台接口】新增应付单");
			logVO.setBilldata(jsonobj == null ? "" : jsonobj.toString());
			logVO.setRsdata(rsdata);
			logVO.setMsg(null);
			logVO.setIssuccess(IReturnStatus.SUCCEED.equals(result.getCode()) ? "Y" : "N");

			WYTwxtUtil.getIWuYeTwxtLogService().insertLog_RequiresNew(logVO);
		}
		return rsdata;
	}
}
