Putilin P. A. Lab work 2 Hard #11

Closed
Pavel wants to merge 15 commits from Lab2Hard into Lab1Hard
29 changed files with 1187 additions and 22 deletions

View File

@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantBusines
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantListImplement", "PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj", "{471A4697-59C0-40C0-8B5E-8BEC01BD20BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{6251B4B7-A78D-458B-A926-FD0A8DF31076}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{471A4697-59C0-40C0-8B5E-8BEC01BD20BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{471A4697-59C0-40C0-8B5E-8BEC01BD20BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{471A4697-59C0-40C0-8B5E-8BEC01BD20BD}.Release|Any CPU.Build.0 = Release|Any CPU
{6251B4B7-A78D-458B-A926-FD0A8DF31076}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6251B4B7-A78D-458B-A926-FD0A8DF31076}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6251B4B7-A78D-458B-A926-FD0A8DF31076}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6251B4B7-A78D-458B-A926-FD0A8DF31076}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -38,8 +38,9 @@
this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.reinforcedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.componentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.buttonAddReinforcedInShop = new System.Windows.Forms.Button();
this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.buttonAddReinforcedInShop = new System.Windows.Forms.Button();
this.buttonSellReinforced = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
@ -47,7 +48,7 @@
// buttonRef
//
this.buttonRef.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonRef.Location = new System.Drawing.Point(840, 276);
this.buttonRef.Location = new System.Drawing.Point(786, 276);
this.buttonRef.Name = "buttonRef";
this.buttonRef.Size = new System.Drawing.Size(147, 32);
this.buttonRef.TabIndex = 12;
@ -58,7 +59,7 @@
// buttonFinish
//
this.buttonFinish.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonFinish.Location = new System.Drawing.Point(840, 217);
this.buttonFinish.Location = new System.Drawing.Point(786, 217);
this.buttonFinish.Name = "buttonFinish";
this.buttonFinish.Size = new System.Drawing.Size(147, 32);
this.buttonFinish.TabIndex = 11;
@ -69,7 +70,7 @@
// buttonReady
//
this.buttonReady.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonReady.Location = new System.Drawing.Point(840, 159);
this.buttonReady.Location = new System.Drawing.Point(786, 159);
this.buttonReady.Name = "buttonReady";
this.buttonReady.Size = new System.Drawing.Size(147, 32);
this.buttonReady.TabIndex = 10;
@ -80,7 +81,7 @@
// buttonTakeOrderInWork
//
this.buttonTakeOrderInWork.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonTakeOrderInWork.Location = new System.Drawing.Point(840, 100);
this.buttonTakeOrderInWork.Location = new System.Drawing.Point(786, 100);
this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork";
this.buttonTakeOrderInWork.Size = new System.Drawing.Size(147, 32);
this.buttonTakeOrderInWork.TabIndex = 9;
@ -91,7 +92,7 @@
// buttonCreateOrder
//
this.buttonCreateOrder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCreateOrder.Location = new System.Drawing.Point(840, 41);
this.buttonCreateOrder.Location = new System.Drawing.Point(786, 41);
this.buttonCreateOrder.Name = "buttonCreateOrder";
this.buttonCreateOrder.Size = new System.Drawing.Size(147, 32);
this.buttonCreateOrder.TabIndex = 8;
@ -108,7 +109,7 @@
this.dataGridView.Location = new System.Drawing.Point(12, 29);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.Size = new System.Drawing.Size(805, 348);
this.dataGridView.Size = new System.Drawing.Size(751, 396);
this.dataGridView.TabIndex = 7;
//
// menuStrip1
@ -117,7 +118,7 @@
this.справочникиToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(999, 24);
this.menuStrip1.Size = new System.Drawing.Size(945, 24);
this.menuStrip1.TabIndex = 13;
this.menuStrip1.Text = "menuStrip1";
//
@ -145,10 +146,17 @@
this.componentToolStripMenuItem.Text = "Компоненты";
this.componentToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click);
//
// магазиныToolStripMenuItem
//
this.магазиныToolStripMenuItem.Name = агазиныToolStripMenuItem";
this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(145, 22);
this.магазиныToolStripMenuItem.Text = "Магазины";
this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click);
//
// buttonAddReinforcedInShop
//
this.buttonAddReinforcedInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddReinforcedInShop.Location = new System.Drawing.Point(840, 337);
this.buttonAddReinforcedInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonAddReinforcedInShop.Location = new System.Drawing.Point(786, 337);
this.buttonAddReinforcedInShop.Name = "buttonAddReinforcedInShop";
this.buttonAddReinforcedInShop.Size = new System.Drawing.Size(147, 31);
this.buttonAddReinforcedInShop.TabIndex = 14;
@ -156,18 +164,23 @@
this.buttonAddReinforcedInShop.UseVisualStyleBackColor = true;
this.buttonAddReinforcedInShop.Click += new System.EventHandler(this.ButtonAddReinforcedInShop_Click);
//
// магазиныToolStripMenuItem
// buttonSellReinforced
//
this.магазиныToolStripMenuItem.Name = агазиныToolStripMenuItem";
this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.магазиныToolStripMenuItem.Text = "Магазины";
this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click);
this.buttonSellReinforced.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSellReinforced.Location = new System.Drawing.Point(786, 395);
this.buttonSellReinforced.Name = "buttonSellReinforced";
this.buttonSellReinforced.Size = new System.Drawing.Size(147, 31);
this.buttonSellReinforced.TabIndex = 15;
this.buttonSellReinforced.Text = "Продать изделие";
this.buttonSellReinforced.UseVisualStyleBackColor = true;
this.buttonSellReinforced.Click += new System.EventHandler(this.ButtonSellReinforced_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(999, 389);
this.ClientSize = new System.Drawing.Size(945, 437);
this.Controls.Add(this.buttonSellReinforced);
this.Controls.Add(this.buttonAddReinforcedInShop);
this.Controls.Add(this.menuStrip1);
this.Controls.Add(this.buttonRef);
@ -178,6 +191,7 @@
this.Controls.Add(this.dataGridView);
this.Name = "FormMain";
this.Text = "Заказы";
this.Load += new System.EventHandler(this.FormMain_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
@ -200,5 +214,6 @@
private ToolStripMenuItem componentToolStripMenuItem;
private Button buttonAddReinforcedInShop;
private ToolStripMenuItem магазиныToolStripMenuItem;
private Button buttonSellReinforced;
}
}

