예제 #1
0
        /// <summary>
        /// Обновить значения атрибутов.
        /// </summary>
        private void UpdateNsiAttributeCollection()
        {
            SplashScreenManager.ShowForm(typeof(frmWaitForm), true, true);
            try
            {
                SplashScreenManager.Default.SetWaitFormCaption("Импортирую...");
                SplashScreenManager.Default.SetWaitFormDescription("Поиск аттрибутов");
                if (SelectedNsi == null)
                {
                    return;
                }

                if (NsiModel == null)
                {
                    return;
                }

                HourGlass.Enable = true;

                NsiModel.Configuration.AutoDetectChangesEnabled = false;
                //Все настройки атрибутов для выбранного справочника
                var attr = NsiModel.GM_NSIReferenceAttributeSet.Where(x => x.ReferenceId == SelectedNsi.ReferenceId);

                List <GM_NSIReferenceRecord> referenceRecordList = NsiModel.GM_NSIReferenceRecordSet.OrderBy(y => y.RecordId)
                                                                   .Where(x => x.ReferenceId.Equals(SelectedNsi.ReferenceId) &&
                                                                          x.GM_NSIReferenceRecordAttribute.Any(
                                                                              a => a.GM_NSIReferenceAttribute.ReferenceAttributeId.Equals(attr.FirstOrDefault().ReferenceAttributeId))).ToList();

                if (ReferenceAttributeCollection == null || ReferenceAttributeCollection.Count < 1)
                {
                    //var attributes = NsiModel.GM_NSIReferenceRecordSet.Where(x => x.ReferenceId == SelectedNsi.ReferenceId);
                    //foreach (var attribute in attributes)
                    //{
                    //    GM_NSIReferenceAttribute gm_nsiRA = new GM_NSIReferenceAttribute();
                    //    gm_nsiRA.DataAreaId = attribute.DataAreaId;
                    //    gm_nsiRA.RecId = attribute.RecId;
                    //    gm_nsiRA.ReferenceAttributeName = attribute.RecordName;
                    //    gm_nsiRA.ReferenceAttributeType = Convert.ToInt32(attribute.RecordId);
                    //    ReferenceAttributeCollection.Add(gm_nsiRA);
                    //}
                    referenceRecordList    = NsiModel.GM_NSIReferenceRecordSet.OrderBy(y => y.RecordId).Where(x => x.ReferenceId.Equals(SelectedNsi.ReferenceId)).ToList();
                    RunExportButtonEnabled = false;
                    //   return;
                }

                #region Добавление колонок
                List <ColumnEx> columns = new List <ColumnEx>();
                columns.Add(new ColumnEx()
                {
                    CellSource = "Id", Header = "Бизнес - ИД записи(RecordId)"
                });
                columns.Add(new ColumnEx()
                {
                    CellSource = "Name", Header = "Описание записи справочника(RecordName)"
                });
                foreach (GM_NSIReferenceAttribute referenceAttribute in ReferenceAttributeCollection.ToList())
                {
                    if (referenceAttribute == null)
                    {
                        continue;
                    }

                    string raName = PointReplace(referenceAttribute.ReferenceAttributeName);

                    switch ((NSIDataTypes)referenceAttribute.ReferenceAttributeType)
                    {
                    case NSIDataTypes.String:
                        columns.Add(new ColumnEx()
                        {
                            CellSource = raName, Header = referenceAttribute.ReferenceAttributeName
                        });
                        break;

                    case NSIDataTypes.Integer:
                        columns.Add(new ColumnEx()
                        {
                            CellSource = raName, Header = referenceAttribute.ReferenceAttributeName
                        });
                        break;

                    case NSIDataTypes.Real:
                        columns.Add(new ColumnEx()
                        {
                            CellSource = raName, Header = referenceAttribute.ReferenceAttributeName
                        });
                        break;

                    case NSIDataTypes.DateTime:
                        columns.Add(new ColumnEx()
                        {
                            CellSource = raName, Header = referenceAttribute.ReferenceAttributeName
                        });
                        break;

                    case NSIDataTypes.ReferenceRecord:
                        columns.Add(new ColumnEx()
                        {
                            CellSource = raName, Header = referenceAttribute.ReferenceAttributeName
                        });
                        //TODO ???
                        //columns.Add(new ColumnEx() { CellSource = raName + ".ReferenceRecordValue" });
                        break;
                    }
                }
                Columns = new ObservableCollection <ColumnEx>(columns);
                #endregion

                #region Добавление значений атрибутов
                List <DynamicListItem> records = new List <DynamicListItem>();
                int nsiReferenceRow            = 1;
                foreach (GM_NSIReferenceRecord nsiReferenceRecord in referenceRecordList)
                {
                    SplashScreenManager.Default.SetWaitFormDescription(String.Format("Загружаю запись {0} из {1}",
                                                                                     nsiReferenceRow, referenceRecordList.Count));
                    dynamic rec = new DynamicListItem();
                    rec.SetMemberSource("RecId", nsiReferenceRecord.RecId);
                    rec.SetMemberSource("Id", nsiReferenceRecord.RecordId);
                    rec.SetMemberSource("Name", nsiReferenceRecord.RecordName);

                    if (nsiReferenceRecord.GM_NSIReferenceRecordAttribute != null && nsiReferenceRecord.GM_NSIReferenceRecordAttribute.Count > 0)
                    {
                        foreach (GM_NSIReferenceRecordAttribute itemRra in nsiReferenceRecord.GM_NSIReferenceRecordAttribute)
                        {
                            if (itemRra.GM_NSIReferenceAttribute == null)
                            {
                                continue;
                            }

                            string raName = itemRra.GM_NSIReferenceAttribute.ReferenceAttributeName;

                            switch ((NSIDataTypes)itemRra.GM_NSIReferenceAttribute.ReferenceAttributeType)
                            {
                            case NSIDataTypes.String:
                                rec.SetMemberSource(raName, itemRra.ValueString);
                                break;

                            case NSIDataTypes.Integer:
                                rec.SetMemberSource(raName, itemRra.ValueInteger);
                                break;

                            case NSIDataTypes.Real:
                                rec.SetMemberSource(raName, itemRra.ValueReal);
                                break;

                            case NSIDataTypes.DateTime:
                                rec.SetMemberSource(raName, itemRra.ValueDate);
                                break;

                            case NSIDataTypes.ReferenceRecord:
                                if (itemRra.ValueRecordRecId != null && itemRra.ValueRecord != null)
                                {
                                    rec.SetMemberSource(raName, itemRra.ValueRecord.RecordId);
                                }
                                break;
                            }
                        }
                    }
                    records.Add(rec);
                    nsiReferenceRow++;
                }
                Records = new ObservableCollection <DynamicListItem>(records);

                RunExportButtonEnabled = Records.Count > 0;

                #endregion

                #region Добавление значений атрибутов (DynamicListItemEx)
                //List<DynamicListItemEx> records = new List<DynamicListItemEx>();

                //foreach (GM_NSIReferenceRecord nsiReferenceRecord in referenceRecordList)
                //{
                //    dynamic rec = new DynamicListItemEx();

                //    rec.SetMemberSource(new DynamicItemEx()
                //    {
                //        Key = "Id",
                //        Value = new DynamicReferenceTypeProperty() { StringValue = nsiReferenceRecord.RecordId },
                //        Obj = null
                //    });
                //    rec.SetMemberSource(new DynamicItemEx()
                //    {
                //        Key = "Name",
                //        Value = new DynamicReferenceTypeProperty() { StringValue = nsiReferenceRecord.RecordName },
                //        Obj = null
                //    });

                //    if (nsiReferenceRecord.GM_NSIReferenceRecordAttribute != null && nsiReferenceRecord.GM_NSIReferenceRecordAttribute.Count > 0)
                //    {
                //        foreach (GM_NSIReferenceRecordAttribute itemRra in nsiReferenceRecord.GM_NSIReferenceRecordAttribute)
                //        {
                //            if (itemRra.GM_NSIReferenceAttribute == null)
                //                continue;

                //            string raName = PointReplace(itemRra.GM_NSIReferenceAttribute.ReferenceAttributeName);

                //            switch ((NSIDataTypes)itemRra.GM_NSIReferenceAttribute.ReferenceAttributeType)
                //            {
                //                case NSIDataTypes.String:
                //                    rec.SetMemberSource(new DynamicItemEx()
                //                    {
                //                        Key = raName,
                //                        Value = new DynamicReferenceTypeProperty() { StringValue = itemRra.ValueString },
                //                        Obj = null
                //                    });
                //                    break;
                //                case NSIDataTypes.Integer:
                //                    rec.SetMemberSource(new DynamicItemEx()
                //                    {
                //                        Key = raName,
                //                        Value = new DynamicReferenceTypeProperty() { IntegerValue = itemRra.ValueInteger },
                //                        Obj = null
                //                    });
                //                    break;
                //                case NSIDataTypes.Real:
                //                    rec.SetMemberSource(new DynamicItemEx()
                //                    {
                //                        Key = raName,
                //                        Value = new DynamicReferenceTypeProperty() { RealValue = itemRra.ValueReal },
                //                        Obj = null
                //                    });
                //                    break;
                //                case NSIDataTypes.DateTime:
                //                    rec.SetMemberSource(new DynamicItemEx()
                //                    {
                //                        Key = raName,
                //                        Value = new DynamicReferenceTypeProperty() { DateTimeValue = itemRra.ValueDate },
                //                        Obj = null
                //                    });
                //                    break;
                //                case NSIDataTypes.ReferenceRecord:
                //                    if (itemRra.ValueRecordRecId != null && itemRra.ValueRecord != null)
                //                    {
                //                        rec.SetMemberSource(new DynamicItemEx()
                //                        {
                //                            Key = raName,
                //                            Value = new DynamicReferenceTypeProperty() { StringValue = itemRra.ValueRecord.RecordId },
                //                            Obj = null
                //                        });
                //                    }
                //                    break;
                //            }
                //        }
                //    }
                //    records.Add(rec);
                //}
                //Records = new ObservableCollection<DynamicListItemEx>(records);

                //RunExportButtonEnabled = Records.Count > 0;

                #endregion

                #region Добавление значений атрибутов (копия)
                //List<DynamicListItem> records = new List<DynamicListItem>();

                //foreach (GM_NSIReferenceRecord nsiReferenceRecord in referenceRecordList)
                //{
                //    dynamic rec = new DynamicListItem();
                //    rec.SetMemberSource("Id", new DynamicReferenceTypeProperty() { StringValue = nsiReferenceRecord.RecordId });
                //    rec.SetMemberSource("Name", new DynamicReferenceTypeProperty() { StringValue = nsiReferenceRecord.RecordName });

                //    if (nsiReferenceRecord.GM_NSIReferenceRecordAttribute != null && nsiReferenceRecord.GM_NSIReferenceRecordAttribute.Count > 0)
                //    {
                //        foreach (GM_NSIReferenceRecordAttribute itemRra in nsiReferenceRecord.GM_NSIReferenceRecordAttribute)
                //        {
                //            if (itemRra.GM_NSIReferenceAttribute == null)
                //                continue;

                //            string raName = PointReplace(itemRra.GM_NSIReferenceAttribute.ReferenceAttributeName);

                //            switch ((NSIDataTypes) itemRra.GM_NSIReferenceAttribute.ReferenceAttributeType)
                //            {
                //                case NSIDataTypes.String:
                //                    rec.SetMemberSource(
                //                        raName,
                //                        new DynamicReferenceTypeProperty()
                //                        {
                //                            StringValue = itemRra.ValueString
                //                        });
                //                    break;
                //                case NSIDataTypes.Integer:
                //                    rec.SetMemberSource(
                //                        raName,
                //                        new DynamicReferenceTypeProperty()
                //                        {
                //                            IntegerValue = itemRra.ValueInteger
                //                        });
                //                    break;
                //                case NSIDataTypes.Real:
                //                    rec.SetMemberSource(
                //                        raName,
                //                        new DynamicReferenceTypeProperty() {RealValue = itemRra.ValueReal});
                //                    break;
                //                case NSIDataTypes.DateTime:
                //                    rec.SetMemberSource(
                //                        raName,
                //                        new DynamicReferenceTypeProperty()
                //                        {
                //                            DateTimeValue = itemRra.ValueDate
                //                        });
                //                    break;
                //                case NSIDataTypes.ReferenceRecord:
                //                    if (itemRra.ValueRecordRecId != null && itemRra.ValueRecord != null)
                //                    {
                //                        rec.SetMemberSource(
                //                            raName,
                //                            new DynamicReferenceTypeProperty()
                //                            {
                //                                StringValue = itemRra.ValueRecord.RecordId
                //                            });
                //                    }
                //                    break;
                //            }
                //        }
                //    }
                //    records.Add(rec);
                //}
                //Records = new ObservableCollection<DynamicListItem>(records);

                //RunExportButtonEnabled = Records.Count > 0;

                #endregion
            }
            catch (Exception ex)
            {
                //TODO log
            }
            finally
            {
                if (NsiModel != null)
                {
                    NsiModel.Configuration.AutoDetectChangesEnabled = true;
                }
                SplashScreenManager.CloseForm();
                HourGlass.Enable = false;
            }
        }
