RhSolutions-AddIn/RhSolutions.AddIn/Controllers/ToolBase.cs

183 lines
5.9 KiB
C#
Raw Normal View History

2023-03-28 10:36:36 +03:00
using RhSolutions.AddIn;
2022-01-27 10:22:30 +03:00
2022-12-20 12:27:47 +03:00
namespace RhSolutions.Controllers
2022-01-27 10:22:30 +03:00
{
2022-12-20 11:53:55 +03:00
internal abstract class ToolBase
2022-01-27 10:22:30 +03:00
{
2022-12-20 12:27:47 +03:00
protected Application ExcelApp = RhSolutionsAddIn.Excel;
2023-03-28 07:25:10 +03:00
protected IAddInConfiguration Configuration = RhSolutionsAddIn.Configuration;
protected TargetPriceList TargetFile { get; set; }
protected ResultBar ResultBar { get; set; }
protected ProgressBar ProgressBar { get; set; }
public abstract void FillTarget();
2022-01-27 10:22:30 +03:00
2022-01-27 17:34:03 +03:00
public void OpenNewPrice()
2022-01-27 10:22:30 +03:00
{
if (ExcelApp.Workbooks
.Cast<Workbook>()
2023-03-28 07:25:10 +03:00
.FirstOrDefault(w => w.FullName == Configuration.GetPriceListPath()) != null)
{
throw new ArgumentException("Шаблонный файл редактируется в другом месте");
}
2023-03-28 07:25:10 +03:00
Workbook wb = ExcelApp.Workbooks.Open(Configuration.GetPriceListPath(), null, true);
2022-01-27 10:22:30 +03:00
try
{
2022-02-02 18:05:49 +03:00
TargetFile = new TargetPriceList(wb);
2022-01-27 10:22:30 +03:00
}
2022-02-08 16:46:53 +03:00
catch (Exception exception)
2022-01-27 10:22:30 +03:00
{
2022-02-08 16:46:53 +03:00
if (wb != null)
{
wb.Close();
}
throw exception;
2022-01-27 10:22:30 +03:00
}
}
2022-12-20 08:30:58 +03:00
protected void FillPositionAmountToColumns(KeyValuePair<Product, double> positionAmount, params int[] columns)
2022-01-27 10:22:30 +03:00
{
2022-03-24 06:51:55 +03:00
Range worksheetCells = TargetFile.Sheet.Cells;
Range skuColumn = TargetFile.SkuCell.EntireColumn;
2022-12-20 08:30:58 +03:00
int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine);
if (row != null)
2022-01-27 10:22:30 +03:00
{
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
2022-01-28 18:15:13 +03:00
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementSuccess();
return;
2022-01-28 18:15:13 +03:00
}
2022-02-12 16:53:34 +03:00
if (TargetFile.OldSkuCell != null)
2022-01-28 18:15:13 +03:00
{
row = GetPositionRow(TargetFile.OldSkuCell.EntireColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine);
2022-01-28 16:19:39 +03:00
2022-02-08 16:27:53 +03:00
if (row != null)
{
foreach (int column in columns)
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementReplaced();
return;
}
2022-02-04 09:17:12 +03:00
}
2022-12-20 08:30:58 +03:00
string sku = positionAmount.Key.ProductSku.Substring(1, 6);
row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLine);
2022-02-04 09:17:12 +03:00
if (row != null)
{
foreach (int column in columns)
2022-02-08 16:27:53 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
2022-02-04 09:17:12 +03:00
ResultBar.IncrementReplaced();
return;
}
FillMissing(positionAmount, columns);
ResultBar.IncrementNotFound();
}
2022-12-20 08:30:58 +03:00
protected void FillMissing(KeyValuePair<Product, double> positionAmount, params int[] columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range worksheetCells = TargetFile.Sheet.Cells;
Range worksheetRows = TargetFile.Sheet.Rows;
int skuColumn = TargetFile.SkuCell.Column;
int groupColumn = TargetFile.GroupCell.Column;
int nameColumn = TargetFile.NameCell.Column;
int row = worksheetCells[worksheetRows.Count, skuColumn]
2022-02-04 09:17:12 +03:00
.End[XlDirection.xlUp]
.Row + 1;
2022-01-28 18:15:13 +03:00
2022-03-24 06:51:55 +03:00
worksheetRows[row]
2022-02-04 09:17:12 +03:00
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
2022-01-28 18:15:13 +03:00
2022-03-24 06:51:55 +03:00
Range previous = worksheetRows[row - 1];
Range current = worksheetRows[row];
2022-01-28 18:15:13 +03:00
2022-02-04 09:17:12 +03:00
previous.Copy(current);
current.ClearContents();
2022-12-20 08:30:58 +03:00
worksheetCells[row, groupColumn].Value2 = positionAmount.Key.ProductLine;
2022-03-24 06:51:55 +03:00
worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name;
2022-02-12 16:53:34 +03:00
if (TargetFile.OldSkuCell != null)
2022-02-04 09:17:12 +03:00
{
2022-03-24 06:51:55 +03:00
worksheetCells[row, skuColumn].Value2 = "Не найден";
2022-12-20 08:30:58 +03:00
worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.ProductSku;
2022-01-28 18:15:13 +03:00
}
else
2022-01-28 18:15:13 +03:00
{
2022-12-20 08:30:58 +03:00
worksheetCells[row, skuColumn].Value2 = positionAmount.Key.ProductSku;
2022-01-28 18:15:13 +03:00
}
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
2022-03-24 06:51:55 +03:00
Range cell = worksheetCells[row, column];
2022-02-08 16:27:53 +03:00
cell.AddValue(positionAmount.Value);
2022-01-28 18:15:13 +03:00
}
}
protected int? GetPositionRow(Range range, string sku, string group)
2022-01-28 18:15:13 +03:00
{
Range found = range.Find(sku);
2022-03-23 17:56:46 +03:00
string foundGroupValue;
2022-01-28 18:15:13 +03:00
if (found == null)
{
return null;
}
2022-01-28 18:15:13 +03:00
2022-03-23 17:56:46 +03:00
int firstFoundRow = found.Row;
if (string.IsNullOrEmpty(group))
{
return found.Row;
}
while (true)
{
2022-03-23 17:56:46 +03:00
foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString();
2022-03-23 17:56:46 +03:00
if (group.Equals(foundGroupValue))
{
return found.Row;
}
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
2022-01-28 18:15:13 +03:00
}
protected void FilterByAmount()
2022-01-27 10:22:30 +03:00
{
2022-01-27 17:34:03 +03:00
AutoFilter filter = TargetFile.Sheet.AutoFilter;
2022-01-28 18:46:31 +03:00
int startColumn = filter.Range.Column;
2022-01-27 10:22:30 +03:00
2023-01-13 08:37:22 +03:00
filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>0", XlAutoFilterOperator.xlAnd, "<>");
2022-01-27 17:34:03 +03:00
TargetFile.Sheet.Range["A1"].Activate();
2022-01-27 10:22:30 +03:00
}
}
}