RhSolutions-AddIn/src/Controllers/ExportTool.cs
2022-12-22 14:52:54 +03:00

101 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using RhSolutions.Models;
using System.Linq;
namespace RhSolutions.Controllers
{
internal class ExportTool : ToolBase
{
private Dictionary<Product, double> PositionAmount;
private readonly Range Selection;
public ExportTool()
{
Selection = ExcelApp.Selection;
GetSelected();
if (PositionAmount.Count == 0)
{
throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
}
}
public override void FillTarget()
{
using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
using (ResultBar = new ResultBar())
{
foreach (var kvp in PositionAmount)
{
FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
ProgressBar.Update();
}
FilterByAmount();
ResultBar.Update();
}
}
private void GetSelected()
{
object[,] cells = Selection.Value2;
PositionAmount = new Dictionary<Product, double>();
int rowsCount = Selection.Rows.Count;
for (int row = 1; row <= rowsCount; row++)
{
if (cells[row, 1] == null || cells[row, 2] == null)
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
object current = cells[row, column];
if (Sku.TryParse(current.ToString(), out var rauSku))
{
sku = rauSku.FirstOrDefault().ToString() ?? null;
}
else if (current.GetType() == typeof(string)
&& double.TryParse(current.ToString(), out _))
{
amount = double.Parse((string)current);
}
else if (current.GetType() == typeof(double))
{
amount = (double)current;
}
}
if (sku == null || amount == null)
{
continue;
}
Product position = new Product
{
ProductSku = sku
};
if (PositionAmount.ContainsKey(position))
{
PositionAmount[position] += amount.Value;
}
else
{
PositionAmount.Add(position, amount.Value);
}
}
}
}
}