package nc.bs.uap.twxt.process;

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.gathering.AggGatheringBillVO;
import nc.vo.arap.gathering.GatheringBillItemVO;
import nc.vo.arap.gathering.GatheringBillVO;
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.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.twxt.log.WYTwxtLogVO;
import uap.json.JSONObject;

/**
 * @Description: 【天文系统->NC】天问收款单同步到NC
 * 
 * @author 周蓝萍 【碧桂园-物业板块-天问系统接口】
 * @date 2019-04-12
 * @version with NC V6.3	
 */
public class WriteGatherBillInfoProcess {

	@SuppressWarnings("unchecked")
	public String writeGatherBillInfo(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		Object pk_bill=null;
		Object twid=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", "ITFUSER"));
				JSONObject headerJsonObject =requestInfo.getJSONObject("Header");
				twid =headerJsonObject.get("def41");

				String sql="select count(def1) from ar_gatherbill where def1 = '"+twid+"' and nvl(dr, 0) = 0";

				BaseDAO baseDAO=new BaseDAO();
				Object count=baseDAO.executeQuery(sql, new ColumnProcessor());

				if(ValueUtils.getInt(count)>0){
					ExceptionUtils.wrappBusinessException("NC系统已存在天问系统单据ID:"+twid+"的单据,请勿重复传递！");
				}

				ParamVO paramVO=new ParamVO();
				paramVO.setAggVOClass(AggGatheringBillVO.class);
				paramVO.setHeadVOClass(GatheringBillVO.class);
				paramVO.setBodyVOClass(GatheringBillItemVO.class);
				paramVO.setBilldata(requestInfo.toString());
				paramVO.sethCfgCode("TWF2_HEAD");
				paramVO.setBfgCode("TWF2_BODY");
				
				AggGatheringBillVO aggGatheringBillVO=JsonTransfVOUtil.json2AggVO(paramVO);
				
				// 转换前非空校验、执行公式、翻译后各项非空校验
				JsonTransfVOUtil.notNullCheckAndExFormula("TWF2_HEAD", "TWF2_BODY", aggGatheringBillVO);
				pk_tradetype=aggGatheringBillVO.getHeadVO().getPk_tradetype();
				//保存应收计提单
				Object saveObject=WYTwxtUtil.getIPFBusiAction().processAction("SAVE", aggGatheringBillVO.getHeadVO().getPk_tradetype(), null, aggGatheringBillVO, null, null);
				pk_bill=aggGatheringBillVO.getPrimaryKey();
				InvocationInfoProxy.getInstance().setUserId(SysInitQuery.getParaString("GLOBLE00000000000000", "ITFUSER"));
				aggGatheringBillVO.getHeadVO().setApprovedated(aggGatheringBillVO.getHeadVO().getBilldate());
				InvocationInfoProxy.getInstance().setBizDateTime(aggGatheringBillVO.getHeadVO().getBilldate().getMillis());
				WYTwxtUtil.getIPFBusiAction().processAction("APPROVE", aggGatheringBillVO.getHeadVO().getPk_tradetype(), null, aggGatheringBillVO, null, null);
				result.setCode(IReturnStatus.SUCCEED);// 成功
				result.setMessage("天问系统同步收款单成功");
				com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
				resJsonObject.put("billno", aggGatheringBillVO.getHeadVO().getBillno());
				result.setResult(resJsonObject);
				WYTwxtUtil.createResultEsbVO(esb);
				esb.setResultInfo(result);
				
			}
		} catch (Exception e) {
			result.setCode(IReturnStatus.ERROR);// 异常
			result.setError(e.getMessage());
			result.setMessage(WYTwxtUtil.getExceptionDetail(e));
			WYTwxtUtil.createResultEsbVO(esb);// 初始化响应对象
			esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_ERROR);
			esb.getEsbInfo().setReturnMsg("异常");
			esb.setResultInfo(result);// 设置响应内容
			ExceptionUtils.marsh(e);
		} finally {
			rsdata =com.alibaba.fastjson.JSONObject.toJSONString(esb);
			WYTwxtLogVO logVO=new WYTwxtLogVO();
			
			logVO.setNc_id(pk_bill+"");
			logVO.setOther_id(twid +"");
			logVO.setBillno("");
			logVO.setTranstypecode(pk_tradetype+"");
			logVO.setItfcode("TW-NC-FI-002");
			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;
	}

}