View File

@ -170,5 +170,14 @@ namespace PrecastConcretePlantView
form.ShowDialog();
}
}
private void ButtonSellReinforced_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSellReinforced));
if (service is FormSellReinforced form)
{
form.ShowDialog();
}
}
}
}

View File

@ -0,0 +1,133 @@
namespace PrecastConcretePlantView
{
partial class FormSellReinforced
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonSell = new System.Windows.Forms.Button();
this.numericUpDownCount = new System.Windows.Forms.NumericUpDown();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.comboBoxReinforced = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit();
this.SuspendLayout();
//
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.Location = new System.Drawing.Point(179, 79);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(123, 23);
this.buttonCancel.TabIndex = 11;
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
//
// buttonSell
//
this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSell.Location = new System.Drawing.Point(32, 79);
this.buttonSell.Name = "buttonSell";
this.buttonSell.Size = new System.Drawing.Size(123, 23);
this.buttonSell.TabIndex = 10;
this.buttonSell.Text = "Продать";
this.buttonSell.UseVisualStyleBackColor = true;
this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click);
//
// numericUpDownCount
//
this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.numericUpDownCount.Location = new System.Drawing.Point(142, 40);
this.numericUpDownCount.Maximum = new decimal(new int[] {
10000000,
0,
0,
0});
this.numericUpDownCount.Name = "numericUpDownCount";
this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.numericUpDownCount.Size = new System.Drawing.Size(160, 23);
this.numericUpDownCount.TabIndex = 9;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(60, 40);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(75, 15);
this.label2.TabIndex = 8;
this.label2.Text = "Количество:";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(123, 15);
this.label1.TabIndex = 7;
this.label1.Text = "Изделие на продажу:";
//
// comboBoxReinforced
//
this.comboBoxReinforced.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.comboBoxReinforced.FormattingEnabled = true;
this.comboBoxReinforced.Location = new System.Drawing.Point(141, 6);
this.comboBoxReinforced.Name = "comboBoxReinforced";
this.comboBoxReinforced.Size = new System.Drawing.Size(161, 23);
this.comboBoxReinforced.TabIndex = 6;
//
// FormSellReinforced
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(314, 114);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSell);
this.Controls.Add(this.numericUpDownCount);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBoxReinforced);
this.Name = "FormSellReinforced";
this.Text = "Продать изделие";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Button buttonCancel;
private Button buttonSell;
private NumericUpDown numericUpDownCount;
private Label label2;
private Label label1;
private ComboBox comboBoxReinforced;
}
}

