Пример #1
0
        public virtual ActionResult JournalClear(int?IdJournal = null)
        {
            var answer = JsonAnswer();

            try
            {
                if (!IdJournal.HasValue)
                {
                    throw new Exception("Не указан идентификатор журнала.");
                }

                var result = AppCore.Get <JournalingManager>().GetJournal(IdJournal.Value);
                if (!result.IsSuccess)
                {
                    throw new Exception(result.Message);
                }

                using (var db = new JournalingDB.DataContext())
                    using (var scope = db.CreateScope(TransactionScopeOption.Required, new TransactionOptions()
                    {
                        IsolationLevel = IsolationLevel.ReadUncommitted
                    }))
                    {
                        int cntDeleted = 1;
                        while (cntDeleted > 0)
                        {
                            cntDeleted = db.ExecuteQuery(@"
                            DELETE T FROM (
                                SELECT TOP (1000) IdJournalData
                                FROM Journal
                                WHERE IdJournal=@IdJournal
                                ORDER BY DateEvent
                            ) T",
                                                         new { IdJournal = result.Result.IdJournal }
                                                         );
                        }
                        scope.Complete();
                    }
                answer.FromSuccess(null);
            }
            catch (Exception ex)
            {
                this.RegisterEventWithCode(System.Net.HttpStatusCode.InternalServerError, "Ошибка во время удаления журнала", $"Журнал №{IdJournal}", ex);
                answer.FromFail("Ошибка во время удаления журнала.");
            }
            return(ReturnJson(answer));
        }