예제 #2
0
        public void Execute(object parameter)
        {
            HourGlass.Enable = true;
            TableView view = parameter as TableView;

            if (view == null)
            {
                return;
            }

            MainViewModel  mainViewModel  = view.DataContext as MainViewModel;
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                DefaultExt       = ".xlsx",
                InitialDirectory = @"C:\",
                Filter           = "excel file (*.xlsx)|*.xlsx"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                SplashScreenManager.ShowForm(typeof(frmWaitForm), true, true);
                SplashScreenManager.Default.SetWaitFormCaption("Импортирую...");
                var excelApp = new Application();
                var workbook = excelApp.Workbooks.Open(openFileDialog.FileName);
                worksheet = (Worksheet)excelApp.ActiveSheet;
                try
                {
                    //last row,column excel
                    SplashScreenManager.Default.SetWaitFormDescription("Определение данных в таблице");
                    var lastRow    = excelApp.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Row;
                    var lastColumn = excelApp.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Column;
                    for (int i = lastColumn; i >= 1; i--)
                    {
                        if (GetValueExcel(7, i) == "")
                        {
                            lastColumn--;
                        }
                        else
                        {
                            break;
                        }
                    }

                    // блок атрибутов справочника
                    ObservableCollection <GM_NSIReferenceAttribute> listAttributes = new ObservableCollection <GM_NSIReferenceAttribute>();

                    for (int i = 3; i <= lastColumn; i++)
                    {
                        GM_NSIReferenceAttribute attribute = new GM_NSIReferenceAttribute();
                        attribute.ReferenceAttributeId   = GetValueExcel(6, i);
                        attribute.ReferenceAttributeName = GetValueExcel(7, i);
                        attribute.ReferenceAttributeType = Convertation(GetValueExcel(8, i));
                        if (Convertation(GetValueExcel(8, i)) == 4)
                        {
                            attribute.RefReferenceId = GetValueExcel(6, i);
                        }
                        listAttributes.Add(attribute);
                    }


                    //Таблица с данными
                    SplashScreenManager.Default.SetWaitFormDescription("Импорт таблицы");
                    ObservableCollection <DynamicListItem> Records = new ObservableCollection <DynamicListItem>();
                    for (int i = 9; i <= lastRow; i++)
                    {
                        DynamicListItem rowItem = new DynamicListItem();
                        for (int j = 1; j <= lastColumn; j++)
                        {
                            rowItem.ValueDictionary.Add(GetValueExcel(7, j), GetValueExcel(i, j));
                        }
                        Records.Add(rowItem);
                    }
                    SplashScreenManager.Default.SetWaitFormDescription("Настраиваю вьюху");
                    ObservableCollection <ColumnEx> Columns = new ObservableCollection <ColumnEx>();
                    for (int j = 1; j <= lastColumn; j++)
                    {
                        ColumnEx CE = new ColumnEx();
                        CE.CellSource = GetValueExcel(7, j);
                        CE.Header     = GetValueExcel(7, j);
                        CE.Type       = 0;
                        Columns.Add(CE);
                    }
                    GM_NSIReference selectedNsi = new GM_NSIReference();

                    selectedNsi.ReferenceId      = GetValueExcel(1, 2);
                    selectedNsi.ReferenceName    = GetValueExcel(2, 2);
                    selectedNsi.ReferenceGroupId = GetValueExcel(3, 2);
                    selectedNsi.UserGroupId      = GetValueExcel(4, 2);

                    mainViewModel.isExcel         = true;
                    mainViewModel.ignoreNsiUpdate = true;
                    mainViewModel.SelectedNsi     = selectedNsi;
                    mainViewModel.ReferenceAttributeCollection = listAttributes;
                    mainViewModel.Columns = Columns;
                    mainViewModel.Records = Records;
                    mainViewModel.RunExportButtonEnabled = true;
                    mainViewModel.ExportSQL = false;

                    //    view.DataContext = mainViewModel;
                }
                catch (Exception e)
                {
                    MessageBox.Show("ЧТо то пошло не так!" + e);
                }
                finally
                {
                    SplashScreenManager.CloseForm();
                    mainViewModel.ignoreNsiUpdate = false;
                    HourGlass.Enable = false;
                    workbook.Close();
                    excelApp.Quit();
                }
            }
        }