Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <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);
        }