package nc.bs.uap.twxt.process;

import com.alibaba.fastjson.JSONObject;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.uap.boss.process.BossWriteGatherBillInfo;
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;

/**
 * @Description: boss系统,中台系统->NC
 * @author xuy
 * @date 2022-04-13
 */
public class BossWriteGatherBillInfoProcess implements BossWriteGatherBillInfo {
	/**
	 * boss系统收款单接收流程
	 * */
	@SuppressWarnings("unchecked")
	public String writeGatherBillInfo(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		Object pk_bill=null;
		Object bossid=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");
				bossid =headerJsonObject.get("bossid");

				String sql="select count(def1) from ar_gatherbill where def1 = '"+bossid+"' 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);// 成功
					result.setMessage("Boss系统同步收款单成功");
					com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
					resJsonObject.put("billno", null);
					result.setResult(resJsonObject);
					WYTwxtUtil.createResultEsbVO(esb);
					esb.setResultInfo(result);
					rsdata =com.alibaba.fastjson.JSONObject.toJSONString(esb);
					return rsdata;
				}

				ParamVO paramVO=new ParamVO();
				paramVO.setAggVOClass(AggGatheringBillVO.class);
				paramVO.setHeadVOClass(GatheringBillVO.class);
				paramVO.setBodyVOClass(GatheringBillItemVO.class);
				paramVO.setBilldata(requestInfo.toString());
				paramVO.sethCfgCode("bossF2_HEAD");//bossF2_HEAD
				paramVO.setBfgCode("bossF2_BODY");//bossF2_BODY
				
				AggGatheringBillVO aggGatheringBillVO=JsonTransfVOUtil.json2AggVO(paramVO);
				
				// 转换前非空校验、执行公式、翻译后各项非空校验
				JsonTransfVOUtil.notNullCheckAndExFormula("bossF2_HEAD", "bossF2_BODY", aggGatheringBillVO);
				pk_tradetype=aggGatheringBillVO.getHeadVO().getPk_tradetype();
				aggGatheringBillVO.getHeadVO().setDef1(null==bossid?null:bossid.toString());//设置bossid到def1
				//保存应收计提单
				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("Boss系统同步收款单成功");
				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(WYTwxtUtil.getExceptionDetail(e));
			result.setMessage(null == bossid ? null : bossid.toString()+":"+e.getMessage());
			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(bossid +"");
			logVO.setBillno("");
			logVO.setTranstypecode(pk_tradetype+"");
			logVO.setItfcode("BO-NC-FI-002");
			logVO.setItfname("【物业Boss接口】新增收款单");
			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;
	}
	/**
	    * 中台系统收款单接收流程
	 * */
	@SuppressWarnings("unchecked")
	public String writeGatherBillInfo2(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		Object pk_bill=null;
		Object ztid=null;
		String def19=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.get("ztId");
				def19=headerJsonObject.getString("def19");
				String sql="select count(def1) from ar_gatherbill where def1 = '"+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);// 成功
					result.setMessage("中台系统同步收款单成功");
					com.alibaba.fastjson.JSONObject resJsonObject=new com.alibaba.fastjson.JSONObject();
					resJsonObject.put("billno", null);
					result.setResult(resJsonObject);
					WYTwxtUtil.createResultEsbVO(esb);
					esb.setResultInfo(result);
					rsdata =com.alibaba.fastjson.JSONObject.toJSONString(esb);
					return rsdata;
				}

				ParamVO paramVO=new ParamVO();
				paramVO.setAggVOClass(AggGatheringBillVO.class);
				paramVO.setHeadVOClass(GatheringBillVO.class);
				paramVO.setBodyVOClass(GatheringBillItemVO.class);
				paramVO.setBilldata(requestInfo.toString());
				paramVO.sethCfgCode("ZTF2_HEAD");
				paramVO.setBfgCode("ZTF2_BODY");
				
				AggGatheringBillVO aggGatheringBillVO=JsonTransfVOUtil.json2AggVO(paramVO);
				
				// 转换前非空校验、执行公式、翻译后各项非空校验
				JsonTransfVOUtil.notNullCheckAndExFormula("ZTF2_HEAD", "ZTF2_BODY", aggGatheringBillVO);
				pk_tradetype=aggGatheringBillVO.getHeadVO().getPk_tradetype();
				aggGatheringBillVO.getHeadVO().setDef1(null==ztid?null:ztid.toString());//设置ztid到def1
				//保存应收计提单
				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(WYTwxtUtil.getExceptionDetail(e));
			result.setMessage(null == def19 ? null : def19.toString()+":"+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 +"");
			logVO.setBillno("");
			logVO.setTranstypecode(pk_tradetype+"");
			logVO.setItfcode("ZT-NC-FI-002");
			logVO.setItfname("【物业中台Boss接口】新增收款单");
			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;
	}

}
