package nc.impl.uapbd.net;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.framework.common.NCLocator;
import nc.itf.ws.esb.IEsbDAO;
import nc.net.pu.utils.PubUtils;
import nc.net.uapbd.activity.PostUtils;
import nc.net.uapbd.pojo.ESB;
import nc.net.uapbd.pojo.Root;
import nc.vo.bd.bankaccount.BankAccbasVO;
import nc.vo.bd.bankdoc.BankdocVO;
import nc.vo.org.DeptVO;
import nc.vo.org.OrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.sap.log.SapPurchaseLogVO;
import nc.vo.ws.esb.EsbinfoVO;

/**
 * NCC传主数据
 * 
 * @author 郭晋贤 2021.7.8
 *
 */
public class PSToMDMListenner implements IBusinessListener {

	private static IEsbDAO service = NCLocator.getInstance().lookup(IEsbDAO.class);
	private static final ObjectMapper mapper = new ObjectMapper();

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {

		String issuccess_sn = "Y";
		String eventType = event.getEventType();
		String sourceID = event.getSourceID();
		String puuid = "";
		String url = "";
		String syncode = null;
		Root meta = new Root();
		ESB root = null;
		String requestS = null;

		try {
			// update by guojx 如果不是【核算组织】、【核算部门】、【银行档案】、【单位银行档案】这四个节点的就不走监听器
			BDCommonEvent bdCommonEvent = (BDCommonEvent) event;
			if (!(bdCommonEvent.getNewObjs()[0] instanceof OrgVO || bdCommonEvent.getNewObjs()[0] instanceof DeptVO || bdCommonEvent.getNewObjs()[0] instanceof BankdocVO || bdCommonEvent.getNewObjs()[0] instanceof BankAccbasVO)) {
				return;
			}
			List<Map<String, String>> list = new ArrayList();
			Map<String, String> map = new HashMap<String, String>();
			// 核算组织
			if ("985be8a4-3a36-4778-8afe-2d8ed3902659".equals(sourceID)) {
				EsbinfoVO[] esbinfos = (EsbinfoVO[]) service.findESB("code='" + "NCC-MDM-UAPBD-001" + "'");
				syncode = esbinfos[0].getDef1();
				url = esbinfos[0].getUrl();
				OrgVO[] orgvos = null;
				if (bdCommonEvent.getNewObjs()[0] instanceof OrgVO) {
					OrgVO object = (OrgVO) bdCommonEvent.getNewObjs()[0];
					//update by guojx 2021年9月23日16:58:10 核算组织接口是否业务单元数据过滤调整
					if(!object.getIsbusinessunit().equals(new UFBoolean(true))) {
						return;
					}
					orgvos = new OrgVO[] { object };
					root = PostUtils.getESB(PostUtils.getSpeciality(PostUtils.VOHandler(orgvos)));
				}
			}
			// 核算部门
			else if ("b26fa3cb-4087-4027-a3b6-c83ab2a086a9".equals(sourceID)) {
				EsbinfoVO[] esbinfos = (EsbinfoVO[]) service.findESB("code='" + "NCC-MDM-UAPBD-002" + "'");
				syncode = esbinfos[0].getDef1();
				url = esbinfos[0].getUrl();
				DeptVO[] deptvos = null;
				if (bdCommonEvent.getNewObjs()[0] instanceof DeptVO) {
					DeptVO object = (DeptVO) bdCommonEvent.getNewObjs()[0];
					deptvos = new DeptVO[] { object };
					root = PostUtils.getESB(PostUtils.getSpeciality(PostUtils.VOHandler(deptvos)));
				}

			}
			// 银行档案
			else if ("bf5aeed4-6b35-4a2e-b750-b9aabce59e21".equals(sourceID)) {
				EsbinfoVO[] esbinfos = (EsbinfoVO[]) service.findESB("code='" + "NCC-MDM-UAPBD-003" + "'");
				syncode = esbinfos[0].getDef1();
				url = esbinfos[0].getUrl();
				BankdocVO[] bankdocs = null;

				if (bdCommonEvent.getNewObjs()[0] instanceof BankdocVO) {
					BankdocVO object = (BankdocVO) bdCommonEvent.getNewObjs()[0];
					bankdocs = new BankdocVO[] { object };
					root = PostUtils.getESB(PostUtils.getSpeciality(PostUtils.VOHandler(bankdocs)));
				}
			}
			// ProjectHeadVO 项目档案
//		else if("".equals(sourceID)){
//			ProjectHeadVO[] postvos = null;
//			if(bdCommonEvent.getNewObjs() instanceof ProjectHeadVO[]) {
//				ProjectHeadVO[] objects=(ProjectHeadVO[]) bdCommonEvent.getNewObjs();
//				}
//		}
			// 单位银行档案 BankAccbasVO
			else if ("611652ad-177f-4d3e-9ae7-ef8f96930b78".equals(sourceID)) {
				EsbinfoVO[] esbinfos = (EsbinfoVO[]) service.findESB("code='" + "NCC-MDM-UAPBD-004" + "'");
				syncode = esbinfos[0].getDef1();
				url = esbinfos[0].getUrl();
				BankAccbasVO[] psndocvos = null;
				if (bdCommonEvent.getNewObjs()[0] instanceof BankAccbasVO) {
					psndocvos = new BankAccbasVO[] { (BankAccbasVO) bdCommonEvent.getNewObjs()[0] };
					root = PostUtils.getESB(PostUtils.getSpeciality(PostUtils.VOHandler(psndocvos)));
				}
			}

			meta.setESB(root);
			JSONObject request = JSONObject.parseObject(JSONObject.toJSONString(meta));
			requestS = request.toString();
//			JSONObject request = mapper.readValue(requestS,JSONObject.class);
			puuid = request.getJSONObject("ESB").getJSONObject("DATA").getJSONObject("DATAINFOS").getString("PUUID");
			JSONObject rtnValue = PostUtils.sendHttpJsonPost(url, request.toString(), syncode, puuid);
			String result = rtnValue.getJSONObject("ESB").getString("RESULT");
			String desc = rtnValue.getJSONObject("ESB").getString("DESC");
			if ("E".equals(result)) {
				PostUtils.updateAfterSuccess((SuperVO) bdCommonEvent.getNewObjs()[0], false);
				throw new Exception("接口调用失败:" + desc);
			}

			PostUtils.updateAfterSuccess((SuperVO) bdCommonEvent.getNewObjs()[0], true);
			// 构建日志
			List<SapPurchaseLogVO> logvo_list = new ArrayList<SapPurchaseLogVO>();
			SapPurchaseLogVO logvo = PubUtils.createSNLog("PSToMDMListenner", "", puuid, "", "", requestS,
					rtnValue.toString(), "成功,接口地址：" + url, issuccess_sn, this.getClass().getName());
			logvo_list.add(logvo);
			PubUtils.getSaplog().insertPurchaseLog_RequiresNew(logvo_list);
		} catch (Exception e) {
			List<SapPurchaseLogVO> logvo_list = new ArrayList<SapPurchaseLogVO>();
			SapPurchaseLogVO logvo = PubUtils.createSNLog("PSToMDMListenner", "", puuid, "", "", requestS, null,
					"失败:" + e.getMessage() + ",接口地址:" + url, "N", this.getClass().getName());
			logvo_list.add(logvo);
			PubUtils.getSaplog().insertPurchaseLog_RequiresNew(logvo_list);
			throw new BusinessException(e.getMessage());
		} finally {

		}

	}
}
