package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp.proto.AkernelMI1.AccountCommandRsp;
import com.muchinfo.mtp.proto.AkernelMI1.AccountFundInfoRsp;
import com.muchinfo.mtp3common.enumtype.FunCodeConstants;
import com.muchinfo.mtp3common.vo.Calculator;
import com.muchinfo.mtp3common.utils.PropertyUtils;
import com.muchinfo.mtp3common.vo.ProtoHelper;
import com.muchinfo.mtp3common.utils.SerialNumberUtils;
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;


/**
 *
 * @Title: MoneyQueueListener
 * @Company: 多元世纪
 * @author liu.bingbing
 * @date 2018年1月19日 上午9:52:16
 * @Description:
 */
@Component
public class MoneyQueueListener implements MessageListener {
	private final static Logger logger = LoggerFactory.getLogger(MoneyQueueListener.class) ;

	@Override
	@RabbitListener(queues = "moneyQueue")
	public void onMessage(Message message) {
		byte[] msgBody = message.getBody();
		//ProtoHelper

		int serverId = ProtoHelper.getServerIdForNTAS(msgBody);
		int funCode = ProtoHelper.getFunCodeForNTAS(msgBody);

		logger.debug("get message-----------MoneyQueueListener-{}-------------------",funCode);
		if(serverId != PropertyUtils.serverId){
			logger.debug("非本服务器消息-------------------------{}-----------",funCode);
			return;
		}
		try{
			byte[] body = ProtoHelper.getProtoForNTAS(msgBody);
			switch (funCode){
			case FunCodeConstants.FunCode_Trade_AccountFundInfoRsp:{
				AccountFundInfoRsp rsp = AccountFundInfoRsp.parseFrom(body);
				long uuid = rsp.getOrderId();
				SerialNumberUtils.putMessageMap(uuid, rsp);
				Calculator c = SerialNumberUtils.lockMap.remove(uuid);
				if(c != null){
					c.start();
				}

				break;
			}
			case FunCodeConstants.FunCode_Trade_AccountCommandRsp:{
				AccountCommandRsp rsp = AccountCommandRsp.parseFrom(body);
				logger.debug(rsp.toBuilder().build().toString());
				String uuid = rsp.getHeader().getUUID();
				if(uuid.startsWith(
						String.valueOf(SerialNumberUtils.prefix_warehouse))){
					//后续有此类情况可使用public Calculator(Long uuid, int funccode) 构造函数
					/**  出库出库注销复审通过请求 发出之后会有两个响应的uuid一样导致异常所以加这个判断
					 * 1376322-出库注销复审通过响应-ReceiptOutApplyLogoutRsp=--------------------Header {
					 FunCode: 1376322
					 ClientTime: 1645510103393
					 UUID: "7041645510103360"
					 MarketID: 4
					 ResponseTopic: "wr_response"
					 }
					 RetCode: 0
					 Common {
					 UserID: 4000
					 RelatedOrderID: 7041645510103360
					 TradeID: 6101645510072000001
					 AccountID: 250000000004
					 }

					 Header {
					 FunCode: 262147
					 AccountID: 250000000004
					 ClientTime: 1645510103393
					 UUID: "7041645510103360"
					 MarketID: 4
					 ResponseTopic: "money_rsp"
					 }
					 RetCode: 0
					 AccountId: 250000000004
					 BusinessCode: 624
					 RelatedOrderId: 7041645510103360
					 TradeId: 6101645510072000001
					 */
				} else {
					boolean flag = false;
					try {
						Long.parseLong(uuid);
						flag = true;
					} catch (NumberFormatException e) {
						logger.error(FunCodeConstants.FunCode_Trade_AccountCommandRsp+"类型转换失败，");
					}
					if (flag) {
						SerialNumberUtils.putMessageMap(Long.valueOf(uuid), rsp);
						//能过funccode 同上“uuid.startsWith”的处理，
						//后续有此类情况可使用public Calculator(Long uuid, int funccode) 构造函数
						//funccode 不为0时，则必须为FunCode_Trade_AccountCommandReq
						Calculator c1 = SerialNumberUtils.lockMap.get(Long.valueOf(uuid));
						if (c1 != null && (c1.funccode == 0 || (c1.funccode != 0 && c1.funccode == FunCodeConstants.FunCode_Trade_AccountCommandReq))) {
							Calculator c = SerialNumberUtils.lockMap.remove(Long.valueOf(uuid));
							if (c != null) {
								c.start();
							}
						}
					}
				}
				break;
			}
			default:break;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
}