View File

@ -0,0 +1,56 @@
using PrecastConcretePlantContracts.BusinessLogicsContracts;
namespace PrecastConcretePlantView
{
public partial class FormSellReinforced : Form
{
private readonly IShopLogic _shopLogic;
private readonly IReinforcedLogic _reinforcedLogic;
public FormSellReinforced(IReinforcedLogic logic, IShopLogic shopLogic)
{
InitializeComponent();
_reinforcedLogic = logic;
_shopLogic = shopLogic;
var list = logic.ReadList(null);
if (list != null)
{
comboBoxReinforced.DisplayMember = "ReinforcedName";
comboBoxReinforced.ValueMember = "Id";
comboBoxReinforced.DataSource = list;
comboBoxReinforced.SelectedItem = null;
}
}
private void ButtonSell_Click(object sender, EventArgs e)
{
if (comboBoxReinforced.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (numericUpDownCount.Value <= 0)
{
MessageBox.Show("Количество должно быть больше нуля", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var count = (int)numericUpDownCount.Value;
var reinforced = _reinforcedLogic.ReadElement(new() { Id = (int)comboBoxReinforced.SelectedValue });
if (reinforced == null || !_shopLogic.SellReinforceds(reinforced, count))
{
MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -40,7 +40,10 @@
this.comboBoxShop = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.dateTimePickerOpening = new System.Windows.Forms.DateTimePicker();
this.label4 = new System.Windows.Forms.Label();
this.numericUpDownMaxReinforced = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxReinforced)).BeginInit();
this.SuspendLayout();
//
// buttonSave
@ -145,11 +148,29 @@
this.dateTimePickerOpening.Size = new System.Drawing.Size(200, 23);
this.dateTimePickerOpening.TabIndex = 18;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(606, 10);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(115, 15);
this.label4.TabIndex = 19;
this.label4.Text = "Максимум изделий";
//
// numericUpDownMaxReinforced
//
this.numericUpDownMaxReinforced.Location = new System.Drawing.Point(606, 26);
this.numericUpDownMaxReinforced.Name = "numericUpDownMaxReinforced";
this.numericUpDownMaxReinforced.Size = new System.Drawing.Size(120, 23);
this.numericUpDownMaxReinforced.TabIndex = 20;
//
// FormShop
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(737, 319);
this.Controls.Add(this.numericUpDownMaxReinforced);
this.Controls.Add(this.label4);
this.Controls.Add(this.dateTimePickerOpening);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonCancel);
@ -163,6 +184,7 @@
this.Text = "Изделия магазина";
this.Load += new System.EventHandler(this.FormShop_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxReinforced)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -182,5 +204,7 @@
private TextBox comboBoxShop;
private Label label1;
private DateTimePicker dateTimePickerOpening;
private Label label4;
private NumericUpDown numericUpDownMaxReinforced;
}
}

View File

@ -48,6 +48,7 @@ namespace PrecastConcretePlantView
var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.Text));
if (model != null)
{
numericUpDownMaxReinforced.Value = model.MaxCountReinforceds;
comboBoxShop.Text = model.Name;
textBoxAddress.Text = model.Address;
dateTimePickerOpening.Value = model.DateOpening;
@ -91,6 +92,7 @@ namespace PrecastConcretePlantView
{
ShopBindingModel model = new()
{
MaxCountReinforceds = (int)numericUpDownMaxReinforced.Value,
Name = comboBoxShop.Text,
Address = textBoxAddress.Text,
DateOpening = dateTimePickerOpening.Value,

View File

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\PrecastConcretePlantBusinessLogic\PrecastConcretePlantBusinessLogic.csproj" />
<ProjectReference Include="..\PrecastConcretePlantContracts\PrecastConcretePlantContracts.csproj" />
<ProjectReference Include="..\PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj" />
</ItemGroup>

View File

@ -1,12 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using PrecastConcretePlantFileImplement.Implements;
using PrecastConcretePlantBusinessLogic.BusinessLogic;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantListImplement.Implements;
using PrecastConcretePlantListImplement;
using PrecastConcretePlantFileImplement;
using PrecastConcretePlantView;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using PrecastConcretePlantBusinessLogic;
namespace PrecastConcretePlant
@ -58,6 +58,7 @@ namespace PrecastConcretePlant
services.AddTransient<FormAddReinforcedInShop>();
services.AddTransient<FormViewShops>();
services.AddTransient<FormShop>();
services.AddTransient<FormSellReinforced>();
}
}
}

