package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp.proto.BankMI1;
import com.muchinfo.mtp.proto.BankMI2;
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;

@Component
public class BankQueueListener implements MessageListener {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    @RabbitListener(queues = "bankQueue")
    public void onMessage(Message message) {
        logger.debug("GET MESSAGE---------------BankQueueListener---------------------");
        byte[] msgBody = message.getBody();
        int serverId = ProtoHelper.getServerIdForNTAS(msgBody);
        int funCode = ProtoHelper.getFunCodeForNTAS(msgBody);
        if(serverId != PropertyUtils.serverId){
            logger.debug("非本服务器消息----sid={}--------------------funcode={}-----",serverId,funCode);
            return;
        }
        try{
            byte[] body = ProtoHelper.getProtoForNTAS(msgBody);
            switch (funCode){
                //签约
                case FunCodeConstants.FunCode_Trade_t2bBankSignRsp:{
                    BankMI1.t2bBankSignRsp rsp = BankMI1.t2bBankSignRsp.parseFrom(body);
                    logger.debug(rsp.toString());
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //解约
                case FunCodeConstants.FunCode_Trade_t2bBankCancelSignRsp:{
                    BankMI1.t2bBankCancelSignRsp rsp = BankMI1.t2bBankCancelSignRsp.parseFrom(body);
                    logger.debug(rsp.toString());
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //出金
                case FunCodeConstants.FunCode_Trade_t2bBankWithdrawRsp:{
                    BankMI1.t2bBankWithdrawRsp rsp = BankMI1.t2bBankWithdrawRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //入金
                case FunCodeConstants.FunCode_Trade_t2bBankDepositRsp:{
                    BankMI1.t2bBankDepositRsp rsp = BankMI1.t2bBankDepositRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }

                //母账户新出入金
                case FunCodeConstants.FunCode_Trade_t2bManageAdjutAmountRsp:{
                    BankMI2.t2bManageAdjutAmountRsp rsp = BankMI2.t2bManageAdjutAmountRsp.parseFrom(body);

                    logger.debug("FunCode_Trade_t2bManageAdjutAmountRsp:"+rsp.toString());

                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //子账户新出入金
                case FunCodeConstants.FunCode_Trade_t2bManageMoveAmountRsp:{
                    BankMI2.t2bManageMoveAmountRsp rsp = BankMI2.t2bManageMoveAmountRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //短信
                case FunCodeConstants.FunCode_Trade_t2bSMSVerificationCodeRsp:{
                    BankMI1.t2bSMSVerificationCodeRsp rsp = BankMI1.t2bSMSVerificationCodeRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //平台审核结果通知应答(t2bExchConfirmBusinessRsp)
                case FunCodeConstants.FunCode_Trade_t2bExchConfirmBusinessRsp:{
                    BankMI2.t2bExchConfirmBusinessRsp rsp = BankMI2.t2bExchConfirmBusinessRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    logger.debug(rsp.toString());
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                case FunCodeConstants.FunCode_Trade_t2bQueryExchAccInfosRsp:{
                    BankMI1.t2bQueryExchAccInfosRsp rsp = BankMI1.t2bQueryExchAccInfosRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    logger.debug(rsp.toString());
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                //平台单边账调整应答(t2bExchDealCheckErrRsp)
                case FunCodeConstants.FunCode_Trade_t2bExchDealCheckErrRsp:{
                    BankMI2.t2bExchDealCheckErrRsp rsp = BankMI2.t2bExchDealCheckErrRsp.parseFrom(body);
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                // 账户信息查询应答(t2bQueryAccInfosRsp)
                case FunCodeConstants.FunCode_Trade_t2bQueryAccInfosRsp:{
                    BankMI1.t2bQueryAccInfosRsp rsp = BankMI1.t2bQueryAccInfosRsp.parseFrom(body);
                    logger.debug(rsp.toString());
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                case FunCodeConstants.FunCode_Trade_t2bExchOutInBankRsp:{
                    BankMI1.t2bExchOutInBankRsp rsp = BankMI1.t2bExchOutInBankRsp.parseFrom(body);
                    logger.debug(rsp.toString());
                    Long uuid = rsp.getExtOperatorID();
                    SerialNumberUtils.putMessageMap(uuid, rsp);
                    Calculator c = SerialNumberUtils.lockMap.get(uuid);
                    if(c != null){
                        c.start();
                    }
                    break;
                }
                case FunCodeConstants.FunCode_Trade_t2bConfirmPassCapitalTransferRsp:{
                    BankMI2.t2bConfirmPassCapitalTransferRsp rsp = BankMI2.t2bConfirmPassCapitalTransferRsp.parseFrom(body);
                    logger.debug(rsp.toString());
                    Long uuid = rsp.getExtOperatorID();
                    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();
        }
    }
}
