package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp.proto.WarehouseInputMI1.ContractForceRsp;
import com.muchinfo.mtp.proto.WarehouseReceiptMI1.*;
import com.muchinfo.mtp.proto.WarehouseReceiptMI2;
import com.muchinfo.mtp.proto.WarehouseReceiptMI2.GetWRFactorTypeIDRsp;
import com.muchinfo.mtp.proto.WarehouseReceiptMI2.WRCreditApplyAuditRsp;
import com.muchinfo.mtp3common.enumtype.FunCodeConstants;
import com.muchinfo.mtp3common.utils.PropertyUtils;
import com.muchinfo.mtp3common.utils.SerialNumberUtils;
import com.muchinfo.mtp3common.vo.Calculator;
import com.muchinfo.mtp3common.vo.ProtoHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WarehouseQueueListener implements MessageListener {
	private final static Logger logger = LoggerFactory.getLogger(WarehouseQueueListener.class) ;
	
	@Override
	@RabbitListener(queues = "warehouseQueue")
	public void onMessage(Message message) {
		logger.debug("GET MESSAGE---------------WarehouseQueueListener---------------------");
		byte[] msgBody = message.getBody();
		int serverId = ProtoHelper.getServerIdForNTAS(msgBody);
		int funCode = ProtoHelper.getFunCodeForNTAS(msgBody);
		if (serverId != PropertyUtils.serverId) {
			logger.debug("非本服务器消息-------------------------{}-----------", funCode);
			return;
		}
		try {
			byte[] body = ProtoHelper.getProtoForNTAS(msgBody);
			switch (funCode) {
				//仓单注册审核
				case FunCodeConstants.FunCode_Trade_ReceiptRegisterSuccessRsp: {
					ReceiptRegisterSuccessRsp rsp = ReceiptRegisterSuccessRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getCommon().getTradeID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 仓单转定单申请回应(ReceiptToHoldApplyRsp)
				case FunCodeConstants.FunCode_Trade_ReceiptToHoldApplyRsp: {
					ReceiptToHoldApplyRsp rsp = ReceiptToHoldApplyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getRelatedOrderID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 定单转仓单申请回应(HoldToReceiptApplyRsp)
				case FunCodeConstants.FunCode_Trade_HoldToReceiptApplyRsp: {
					HoldToReceiptApplyRsp rsp = HoldToReceiptApplyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getRelatedOrderID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 仓单转定单审核回应(ReceiptToHoldVerifyRsp)
				case FunCodeConstants.FunCode_Trade_ReceiptToHoldVerifyRsp: {
					ReceiptToHoldVerifyRsp rsp = ReceiptToHoldVerifyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getRelatedOrderID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 定单转仓单审核回应(HoldToReceiptVerifyRsp)
				case FunCodeConstants.FunCode_Trade_HoldToReceiptVerifyRsp: {
					HoldToReceiptVerifyRsp rsp = HoldToReceiptVerifyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getRelatedOrderID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 提货申请回应(LadingApplyRsp)
				case FunCodeConstants.FunCode_Trade_LadingApplyRsp: {
					LadingApplyRsp rsp = LadingApplyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getCommon().getRelatedOrderID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 提货审核回应(LadingVerifyRsp)
				case FunCodeConstants.FunCode_Trade_LadingVerifyRsp: {
					LadingVerifyRsp rsp = LadingVerifyRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getCommon().getTradeID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				// 提货删除回应(LadingCancelRsp)
				case FunCodeConstants.FunCode_Trade_LadingCancelRsp: {
					LadingCancelRsp rsp = LadingCancelRsp.parseFrom(body);
					logger.debug("收到响应：{}", rsp.toString());
					Long uuid = rsp.getCommon().getTradeID();
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_HoldToReceiptDelistRsp: {    //退市转出 响应
					HoldToReceiptDelistRsp holderRsp = HoldToReceiptDelistRsp.parseFrom(body);
					logger.debug("HoldToReceiptDelistRsp={}", holderRsp.toString());
					Long uuid = Long.valueOf(holderRsp.getHeader().getUUID());
					SerialNumberUtils.endCalculator(uuid, holderRsp);
					break;
				}
				case FunCodeConstants.FunCode_Trade_FreightConfirmRsp: {
					//运费确认
					FreightConfirmRsp rsp = FreightConfirmRsp.parseFrom(body);
					logger.warn("FreightConfirmRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_FreightRejectRsp: {
					//运费拒绝
					FreightRejectRsp rsp = FreightRejectRsp.parseFrom(body);
					logger.warn("FreightRejectRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ReceiveConfirmRsp: {
					//收货确认
					ReceiveConfirmRsp rsp = ReceiveConfirmRsp.parseFrom(body);
					logger.warn("ReceiveConfirmRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_RetransmitDistributeRsp: {
					//重新发布
					RetransmitDistributeRsp rsp = RetransmitDistributeRsp.parseFrom(body);
					logger.warn("RetransmitDistributeRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}

				case FunCodeConstants.FunCode_Trade_ContractForceRsp: {
					//提货计划审核通过响应
					ContractForceRsp rsp = ContractForceRsp.parseFrom(body);
					logger.warn("FunCode_Trade_ContractForceRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ReceiptInApplyRegisterRsp: {
					//入库注册复审通过响应
					ReceiptInApplyRegisterRsp rsp = ReceiptInApplyRegisterRsp.parseFrom(body);
					logger.warn("ReceiptInApplyRegisterRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ReceiptOutApplyLogoutRsp: {
					//出库注销复审通过响应
					ReceiptOutApplyLogoutRsp rsp = ReceiptOutApplyLogoutRsp.parseFrom(body);
					logger.warn(FunCodeConstants.FunCode_Trade_ReceiptOutApplyLogoutRsp + "-出库注销复审通过响应-ReceiptOutApplyLogoutRsp-------------={}", rsp.toString());
					logger.warn(FunCodeConstants.FunCode_Trade_ReceiptOutApplyLogoutRsp + "-出库注销复审通过响应-ReceiptOutApplyLogoutRsp=--------------------{}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ReceiptOutApplyLogoutRefuseRsp: {
					//出库注销复审拒绝响应
					ReceiptOutApplyLogoutRefuseRsp rsp = ReceiptOutApplyLogoutRefuseRsp.parseFrom(body);
					logger.warn("ReceiptOutApplyLogoutRefuseRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ReceiptOutApplyRsp: {
					//出库预约申请响应
					ReceiptOutApplyRsp rsp = ReceiptOutApplyRsp.parseFrom(body);
					logger.warn("ReceiptOutApplyRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_SpotContractAuditRsp: {
					//现货合同审核响应
					WarehouseReceiptMI2.SpotContractAuditRsp rsp = WarehouseReceiptMI2.SpotContractAuditRsp.parseFrom(body);
					logger.warn("SpotContractAuditRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ContractReckonRsp: {
					//现货合同结算响应
					WarehouseReceiptMI2.ContractReckonRsp rsp = WarehouseReceiptMI2.ContractReckonRsp.parseFrom(body);
					logger.warn("ContractReckonRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_MakeOutAnInvoiceRsp: {
					//现货合同开票响应
					MakeOutAnInvoiceRsp rsp = MakeOutAnInvoiceRsp.parseFrom(body);
					logger.warn("MakeOutAnInvoiceRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ContractDeliveryRsp: {
					//现货合同交收响应
					ContractDeliveryRsp rsp = ContractDeliveryRsp.parseFrom(body);
					logger.warn("ContractDeliveryRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ContractPointOrderRsp: {
					//现货合同点价非业务响应
					ContractPointOrderRsp rsp = ContractPointOrderRsp.parseFrom(body);
					logger.warn("ContractPointOrderRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ContractCancelNonBusinessQtyRsp: {
					//现货合同撤销非业务响应
					ContractCancelNonBusinessQtyRsp rsp = ContractCancelNonBusinessQtyRsp.parseFrom(body);
					logger.warn("ContractCancelNonBusinessQtyRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ContractCloseRsp: {
					//现货合同完结响应
					WarehouseReceiptMI2.ContractReckonRsp rsp = WarehouseReceiptMI2.ContractReckonRsp.parseFrom(body);
					logger.warn("ContractCloseRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_BizDeliveryRsp: {
					//现货交收响应
					WarehouseReceiptMI2.BizDeliveryRsp rsp = WarehouseReceiptMI2.BizDeliveryRsp.parseFrom(body);
					logger.warn("Trade_BizDeliveryRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ClientSpotContractInvoiceApplyAuditRsp: {
					//现货合同开票申请审核响应
					WarehouseReceiptMI2.ClientSpotContractInvoiceApplyAuditRsp rsp = WarehouseReceiptMI2.ClientSpotContractInvoiceApplyAuditRsp.parseFrom(body);
					logger.warn("ClientSpotContractInvoiceApplyAuditRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_ClientSpotContractReckonApplyAuditRsp: {
					//现货合同结算申请审核响应
					WarehouseReceiptMI2.ClientSpotContractReckonApplyAuditRsp rsp = WarehouseReceiptMI2.ClientSpotContractReckonApplyAuditRsp.parseFrom(body);
					logger.warn("ClientSpotContractReckonApplyAuditRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_WRCreditApplyAuditRsp: {
					// 仓单授信申请审核响应
					WRCreditApplyAuditRsp rsp = WRCreditApplyAuditRsp.parseFrom(body);
					logger.warn("WRCreditApplyAuditRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_GetWRFactorTypeIDRsp: {
					// 仓单要素获取申请响应
					GetWRFactorTypeIDRsp rsp = GetWRFactorTypeIDRsp.parseFrom(body);
					logger.warn("GetWRFactorTypeIDRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_WRFactorTypeNameUpdateRsp: {
					// 仓单要素类型名称更新响应
					WRFactorTypeNameUpdateRsp rsp = WRFactorTypeNameUpdateRsp.parseFrom(body);
					logger.warn("WRFactorTypeNameUpdateRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				default:
					break;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
}