View File

@ -12,10 +12,14 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IReinforcedStorage _reinforcedStorage;
private readonly IShopLogic _shopLogic;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IReinforcedStorage reinforcedStorage, IShopLogic shopLogic)
{
_logger = logger;
_orderStorage = orderStorage;
_reinforcedStorage = reinforcedStorage;
_shopLogic = shopLogic;
}
public bool CreateOrder(OrderBindingModel model)
@ -94,6 +98,15 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
$"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}",
nameof(vmodel));
}
if (orderStatus == OrderStatus.Готов)
{
var vreinforced = _reinforcedStorage.GetElement(new() { Id = vmodel.ReinforcedId });
if (vreinforced == null || !_shopLogic.AddReinforcedsInShops(vreinforced, vmodel.Count))
{
throw new Exception($"Не удалось заполнить магазины изделием '{vreinforced?.ReinforcedName ?? string.Empty}' из заказа {vmodel.Id}");
}
}
model.Status = orderStatus;
model.ReinforcedId = vmodel.ReinforcedId;
model.Sum = vmodel.Sum;

View File

@ -100,6 +100,12 @@ namespace PrecastConcretePlantBusinessLogic
throw new ArgumentNullException("Нет названия магазина",
nameof(model.Name));
}
if (model.MaxCountReinforceds < 0)
{
throw new ArgumentException(
"Максимальное количество изделий в магазине не должно быть отрицательным",
nameof(model.MaxCountReinforceds));
}
_logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}",
model.Name, model.Address, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
@ -122,9 +128,14 @@ namespace PrecastConcretePlantBusinessLogic
{
throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count));
}
_logger.LogInformation("AddreinforcedInShop. ShopName:{ShopName}.Id:{ Id}",
model.Name, model.Id);
var element = _shopStorage.GetElement(model);
if (element.MaxCountReinforceds < count + element.Reinforceds.Sum(x => x.Value.Item2))
{
throw new ArgumentException("Количество добавляемого изделия не должно быть больше максимального кол-ва изделий в магазине", nameof(count));
}
if (element == null)
{
_logger.LogWarning("AddreinforcedInShop element not found");
@ -144,10 +155,59 @@ namespace PrecastConcretePlantBusinessLogic
Id = element.Id,
Address = element.Address,
Name = element.Name,
MaxCountReinforceds = element.MaxCountReinforceds,
DateOpening = element.DateOpening,
Reinforceds = element.Reinforceds
});
return true;
}
public int GetFreePlacesWithReinforcedsInShops(int countReinforceds)
{
return _shopStorage.GetFullList()
.Select(x => x.MaxCountReinforceds - x.Reinforceds
.Select(p => p.Value.Item2).Sum())
.Sum() - countReinforceds;
}
public bool AddReinforcedsInShops(IReinforcedModel reinforced, int count)
{
if (count <= 0)
{
_logger.LogWarning("AddReinforcedsInShops. Количество добавляемых изделий должно быть больше 0. Количество - {count}", count);
return false;
}
var freePlaces = GetFreePlacesWithReinforcedsInShops(count);
if (freePlaces < 0)
{
_logger.LogInformation("AddReinforcedsInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." +
"Освободите магазины на {places} изделий", -freePlaces);
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
var cnt = Math.Min(count, shop.MaxCountReinforceds - shop.Reinforceds.Select(x => x.Value.Item2).Sum());
if (cnt <= 0)
{
continue;
}
if (!AddReinforced(new() { Id = shop.Id }, reinforced, cnt))
{
_logger.LogWarning("При добавления изделий во все магазины произошла ошибка");
return false;
}
count -= cnt;
if (count == 0)
{
return true;
}
}
return true;
}
public bool SellReinforceds(IReinforcedModel reinforced, int needCount)
{
return _shopStorage.SellReinforceds(reinforced, needCount);
}
}
}

