/// <summary> /// 新建 /// </summary> /// <param name="record"></param> /// <returns></returns> public async Task Add(SMSRecord record) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateSMSRecordAllForSMS(), 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 (record.ID == Guid.Empty) { command.CommandText = @" INSERT INTO [dbo].[SMSRecord] ( [id] ,[phonenumbers] ,[content] ,[sendendpointname] ,[extensioninfo] ,[statusdescription] ,[status] ,[createtime] ,[modifytime] ) VALUES (default , @phonenumbers , @content , @sendendpointname , @extensioninfo , @statusdescription , @status , GETUTCDATE() , GETUTCDATE()); select @newid =[id] from [dbo].[SMSRecord] where [sequence] = SCOPE_IDENTITY()"; parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = @" INSERT INTO [dbo].[SMSRecord] ( [id] ,[phonenumbers] ,[content] ,[sendendpointname] ,[extensioninfo] ,[statusdescription] ,[status] ,[createtime] ,[modifytime] ) VALUES (@id , @phonenumbers , @content , @sendendpointname , @extensioninfo , @statusdescription , @status , GETUTCDATE() , GETUTCDATE())"; parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = record.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@phonenumbers", SqlDbType.NVarChar, 4000) { Value = record.PhoneNumbers }; command.Parameters.Add(parameter); parameter = new SqlParameter("@content", SqlDbType.NVarChar, 4000) { Value = record.Content }; command.Parameters.Add(parameter); parameter = new SqlParameter("@sendendpointname", SqlDbType.NVarChar, 500) { Value = record.SendEndpointName }; command.Parameters.Add(parameter); parameter = new SqlParameter("@extensioninfo", SqlDbType.NVarChar, 4000) { Value = record.ExtensionInfo }; command.Parameters.Add(parameter); parameter = new SqlParameter("@statusdescription", SqlDbType.NVarChar, 4000) { Value = record.StatusDescription }; command.Parameters.Add(parameter); parameter = new SqlParameter("@status", SqlDbType.Int) { Value = record.Status }; command.Parameters.Add(parameter); await command.PrepareAsync(); await command.ExecuteNonQueryAsync(); //如果用户未赋值ID则创建成功后返回ID if (record.ID == Guid.Empty) { record.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }
public async Task Add(SMSSendEndpoint endpoint) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateSMSRecordAllForSMS(), 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 (endpoint.ID == Guid.Empty) { command.CommandText = @" INSERT INTO [dbo].[SMSSendEndpoint] ( [id] ,[name] ,[configuration] ,[createtime] ,[modifytime] ) VALUES (default , @name , @Configuration , GETUTCDATE() , GETUTCDATE()); select @newid =[id] from [dbo].[SMSSendEndpoint] where [sequence] = SCOPE_IDENTITY()"; parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); } else { command.CommandText = @"INSERT INTO [dbo].[SMSSendEndpoint] ( [id] ,[name] ,[configuration] ,[createtime] ,[modifytime] ) VALUES ( @id , @name , @configuration , GETUTCDATE() , GETUTCDATE())"; parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = endpoint.ID }; command.Parameters.Add(parameter); } parameter = new SqlParameter("@name", SqlDbType.NVarChar, 500) { Value = endpoint.Name }; command.Parameters.Add(parameter); parameter = new SqlParameter("@configuration", SqlDbType.NVarChar, 4000) { Value = endpoint.Configuration }; command.Parameters.Add(parameter); await command.PrepareAsync(); try { await command.ExecuteNonQueryAsync(); } catch (SqlException ex) { if (ex == null) { throw; } if (ex.Number == 2601) { var fragment = new TextFragment() { Code = TextCodes.ExistSMSSendEndpointByName, DefaultFormatting = "短信发送终结点中存在相同的名称\"{0}\"数据", ReplaceParameters = new List <object>() { endpoint.Name } }; throw new UtilityException((int)Errors.ExistSMSSendEndpointByName, fragment); } else { throw; } } //如果用户未赋值ID则创建成功后返回ID if (endpoint.ID == Guid.Empty) { endpoint.ID = (Guid)command.Parameters["@newid"].Value; } ; } }); }