package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp.proto.TkernelMI1.*;
import com.muchinfo.mtp.proto.TkernelMI2;
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 OrderListener implements MessageListener {
	
	private final static Logger logger = LoggerFactory.getLogger(OrderListener.class);
	@Override
	@RabbitListener(queues = "tradeCpRspQueue")
	public void onMessage(Message message) {
		logger.debug("GET MESSAGE---------------OrderListener---------------------");
		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_ManageAmendHolderRsp:{
					ManageAmendHolderRsp rsp = ManageAmendHolderRsp.parseFrom(body);
					logger.debug(rsp.toBuilder().build().toString());
					Long uuid = Long.valueOf(rsp.getEntrustID());
	                SerialNumberUtils.putMessageMap(uuid, rsp);
	                Calculator c = SerialNumberUtils.lockMap.remove(uuid);
	                if (c != null) {
	                    c.start();
	                }else{
	                	//修改状态
	                	MessageHead hd = rsp.getHeader();
	                	StringBuffer sb = new StringBuffer();
	            		sb.append(hd.getAccessID()).append(hd.getGoodsID());
	            		Long key = Long.valueOf(sb.toString());
	                	Long applyid = uuid - key;
	                	
	                	updatePositionApplyService.editApplyByMsg(applyid, rsp.getRetCode());
	                	
	                }
					break;
				}*/
				case FunCodeConstants.FunCode_Trade_ManageForceCloseRsp:{
					ManageForceCloseRsp rsp = ManageForceCloseRsp.parseFrom(body);
					logger.debug(rsp.toBuilder().build().toString());
					Long uuid = Long.valueOf(rsp.getEntrustID());
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                    if (c != null) {
                        c.start();
                    }
					break;
				}
				/*case FunCodeConstants.FunCode_Trade_PreSaleListingInfoRsp:{
					PreSaleListingInfoRsp rsp = PreSaleListingInfoRsp.parseFrom(body);
					logger.debug(rsp.toBuilder().build().toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                    if (c != null) {
                        c.start();
                    }else{
                    	PresaleOrderapply apply = presaleOrderapplyService.get(rsp.getApplyID());
                    	if(rsp.getRetCode() == 0){
                    		apply.setApplystatus(3);
                    	}else{
                    		apply.setApplystatus(5);
                    		apply.setRemark(ProtoErrorCodes.resultMsg(rsp.getRetCode()));
                    	}
                    	presaleOrderapplyService.update(apply);
                    }
					break;
				}*/
				case FunCodeConstants.FunCode_Trade_PreSaleCancelRsp:{
					PreSaleCancelRsp rsp = PreSaleCancelRsp.parseFrom(body);
					logger.debug(rsp.toBuilder().build().toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.remove(uuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FunCode_Trade_GoodsDelistRsp: {
					GoodsDelistRsp rsp = GoodsDelistRsp.parseFrom(body);
					logger.debug("GoodsDelistRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.endCalculator(uuid, rsp);
					break;
				}
				case FunCodeConstants.FunCode_Trade_DelistingRepoRsp: {
					DelistingRepoRsp rsp = DelistingRepoRsp.parseFrom(body);
					logger.debug("DelistingRepoRsp={}", rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                    SerialNumberUtils.endCalculator(uuid,rsp);
                    break;
                }
                case FunCodeConstants.FunCode_Trade_CancelOrderRsp:{
                	CancelOrderRsp rsp = CancelOrderRsp.parseFrom(body);
                	logger.debug(rsp.toBuilder().build().toString());
                	Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                	SerialNumberUtils.putMessageMap(uuid, rsp);
                	Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                	if (c != null) {
                        c.start();
                    }
                	break;
                }
                case FunCodeConstants.FunCode_Trade_ManageAmendTradeRsp:{
                	ManageAmendTradeRsp rsp = ManageAmendTradeRsp.parseFrom(body);
                	logger.debug(rsp.toBuilder().build().toString());
                	Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                	SerialNumberUtils.putMessageMap(uuid, rsp);
                	Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                	if (c != null) {
                        c.start();
                    }
                	break;
                }
                case FunCodeConstants.FunCode_Trade_OrderTransferRsp:{
                    OrderTransferRsp rsp = OrderTransferRsp.parseFrom(body);
                    logger.debug("TkernelMI1.OrderTransferRsp={}",rsp.toString());
                    Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                    SerialNumberUtils.endCalculator(uuid,rsp);
                    break;
                }
                
                case FunCodeConstants.FunCode_Trade_ManageAmendOrderRsp:{
                	ManageAmendOrderRsp rsp = ManageAmendOrderRsp.parseFrom(body);
                	logger.debug("ManageAmendOrderRsp:"+rsp.toBuilder().build().toString());
                	Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                	SerialNumberUtils.putMessageMap(uuid, rsp);
                	Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                	if (c != null) {
                        c.start();
                    }
                	break;
                }
                case FunCodeConstants.FunCode_Trade_ManageUnBoundOrderRsp:{
                	ManageUnBoundOrderRsp rsp = ManageUnBoundOrderRsp.parseFrom(body);
                	logger.debug("ManageUnBoundOrderRsp:"+rsp.toBuilder().build().toString());
                	Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                	SerialNumberUtils.putMessageMap(uuid, rsp);
                	Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                	if (c != null) {
                        c.start();
                    }
                	break;
                }
                case FunCodeConstants.FunCode_Trade_SwapProtoCloseApplyPassRsp:{
                	SwapProtoCloseApplyPassRsp rsp = SwapProtoCloseApplyPassRsp.parseFrom(body);
                	logger.debug("FunCode_Trade_SwapProtoCloseApplyPassRsp:"+rsp.toBuilder().build().toString());
                	Long uuid = Long.valueOf(rsp.getHeader().getUUID());
                	SerialNumberUtils.putMessageMap(uuid, rsp);
                	Calculator c = SerialNumberUtils.lockMap.remove(uuid);
                	if (c != null) {
                        c.start();
                    }
                	break;
                }
				case FunCodeConstants.FuncCode_HolderTransferApplyRsp: {
					//持仓过户申请接口响应
					TkernelMI2.HolderTransferApplyRsp rsp = TkernelMI2.HolderTransferApplyRsp.parseFrom(body);
					Long endUuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(endUuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(endUuid);
					if (c != null) {
						c.start();
					}
					break;
				}
				case FunCodeConstants.FuncCode_HolderTransferRefuseRsp: {
					//持仓过户审核拒绝接口响应
					TkernelMI2.HolderTransferRefuseRsp rsp = TkernelMI2.HolderTransferRefuseRsp.parseFrom(body);
					Long endUuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(endUuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(endUuid);
					if (c != null) {
						c.start();
					}
					break;
				}

				// 定金转让持仓操作响应
				/*case FunCodeConstants.FuncCode_DepositTransferOperateRsp : {
					TkernelMI2.DepositTransferOperateRsp rsp = TkernelMI2.DepositTransferOperateRsp.parseFrom(body);
					Long endUuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(endUuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(endUuid);
					if (c != null) {
						c.start();
					}
					break;
				}*/
			}
		}catch (Exception e) {
            logger.error("解包异常，功能编号为:" + funCode,e);
        }
	}
}
