package nc.bs.pu.listener.approve.poinvoice;

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

import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.logging.Logger;
import nc.bs.pu.event.PUBusinessEvent;
import nc.util.uap.wgpt.WuYeWgptUtil;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.data.ValueUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.tmpub.util.DateUtil;
import nc.vo.wgpt.log.WuYeWgptLogVO;
import ncc.pub.wywgpt.utils.WyWGPTUtils;
/**
 * 二开新加的类，用于采购发票审批后回写网购平台
 * @author 符耀铭
 * @time 2019-12-2
 */
public class InvoiceApproveWriteWGPTListener implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		if ("1020".equals(event.getEventType())) {
			WuYeWgptLogVO logVO = new WuYeWgptLogVO();
			logVO.setItfcode("NC-WGPT-PU-003");
			logVO.setTranstypecode("F3");
			logVO.setItfname("物业采购发票审批后回写网购平台");
			logVO.setIssuccess(UFBoolean.TRUE.toString());
			StringBuilder sb = new StringBuilder();
			PUBusinessEvent e = (PUBusinessEvent) event;
			Object[] value = e.getNewObjs();
			if (value == null) {
				return;
			}

			if (value.getClass().isArray()) {
				Object[] obj = (Object[]) value;
				InvoiceVO[] invoiceVOs = new InvoiceVO[obj.length];
				System.arraycopy(obj, 0, invoiceVOs, 0, obj.length);
				if (invoiceVOs.length > 0) {
					sb.append("进入日志记录物业采购发票审批后回写网购平台----" + e.getEventType());
					UFBoolean isfromwgpt = UFBoolean.FALSE;
					String esbResultinfoStr = null;
					JSONObject requestInfo = new JSONObject();
					JSONArray billList = new JSONArray();
					requestInfo.put("method", "getPaymentMsg");
					JSONObject billdata = null;
					for (int i = 0; i < invoiceVOs.length; i++) {
						isfromwgpt = ValueUtils.getUFBoolean(invoiceVOs[i]
								.getParentVO().getVdef8());

						if (isfromwgpt.booleanValue() && invoiceVOs[i].getParentVO().getFbillstatus()==3) {
							billdata = new JSONObject();

							billdata.put("invoiceType", "2");

							billdata.put("billNo", invoiceVOs[i].getParentVO()
									.getVdef10());

							billdata.put("P_billno", invoiceVOs[i]
									.getParentVO().getVdef10());

							billdata.put("status", "0");

							billdata.put("Status", "0");

							billdata.put("paymentTime",ValueUtils.getUFDateTime(invoiceVOs[i]
									.getParentVO().getTaudittime()).toString());
							

							billdata.put("Create_time", ValueUtils.getUFDateTime(invoiceVOs[i]
									.getParentVO().getTaudittime()).toString());

							billList.add(billdata);
						}
					}
					if(billList.size()>0){
						requestInfo.put("billList", billList);
						String sendJsonData = WyWGPTUtils.getSendToEsbData(
								"NC-WGPT-PU-003"
										+ DateUtil.getCurrTime().getMillis(),
										requestInfo);
						sb.append("请求报文：" + requestInfo);
						logVO.setBilldata(requestInfo.toString());
						// 发送请求
						try {
							JSONObject esbResultinfo = WyWGPTUtils.sendToESB(
									sendJsonData, "NC-WGPT-PU-003");
							if (esbResultinfo != null) {
								esbResultinfoStr = esbResultinfo.toString();
								sb.append("返回报文：" + esbResultinfoStr);
								logVO.setRsdata(esbResultinfoStr);
								Logger.error("【物业采购发票审批后回写网购平台】请求参数："
										+ sendJsonData + "；\r\n返回参数："
										+ esbResultinfoStr);
								JSONObject esbInfo = (JSONObject) esbResultinfo
										.get("esbInfo");
								if (esbInfo != null) {
									// 接口返回状态：S（成功）/W（警告）/E（错误）
									String returnStatus = esbInfo
											.get("returnStatus") + "";
									returnStatus = "E".equals(returnStatus) ? "否"
											: "是";
									if ("否".equals(returnStatus)) {
										String returnMsg = esbInfo.get("returnMsg")
												+ "";
										ExceptionUtils
										.wrappBusinessException("接口错误："
												+ returnMsg);
									}
								}
								JSONObject resultInfo = esbResultinfo
										.getJSONObject("resultInfo");
								Object success = resultInfo.get("success");
								if (!"true".equals(success + "")) {
									Object result = resultInfo.get("result");
									ExceptionUtils.wrappBusinessException("碧彩商城提示:"
											+ result);
								}
							}
						} catch (Exception ex) {
							logVO.setIssuccess(UFBoolean.FALSE.toString());
							sb.append(WuYeWgptUtil.getExceptionDetail(ex));
							ExceptionUtils.wrappBusinessException("异常：\n"
									+ ex.getMessage() + "\n请求报文：\n" + sendJsonData
									+ "\n返回报文：\n" + esbResultinfoStr);
						} finally {
							logVO.setMsg(sb.toString());
							WuYeWgptUtil.getIWuYeWgptLogService()
							.insertLog_RequiresNew(logVO);
						}
					}
				}
			}
		}
	}

}