View File

@ -9,6 +9,8 @@ namespace PrecastConcretePlantContracts.BindingModels
public string Address { get; set; } = string.Empty;
public int MaxCountReinforceds { get; set; }
public DateTime DateOpening { get; set; } = DateTime.Now;
public Dictionary<int, (IReinforcedModel, int)> Reinforceds

View File

@ -13,5 +13,7 @@ namespace PrecastConcretePlantContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool AddReinforced(ShopSearchModel model, IReinforcedModel reinforced, int count);
bool AddReinforcedsInShops(IReinforcedModel reinforced, int count);
public bool SellReinforceds(IReinforcedModel reinforced, int needCount);
}
}

View File

@ -13,5 +13,6 @@ namespace PrecastConcretePlantContracts.StoragesContract
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
public bool SellReinforceds(IReinforcedModel reinforced, int needCount);
}
}

View File

@ -13,6 +13,9 @@ namespace PrecastConcretePlantContracts.ViewModels
[DisplayName("Адрес магазина")]
public string Address { get; set; } = string.Empty;
[DisplayName("Максимальное количество изделий в магазине")]
public int MaxCountReinforceds { get; set; }
[DisplayName("Время открытия")]
public DateTime DateOpening { get; set; } = DateTime.Now;

View File

@ -6,6 +6,7 @@ namespace PrecastConcretePlantDataModels
{
string Name { get; }
string Address { get; }
int MaxCountReinforceds { get; }
DateTime DateOpening { get; }
Dictionary<int, (IReinforcedModel, int)> Reinforceds { get; }
}

View File

@ -0,0 +1,62 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel? model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel? model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,80 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
namespace PrecastConcretePlantFileImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
private readonly DataFileSingleton _source;
public ComponentStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return _source.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return _source.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return _source.Components.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) &&
x.ComponentName == model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
_source.Components.Add(newComponent);
_source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Components.Remove(element);
_source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,54 @@
using PrecastConcretePlantFileImplement.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string ReinforcedFileName = "Reinforced.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Reinforced> Reinforceds { get; set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveReinforceds() => SaveData(Reinforceds, ReinforcedFileName, "Reinforceds", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Reinforceds = LoadData(ReinforcedFileName, "Reinforced", x => Reinforced.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}

View File

@ -0,0 +1,91 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Enums;
using PrecastConcretePlantDataModels.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int ReinforcedId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
ReinforcedId = model.ReinforcedId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
var dateImplement = element.Element("DateImplement")!.Value;
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Convert.ToInt32(element.Element("Status")!.Value),
ReinforcedId = Convert.ToInt32(element.Element("ReinforcedId")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement),
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
ReinforcedId = ReinforcedId,
ReinforcedName = DataFileSingleton.GetInstance().Reinforceds.FirstOrDefault(x => x.Id == ReinforcedId)?.ReinforcedName ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("ReinforcedId", ReinforcedId),
new XElement("Count", Count),
new XElement("Sum", Sum.ToString()),
new XElement("Status", (int)Status),
new XElement("DateCreate", DateCreate),
new XElement("DateImplement", DateImplement)
);
}
}

View File

