Exemple #1
0
        /// <summary>
        /// 聊天计费
        /// </summary>
        /// <param name="messageString"></param>
        private void ProChatFee(string messageString)
        {
            var client = _connections.GetConnection(Context.ConnectionId);

            if (client == null || client.Nodeid == 0)
            {
                this.OnReconnected();
                return;
            }
            var chatFee = new ChatFee(messageString);

            MessageInfo("调用聊天计费逻辑之前");
            var result = chatFeeFacade.ProChatFee(chatFee.ToBytes(), client.Nodeid);

            MessageInfo("调用聊天计费逻辑之后");
            if (result.Count == 0)
            {
                MessageInfo("调用聊天计费逻辑失败");
            }
            foreach (var item in result)
            {
                var receiveClient = _connections.GetConnections().Values.FirstOrDefault(c => c.Nodeid == item.GetNodeid());
                SendChatMessage(receiveClient, item.ToJson());
            }
        }
Exemple #2
0
        /// <summary>
        /// 聊天扣费表添加业务
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="chatFeeBody"></param>
        /// <param name="pXinDb"></param>
        /// <returns></returns>
        private TchatFeehis AddTchatFeehisBusiness(int nodeId, ChatFee chatFee, OracleTransaction transaction)
        {
            TchatFeehis tchatFeehis = new TchatFeehis();

            try
            {
                var feeRuleArray = pXinDb.GetFeeRules(81127, "FeeRules").Propertyvalue.Split('-');

                tchatFeehis.Nodeid       = nodeId;
                tchatFeehis.Feetype      = Convert.ToInt32(chatFee.Body.FeeType);
                tchatFeehis.Businesstype = Convert.ToInt32(chatFee.Body.BusinessType);
                tchatFeehis.Num          = chatFee.Body.Num;
                for (int i = 0; i < feeRuleArray.Length; i++)
                {
                    if (tchatFeehis.Businesstype == i + 1)                                                                //匹配到计费规则
                    {
                        tchatFeehis.Amount = -(tchatFeehis.Num * Convert.ToDecimal(feeRuleArray[i]) * chatFee.Body.Rate); //根据规则计算金额
                        if (tchatFeehis.Amount == 0)
                        {
                            Log.MessageInfo($"匹配到计费规则失败,nodeId={nodeId},tchatFeehis.Num={tchatFeehis.Num},Convert.ToDecimal(feeRuleArray[i])={Convert.ToDecimal(feeRuleArray[i])},chatFee.Body.Rate={chatFee.Body.Rate},tchatFeehis.Amount={tchatFeehis.Amount}");
                            return(null);
                        }
                        break;
                    }
                }
                if (chatFee.Body.ReceiveType == 1)
                {
                    tchatFeehis.Receiver = Convert.ToInt32(chatFee.Body.Receiver);
                }
                else if (chatFee.Body.ReceiveType == 2)
                {
                    tchatFeehis.Groupid = Convert.ToInt32(chatFee.Body.Receiver);
                }
                else if (chatFee.Body.ReceiveType == 3)
                {//讨论组
                    tchatFeehis.Groupid = -1;
                }
                tchatFeehis.Sendtime   = chatFee.Body.FeeTime;
                tchatFeehis.Status     = 1;
                tchatFeehis.Createtime = DateTime.Now;
                tchatFeehis.Remarks    = "聊天计费";
                tchatFeehis.Sequenceid = nodeId.ToString() + chatFee.Header.Sequence_Id;
                if (!pXinDb.AddChatFee(tchatFeehis, transaction))
                {
                    Log.MessageInfo("聊天计费AddChatFee失败,nodeId" + nodeId);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Log.MessageInfo("聊天计费AddChatFee失败," + ex.ToString());
                return(null);
            }
            return(tchatFeehis);
        }
        /// <summary>
        /// 聊天扣费表添加业务
        /// </summary>
        /// <param name="nodeId"></param>
        /// <param name="chatFee"></param>
        /// <returns></returns>
        private TchatFeehis AddTchatFeehisBusiness(int nodeId, ChatFee chatFee)
        {
            TchatFeehis tchatFeehis = new TchatFeehis();

            try
            {
                var feeRuleArray = db.TappConfigSet.FirstOrDefault(p => p.Sid == 81127 && p.Propertyname == "FeeRules").Propertyvalue.Split('-');

                tchatFeehis.Nodeid       = nodeId;
                tchatFeehis.Feetype      = Convert.ToInt32(chatFee.Body.FeeType);
                tchatFeehis.Businesstype = Convert.ToInt32(chatFee.Body.BusinessType);
                tchatFeehis.Num          = chatFee.Body.Num;
                for (int i = 0; i < feeRuleArray.Length; i++)
                {
                    if (tchatFeehis.Businesstype == i + 1)                                                                //匹配到计费规则
                    {
                        tchatFeehis.Amount = -(tchatFeehis.Num * Convert.ToDecimal(feeRuleArray[i]) * chatFee.Body.Rate); //根据规则计算金额
                        break;
                    }
                }
                if (chatFee.Body.ReceiveType == 1)
                {
                    tchatFeehis.Receiver = Convert.ToInt32(chatFee.Body.Receiver);
                }
                else if (chatFee.Body.ReceiveType == 2)
                {
                    tchatFeehis.Groupid = Convert.ToInt32(chatFee.Body.Receiver);
                }
                else if (chatFee.Body.ReceiveType == 3)
                {//讨论组
                    tchatFeehis.Groupid = -1;
                }
                tchatFeehis.Sendtime   = chatFee.Body.FeeTime;
                tchatFeehis.Status     = 1;
                tchatFeehis.Createtime = DateTime.Now;
                tchatFeehis.Remarks    = "聊天计费";
                tchatFeehis.Sequenceid = nodeId.ToString() + chatFee.Header.SequenceId;
                db.TchatFeehisSet.Add(tchatFeehis);
                if (db.SaveChanges() <= 0)
                {
                    logger.Info("聊天计费AddChatFee失败,nodeId" + nodeId);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                logger.Info("聊天计费AddChatFee失败," + ex.ToString());
                return(null);
            }
            return(tchatFeehis);
        }
Exemple #4
0
        /// <summary>
        /// 聊天计费
        /// </summary>
        /// <param name="client"></param>
        /// <param name="buffer"></param>
        private void ProChatFee(CommuTcpClient client, byte[] buffer)
        {
            ChatFee chatFee = new ChatFee(buffer);

            OnRaiseMsgRecvEvent(client, chatFee.ToString());
            ChatFeeResp chatFeeResp = new ChatFeeResp(chatFee.Header.Sequence_Id);
            //处理计费业务
            List <PushPDian> pushPList = new List <PushPDian>();
            var result     = (true, "");
            var key        = client.Nodeid.ToString() + chatFee.Header.Sequence_Id;
            var chatFeeHis = pXinDb.GetChatFeeHisBySeq(client.Nodeid.ToString() + chatFee.Header.Sequence_Id);

            if (chatFeeHis.Hisid == 0)//判断是否是丢包情况重发
            {
                if (!string.IsNullOrEmpty(chatFee.Body.Receiver))
                {
                    result = HandChatFeeBusiness(chatFee, client.Nodeid, pushPList);
                }
                else
                {
                    result = (false, "无效参数:Receiver");
                }
            }
            var tpxinUserinfo = pXinDb.GetUserInfoByNodeId(client.Nodeid);

            chatFeeResp.Body.FeeType = chatFee.Body.FeeType;
            chatFeeResp.Body.Status  = result.Item1 == true?Convert.ToUInt32(1) : Convert.ToUInt32(0);

            chatFeeResp.Body.StatusDesc   = result.Item1 == true ? "成功" : result.Item2;
            chatFeeResp.Body.VDianBalance = tpxinUserinfo.V;
            chatFeeResp.Body.PDianBalance = tpxinUserinfo.P;
            client.SendData(chatFeeResp.ToBytes());
            if (result.Item1)
            {
                if (chatFee.Body.ReceiveType == 1)//接收者类型为用户
                {
                    int receiverid = 0;
                    Int32.TryParse(chatFee.Body.Receiver, out receiverid);
                    var pushPDian = pushPList.FirstOrDefault(p => p.NodeId == receiverid);
                    PushReceive(pushPDian);
                }
                else if (chatFee.Body.ReceiveType == 2 || chatFee.Body.ReceiveType == 3) //接收者类型为群时
                {
                    foreach (var item in pushPList)                                      //遍历群成员进行推送
                    {
                        PushReceive(item);
                    }
                }
            }
        }
Exemple #5
0
        public bool SendChatFee(uint ReceiveType = 1, string Receiver = "4242292")
        {
            ChatFee chatFee = new ChatFee();
            Random  random  = new Random();

            chatFee.Body = new ChatFeeBody
            {
                BusinessType = (uint)random.Next(1, 6),
                FeeType      = (uint)random.Next(1, 3),
                Num          = (uint)random.Next(1, 20),
                ReceiveType  = ReceiveType,
                Receiver     = Receiver,
                //ReceiveType = (uint)2,
                //Receiver = "449",
                FeeTime = DateTime.Now,
                Rate    = random.Next(1, 10)
            };
            SendData(chatFee.ToBytes());
            OnRaiseMsgSendEvent(chatFee.Header.Command_Id, chatFee, "发送消息:" + chatFee.ToString());
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// 聊天包重复发送
        /// </summary>
        /// <param name="ReceiveType"></param>
        /// <param name="Receiver"></param>
        /// <returns></returns>
        public bool SendChatFeeRepeat()
        {
            ChatFee chatFee = new ChatFee();

            chatFee.Header.Sequence_Id = 2019110818353116;
            Random random = new Random();

            chatFee.Body = new ChatFeeBody
            {
                BusinessType = (uint)random.Next(1, 6),
                FeeType      = (uint)random.Next(1, 3),
                Num          = (uint)random.Next(1, 20),
                ReceiveType  = 1,
                Receiver     = "2000",
                //ReceiveType = (uint)2,
                //Receiver = "449",
                FeeTime = DateTime.Now,
                Rate    = random.Next(1, 10)
            };
            SendData(chatFee.ToBytes());
            OnRaiseMsgSendEvent(chatFee.Header.Command_Id, chatFee, "发送消息:" + chatFee.ToString());
            return(true);
        }
        private void OnRaiseMsgSendEvent(byte[] buffer)
        {
            if (MsgSendEventHandler != null)
            {
                string        msg    = string.Empty;
                MessageHeader header = new MessageHeader(buffer);
                switch (header.Command_Id)
                {
                case PXin_COMMAND_TYPE.Login:
                    msg = new Login(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.LoginResp:
                    msg = new LoginResp(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.Logout:
                    msg = new Wt_Logout(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.LogoutResp:
                    msg = new LogoutResp(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.Active:
                    msg = new Active(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ActiveResp:
                    msg = new ActiveResp(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFee:
                    msg = new ChatFee(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeeResp:
                    msg = new ChatFeeResp(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeePush:
                    msg = new ChatFeePush(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeeRateSet:
                    msg = new ChatFeeRateSet(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeeRateSetResp:
                    msg = new ChatFeeRateSetResp(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeeRateQuery:
                    msg = new ChatFeeRateQuery(buffer).ToString();
                    break;

                case PXin_COMMAND_TYPE.ChatFeeRateQueryResp:
                    msg = new ChatFeeRateQueryResp(buffer).ToString();
                    break;

                default:
                    break;
                }
                //发布"消息发送"事件
                if (header.Command_Id != PXin_COMMAND_TYPE.Active && header.Command_Id != PXin_COMMAND_TYPE.ActiveResp)
                {
                    MsgSendEventHandler?.Invoke(this, new MessageEventArgs(string.Empty, Identity, msg));
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// 处理聊天计费业务
        /// </summary>
        /// <param name="chatFeeBody"></param>
        /// <param name="nodeId"></param>
        /// <param name="pushPList"></param>
        /// <returns></returns>
        private (bool, string) HandChatFeeBusiness(ChatFee chatFee, int nodeId, List <PushPDian> pushPList)
        {
            var result = (false, "");

            using (var connection = OracleHelper.GetConnection())
            {
                connection.Open();
                //开启一个事务
                OracleTransaction transaction = connection.BeginTransaction();
                try
                {
                    TchatFeehis tchatFeehis = AddTchatFeehisBusiness(nodeId, chatFee, transaction);
                    if (tchatFeehis == null)
                    {
                        transaction.Rollback();
                        return(false, "聊天计费失败,请联系管理员!");
                    }
                    var transferId = Guid.NewGuid().ToString();
                    if (chatFee.Body.ReceiveType == 1)//个人
                    {
                        //发送者计费操作
                        result = SendFeeOperation(nodeId, tchatFeehis.Amount, 1, transaction, transferId);
                        //接收者计费操作
                        if (result.Item1)
                        {
                            result = ReceiverFeeOperation(tchatFeehis.Receiver, tchatFeehis.Amount, transaction, pushPList, transferId);
                        }
                    }
                    else if (chatFee.Body.ReceiveType == 2)//群
                    {
                        List <int> userIdList = pXinDb.GetGroupUserId(tchatFeehis.Groupid).ToList();
                        if (userIdList.Count == 0)
                        {
                            transaction.Rollback();
                            return(false, "群组不存在,请创建一个群组!");
                        }
                        //发送者
                        result = SendFeeOperation(nodeId, tchatFeehis.Amount, userIdList.Count(), transaction, transferId);
                        //接收者
                        if (result.Item1)
                        {
                            foreach (var userId in userIdList.Where(p => p != nodeId))
                            {
                                result = ReceiverFeeOperation(userId, tchatFeehis.Amount /** (userIdList.Count() + 1)*/, transaction, pushPList, transferId);
                                if (!result.Item1)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    else if (chatFee.Body.ReceiveType == 3)
                    {
                        List <int> useridList       = new List <int>();
                        var        userids          = chatFee.Body.Receiver.Split(',');
                        int        validUseridCount = 0;//有效消息接收者ID数量
                        foreach (var item in userids)
                        {
                            try
                            {
                                useridList.Add(Convert.ToInt32(item));
                            }
                            catch (Exception) { }
                        }
                        if (useridList.Count == 0)
                        {
                            transaction.Rollback();
                            return(false, "参数格式错误:Receiver");
                        }
                        //消息接收者获得金额
                        foreach (var userId in useridList.Where(p => p != nodeId))
                        {
                            result = ReceiverFeeOperation(userId, tchatFeehis.Amount /** (userIdList.Count() + 1)*/, transaction, pushPList, transferId);
                            if (result.Item1)
                            {
                                validUseridCount++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        //消息发送者扣除金额
                        result = SendFeeOperation(nodeId, tchatFeehis.Amount, validUseridCount + 1, transaction, transferId);
                    }
                    if (result.Item1)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Log.MessageInfo(ex.ToString());
                }
                finally
                {
                    connection.Close();
                }
            }
            return(result);
        }
        /// <summary>
        /// 处理聊天计费业务
        /// </summary>
        /// <param name="chatFee"></param>
        /// <param name="nodeId"></param>
        /// <param name="pushPList"></param>
        /// <returns></returns>
        public (bool, string) HandChatFeeBusiness(ChatFee chatFee, int nodeId, List <PushPDian> pushPList)
        {
            var result = (false, "");

            //开启一个事务
            db.BeginTransaction();
            try
            {
                TchatFeehis tchatFeehis = AddTchatFeehisBusiness(nodeId, chatFee);
                if (tchatFeehis == null)
                {
                    db.Rollback();
                    return(false, "聊天计费失败,请联系管理员!");
                }
                var transferId = Guid.NewGuid().ToString();
                if (chatFee.Body.ReceiveType == 1)//个人
                {
                    //发送者计费操作
                    result = SendFeeOperation(nodeId, tchatFeehis.Amount, 1, transferId);
                    //接收者计费操作
                    if (result.Item1)
                    {
                        result = ReceiverFeeOperation(tchatFeehis.Receiver, tchatFeehis.Amount, pushPList, transferId);
                    }
                }
                else if (chatFee.Body.ReceiveType == 2)//群
                {
                    var userIdList = db.TchatGroupUserSet.Where(p => p.Groupid == tchatFeehis.Groupid).Select(p => p.Userid).ToList();
                    if (userIdList.Count == 0)
                    {
                        db.Rollback();
                        return(false, "群组不存在,请创建一个群组!");
                    }
                    //发送者
                    result = SendFeeOperation(nodeId, tchatFeehis.Amount, userIdList.Count(), transferId);
                    //接收者
                    if (result.Item1)
                    {
                        foreach (var userId in userIdList.Where(p => p != nodeId))
                        {
                            result = ReceiverFeeOperation(userId, tchatFeehis.Amount, pushPList, transferId);
                            if (!result.Item1)
                            {
                                break;
                            }
                        }
                    }
                }
                else if (chatFee.Body.ReceiveType == 3)
                {
                    List <int> useridList       = new List <int>();
                    var        userids          = chatFee.Body.Receiver.Split(',');
                    int        validUseridCount = 0;//有效消息接收者ID数量
                    foreach (var item in userids)
                    {
                        try
                        {
                            useridList.Add(Convert.ToInt32(item));
                        }
                        catch (Exception) { }
                    }
                    if (useridList.Count == 0)
                    {
                        db.Rollback();
                        return(false, "参数格式错误:Receiver");
                    }
                    //消息接收者获得金额
                    foreach (var userId in useridList.Where(p => p != nodeId))
                    {
                        result = ReceiverFeeOperation(userId, tchatFeehis.Amount, pushPList, transferId);
                        if (result.Item1)
                        {
                            validUseridCount++;
                        }
                    }
                    //消息发送者扣除金额
                    result = SendFeeOperation(nodeId, tchatFeehis.Amount, validUseridCount + 1, transferId);
                }
                if (result.Item1)
                {
                    db.Commit();
                }
                else
                {
                    db.Rollback();
                }
            }
            catch (Exception ex)
            {
                db.Rollback();
                logger.Info(ex.ToString());
            }
            return(result);
        }