public void PopulateDocument(int markId, MemoryStream memory)
        {
            var mark = _markRepo.GetById(markId);

            if (mark == null)
            {
                throw new ArgumentNullException(nameof(mark));
            }
            var subnode = mark.Subnode;
            var node    = subnode.Node;
            var project = node.Project;

            var departmentHead = _employeeRepo.GetByDepartmentIdAndPosition(
                mark.Department.Id, _appSettings.DepartmentHeadPosId);

            if (departmentHead == null)
            {
                departmentHead = _employeeRepo.GetByDepartmentIdAndPosition(
                    mark.Department.Id, _appSettings.ActingDepartmentHeadPosId);
            }
            if (departmentHead == null)
            {
                departmentHead = _employeeRepo.GetByDepartmentIdAndPosition(
                    mark.Department.Id, _appSettings.DeputyDepartmentHeadPosId);
            }
            if (departmentHead == null)
            {
                departmentHead = _employeeRepo.GetByDepartmentIdAndPosition(
                    mark.Department.Id, _appSettings.ActingDeputyDepartmentHeadPosId);
            }
            if (departmentHead == null)
            {
                throw new ConflictException();
            }

            // Вкл в состав спецификации
            var constructions         = _constructionRepo.GetAllByMarkId(markId);
            var standardConstructions = _standardConstructionRepo.GetAllByMarkId(markId);
            var opCond = _markOperatingConditionsRepo.GetByMarkId(markId);

            if (opCond == null)
            {
                throw new ArgumentNullException(nameof(opCond));
            }
            var estTask = _estimateTaskRepo.GetByMarkId(markId);

            if (estTask == null)
            {
                throw new ArgumentNullException(nameof(estTask));
            }
            var markApprovals = new List <MarkApproval> {
            };

            if (estTask.ApprovalEmployee != null)
            {
                markApprovals.Add(new MarkApproval
                {
                    MarkId   = markId,
                    Employee = estTask.ApprovalEmployee,
                });
            }

            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memory, true))
            {
                var markName = MarkHelper.MakeMarkName(
                    project.BaseSeries, node.Code, subnode.Code, mark.Code);
                (var complexName, var objectName) = MarkHelper.MakeComplexAndObjectName(
                    project.Name, node.Name, subnode.Name, mark.Name);

                AppendText(wordDoc, estTask.TaskText);

                var arr = new List <ListText>
                {
                    new ListText
                    {
                        Text            = "Изготовление и монтаж конструкций",
                        LevelNum        = 0,
                        IsBold          = true,
                        WithSuperscript = false,
                    },
                    new ListText
                    {
                        Text            = "Дополнительно учитывать:",
                        LevelNum        = 4,
                        IsBold          = false,
                        WithSuperscript = false,
                    },
                    new ListText
                    {
                        Text            = $"коэффициент надежности по ответственности: {opCond.SafetyCoeff}",
                        LevelNum        = 2,
                        IsBold          = false,
                        WithSuperscript = false,
                    },
                    new ListText
                    {
                        Text            = $"среда: {opCond.EnvAggressiveness.Name}",
                        LevelNum        = 2,
                        IsBold          = false,
                        WithSuperscript = false,
                    },
                    new ListText
                    {
                        Text            = $"расчетная температура эксплуатации: {(opCond.Temperature < 0 ? ("минус " + -opCond.Temperature) : opCond.Temperature)}",
                        LevelNum        = 2,
                        IsBold          = false,
                        WithSuperscript = false,
                    },
                };
                AppendList(wordDoc, arr);

                arr = new List <ListText> {
                };
                double overallInitialWeightSum = 0.0;
                double overallAreaSum          = 0.0;
                foreach (var construction in constructions)
                {
                    arr.Add(new ListText
                    {
                        Text            = construction.Name,
                        LevelNum        = 1,
                        IsBold          = false,
                        WithSuperscript = false,
                    });
                    if (construction.Valuation != null)
                    {
                        arr.Add(new ListText
                        {
                            Text            = $"Расценка: {construction.Valuation}",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.StandardAlbumCode != null)
                    {
                        arr.Add(new ListText
                        {
                            Text            = $"Шифр типового альбома конструкций: {construction.StandardAlbumCode}",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.NumOfStandardConstructions != 0)
                    {
                        arr.Add(new ListText
                        {
                            Text            = $"Число типовых конструкций: {construction.NumOfStandardConstructions}",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.HasEdgeBlunting)
                    {
                        arr.Add(new ListText
                        {
                            Text            = "Притупление кромок",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.HasDynamicLoad)
                    {
                        arr.Add(new ListText
                        {
                            Text            = "Динамическая нагрузка",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.HasFlangedConnections)
                    {
                        arr.Add(new ListText
                        {
                            Text            = "Фланцевые соединения",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    if (construction.WeldingControl.Id > 1)
                    {
                        arr.Add(new ListText
                        {
                            Text            = $"Контроль плотности сварных швов {construction.WeldingControl.Name}",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                    var constructionElements = _constructionElementRepo.GetAllByConstructionId(construction.Id);
                    if (constructionElements.Count() > 0)
                    {
                        var groupedSteel = constructionElements.Where(
                            v => v.Steel != null).GroupBy(v => v.Steel).Select(
                            v => new GroupedSteel
                        {
                            Name   = v.First().Steel.Name,
                            Length = v.Sum(v => v.Length),
                            Weight = v.Sum(v => v.Profile.Weight),
                            Area   = v.Sum(v => v.Profile.Area),
                        });
                        arr.Add(new ListText
                        {
                            Text            = "в том числе по маркам стали:",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                        double initialWeightSum = 0.0;
                        double areaSum          = 0.0;
                        foreach (var s in groupedSteel)
                        {
                            // var initialWeightValue = s.Weight * s.Length * 0.001;
                            // var initialWeightValueRounded = Math.Ceiling(initialWeightValue * 1000) / 1000;
                            var initialWeightValueRounded = Math.Ceiling(
                                s.Weight * s.Length) / 1000;
                            var finalWeightValueRounded = Math.Ceiling(
                                initialWeightValueRounded * 1040) / 1000;
                            arr.Add(new ListText
                            {
                                // Text = $"{s.Name} {initialWeightValueRounded} x 1,04 = {Math.Ceiling(initialWeightValueRounded * 1.04 * 1000) / 1000} т",
                                Text            = $"{s.Name} {initialWeightValueRounded.ToStringWithComma()} x 1,04 = {finalWeightValueRounded.ToStringWithComma()} т",
                                LevelNum        = 6,
                                IsBold          = false,
                                WithSuperscript = false,
                            });
                            // initialWeightSum += initialWeightValue;
                            initialWeightSum += initialWeightValueRounded;
                            areaSum          += s.Area * s.Length;
                        }
                        overallInitialWeightSum += initialWeightSum;
                        var initialWeightSumRounded = Math.Ceiling(
                            initialWeightSum * 1000) / 1000;
                        var finalWeightSumRounded = Math.Ceiling(
                            initialWeightSumRounded * 1040) / 1000;
                        arr.Add(new ListText
                        {
                            // Text = $"Итого масса металла: {initialWeightSumRounded} x 1,04 = {Math.Ceiling(initialWeightSumRounded * 1.04 * 1000) / 1000} т",
                            Text            = $"Итого масса металла: {initialWeightSumRounded.ToStringWithComma()} x 1,04 = {finalWeightSumRounded.ToStringWithComma()} т",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                        var areaSumRounded = Math.Round(areaSum, 3);
                        // overallAreaSum += areaSum;
                        overallAreaSum += areaSumRounded;
                        arr.Add(new ListText
                        {
                            // Text = $"Площадь поверхности для окраски: {Math.Round(areaSum, 3).ToStringWithComma()} x 100 м^2",
                            Text            = $"Площадь поверхности для окраски: {areaSumRounded.ToStringWithComma()} x 100 м^2",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = true,
                        });
                        arr.Add(new ListText
                        {
                            // Text = $"Относительная площадь окраски: {Math.Round(areaSum * 100 / (initialWeightSum * 1.04), 3).ToStringWithComma()} м^2/т",
                            Text            = $"Относительная площадь окраски: {Math.Round(areaSumRounded * 100 / (initialWeightSumRounded * 1.04), 1).ToStringWithComma()} м^2/т",
                            LevelNum        = 5,
                            IsBold          = false,
                            WithSuperscript = true,
                        });
                    }
                }

                foreach (var standardConstruction in standardConstructions)
                {
                    arr.Add(new ListText
                    {
                        Text            = $"{standardConstruction.Name}: {(Math.Ceiling(standardConstruction.Weight * 1000) / 1000).ToStringWithComma()} т",
                        LevelNum        = 1,
                        IsBold          = false,
                        WithSuperscript = false,
                    });
                }

                arr.Add(new ListText
                {
                    Text            = "Окраска конструкций",
                    LevelNum        = 0,
                    IsBold          = true,
                    WithSuperscript = false,
                });

                var overallInitialWeightSumRounded = Math.Ceiling(
                    overallInitialWeightSum * 1000) / 1000;
                var overallFinalWeightSumRounded = Math.Ceiling(
                    overallInitialWeightSumRounded * 1040) / 1000;
                arr.Add(new ListText
                {
                    // Text = $"Масса металла для окраски: {Math.Round(overallInitialWeightSum, 3)} x 1,04 = {Math.Round(Math.Round(overallInitialWeightSum, 3) * 1.04, 3)} т",
                    Text            = $"Масса металла для окраски: {overallInitialWeightSumRounded.ToStringWithComma()} x 1,04 = {overallFinalWeightSumRounded.ToStringWithComma()} т",
                    LevelNum        = 4,
                    IsBold          = false,
                    WithSuperscript = false,
                });
                arr.Add(new ListText
                {
                    Text            = $"Площадь поверхности для окраски: {Math.Round(overallAreaSum, 3).ToStringWithComma()} x 100 м^2",
                    LevelNum        = 4,
                    IsBold          = false,
                    WithSuperscript = true,
                });

                var points = _markGeneralDataPointRepo.GetAllByMarkAndSectionId(
                    markId, _paintingGeneralDataSectionId);
                for (int i = 1; i < points.Count(); i++)
                {
                    var pointText = points.ToList()[i].Text;
                    if (pointText[0] == '#' && pointText[1] == ' ')
                    {
                        pointText = pointText.Substring(2) + ".";
                    }
                    else if (pointText[0] == '-' && pointText[1] == ' ')
                    {
                        pointText = pointText.Substring(2) + ".";
                    }
                    arr.Add(new ListText
                    {
                        Text            = pointText,
                        LevelNum        = 3,
                        IsBold          = false,
                        WithSuperscript = false,
                    });
                }

                if (estTask.AdditionalText != null && estTask.AdditionalText != "")
                {
                    arr.Add(new ListText
                    {
                        Text            = "Дополнительно",
                        LevelNum        = 0,
                        IsBold          = true,
                        WithSuperscript = false,
                    });
                    var split = estTask.AdditionalText.Split("\n");
                    foreach (var splitText in split)
                    {
                        arr.Add(new ListText
                        {
                            Text            = splitText,
                            LevelNum        = 3,
                            IsBold          = false,
                            WithSuperscript = false,
                        });
                    }
                }
                AppendList(wordDoc, arr);

                Word.AppendToBigFooterTable(
                    wordDoc,
                    markName,
                    complexName,
                    objectName,
                    -1,
                    mark,
                    markApprovals.ToList(),
                    departmentHead);
                Word.AppendToSmallFooterTable(wordDoc, markName);
            }
        }
 public EstimateTask GetByMarkId(int markId)
 {
     return(_repository.GetByMarkId(markId));
 }