/// <summary> /// 根据调度动作组Id查询 /// </summary> /// <param name="id"></param> /// <param name="groupId"></param> /// <returns></returns> public async Task <ScheduleAction> QueryByGroup(Guid id, Guid groupId) { ScheduleAction result = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSchedule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, CommandText = string.Format(@"SELECT {0} FROM [dbo].[ScheduleAction] WHERE id=@id AND groupid=@groupid", StoreHelper.GetScheduleActionStoreSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = id }; command.Parameters.Add(parameter); parameter = new SqlParameter("@groupid", SqlDbType.UniqueIdentifier) { Value = groupId }; command.Parameters.Add(parameter); command.Prepare(); SqlDataReader reader = null; using (reader = await command.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { result = new ScheduleAction(); StoreHelper.SetScheduleActionStoreSelectFields(result, reader, string.Empty); } reader.Close(); } } }); return(result); }
/// <summary> /// 查询调度动作id下面的所有指定状态的调度动作 /// </summary> /// <param name="groupId"></param> /// <param name="status"></param> /// <param name="callback"></param> /// <returns></returns> public async Task QueryAllAction(Guid groupId, int status, Func <ScheduleAction, Task> callback) { List <ScheduleAction> listAction = new List <ScheduleAction>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSchedule(), async (conn, transaction) => { int sequence = 0; int pageSize = 500; while (true) { listAction.Clear(); SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, CommandText = string.Format(@"SELECT {0} FROM [ScheduleAction] WHERE [groupid]=@groupid AND [status]=@status ORDER BY sequence OFFSET @sequence ROWS FETCH NEXT @pagesize ROWS ONLY;", StoreHelper.GetScheduleActionStoreSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@groupId", SqlDbType.UniqueIdentifier) { Value = groupId }; command.Parameters.Add(parameter); parameter = new SqlParameter("@status", SqlDbType.Int) { Value = status }; command.Parameters.Add(parameter); parameter = new SqlParameter("@pagesize", SqlDbType.Int) { Value = pageSize }; command.Parameters.Add(parameter); parameter = new SqlParameter("@sequence", SqlDbType.Int) { Value = sequence }; command.Parameters.Add(parameter); command.Prepare(); SqlDataReader reader = null; using (reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var scheduleAction = new ScheduleAction(); StoreHelper.SetScheduleActionStoreSelectFields(scheduleAction, reader, string.Empty); listAction.Add(scheduleAction); } reader.Close(); } } foreach (var actionItem in listAction) { await callback(actionItem); } if (listAction.Count != pageSize) { break; } else { sequence += listAction.Count; } } }); }
/// <summary> /// 分页查询尚未分配组且匹配动作名称的调度动作 /// </summary> /// <param name="name"></param> /// <param name="page"></param> /// <param name="pageSize"></param> /// <returns></returns> public async Task <QueryResult <ScheduleAction> > QueryByNullGroup(string name, int page, int pageSize) { QueryResult <ScheduleAction> result = new QueryResult <ScheduleAction>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _dbConnectionFactory.CreateReadForSchedule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand command = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, CommandText = string.Format(@"SET @currentpage = @page; SELECT @count = COUNT(*) FROM [ScheduleAction] WHERE [name] LIKE @name AND [groupid] IS NULL; IF @pagesize * @page >= @count BEGIN SET @currentpage = @count / @pagesize; IF @count % @pagesize <> 0 BEGIN SET @currentpage = @currentpage + 1; END; IF @currentpage = 0 SET @currentpage = 1; END; ELSE IF @page < 1 BEGIN SET @currentpage = 1; END; SELECT {0} FROM [ScheduleAction] WHERE [name] LIKE @name AND [groupid] IS NULL ORDER BY sequence OFFSET (@pagesize * (@currentpage - 1)) ROWS FETCH NEXT @pagesize ROWS ONLY;", StoreHelper.GetScheduleActionStoreSelectFields(string.Empty)), Transaction = sqlTran }) { var parameter = new SqlParameter("@page", SqlDbType.Int) { Value = page }; command.Parameters.Add(parameter); parameter = new SqlParameter("@pagesize", SqlDbType.Int) { Value = pageSize }; command.Parameters.Add(parameter); parameter = new SqlParameter("@count", SqlDbType.Int) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); parameter = new SqlParameter("@currentpage", SqlDbType.Int) { Direction = ParameterDirection.Output }; command.Parameters.Add(parameter); parameter = new SqlParameter("@name", SqlDbType.VarChar, 200) { Value = $"{name.ToSqlLike()}%" }; command.Parameters.Add(parameter); command.Prepare(); SqlDataReader reader = null; using (reader = await command.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var scheduleAction = new ScheduleAction(); StoreHelper.SetScheduleActionStoreSelectFields(scheduleAction, reader, string.Empty); result.Results.Add(scheduleAction); } reader.Close(); result.TotalCount = (int)command.Parameters["@count"].Value; result.CurrentPage = (int)command.Parameters["@currentpage"].Value; } } }); return(result); }