@ -0,0 +1,76 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
namespace PrecastConcretePlantFileImplement
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Orders.Remove(element);
_source.SaveOrders();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return _source.Orders.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = GetElement(model);
return result != null ? new() { result } : new();
}
public List<OrderViewModel> GetFullList()
{
return _source.Orders
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
_source.SaveOrders();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
_source.SaveOrders();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PrecastConcretePlantContracts\PrecastConcretePlantContracts.csproj" />
<ProjectReference Include="..\PrecastConcretePlantDataModels\PrecastConcretePlantDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,86 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement.Models
{
public class Reinforced : IReinforcedModel
{
public int Id { get; private set; }
public string ReinforcedName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _ReinforcedComponents = null;
public Dictionary<int, (IComponentModel, int)> ReinforcedComponents
{
get
{
if (_ReinforcedComponents == null)
{
var source = DataFileSingleton.GetInstance();
_ReinforcedComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
y.Value));
}
return _ReinforcedComponents;
}
}
public static Reinforced? Create(ReinforcedBindingModel model)
{
if (model == null)
{
return null;
}
return new Reinforced()
{
Id = model.Id,
ReinforcedName = model.ReinforcedName,
Price = model.Price,
Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Reinforced? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Reinforced()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ReinforcedName = element.Element("ReinforcedName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("ReinforcedComponents")!.Elements("ReinforcedComponent").ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x =>
Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ReinforcedBindingModel model)
{
if (model == null)
{
return;
}
ReinforcedName = model.ReinforcedName;
Price = model.Price;
Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_ReinforcedComponents = null;
}
public ReinforcedViewModel GetViewModel => new()
{
Id = Id,
ReinforcedName = ReinforcedName,
Price = Price,
ReinforcedComponents = ReinforcedComponents
};
public XElement GetXElement => new("Reinforced",
new XAttribute("Id", Id),
new XElement("ReinforcedName", ReinforcedName),
new XElement("Price", Price.ToString()),
new XElement("ReinforcedComponents", Components.Select(x =>
new XElement("ReinforcedComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -0,0 +1,85 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
namespace PrecastConcretePlantFileImplement
{
public class ReinforcedStorage : IReinforcedStorage
{
private readonly DataFileSingleton _source;
public ReinforcedStorage()
{
_source = DataFileSingleton.GetInstance();
}
public ReinforcedViewModel? Delete(ReinforcedBindingModel model)
{
var element = _source.Reinforceds.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Reinforceds.Remove(element);
_source.SaveReinforceds();
return element.GetViewModel;
}
return null;
}
public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue)
{
return null;
}
return _source.Reinforceds.FirstOrDefault
(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) ||
(model.Id.HasValue && x.Id == model.Id)
)?.GetViewModel;
}
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName))
{
return new();
}
return _source.Reinforceds
.Select(x => x.GetViewModel)
.Where(x => x.ReinforcedName.Contains(model.ReinforcedName))
.ToList();
}
public List<ReinforcedViewModel> GetFullList()
{
return _source.Reinforceds
.Select(x => x.GetViewModel)
.ToList();
}
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
{
model.Id = _source.Reinforceds.Count > 0 ? _source.Reinforceds.Max(x => x.Id) + 1 : 1;
var newReinforced = Reinforced.Create(model);
if (newReinforced == null)
{
return null;
}
_source.Reinforceds.Add(newReinforced);
_source.SaveReinforceds();
return newReinforced.GetViewModel;
}
public ReinforcedViewModel? Update(ReinforcedBindingModel model)
{
var reinforced = _source.Reinforceds.FirstOrDefault(x => x.Id == model.Id);
if (reinforced == null)
{
return null;
}
reinforced.Update(model);
_source.SaveReinforceds();
return reinforced.GetViewModel;
}
}
}

View File

