package nc.bs.zt.wuye;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.impl.pubapp.pattern.data.bill.EfficientBillQuery;
import nc.net.servlet.IReturnStatus;
import nc.util.mmpub.dpub.db.SqlBuilder;
import nc.util.uap.twxt.WYTwxtUtil;
import nc.vo.arap.gathering.AggGatheringBillVO;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pub.BusinessException;
import nc.vo.twxt.log.WYTwxtLogVO;

/**
 * @Description: 【ZT系统->NC】中台收款撤销删除到NC
 * 
 * @author tsy 【碧桂园-物业板块-ZT系统系统接口】
 * @date 2021-04-25
 * @version with NCC
 */
public class GatherDelApplyInfoProcess {

	public String writeGatherDelApplyInfoProcess(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		Object pk_bill = null;
		String ztId = null;
		String billno = null;
		String pk_tradetype = null;
		ResultVO result = new ResultVO();
//		JSONArray returnarray = new JSONArray();
		result.setResult("");
		ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
		esb.setResultInfo(result);
		try {
			if (!jsonobj.containsKey("id")) {
				throw new BusinessException("id不能为空！");
			}

			ztId  = jsonobj.getString("id");
			if (ztId == null || ztId.isEmpty()) {
				throw new BusinessException("id参数不能为空！");
			}

			SqlBuilder headCond = new SqlBuilder();
			headCond.append(" from ar_gatherbill where ");
			headCond.append("def1", ztId);
			headCond.append(" and ");
			headCond.append("dr", 0);
			EfficientBillQuery<AggGatheringBillVO> query = new EfficientBillQuery<AggGatheringBillVO>(
					AggGatheringBillVO.class);
			AggGatheringBillVO[] vos = (AggGatheringBillVO[]) query.query(headCond.toString());

			if (vos == null || vos.length <= 0) {
				result.setCode(IReturnStatus.SUCCEED);// 成功
				com.alibaba.fastjson.JSONObject resJsonObject = new com.alibaba.fastjson.JSONObject();
//				resJsonObject.put("ids", ztIds);
				result.setResult(resJsonObject);
				result.setMessage("删除成功");
				WYTwxtUtil.createResultEsbVO(esb);
				esb.setResultInfo(result);
				rsdata = com.alibaba.fastjson.JSONObject.toJSONString(esb);
				return rsdata;
			}

			for (AggGatheringBillVO aggvo : vos) {
				
				billno = aggvo.getHeadVO().getDef19();
				ztId = aggvo.getHeadVO().getDef1();
				if(!"-1".equals(aggvo.getHeadVO().getApprovestatus().toString()))
					WYTwxtUtil.getIPFBusiAction().processAction("UNAPPROVE", aggvo.getHeadVO().getPk_tradetype(), null,
							aggvo, null, null);
				WYTwxtUtil.getIPFBusiAction().processAction("DELETE", aggvo.getHeadVO().getPk_tradetype(), null, aggvo,
						null, null);

			}

			result.setCode(IReturnStatus.SUCCEED);// 成功
			com.alibaba.fastjson.JSONObject resJsonObject = new com.alibaba.fastjson.JSONObject();
			resJsonObject.put("ztId", ztId);
			result.setResult(resJsonObject);
			result.setMessage("删除成功");
			WYTwxtUtil.createResultEsbVO(esb);
			esb.setResultInfo(result);

		} catch (Exception e) {
			result.setCode(IReturnStatus.ERROR);// 异常
//			result.setError(null == ztI d ? null : ztId.toString() + ":" + e.getMessage());
			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 + "");
			logVO.setBillno("");
			logVO.setTranstypecode(pk_tradetype + "");
			logVO.setItfcode("NC-ZT-FI-DEL");
			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;
	}
}
