/// <summary> /// 为消息历史监听记录从DbDataReader中赋值 /// </summary> /// <param name="queue"></param> /// <param name="reader"></param> /// <param name="prefix"></param> public static void SetSMessageHistoryListenerDetailSelectFields(SMessageHistoryListenerDetail detail, DbDataReader reader, string prefix) { detail.ID = (Guid)reader[string.Format("{0}id", prefix)]; if (reader[string.Format("{0}SMessageHistoryID", prefix)] != DBNull.Value) { detail.SMessageHistoryID = new Guid(reader[string.Format("{0}SMessageHistoryID", prefix)].ToString()); } if (reader[string.Format("{0}ListenerName", prefix)] != DBNull.Value) { detail.ListenerName = reader[string.Format("{0}ListenerName", prefix)].ToString(); } if (reader[string.Format("{0}ListenerMode", prefix)] != DBNull.Value) { detail.ListenerMode = int.Parse(reader[string.Format("{0}ListenerMode", prefix)].ToString()); } if (reader[string.Format("{0}ListenerFactoryType", prefix)] != DBNull.Value) { detail.ListenerFactoryType = reader[string.Format("{0}ListenerFactoryType", prefix)].ToString(); } if (reader[string.Format("{0}ListenerWebUrl", prefix)] != DBNull.Value) { detail.ListenerWebUrl = reader[string.Format("{0}ListenerWebUrl", prefix)].ToString(); } if (reader[string.Format("{0}ListenerRealWebUrl", prefix)] != DBNull.Value) { detail.ListenerRealWebUrl = reader[string.Format("{0}ListenerRealWebUrl", prefix)].ToString(); } if (reader[string.Format("{0}createtime", prefix)] != DBNull.Value) { detail.CreateTime = (DateTime)reader[string.Format("{0}createtime", prefix)]; } if (reader[string.Format("{0}modifytime", prefix)] != DBNull.Value) { detail.ModifyTime = (DateTime)reader[string.Format("{0}modifytime", prefix)]; } }
public async Task Add(SMessageHistoryListenerDetail detail) { var storeInfo = await StoreInfoHelper.GetHashStoreInfo(_storeInfoResolveService, _hashGroupRepository, _messageHistoryListenerDetailHashGroupName, detail.SMessageHistoryID.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); } await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _messageQueueConnectionFactory.CreateAllForSMessageHistoryListenerDetail(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, Transaction = sqlTran, }) { SqlParameter parameter; if (detail.ID == Guid.Empty) { command.CommandText = string.Format(@" INSERT INTO {0} ( [id] ,[SMessageHistoryID] ,[ListenerName] ,[ListenerMode] ,[ListenerFactoryType] ,[ListenerWebUrl] ,[ListenerRealWebUrl] ,[createtime] ,[modifytime] ) VALUES (default , @SMessageHistoryID , @ListenerName , @ListenerMode , @ListenerFactoryType , @ListenerWebUrl , @ListenerRealWebUrl , GETUTCDATE() , GETUTCDATE()); select @newid =[id] from {0} where [sequence] = SCOPE_IDENTITY()", tableNameListenerDetail); parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = string.Format(@" INSERT INTO {0} ( [id] ,[SMessageHistoryID] ,[ListenerName] ,[ListenerMode] ,[ListenerFactoryType] ,[ListenerWebUrl] ,[ListenerRealWebUrl] ,[createtime] ,[modifytime] ) VALUES ( @id , @SMessageHistoryID , @ListenerName , @ListenerMode , @ListenerFactoryType , @ListenerWebUrl , @ListenerRealWebUrl , GETUTCDATE() , GETUTCDATE());", tableNameListenerDetail); parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = detail.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@SMessageHistoryID", SqlDbType.UniqueIdentifier) { Value = detail.SMessageHistoryID }; command.Parameters.Add(parameter); parameter = new SqlParameter("@ListenerName", SqlDbType.VarChar, 150) { Value = detail.ListenerName }; command.Parameters.Add(parameter); parameter = new SqlParameter("@ListenerMode", SqlDbType.Int) { Value = detail.ListenerMode }; command.Parameters.Add(parameter); parameter = new SqlParameter("@ListenerFactoryType", SqlDbType.VarChar, 150) { Value = detail.ListenerFactoryType }; command.Parameters.Add(parameter); if (detail.ListenerWebUrl == null) { parameter = new SqlParameter("@ListenerWebUrl", SqlDbType.VarChar, 200) { Value = DBNull.Value }; } else { parameter = new SqlParameter("@ListenerWebUrl", SqlDbType.VarChar, 200) { Value = detail.ListenerWebUrl }; } command.Parameters.Add(parameter); if (detail.ListenerRealWebUrl == null) { parameter = new SqlParameter("@ListenerRealWebUrl", SqlDbType.VarChar, 200) { Value = DBNull.Value }; } else { parameter = new SqlParameter("@ListenerRealWebUrl", SqlDbType.VarChar, 200) { Value = detail.ListenerRealWebUrl }; } command.Parameters.Add(parameter); await command.PrepareAsync(); try { await command.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (ex.Number == 2601) { var fragment = new TextFragment() { Code = TextCodes.ExistSMessageHistoryDetailByName, DefaultFormatting = "消息历史监听明细中存在相同的名称\"{0}\"数据", ReplaceParameters = new List <object>() { detail.ListenerName } }; throw new UtilityException((int)Errors.ExistSMessageHistoryDetailByName, fragment); } else { throw; } } //如果用户未赋值ID则创建成功后返回ID if (detail.ID == Guid.Empty) { detail.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }
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); }