package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp3common.enumtype.FunCodeConstants;
import com.muchinfo.mtp3common.vo.Calculator;
import com.muchinfo.mtp3common.vo.ProtoHelper;
import com.muchinfo.mtp3common.utils.SerialNumberUtils;
import com.muchinfo.mtp.proto.TkernelMI1.*;
import com.muchinfo.mtp.proto.TkernelMI2;
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 TradeCpListener implements MessageListener {

    private final static Logger logger = LoggerFactory.getLogger(TradeCpListener.class);

    @Override
    @RabbitListener(queues = "tradeCpRspQueue")
    public void onMessage(Message message) {
        byte[] msgBody = message.getBody();
        int funCode = ProtoHelper.getFunCodeForNTAS(msgBody);
        Long uuid = 0L;

        try {
            byte[] body = ProtoHelper.getProtoForNTAS(msgBody);
            switch (funCode) {
                case FunCodeConstants.FunCode_Trade_CpPositionTransferInRetrialPassRsp: {
                    //头寸转入复审通过响应(196680)
                    CpPositionTransferInRetrialPassRsp rsp = CpPositionTransferInRetrialPassRsp.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.FunCode_Trade_CpPresaleRsp: {
                    //产能预售申请响应(196684)
                    CpPresaleRsp rsp = CpPresaleRsp.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.FunCode_Trade_CpPresaleOperateRsp: {
                    //产能预售申请撤销、拒绝应答
                    CpPresaleOperateRsp rsp = CpPresaleOperateRsp.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.FunCode_Trade_CpPositionTransferInCancelRetrialPassRsp: {
                    //产能预售注销复审应答
                    CpPositionTransferInCancelRetrialPassRsp rsp = CpPositionTransferInCancelRetrialPassRsp.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.FunCode_Trade_CpPresalePaymentRsp: {
                    //补齐尾款应答
                    CpPresalePaymentRsp rsp = CpPresalePaymentRsp.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.FunCode_Trade_TerminatePreSellRsp: {
                    //预售终止
                    TerminatePreSellRsp rsp = TerminatePreSellRsp.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.FunCode_Trade_CpPresaleFinishRsp: {
                    //预售完成应答
                    CpPresaleFinishRsp rsp = CpPresaleFinishRsp.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.FunCode_Trade_OrderPresaleLotteryRsp: {
                    //定金预售摇号应答
                    OrderPresaleLotteryRsp rsp = OrderPresaleLotteryRsp.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.FunCode_Trade_OrderPresaleFinishRsp: {
                    //定金预售完成应答
                    TkernelMI2.OrderPresaleFinishRsp rsp = TkernelMI2.OrderPresaleFinishRsp.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.FunCode_Trade_AdjustmentTransferDepositRatioRsp: {
                    // 调整商品转让定金比例应答
                    AdjustmentTransferDepositRatioRsp rsp = AdjustmentTransferDepositRatioRsp.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.FunCode_DeliveryManageOperatorRsp: {
                    // 交收管理端操作接口请求-YJHJ
                    TkernelMI2.DeliveryManageOperatorRsp rsp = TkernelMI2.DeliveryManageOperatorRsp.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_RiskCloseRecoverRsp: {
                    // 斩仓恢复接口
                    TkernelMI2.RiskCloseRecoverRsp rsp = TkernelMI2.RiskCloseRecoverRsp.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_DeliveryProviderPaySellAmountRsp : {
                    TkernelMI2.DeliveryProviderPaySellAmountRsp cpRsp = TkernelMI2.DeliveryProviderPaySellAmountRsp.parseFrom(body);
                    Long endUuid = Long.valueOf(cpRsp.getHeader().getUUID());
                    SerialNumberUtils.putMessageMap(endUuid, cpRsp);
                    Calculator c = SerialNumberUtils.lockMap.get(endUuid);
                    if (c != null) {
                        c.start();
                    }
                    break;
                }

                // 录入建平仓接口响应
                case FunCodeConstants.FuncCode_InputOpenCloseOrderRsp : {
                    TkernelMI2.InputOpenCloseOrderRsp cpRsp = TkernelMI2.InputOpenCloseOrderRsp.parseFrom(body);
                    Long endUuid = Long.valueOf(cpRsp.getHeader().getUUID());
                    SerialNumberUtils.putMessageMap(endUuid, cpRsp);
                    Calculator c = SerialNumberUtils.lockMap.get(endUuid);
                    if (c != null) {
                        c.start();
                    }
                    break;
                }

            }
        } catch (Exception e) {
            logger.info("解包异常，功能编号为：" + funCode);
        }
    }
}
