namespace Bars.Alpha.Forms.Kb.EtlSteps.FormFilling.NaRealization
using System.Collections.Generic;
using Bars.Alpha.Data.Impl;
using Bars.Alpha.DataAccess;
using Bars.Alpha.Dictionary;
using Bars.Alpha.Forms.Kb.Utils;
using Bars.Alpha.Meta.Fields;
using Bars.Calculator.Impl;
using Bars.Calculator.Impl.Conditions;
using Bars.Calculator.Impl.Expressions;
using Bars.Calculator.Impl.Functions;
using Bars.Calculator.Impl.Functions.Date;
using Bars.Calculator.Impl.Functions.Table;
using Bars.Calculator.Impl.Functions.Utils;
[Step("Реализация НА", "Заполнение форм")]
internal class NaRealizationFilling : BaseSummhPeriodFieldGenerateStep
private readonly List<string> ignoreFieldsNamesList = new List<string>
private const string FormTableAlias = "form";
private const string TypeOperacName = "TypeOperac";
private const string TypeOperacAlias = "typeop";
private const string TipStatName = "Tip_stat";
private const string TipStatAlias = "ts";
private const string Storno = "Сторно";
private const string Income = "Дхд";
private const string Outgo = "Рсх";
private Metadata formTableMetadata;
private Metadata auditMetadata;
private Metadata typeOperacMetadata;
private Metadata statDdsMetadata;
private Metadata tipStatMetadata;
private string formTableName;
protected override void ExecuteStep()
this.formTableName = this.FormStorageMetadata.StorageName;
this.formTableMetadata = this.FormStorageMetadata;
this.auditMetadata = this.GetMetadata(Constants.TypeIdAudit);
this.typeOperacMetadata = this.GetMetadata(NaRealizationFilling.TypeOperacName);
this.statDdsMetadata = this.GetMetadata(Constants.TypeIdArticleDds);
this.tipStatMetadata = this.GetMetadata(NaRealizationFilling.TipStatName);
!x.Field.GetIsCalculated() && x.Field.FieldType != FieldType.ReferenceObject &&
x.Field.FieldType != FieldType.ReferenceTable);
var agregatFromExpression = new FromExpression().AddArgument(
new AliasExpression(this.formTableName, NaRealizationFilling.FormTableAlias));
var agregatFirstJoinConditionExpression = new EqualExpression(
new FieldExpression("dim195349114_id", this.formTableMetadata, NaRealizationFilling.FormTableAlias),
new FieldExpression("id", this.typeOperacMetadata, NaRealizationFilling.TypeOperacAlias));
var agregatFirstJoinExpression = new JoinExpression("INNER",
NaRealizationFilling.TypeOperacAlias),
agregatFirstJoinConditionExpression);
var agregatSecondJoinFirstConditionExpression = new EqualExpression(
new FieldExpression("id", this.tipStatMetadata, NaRealizationFilling.TipStatAlias),
new FieldExpression("incomecosts", this.typeOperacMetadata, NaRealizationFilling.TypeOperacAlias));
var agregatSecondJoinSecondConditionFirstVariableExpression = new EqualExpression(
new FieldExpression(NaRealizationFilling.TipStatName,
NaRealizationFilling.TipStatAlias),
new ConstantExpression(NaRealizationFilling.Income));
var agregatSecondJoinSecondConditionSecondVariableExpression = new EqualExpression(
new FieldExpression(NaRealizationFilling.TipStatName,
NaRealizationFilling.TipStatAlias),
new ConstantExpression(NaRealizationFilling.Outgo));
var agregatSecondJoinSecondConditionExpression = new ConditionOrExpression()
agregatSecondJoinSecondConditionFirstVariableExpression,
agregatSecondJoinSecondConditionSecondVariableExpression);
var agregatSecondJoinConditionAndExpression =
new ConditionAndExpression(agregatSecondJoinFirstConditionExpression,
agregatSecondJoinSecondConditionExpression);
var agregatSecondJoinExpression = new JoinExpression("INNER",
agregatFirstJoinExpression,
new AliasExpression(this.tipStatMetadata, NaRealizationFilling.TipStatAlias),
agregatSecondJoinConditionAndExpression);
var agregatInExpression = new InExpression()
.AddArguments(new FieldExpression(
Constants.CubeFieldCodeAudit,
NaRealizationFilling.FormTableAlias),
CalculatorExpressionHelper.GetIdSelectExpression(
var agregatBetweenExpression = new BetweenExpression()
.AddArgument(new DateToUTCExpression().AddArgument(new NowExpression()))
.AddArgument(new FieldExpression(Constants.FieldCodeDateBegin,
NaRealizationFilling.FormTableAlias))
.AddArgument(new FieldExpression(Constants.FieldCodeDateEnd,
NaRealizationFilling.FormTableAlias));
var agregatAndExpression = new ConditionAndExpression(agregatInExpression, agregatBetweenExpression);
var agregatFilterExpression = new FilterExpression(agregatSecondJoinExpression, agregatAndExpression);
var agregatSelectExpression = new SelectExpression(true).AddArgument(agregatFilterExpression);
foreach (var field in destinationFields.Where(field => !this.ignoreFieldsNamesList.Contains(field.CodePath)))
case Constants.CubeFieldCodeAudit:
var auditIdSelectExpression = CalculatorExpressionHelper.GetIdSelectExpression(
"Распределение ДС от РНА",
agregatSelectExpression.AddArgument(
new AliasExpression(auditIdSelectExpression, Constants.CubeFieldCodeAudit));
var typeOperacIdSelectExpression = CalculatorExpressionHelper.GetIdSelectExpression(
this.typeOperacMetadata);
agregatSelectExpression.AddArgument(
new AliasExpression(typeOperacIdSelectExpression, "dim195349114_id"));
agregatSelectExpression.AddArgument(
new AliasExpression(new NativeExpression("NULL::DOUBLE PRECISION"), "precent"));
case Constants.CubeFieldCodeArticleDds:
agregatSelectExpression.AddArguments(
new AliasExpression(CalculatorExpressionHelper.GetIdSelectExpression(
Constants.FieldCodeStateDdsNumber,
Constants.CubeFieldCodeArticleDds));
if (field.CodePath.StartsWith("col") || field.CodePath.StartsWith("scol"))
var fieldExpression = new FieldExpression(field.CodePath,
NaRealizationFilling.FormTableAlias);
var miltiplication = new MultiplicationExpression();
miltiplication.AddArgument(fieldExpression);
miltiplication.AddArgument(new NativeExpression("-1"));
var subcondition = new ConditionExpression().AddArguments(
new FieldExpression(NaRealizationFilling.TipStatName,
NaRealizationFilling.TipStatAlias),
new ConstantExpression(NaRealizationFilling.Outgo)),
fieldExpression, new NativeExpression("NULL"));
var condition = new ConditionExpression().AddArguments(
new FieldExpression(NaRealizationFilling.TipStatName,
NaRealizationFilling.TipStatAlias),
new ConstantExpression(NaRealizationFilling.Income)),
var aggregation = new AggregateExpression();
if (field.CodePath.EndsWith(Constants.FieldCodeDestinationValue))
aggregation.AddArguments(
new NativeExpression("SUM"),
aggregation.AddArguments(
new NativeExpression("MAX"),
agregatSelectExpression.AddArgument(new AliasExpression(aggregation, field.CodePath));
agregatSelectExpression.AddArgument(new FieldExpression(field.CodePath,
NaRealizationFilling.FormTableAlias));
var agregatGroupByExpression = new GroupByExpression().AddArgument(
agregatSelectExpression).AddArguments(this.GenerateGroupingFields());
var data = this.DataDocumentService.GetAll(agregatGroupByExpression, this.GetMetadata()).ToList();
protected override Metadata GetSourceMetadata()
return this.GetMetadata();
protected override bool IsNeedPeriodFieldCodeInit
protected override string ScenaryFieldName
protected override string SourceCurrencyFieldCode
private IEnumerable<FieldExpression> GenerateGroupingFields()
return new List<FieldExpression>
new FieldExpression(Constants.CubeFieldCodeEntryCurrency,
NaRealizationFilling.FormTableAlias),
new FieldExpression("NaprRash",
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.CubeFieldCodeBudgetVersion,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.CubeFieldCodeBudgetKind,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.CubeFieldCodeAdminCosts,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.CubeFieldCodeInitiationDepartment,
NaRealizationFilling.FormTableAlias),
new FieldExpression("dim195374841_id",
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.CubeFieldCodeFinancingSources,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.FieldCodeBaseCurrency,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.FieldCodeYear,
NaRealizationFilling.FormTableAlias),
new FieldExpression(Constants.FieldCodeComment,
NaRealizationFilling.FormTableAlias),