예제 #1
0
        public static DataTableEx ExecuteFormulasValueExpand(FormulasResult formulasResult, ArchivesValues_List2 archivesList,
                                                             IVisualDataRequestObjectsNames getNameInterface, int doublePrecision, DateTime dtStart, DateTime dtEnd, enumTimeDiscreteType discreteType, string timeZoneId,
                                                             ArchivesTPValueAllChannels archivesTpList = null, SectionIntegralActsTotalResults?archivesSection = null,
                                                             bool isSumTiForChart = false, FormulaConstantArchives constantArchives             = null, ArchTariffIntegralsTIs cumulativeIntegrals = null,
                                                             ForecastConsumptionResult fResult = null, List <TRowObjectForGrid> listTiViewModel = null, bool isDetailOv = true,
                                                             BalanceFreeHierarchyResults balanceFreeHierarchy = null)
        {
            if (archivesList == null && formulasResult == null && archivesTpList == null && archivesSection == null &&
                constantArchives == null && cumulativeIntegrals == null && fResult == null && balanceFreeHierarchy == null)
            {
                return(null);
            }

#if DEBUG
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
#endif

            var dts           = getNameInterface.GetDateTimeListForPeriod(dtStart, dtEnd, discreteType, timeZoneId.GeTimeZoneInfoById());
            var numbersValues = dts.Count;
            //Расчетный ли это профиль
            var deltaDiscrete = (int)discreteType + 1;

            //-------------Таблица для представления данных----------------------------
            var userTable = new DataTableEx();
            userTable.BeginLoadData();
            try
            {
                userTable.Columns.Add("EventDateTime", typeof(DateTime));

                #region Создаем колонки в пользовательском представлениее

                var vType = typeof(IFValue);
                var k     = 0;

                #region ------------Перебираем точки---------------------------------------------

                var tiColumnValues = userTable.AddColumnsTi(ref k, archivesList, isSumTiForChart, isDetailOv);

                #endregion

                #region ------------ТП---------------------------------------------

                var tpColumnValues = userTable.AddColumnsTp(ref k, archivesTpList, listTiViewModel);

                //      var tpArchives = new List<Tuple<TPoint, Dictionary<byte, List<TPointValue>>>>();

                //if (archivesTpList != null)
                //{

                //}

                #endregion

                #region ------------Перебираем формулы---------------------------------------------

                var formulaArchives = new List <Tuple <IFormulaInfo, TFormulasResult, List <TFormulaConsist> > >();
                if (formulasResult != null && formulasResult.Result_Values != null)
                {
                    foreach (var formulas in formulasResult.Result_Values
                             .Select(f => new Tuple <IFormulaInfo, TFormulasResult, List <TFormulaConsist> >(f.GetFormulaInfo(getNameInterface),
                                                                                                             f.Result_Values.FirstOrDefault(), f.FormulaConsist))
                             .Where(f => f.Item1 != null)
                             .OrderBy(f => f.Item1.FormulaName))
                    {
                        var fVal = formulas.Item2;
                        if (fVal != null && fVal.Val_List != null && fVal.Val_List.Count > 0)
                        {
                            var col = new DataColumn("F_VALUE" + k, vType)
                            {
                                ReadOnly = true,
                            };

                            userTable.Columns.Add(col);
                            k++;
                        }

                        //------------ТИ которые входят в формулу-------------------------------------------------------
                        if (formulasResult.IsValuesAllTIEnabled && formulas.Item3 != null)
                        {
                            foreach (var archivesValue in formulas.Item3)
                            {
                                if (archivesValue.Val_List == null)
                                {
                                    continue;
                                }

                                //var hierObj =
                                //    getNameInterface.GetHierarchyDbTreeObject(archivesValue.TI_Ch_ID.TI_ID.ToString(),
                                //        archivesValue.TypeTIinFormula);

                                var col = new DataColumn("F_VALUE" + k, vType)
                                {
                                    ReadOnly = true,
                                };

                                userTable.Columns.Add(col);
                                k++;
                            }
                        }

                        formulaArchives.Add(formulas);
                    }
                }

                #endregion

                #region ------------Перебираем константы---------------------------------------------

                if (constantArchives != null && constantArchives.FormulaConstantDict != null)
                {
                    foreach (var formulaConstant in constantArchives.FormulaConstantDict.Values)
                    {
                        if (formulaConstant.ArchiveValues != null && formulaConstant.ArchiveValues.Count > 0)
                        {
                            var col = new DataColumn("F_VALUE" + k, vType)
                            {
                                //Caption = "Конст. -" + formulaConstant.ConstantaParams.FormulaConstantName,
                                ReadOnly = true,
                            };

                            userTable.Columns.Add(col);
                            //namesFormulas.Add(new GridTitle
                            //{
                            //    //TitleString = "Конст. -" + formulaConstant.ConstantaParams.FormulaConstantName,
                            //    TitleString = "Конст. -" + formulaConstant.ConstantaParams.FormulaConstantName
                            //});

                            k++;
                        }
                    }
                }

                #endregion

                #region ------------Перебираем сечения---------------------------------------------

                if (archivesSection != null && archivesSection.Value.Total_Result != null &&
                    archivesSection.Value.Total_Result.Count > 0)
                {
                    foreach (var sectionPair in archivesSection.Value.Total_Result)
                    {
                        var section = sectionPair.Value;
                        if (section.TotalVals != null && section.TotalVals.Count > 0)
                        {
                            var sectionName = getNameInterface.GetSectionName(sectionPair.Key);

                            var col = new DataColumn("F_VALUE" + k, vType)
                            {
                                //Caption = "Сеч. -" + sectionName,
                                ReadOnly = true,
                            };

                            userTable.Columns.Add(col);
                            //namesFormulas.Add(new GridTitle
                            //{
                            //    //TitleString = "Сеч. -" + sectionName,
                            //    TitleString = "Сеч. -" + sectionName
                            //});

                            k++;
                        }
                    }
                }

                #endregion

                #region ------------Перебираем накопительные интегралы------------------------------

                if (cumulativeIntegrals != null && cumulativeIntegrals.IntegralsValue30orHour != null)
                {
                    var nType = typeof(IConvertible);

                    foreach (var integral in cumulativeIntegrals.IntegralsValue30orHour)
                    {
                        if (integral.Cumulate_Val_List == null || integral.Cumulate_Val_List.Count == 0)
                        {
                            continue;
                        }

                        //var tiName = getNameInterface.GetTIName(integral.TI_Ch_ID.TI_ID, integral.TI_Ch_ID.IsCA);
                        //var psName = getNameInterface.GetPSName(integral.PS_ID, integral.TI_Ch_ID.IsCA);

                        var col = new DataColumn("F_VALUE" + k, nType)
                        {
                            //Caption = tiName +
                            //          getNameInterface.GetChanelTypeNameByID(integral.TI_Ch_ID.TI_ID,
                            //              integral.TI_Ch_ID.ChannelType, false,
                            //              cumulativeIntegrals.DTStart, cumulativeIntegrals.DTEnd) + " \n" +
                            //          psName,
                            ReadOnly = true,
                        };

                        userTable.Columns.Add(col);
                        k++;
                    }
                }

                #endregion

                #region ------------Прогнозирование------------------------------

                if (fResult != null && fResult.Result_Values != null)
                {
                    var hierarchyObject =
                        getNameInterface.GetHierarchyDbTreeObject(fResult.ID.ID, fResult.ID.TypeHierarchy);

                    if (hierarchyObject != null)
                    {
                        foreach (var archive in fResult.Result_Values)
                        {
                            if (archive.Value == null)
                            {
                                continue;
                            }

                            var col = new DataColumn("F_VALUE" + k, vType)
                            {
                                Caption  = hierarchyObject.Name + " " + getNameInterface.GetChanelTypeNameByID(archive.Key.Channel, false),
                                ReadOnly = true,
                            };

                            userTable.Columns.Add(col);
                            k++;
                        }
                    }
                }

                #endregion

                #region ------------Перебираем балансы---------------------------------------------

                var balanceColumnValues = userTable.AddColumnsBalances(ref k, balanceFreeHierarchy);

                #endregion

                #endregion

#if DEBUG
                sw.Stop();
                Console.WriteLine("Создаем колонки в пользовательском представлениее - > {0} млс", sw.ElapsedMilliseconds);
                sw.Restart();
#endif

                #region Наполняем поставщик

                if (archivesList != null)
                {
                    userTable.Description = new DataTableDescription
                    {
                        DtStart      = archivesList.DTStart,
                        DiscreteType = archivesList.DiscreteType,
                        TimeZoneId   = archivesList.TimeZoneId,
                    };
                }
                else
                {
                    userTable.Description = null;
                }

                for (var i = 0; i < numbersValues; i++)
                {
                    k = 0;

                    //--------колонки в промежуточную таблицу-----------
                    var row = userTable.NewRow() as DataRowEx;
                    var dt  = dts[i];

                    row["EventDateTime"] = dt;
                    //row["Time"] = dt.TimeOfDay;

                    //-------пишем в промежуточную таблицу значения--------

                    //TVALUES_DB mainChannelVal = null;

                    #region ТИ

                    userTable.PopulateRowsTi(ref k, tiColumnValues, numbersValues, isSumTiForChart, row, i);

                    #endregion

                    #region ТП

                    userTable.PopulateRowsTp(ref k, tpColumnValues, row, i);

                    #endregion

                    #region Формулы

                    if (formulasResult != null && formulasResult.Result_Values != null)
                    {
                        foreach (var formulas in formulaArchives)
                        {
                            enumTypeHierarchy typeHierarchy;
                            switch (formulas.Item1.FormulasTable)
                            {
                            case enumFormulasTable.Info_Formula_Description:
                                typeHierarchy = enumTypeHierarchy.Formula;
                                break;

                            case enumFormulasTable.Info_TP2_OurSide_Formula_Description:
                                typeHierarchy = enumTypeHierarchy.Formula_TP_OurSide;
                                break;

                            case enumFormulasTable.Info_TP2_Contr_Formula_Description:
                                typeHierarchy = enumTypeHierarchy.Formula_TP_CA;
                                break;

                            default:
                                typeHierarchy = enumTypeHierarchy.Unknown;
                                break;
                            }

                            var id = new ID_Hierarchy
                            {
                                ID            = formulas.Item1.Formula_UN,
                                TypeHierarchy = typeHierarchy
                            };

                            var fVal = formulas.Item2;
                            if (fVal != null && fVal.Val_List != null && fVal.Val_List.Count > 0)
                            {
                                var v = fVal.Val_List.ElementAtOrDefault(i);
                                if (v != null)
                                {
                                    var useMeasureModule =
                                        string.IsNullOrEmpty(fVal.MeasureUnit_UN)
                                        &&
                                        (string.Equals(fVal.MeasureQuantityType_UN, "EnergyUnit") ||
                                         string.Equals(fVal.MeasureQuantityType_UN, "PowerUnit"));

                                    //row["F_VALUE" + k] = v;
                                    row.SetValue("F_VALUE" + k, i, 0, id, null, fVal.MeasureUnit_UN, v, useMeasureModule);
                                }

                                k++;
                            }

                            if (formulasResult.IsValuesAllTIEnabled && formulas.Item3 != null)
                            {
                                foreach (var archivesValue in formulas.Item3)
                                {
                                    if (archivesValue.Val_List == null)
                                    {
                                        continue;
                                    }

                                    var v = archivesValue.Val_List.ElementAtOrDefault(i);
                                    if (v != null)
                                    {
                                        if (archivesValue.Id == null)
                                        {
                                            row["F_VALUE" + k] = new TVALUES_DB
                                            {
                                                F_VALUE = v.F_VALUE,
                                                F_FLAG  = v.F_FLAG,
                                            };
                                        }
                                        else
                                        {
                                            row.SetValue("F_VALUE" + k, i, archivesValue.ChannelType, archivesValue.Id, null, null, v);
                                        }
                                    }

                                    k++;
                                }
                            }
                        }
                    }

                    #endregion

                    #region Константы

                    if (constantArchives != null && constantArchives.FormulaConstantDict != null)
                    {
                        foreach (var formulaConstant in constantArchives.FormulaConstantDict.Values)
                        {
                            if (formulaConstant.ArchiveValues != null)
                            {
                                var v = formulaConstant.ArchiveValues.ElementAtOrDefault(i);
                                if (v != null)
                                {
                                    row.SetValue("F_VALUE" + k, i, 0, new ID_Hierarchy
                                    {
                                        ID            = formulaConstant.ConstantaParams.FormulaConstant_UN,
                                        TypeHierarchy = enumTypeHierarchy.FormulaConstant
                                    }, null, null, v);
                                }

                                k++;
                            }
                        }
                    }

                    #endregion

                    #region Сечения

                    if (archivesSection != null && archivesSection.Value.Total_Result != null &&
                        archivesSection.Value.Total_Result.Count > 0)
                    {
                        foreach (var sectionPair in archivesSection.Value.Total_Result)
                        {
                            var section = sectionPair.Value;
                            List <TVALUES_DB> values;
                            if (section.TotalVals != null && section.TotalVals.Count > 0 &&
                                section.TotalVals.TryGetValue(enumInputType.Saldo, out values))
                            {
                                var v = values.ElementAtOrDefault(i);
                                if (v != null)
                                {
                                    //row["F_VALUE" + k] = v;
                                    row.SetValue("F_VALUE" + k, i, 0, new ID_Hierarchy
                                    {
                                        ID            = sectionPair.Key.ToString(),
                                        TypeHierarchy = enumTypeHierarchy.Section
                                    }, null, null, v);
                                }

                                k++;
                            }
                        }
                    }

                    #endregion

                    #region ------------Перебираем накопительные интегралы------------------------------

                    if (cumulativeIntegrals != null && cumulativeIntegrals.IntegralsValue30orHour != null)
                    {
                        var unitDigitCoeff = (double)cumulativeIntegrals.UnitDigit;

                        foreach (var integral in cumulativeIntegrals.IntegralsValue30orHour)
                        {
                            if (integral.Cumulate_Val_List == null || integral.Cumulate_Val_List.Count == 0)
                            {
                                continue;
                            }

                            var val = integral.Cumulate_Val_List.ElementAtOrDefault(i);
                            if (val != null)
                            {
                                var v = new TVal
                                {
                                    F_FLAG  = val.F_FLAG,
                                    F_VALUE = val.Value / unitDigitCoeff,
                                };

                                //var v = val.Value / unitDigitCoeff;

                                //------------ТИ ФСК-----------------------------

                                row.SetValue("F_VALUE" + k, i, integral.TI_Ch_ID.ChannelType, new ID_Hierarchy
                                {
                                    ID            = integral.TI_Ch_ID.TI_ID.ToString(),
                                    TypeHierarchy = enumTypeHierarchy.Info_TI
                                }, integral.TI_Ch_ID.DataSourceType, null, v);

                                //row["F_VALUE" + k] = v;
                                //if (integral.TI_Ch_ID.ChannelType < 3 && v > 0)
                                //{
                                //    mainChannelVal = v;
                                //}
                            }

                            k++;
                        }
                    }

                    #endregion

                    #region ------------Прогнозирование------------------------------

                    if (fResult != null && fResult.Result_Values != null)
                    {
                        var hierarchyObject =
                            getNameInterface.GetHierarchyDbTreeObject(fResult.ID.ID, fResult.ID.TypeHierarchy);

                        if (hierarchyObject != null)
                        {
                            var unitDigitCoeff = (double)fResult.UnitDigit;

                            foreach (var archive in fResult.Result_Values)
                            {
                                if (archive.Value == null)
                                {
                                    continue;
                                }

                                var val = archive.Value.ElementAtOrDefault(i);
                                if (val != null)
                                {
                                    var v = new TVALUES_DB
                                    {
                                        F_FLAG  = val.F_FLAG,
                                        F_VALUE = val.F_VALUE / unitDigitCoeff,
                                    };

                                    //------------ТИ ФСК-----------------------------
                                    //row["F_VALUE" + k] = v;

                                    row.SetValue("F_VALUE" + k, i, archive.Key.Channel, new ID_Hierarchy
                                    {
                                        ID            = archive.Key.ID,
                                        TypeHierarchy = archive.Key.TypeHierarchy,
                                    }, null, null, v);
                                }

                                k++;
                            }
                        }
                    }

                    #endregion

                    #region Балансы

                    userTable.PopulateRowsBalances(ref k, balanceColumnValues, row, i);

                    #endregion

                    //      if (consumptionSchedule != null && mainChannelVal != null && mainChannelVal.F_VALUE != null &&
                    //    mainChannelVal.F_FLAG.HasFlag(VALUES_FLAG_DB.ConsumptionScheduleOverflow))
                    //{
                    //    //Ищем значение типового графика для данной получасовки
                    //    var cs_val =
                    //        consumptionSchedule.ConsumptionScheduleValues.FirstOrDefault(
                    //            cs => cs.TotalDay == (dt.Date - dtStart.Date).TotalDays && cs.TotalNumberPerDay ==
                    //                  dt.TimeOfDay.TotalMinutes / (30 * deltaDiscrete));

                    //    if (cs_val != null && cs_val.F_VALUE.HasValue)
                    //    {
                    //        double delta = 0;
                    //        if (mainChannelVal.F_VALUE > cs_val.MAX_VALUE)
                    //        {
                    //            delta = mainChannelVal.F_VALUE - cs_val.F_VALUE.Value;
                    //        }
                    //        else
                    //        {
                    //            delta = mainChannelVal.F_VALUE - cs_val.F_VALUE.Value;
                    //        }

                    //        row["ConsumptionSchedule"] = delta;
                    //        row["ConsumptionSchedulePercent"] = delta / mainChannelVal.F_VALUE * 100;
                    //    }
                    //}

                    userTable.Rows.Add(row);
                }

                #endregion
            }
            finally
            {
                userTable.EndLoadData();
                userTable.AcceptChanges();
            }

#if DEBUG
            sw.Stop();
            Console.WriteLine("ExecuteFormulasValueExpand - > {0} млс", sw.ElapsedMilliseconds);
#endif

            return(userTable);
        }