Ejemplo n.º 1
0
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public async Task Add(ScheduleAction action)
        {
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForSchedule(), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }
                using (SqlCommand command = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                })
                {
                    SqlParameter parameter;
                    if (action.ID == Guid.Empty)
                    {
                        command.CommandText = @"INSERT INTO [dbo].[ScheduleAction]
                                                    ([id],[name],[triggercondition],[configuration],[mode],[scheduleactionservicefactorytype],[scheduleactionservicefactorytypeusedi],[scheduleactionserviceweburl],[websignature],[status],[createtime],[modifytime])
                                                VALUES
                                                    (default, @name, @triggercondition,@configuration, @mode, @scheduleactionservicefactorytype, @scheduleactionservicefactorytypeusedi, @scheduleactionserviceweburl, @websignature, @status, GETUTCDATE(), GETUTCDATE());
                                                select @newid =[id] from [dbo].[ScheduleAction] where [sequence] = SCOPE_IDENTITY()";
                        parameter           = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        command.CommandText = @"INSERT INTO [dbo].[ScheduleAction]
                                                    ([id],[name],[triggercondition],[configuration],[mode],[scheduleactionservicefactorytype],[scheduleactionservicefactorytypeusedi],[scheduleactionserviceweburl],[websignature],[status],[createtime],[modifytime])
                                                VALUES
                                                    (@id, @name, @triggercondition,@configuration, @mode, @scheduleactionservicefactorytype, @scheduleactionservicefactorytypeusedi, @scheduleactionserviceweburl, @websignature, @status, GETUTCDATE(), GETUTCDATE());";
                        parameter           = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = action.ID
                        };
                        command.Parameters.Add(parameter);
                    }
                    parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = action.Name
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@triggercondition", SqlDbType.VarChar, 200)
                    {
                        Value = action.TriggerCondition
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@configuration", SqlDbType.NVarChar, action.Configuration.Length)
                    {
                        Value = action.Configuration
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@mode", SqlDbType.Int)
                    {
                        Value = action.Mode
                    };
                    command.Parameters.Add(parameter);
                    if (action.ScheduleActionServiceFactoryType != null)
                    {
                        parameter = new SqlParameter("@scheduleactionservicefactorytype", SqlDbType.VarChar, 200)
                        {
                            Value = action.ScheduleActionServiceFactoryType
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@scheduleactionservicefactorytype", SqlDbType.VarChar, 200)
                        {
                            Value = DBNull.Value
                        };
                        command.Parameters.Add(parameter);
                    }
                    if (action.ScheduleActionServiceFactoryTypeUseDI != null)
                    {
                        parameter = new SqlParameter("@scheduleactionservicefactorytypeusedi", SqlDbType.Bit)
                        {
                            Value = action.ScheduleActionServiceFactoryTypeUseDI
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@scheduleactionservicefactorytypeusedi", SqlDbType.Bit)
                        {
                            Value = DBNull.Value
                        };
                        command.Parameters.Add(parameter);
                    }
                    if (action.ScheduleActionServiceWebUrl != null)
                    {
                        parameter = new SqlParameter("@scheduleactionserviceweburl", SqlDbType.VarChar, 200)
                        {
                            Value = action.ScheduleActionServiceWebUrl
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@scheduleactionserviceweburl", SqlDbType.VarChar, 200)
                        {
                            Value = DBNull.Value
                        };
                        command.Parameters.Add(parameter);
                    }
                    if (action.WebSignature != null)
                    {
                        parameter = new SqlParameter("@websignature", SqlDbType.VarChar, 200)
                        {
                            Value = action.WebSignature
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        parameter = new SqlParameter("@websignature", SqlDbType.VarChar, 200)
                        {
                            Value = DBNull.Value
                        };
                        command.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@status", SqlDbType.Int)
                    {
                        Value = action.Status
                    };
                    command.Parameters.Add(parameter);
                    command.Prepare();

                    try
                    {
                        await command.ExecuteNonQueryAsync();
                    }
                    catch (SqlException ex)
                    {
                        if (ex == null)
                        {
                            throw;
                        }
                        if (ex.Number == 2601)
                        {
                            var fragment = new TextFragment()
                            {
                                Code = TextCodes.ExistScheduleActionByName,
                                DefaultFormatting = "调度动作中存在相同名称\"{0}\"数据",
                                ReplaceParameters = new List <object>()
                                {
                                    action.Name
                                }
                            };

                            throw new UtilityException((int)Errors.ExistScheduleActionByName, fragment);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    //如果用户未赋值ID则创建成功后返回ID
                    if (action.ID == Guid.Empty)
                    {
                        action.ID = (Guid)command.Parameters["@newid"].Value;
                    }
                    ;
                }
            });
        }
Ejemplo n.º 2
0
        public async Task Add(ScheduleActionGroup group)
        {
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _dbConnectionFactory.CreateAllForSchedule(), 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 (group.ID == Guid.Empty)
                    {
                        command.CommandText = @"insert into [dbo].[ScheduleActionGroup] ([id] ,[name] ,[createtime] ,[modifytime] ,[uselog],[executeactioninittype],[executeactioninitconfiguration])
                                                values (default ,@name ,GETUTCDATE() ,GETUTCDATE() ,@uselog,@executeactioninittype,@executeactioninitconfiguration);
                                                select @newid =[id] from [dbo].[ScheduleActionGroup] where [sequence] = SCOPE_IDENTITY()";
                        parameter           = new SqlParameter("@newid", SqlDbType.UniqueIdentifier)
                        {
                            Direction = ParameterDirection.Output
                        };
                        command.Parameters.Add(parameter);
                    }
                    else
                    {
                        command.CommandText = @"insert into [dbo].[ScheduleActionGroup] ([id] ,[name] ,[createtime] ,[modifytime] ,[uselog],[executeactioninittype],[executeactioninitconfiguration]) 
                                                values (@id ,@name ,GETUTCDATE() ,GETUTCDATE() ,@uselog)";
                        parameter           = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = group.ID
                        };
                        command.Parameters.Add(parameter);
                    }

                    parameter = new SqlParameter("@name", SqlDbType.VarChar, 150)
                    {
                        Value = group.Name
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@uselog", SqlDbType.Bit)
                    {
                        Value = group.UseLog
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@executeactioninittype", SqlDbType.VarChar, 150)
                    {
                        Value = group.ExecuteActionInitType
                    };
                    command.Parameters.Add(parameter);

                    parameter = new SqlParameter("@executeactioninitconfiguration", SqlDbType.NVarChar, group.ExecuteActionInitConfiguration.Length)
                    {
                        Value = group.ExecuteActionInitConfiguration
                    };
                    command.Parameters.Add(parameter);


                    await command.PrepareAsync();


                    try
                    {
                        await command.ExecuteNonQueryAsync();
                    }
                    catch (SqlException ex)
                    {
                        if (ex.Number == 2601)
                        {
                            var fragment = new TextFragment()
                            {
                                Code = TextCodes.ExistScheduleActionGroupName,
                                DefaultFormatting = "调度作业组已存在相同名称\"{0}\"数据",
                                ReplaceParameters = new List <object>()
                                {
                                    group.Name
                                }
                            };

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

                        throw;
                    }


                    if (group.ID == Guid.Empty)
                    {
                        group.ID = (Guid)command.Parameters["@newid"].Value;
                    }
                }
            });
        }