Esempio n. 1
0
        public async Task <SMessageHistory> QueryById(Guid id)
        {
            var storeInfo = await StoreInfoHelper.GetHashStoreInfo(_storeInfoResolveService, _hashGroupRepository, _messageHistoryHashGroupName, id.ToString());

            if (!storeInfo.TableNames.TryGetValue(HashEntityNames.SMessageHistory, out string tableName))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundKeyInHashNodeKeyInfo,
                    DefaultFormatting = "哈希组{0}中的哈希节点关键信息中找不到键值{1}",
                    ReplaceParameters = new List <object>()
                    {
                        _messageHistoryHashGroupName, HashEntityNames.SMessageHistory
                    }
                };

                throw new UtilityException((int)Errors.NotFoundKeyInHashNodeKeyInfo, fragment);
            }
            SMessageHistory smssageHistory = null;

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _messageQueueConnectionFactory.CreateReadForSMessageHistory(storeInfo.DBConnectionNames), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }
                await using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"SELECT {0} FROM {1} WHERE id=@id", StoreHelper.GetSMessageHistorySelectFields(string.Empty), tableName),
                    Transaction = sqlTran,
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = id
                    };
                    command.Parameters.Add(parameter);
                    command.Prepare();
                    SqlDataReader reader = null;

                    await using (reader = await command.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            smssageHistory = new SMessageHistory();
                            StoreHelper.SetSMessageHistorySelectFields(smssageHistory, reader, string.Empty);
                        }
                        await reader.CloseAsync();
                    }
                }
            });


            return(smssageHistory);
        }
        public async Task <SMessageHistoryListenerDetail> QueryByName(Guid historyId, string name)
        {
            var storeInfo = await StoreInfoHelper.GetHashStoreInfo(_storeInfoResolveService, _hashGroupRepository, _messageHistoryListenerDetailHashGroupName, historyId.ToString());

            if (!storeInfo.TableNames.TryGetValue(HashEntityNames.SMessageHistoryListenerDetail, out string tableNameListenerDetail))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundKeyInHashNodeKeyInfo,
                    DefaultFormatting = "哈希组{0}中的哈希节点关键信息中找不到键值{1}",
                    ReplaceParameters = new List <object>()
                    {
                        _messageHistoryListenerDetailHashGroupName, HashEntityNames.SMessageHistoryListenerDetail
                    }
                };

                throw new UtilityException((int)Errors.NotFoundKeyInHashNodeKeyInfo, fragment);
            }
            if (!storeInfo.TableNames.TryGetValue(HashEntityNames.SMessageHistory, out string tableNameHistory))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundKeyInHashNodeKeyInfo,
                    DefaultFormatting = "哈希组{0}中的哈希节点关键信息中找不到键值{1}",
                    ReplaceParameters = new List <object>()
                    {
                        _messageHistoryListenerDetailHashGroupName, HashEntityNames.SMessageHistory
                    }
                };

                throw new UtilityException((int)Errors.NotFoundKeyInHashNodeKeyInfo, fragment);
            }

            SMessageHistoryListenerDetail smessageHistoryListenerDetail = null;

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _messageQueueConnectionFactory.CreateReadForSMessageHistoryListenerDetail(storeInfo.DBConnectionNames), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }
                await using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    CommandText = string.Format(@"SELECT {0},{1} 
                                                  FROM {2} as d join  {3} as h on (d.smessagehistoryid = h.id )
                                                  WHERE [ListenerName]=@listenername and SMessageHistoryID = @historyId",
                                                StoreHelper.GetSMessageHistoryListenerDetailSelectFields("d"), StoreHelper.GetSMessageHistorySelectFields("h"),
                                                tableNameListenerDetail, tableNameHistory),
                    Transaction = sqlTran,
                })
                {
                    var parameter = new SqlParameter("@listenername", SqlDbType.NVarChar, 100)
                    {
                        Value = name
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@historyId", SqlDbType.UniqueIdentifier)
                    {
                        Value = historyId
                    };
                    command.Parameters.Add(parameter);


                    await command.PrepareAsync();
                    SqlDataReader reader = null;

                    using (reader = await command.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            smessageHistoryListenerDetail = new SMessageHistoryListenerDetail();
                            StoreHelper.SetSMessageHistoryListenerDetailSelectFields(smessageHistoryListenerDetail, reader, "d");
                            smessageHistoryListenerDetail.SMessageHistory = new SMessageHistory();
                            StoreHelper.SetSMessageHistorySelectFields(smessageHistoryListenerDetail.SMessageHistory, reader, "h");
                        }
                        await reader.CloseAsync();
                    }
                }
            });


            return(smessageHistoryListenerDetail);
        }