Создание и чтение (получение товара) штрих-кодов

This commit is contained in:
the 2024-06-24 14:09:32 +04:00
parent da4de1eb95
commit f8f28c3cfd
4 changed files with 109 additions and 1 deletions

View File

@ -11,6 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BarCode" Version="2024.6.1" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contracts.BindingModels;
using IronBarCode;
namespace BusinessLogic.BusinessLogic
{
public class BarcodeLogic
{
public GeneratedBarcode CreateBarcode(ProductBindingModel model)
{
var barCode = IronBarCode.BarcodeWriter.CreateBarcode(model.Id.ToString(), BarcodeEncoding.Code128);
return barCode.SaveAsPng($"product{model.Id}.png");
}
}
}

View File

@ -31,6 +31,9 @@
dataGridView = new DataGridView();
buttonCreateProduct = new Button();
groupBoxControls = new GroupBox();
buttonReadBarCode = new Button();
pictureBox1 = new PictureBox();
buttonGenerateBarCode = new Button();
buttonDeleteProduct = new Button();
buttonUpdateProduct = new Button();
groupBoxCreateProduct = new GroupBox();
@ -42,6 +45,7 @@
textBoxName = new TextBox();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
groupBoxControls.SuspendLayout();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
groupBoxCreateProduct.SuspendLayout();
((System.ComponentModel.ISupportInitialize)numericUpDownAmount).BeginInit();
((System.ComponentModel.ISupportInitialize)numericUpDownPrice).BeginInit();
@ -69,6 +73,9 @@
// groupBoxControls
//
groupBoxControls.BackColor = Color.Transparent;
groupBoxControls.Controls.Add(buttonReadBarCode);
groupBoxControls.Controls.Add(pictureBox1);
groupBoxControls.Controls.Add(buttonGenerateBarCode);
groupBoxControls.Controls.Add(buttonDeleteProduct);
groupBoxControls.Controls.Add(buttonUpdateProduct);
groupBoxControls.Controls.Add(buttonCreateProduct);
@ -80,6 +87,34 @@
groupBoxControls.TabStop = false;
groupBoxControls.Text = "Действия";
//
// buttonReadBarCode
//
buttonReadBarCode.Location = new Point(69, 379);
buttonReadBarCode.Name = "buttonReadBarCode";
buttonReadBarCode.Size = new Size(139, 52);
buttonReadBarCode.TabIndex = 6;
buttonReadBarCode.Text = "Получить товар по штрих-коду";
buttonReadBarCode.UseVisualStyleBackColor = true;
buttonReadBarCode.Click += buttonReadBarCode_Click;
//
// pictureBox1
//
pictureBox1.Location = new Point(69, 235);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new Size(139, 112);
pictureBox1.TabIndex = 5;
pictureBox1.TabStop = false;
//
// buttonGenerateBarCode
//
buttonGenerateBarCode.Location = new Point(69, 153);
buttonGenerateBarCode.Name = "buttonGenerateBarCode";
buttonGenerateBarCode.Size = new Size(139, 52);
buttonGenerateBarCode.TabIndex = 4;
buttonGenerateBarCode.Text = "Сгенерировать штрих-код товара";
buttonGenerateBarCode.UseVisualStyleBackColor = true;
buttonGenerateBarCode.Click += buttonGenerateBarCode_Click;
//
// buttonDeleteProduct
//
buttonDeleteProduct.Location = new Point(69, 102);
@ -184,6 +219,7 @@
Load += FormProducts_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
groupBoxControls.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)pictureBox1).EndInit();
groupBoxCreateProduct.ResumeLayout(false);
groupBoxCreateProduct.PerformLayout();
((System.ComponentModel.ISupportInitialize)numericUpDownAmount).EndInit();
@ -205,5 +241,8 @@
private Button buttonCancel;
private Button buttonUpdateProduct;
private Button buttonDeleteProduct;
private Button buttonGenerateBarCode;
private PictureBox pictureBox1;
private Button buttonReadBarCode;
}
}

View File

@ -2,6 +2,7 @@
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using IronBarCode;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
@ -21,11 +22,15 @@ namespace WinFormsApp
private Guid? _id;
private readonly ILogger _logger;
private readonly IProductLogic _productLogic;
private readonly BarcodeLogic _barcodeLogic;
private BarcodeResults? _barcode;
public FormProducts(ILogger<FormMain> logger, IProductLogic productLogic)
{
InitializeComponent();
_productLogic = productLogic;
_logger = logger;
_barcodeLogic = new BarcodeLogic();
_barcode = null;
}
private void FormProducts_Load(object sender, EventArgs e)
@ -124,7 +129,7 @@ namespace WinFormsApp
private void buttonUpdateProduct_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count <= 0) return;
if (dataGridView.SelectedRows.Count != 1) return;
_id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value;
groupBoxControls.Hide();
groupBoxCreateProduct.Show();
@ -181,5 +186,49 @@ namespace WinFormsApp
}
}
}
private void buttonGenerateBarCode_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var barcode = _barcodeLogic.CreateBarcode(new ProductBindingModel()
{
Id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value,
Name = Convert.ToString(dataGridView.SelectedRows[0].Cells["Name"].Value),
});
pictureBox1.Image = barcode.Image;
_barcode = BarcodeReader.Read($"product{barcode.Value}.png");
}
}
private void buttonReadBarCode_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "Image Files(*.jpg; *.png; *.jpeg; *.gif; *.bmp)|*.jpg;*.png;*.jpeg;*.gif;*.bmp";
if (dialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(dialog.FileName);
_barcode = BarcodeReader.Read(dialog.FileName);
Debug.WriteLine(_barcode.ToList()[0]);
try
{
var product = _productLogic.ReadElement(new ProductSearchModel()
{
Id = new Guid(_barcode.ToList()[0].Text),
});
groupBoxControls.Hide();
groupBoxCreateProduct.Show();
_logger.LogInformation("Получение товара");
textBoxName.Text = product.Name;
numericUpDownPrice.Value = Convert.ToDecimal(product.Price);
numericUpDownAmount.Value = product.Amount;
checkBoxIsSold.Checked = product.IsBeingSold;
}
catch (Exception ex)
{
_logger.LogInformation("Ошибка получения товара по штрихкоду");
}
}
}
}
}