@ -0,0 +1,110 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels;
using PrecastConcretePlantDataModels.Models;
using System.Xml.Linq;
namespace PrecastConcretePlantFileImplement
{
public class Shop : IShopModel
{
public string Name { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public int MaxCountReinforceds { get; private set; }
public DateTime DateOpening { get; private set; }
public Dictionary<int, int> CountReinforceds { get; private set; } = new();
private Dictionary<int, (IReinforcedModel, int)>? _cachedReinforceds = null;
public Dictionary<int, (IReinforcedModel, int)> Reinforceds
{
get
{
if (_cachedReinforceds == null)
{
var source = DataFileSingleton.GetInstance();
_cachedReinforceds = CountReinforceds
.ToDictionary(x => x.Key, x => (source.Reinforceds
.FirstOrDefault(y => y.Id == x.Key)! as IReinforcedModel, x.Value));
}
return _cachedReinforceds;
}
}
public int Id { get; private set; }
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
MaxCountReinforceds = model.MaxCountReinforceds,
CountReinforceds = new()
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
Name = element.Element("Name")!.Value,
Address = element.Element("Address")!.Value,
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
MaxCountReinforceds = Convert.ToInt32(element.Element("MaxCountReinforceds")!.Value),
CountReinforceds = element.Element("CountReinforceds")!.Elements("CountReinforced")
.ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value)
)
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
MaxCountReinforceds = model.MaxCountReinforceds;
CountReinforceds = model.Reinforceds.ToDictionary(x => x.Key, x => x.Value.Item2);
_cachedReinforceds = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
Reinforceds = Reinforceds,
DateOpening = DateOpening,
MaxCountReinforceds = MaxCountReinforceds,
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("Name", Name),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening),
new XElement("MaxCountReinforceds", MaxCountReinforceds),
new XElement("CountReinforceds", CountReinforceds
.Select(x => new XElement("CountReinforced",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
))
);
}
}

View File

@ -0,0 +1,107 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
namespace PrecastConcretePlantFileImplement
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton _source;
public ShopStorage()
{
_source = DataFileSingleton.GetInstance();
}
public ShopViewModel? Delete(ShopBindingModel model)
{
var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Shops.Remove(element);
_source.SaveShops();
return element.GetViewModel;
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
return _source.Shops
.Select(x => x.GetViewModel)
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
return _source.Shops
.Select(shop => shop.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1;
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
_source.Shops.Add(newShop);
_source.SaveShops();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
var shop = _source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
_source.SaveShops();
return shop.GetViewModel;
}
public bool SellReinforceds(IReinforcedModel reinforced, int needCount)
{
var resultCount = _source.Shops
.Select(shop => shop.Reinforceds
.FirstOrDefault(x => x.Key == reinforced.Id).Value.Item2)
.Sum();
if (resultCount < needCount)
{
return false;
}
foreach (var shop in _source.Shops.Where(shop => shop.Reinforceds.ContainsKey(reinforced.Id)))
{
var tuple = shop.Reinforceds[reinforced.Id];
var diff = Math.Min(tuple.Item2, needCount);
shop.Reinforceds[reinforced.Id] = (tuple.Item1, tuple.Item2 - diff);
needCount -= diff;
if (needCount <= 0)
{
return true;
}
}
return true;
}
}
}

View File

@ -11,6 +11,8 @@ namespace PrecastConcretePlantListImplement
public string Address { get; private set; } = string.Empty;
public int MaxCountReinforceds { get; private set; }
public DateTime DateOpening { get; private set; }
public Dictionary<int, (IReinforcedModel, int)> Reinforceds
@ -33,6 +35,7 @@ namespace PrecastConcretePlantListImplement
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
MaxCountReinforceds = model.MaxCountReinforceds,
Reinforceds = new()
};
}
@ -45,6 +48,7 @@ namespace PrecastConcretePlantListImplement
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
MaxCountReinforceds = model.MaxCountReinforceds;
Reinforceds = model.Reinforceds;
}
public ShopViewModel GetViewModel => new()
@ -54,6 +58,7 @@ namespace PrecastConcretePlantListImplement
Address = Address,
Reinforceds = Reinforceds,
DateOpening = DateOpening,
MaxCountReinforceds = MaxCountReinforceds,
};
}
}

View File

@ -2,6 +2,7 @@
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContract;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
namespace PrecastConcretePlantListImplement
{
@ -103,5 +104,10 @@ namespace PrecastConcretePlantListImplement
}
return null;
}
public bool SellReinforceds(IReinforcedModel reinforced, int needCount)
{
throw new NotImplementedException();
}
}
}