Пример #2
0
        public virtual ActionResult JournalDetails(int?IdJournal = null)
        {
            if (!IdJournal.HasValue)
            {
                throw new Exception("Не указан идентификатор журнала.");
            }

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadUncommitted
                }))
                {
                    var result = IdJournal.Value != -1 ? AppCore.Get <JournalingManager>().GetJournal(IdJournal.Value) : new ExecutionResult <Journaling.Model.JournalInfo>();
                    if (IdJournal.Value != -1 && !result.IsSuccess)
                    {
                        throw new Exception(result.Message);
                    }

                    var dbAccessor = AppCore.Get <JournalingDB.JournalingManagerDatabaseAccessor>();

                    using (var db = new JournalingDB.DataContext())
                    {
                        var query = dbAccessor.CreateQueryJournalData(db);
                        if (IdJournal.Value != -1)
                        {
                            query = query.Where(x => x.JournalData.IdJournal == IdJournal.Value);
                        }
                        var count = query.Count();
                        return(View("JournalDetails.cshtml", new ViewModels.JournalDetails()
                        {
                            IdJournal = IdJournal.Value,
                            NameJournal = IdJournal.Value != -1 ? result.Result.Name : "Все журналы",
                            JournalDataCountAll = count
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                var answer = JsonAnswer();
                answer.FromException(ex);
                return(ReturnJson(answer, HttpStatusCode.BadRequest));
            }
        }
Пример #3
0
        public virtual ActionResult Journals()
        {
            using (var db = new JournalingDB.DataContext())
            {
                var dbAccessor = AppCore.Get <JournalingDB.JournalingManagerDatabaseAccessor>();

                //test group
                var queryDataBase    = dbAccessor.CreateQueryJournalData(db);
                var queryDataGrouped = from row in queryDataBase
                                       group row.JournalData by row.JournalName.IdJournal into gr
                                       select new { Count = gr.Count(), IdJournalDataLast = gr.Max(x => x.IdJournalData) };

                //test group
                var query = from row in queryDataBase
                            join sq2 in queryDataGrouped on row.JournalData.IdJournalData equals sq2.IdJournalDataLast
                            select new Model.JournalQueries.QueryJournalData
                {
                    JournalData = row.JournalData,
                    JournalName = row.JournalName,
                    User        = row.User,
                    Count       = sq2.Count
                };

                var data = dbAccessor.
                           FetchQueryJournalData <Model.JournalQueries.QueryJournalData, Model.JournalQueries.JournalData>(query, (row, instance) => instance.Count = row.Count).
                           Select(x => new ViewModels.JournalsList()
                {
                    JournalName     = x.JournalInfo,
                    EventsCount     = x.Count,
                    LatestEventType = x.EventType,
                    LatestEventDate = x.DateEvent
                }).ToList();

                return(View("Journals.cshtml", data));
            }
        }
Пример #4
0
        public virtual JsonResult JournalDetailsList(int?IdJournal = null, Universal.Pagination.PrimeUiDataTableSourceRequest requestOptions = null)
        {
            if (!IdJournal.HasValue)
            {
                throw new Exception("Не указан идентификатор журнала.");
            }
            else if (!ModelState.IsValid)
            {
                throw new Exception("Некорректные параметры запроса.");
            }

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Suppress, new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadUncommitted
                }))
                {
                    var result = IdJournal.Value != -1 ? AppCore.Get <JournalingManager>().GetJournal(IdJournal.Value) : new ExecutionResult <Journaling.Model.JournalInfo>();
                    if (IdJournal.Value != -1 && !result.IsSuccess)
                    {
                        throw new Exception(result.Message);
                    }

                    var dbAccessor = AppCore.Get <JournalingDB.JournalingManagerDatabaseAccessor>();

                    using (var db = new JournalingDB.DataContext())
                    {
                        var sorted = false;
                        var query  = dbAccessor.CreateQueryJournalData(db);
                        if (IdJournal.Value != -1)
                        {
                            query = query.Where(x => x.JournalData.IdJournal == IdJournal.Value);
                        }
                        if (requestOptions != null)
                        {
                            if (requestOptions.FilterFields != null)
                            {
                                foreach (var filter in requestOptions.FilterFields)
                                {
                                    switch (filter.FieldName)
                                    {
                                    case nameof(JournalingDB.QueryJournalData.JournalData.IdJournalData):
                                        if (!int.TryParse(filter.Value, out var idJournalData))
                                        {
                                            throw new HandledException($"Некорректное значение фильтра для поля '{filter.FieldName}'.");
                                        }
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => Convert.ToString(x.JournalData.IdJournalData).Contains(idJournalData.ToString()));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => Convert.ToString(x.JournalData.IdJournalData).StartsWith(idJournalData.ToString()));
                                            break;
                                        }
                                        break;

                                    case nameof(JournalingDB.QueryJournalData.JournalData.EventCode):
                                        if (!int.TryParse(filter.Value, out var eventCode))
                                        {
                                            throw new HandledException($"Некорректное значение фильтра для поля '{filter.FieldName}'.");
                                        }
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => Convert.ToString(x.JournalData.EventCode).Contains(eventCode.ToString()));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => Convert.ToString(x.JournalData.EventCode).StartsWith(eventCode.ToString()));
                                            break;
                                        }
                                        break;

                                    case nameof(JournalingDB.QueryJournalData.JournalData.EventType):
                                        if (!Enum.TryParse <EventType>(filter.Value, out var eventType))
                                        {
                                            throw new HandledException($"Некорректное значение фильтра для поля '{filter.FieldName}'.");
                                        }
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalData.EventType == eventType);
                                            break;
                                        }
                                        break;

                                    case nameof(JournalingDB.QueryJournalData.JournalData.EventInfo):
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => x.JournalData.EventInfo.Contains(filter.Value));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalData.EventInfo.StartsWith(filter.Value));
                                            break;
                                        }
                                        break;

                                    case nameof(JournalingDB.QueryJournalData.JournalData.EventInfoDetailed):
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => x.JournalData.EventInfoDetailed.Contains(filter.Value));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalData.EventInfoDetailed.StartsWith(filter.Value));
                                            break;
                                        }
                                        break;

                                    case nameof(JournalingDB.QueryJournalData.JournalData.ExceptionDetailed):
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => x.JournalData.ExceptionDetailed.Contains(filter.Value));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalData.ExceptionDetailed.StartsWith(filter.Value));
                                            break;
                                        }
                                        break;

                                    case "EventInfoFull":
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => x.JournalData.EventInfoDetailed.Contains(filter.Value) || x.JournalData.ExceptionDetailed.Contains(filter.Value));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalData.EventInfoDetailed.StartsWith(filter.Value) || x.JournalData.ExceptionDetailed.StartsWith(filter.Value));
                                            break;
                                        }
                                        break;

                                    case "JournalName":
                                        switch (filter.MatchType)
                                        {
                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.Contains:
                                            query = query.Where(x => x.JournalName.Name.Contains(filter.Value));
                                            break;

                                        case Universal.Pagination.PrimeUiDataTableFieldFilterMatchMode.StartsWith:
                                            query = query.Where(x => x.JournalName.Name.StartsWith(filter.Value));
                                            break;
                                        }
                                        break;

                                    default:
                                        throw new HandledException($"Фильтр по полю '{filter.FieldName}' не поддерживается.");
                                    }
                                }
                            }

                            if (!string.IsNullOrEmpty(requestOptions.SortByFieldName))
                            {
                                switch (requestOptions.SortByFieldName)
                                {
                                case nameof(JournalingDB.QueryJournalData.JournalData.IdJournalData):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ? query.OrderBy(x => x.JournalData.IdJournalData) : query.OrderByDescending(x => x.JournalData.IdJournalData);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.DateEvent):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ? query.OrderBy(x => x.JournalData.DateEvent) : query.OrderByDescending(x => x.JournalData.DateEvent);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.EventType):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ? query.OrderBy(x => x.JournalData.EventType) : query.OrderByDescending(x => x.JournalData.EventType);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.EventCode):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ? query.OrderBy(x => x.JournalData.EventCode) : query.OrderByDescending(x => x.JournalData.EventCode);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.EventInfo):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ? query.OrderBy(x => x.JournalData.EventInfo) : query.OrderByDescending(x => x.JournalData.EventInfo);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.EventInfoDetailed):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ?
                                             query.OrderBy(x => x.JournalData.EventInfoDetailed) :
                                             query.OrderByDescending(x => x.JournalData.EventInfoDetailed);
                                    break;

                                case nameof(JournalingDB.QueryJournalData.JournalData.ExceptionDetailed):
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ?
                                             query.OrderBy(x => x.JournalData.ExceptionDetailed) :
                                             query.OrderByDescending(x => x.JournalData.ExceptionDetailed);
                                    break;

                                case "EventInfoFull":
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ?
                                             query.OrderBy(x => x.JournalData.EventInfoDetailed).ThenBy(x => x.JournalData.ExceptionDetailed) :
                                             query.OrderByDescending(x => x.JournalData.EventInfoDetailed).ThenByDescending(x => x.JournalData.ExceptionDetailed);
                                    break;

                                case "JournalName":
                                    sorted = true;
                                    query  = requestOptions.SortByAcsending ?
                                             query.OrderBy(x => x.JournalName.Name) :
                                             query.OrderByDescending(x => x.JournalName.Name);
                                    break;
                                }
                            }
                        }

                        var dataAllCount = query.Count();

                        if (requestOptions != null)
                        {
                            if (!sorted)
                            {
                                query = query.OrderByDescending(x => x.JournalData.IdJournalData);
                            }

                            if (requestOptions.FirstRow > 0)
                            {
                                query = query.Skip((int)requestOptions.FirstRow);
                            }
                            if (requestOptions.RowsLimit > 0)
                            {
                                query = query.Take((int)requestOptions.RowsLimit);
                            }
                        }

                        var data = dbAccessor.FetchQueryJournalData(query);
                        return(ReturnJson(true, null, new ViewModels.JournalDetails()
                        {
                            JournalDataCountAll = dataAllCount,
                            JournalData = data
                        }));
                    }
                }
            }
            catch (HandledException ex)
            {
                RegisterEventWithCode(HttpStatusCode.InternalServerError, "Ошибка при загрузке данных журнала", $"Журнал №{IdJournal}. {ex.Message}");
                return(ReturnJson(false, $"Ошибка при загрузке данных журнала. {ex.Message}"));
            }
            catch (Exception ex)
            {
                RegisterEventWithCode(HttpStatusCode.InternalServerError, "Неожиданная ошибка при загрузке данных журнала", $"Журнал №{IdJournal}.", ex);
                return(ReturnJson(false, "Неожиданная ошибка при загрузке данных журнала"));
            }
        }