готова к сдаче

This commit is contained in:
MorozovDanil 2024-12-24 02:39:49 +04:00
parent 2503c9f9c0
commit c7d41abb50
9 changed files with 146 additions and 104 deletions

View File

@ -1,10 +1,5 @@
using ProjectRepairCompany.Entities.Enums; using ProjectRepairCompany.Entities.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRepairCompany.Entities; namespace ProjectRepairCompany.Entities;

View File

@ -9,6 +9,7 @@ namespace ProjectRepairCompany.Entities;
public class Order public class Order
{ {
[DisplayName("Номер заказа")]
public int Id { get; private set; } public int Id { get; private set; }
[DisplayName("Дата заказа")] [DisplayName("Дата заказа")]
@ -27,9 +28,15 @@ public class Order
public int MasterId { get; private set; } public int MasterId { get; private set; }
[DisplayName("Работник")] [DisplayName("Работник")]
public string MasterName { get; private set; } = string.Empty; public string MasterName { get; set; } = string.Empty;
public IEnumerable<OrderDetail> OrderDetails { get; private set; } = []; [DisplayName("Детали и кол-во")]
public string OrderAndDetail => OrderDetails != null && OrderDetails.Any()
? string.Join(", ", OrderDetails.Select(od => $"{od.DetailName}: {od.DetailCount}"))
: "Нет данных";
[Browsable(false)]
public IEnumerable<OrderDetail> OrderDetails { get; set; } = [];
public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion, public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion,
DateTime dateIssue, IEnumerable<OrderDetail> orderDetails) DateTime dateIssue, IEnumerable<OrderDetail> orderDetails)
@ -45,18 +52,14 @@ public class Order
OrderDetails = orderDetails OrderDetails = orderDetails
}; };
} }
public static Order CreateOperation(TempOrderDetail tempOrderDetail, IEnumerable<OrderDetail> orderDetails)
public void SetOrderDetails(IEnumerable<OrderDetail> orderDetails)
{ {
return new Order if (orderDetails != null && orderDetails.Any())
{ {
Id = tempOrderDetail.Id, OrderDetails = orderDetails;
OrderDate = tempOrderDetail.OrderDate, }
DateCompletion = tempOrderDetail.DateCompletion,
DateIssue = tempOrderDetail.DateIssue,
FullPrice = tempOrderDetail.FullPrice,
MasterId = tempOrderDetail.MasterId,
OrderDetails = orderDetails
};
} }
} }

View File

@ -1,16 +1,11 @@
using System; namespace ProjectRepairCompany.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRepairCompany.Entities;
public class OrderDetail public class OrderDetail
{ {
public int OrderId { get; private set; } public int OrderId { get; private set; }
public int DetailId { get; private set; } public int DetailId { get; private set; }
public int DetailCount { get; private set; } public int DetailCount { get; private set; }
public string DetailName { get; private set; } = string.Empty;
public static OrderDetail CreateOperation(int orderId, int detailId, int detailCount) public static OrderDetail CreateOperation(int orderId, int detailId, int detailCount)
{ {

View File

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRepairCompany.Entities;
public class TempOrderDetail
{
public int Id { get; private set; }
public DateTime OrderDate { get; private set; }
public DateTime DateCompletion { get; private set; }
public DateTime DateIssue { get; private set; }
public int FullPrice { get; private set; }
public int MasterId { get; private set; }
public int DetailId { get; private set; }
public int DetailCount { get; private set; }
}

View File

@ -41,18 +41,18 @@
panel1.Controls.Add(ButtonDel); panel1.Controls.Add(ButtonDel);
panel1.Controls.Add(ButtonAdd); panel1.Controls.Add(ButtonAdd);
panel1.Dock = DockStyle.Right; panel1.Dock = DockStyle.Right;
panel1.Location = new Point(871, 0); panel1.Location = new Point(1169, 0);
panel1.Name = "panel1"; panel1.Name = "panel1";
panel1.Size = new Size(193, 450); panel1.Size = new Size(193, 397);
panel1.TabIndex = 2; panel1.TabIndex = 2;
// //
// ButtonDel // ButtonDel
// //
ButtonDel.BackgroundImage = Properties.Resources.Ic_remove_circle_48px_svg; ButtonDel.BackgroundImage = Properties.Resources.Ic_remove_circle_48px_svg;
ButtonDel.BackgroundImageLayout = ImageLayout.Stretch; ButtonDel.BackgroundImageLayout = ImageLayout.Stretch;
ButtonDel.Location = new Point(42, 213); ButtonDel.Location = new Point(42, 188);
ButtonDel.Name = "ButtonDel"; ButtonDel.Name = "ButtonDel";
ButtonDel.Size = new Size(117, 83); ButtonDel.Size = new Size(117, 73);
ButtonDel.TabIndex = 1; ButtonDel.TabIndex = 1;
ButtonDel.UseVisualStyleBackColor = true; ButtonDel.UseVisualStyleBackColor = true;
ButtonDel.Click += ButtonDel_Click; ButtonDel.Click += ButtonDel_Click;
@ -61,9 +61,9 @@
// //
ButtonAdd.BackgroundImage = Properties.Resources._43_436254_plus_green_plus_icon_png; ButtonAdd.BackgroundImage = Properties.Resources._43_436254_plus_green_plus_icon_png;
ButtonAdd.BackgroundImageLayout = ImageLayout.Stretch; ButtonAdd.BackgroundImageLayout = ImageLayout.Stretch;
ButtonAdd.Location = new Point(42, 20); ButtonAdd.Location = new Point(42, 18);
ButtonAdd.Name = "ButtonAdd"; ButtonAdd.Name = "ButtonAdd";
ButtonAdd.Size = new Size(117, 74); ButtonAdd.Size = new Size(117, 65);
ButtonAdd.TabIndex = 0; ButtonAdd.TabIndex = 0;
ButtonAdd.UseVisualStyleBackColor = true; ButtonAdd.UseVisualStyleBackColor = true;
ButtonAdd.Click += ButtonAdd_Click; ButtonAdd.Click += ButtonAdd_Click;
@ -83,14 +83,14 @@
dataGridView.ReadOnly = true; dataGridView.ReadOnly = true;
dataGridView.RowHeadersVisible = false; dataGridView.RowHeadersVisible = false;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(871, 450); dataGridView.Size = new Size(1169, 397);
dataGridView.TabIndex = 3; dataGridView.TabIndex = 3;
// //
// FormOrders // FormOrders
// //
AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(1064, 450); ClientSize = new Size(1362, 397);
Controls.Add(dataGridView); Controls.Add(dataGridView);
Controls.Add(panel1); Controls.Add(panel1);
Name = "FormOrders"; Name = "FormOrders";

View File

@ -27,8 +27,8 @@ public class ChartReport
{ {
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
new PdfBuilder(filePath) new PdfBuilder(filePath)
.AddHeader("Заказы мастера") .AddHeader("Заказы выполненные мастерами")
.AddPieChart("Выполненные заказы", GetData(dateTime)) .AddPieChart($"Выполненные заказы за {dateTime:dd MMMM yyyy}", GetData(dateTime))
.Build(); .Build();
return true; return true;
} }
@ -41,13 +41,12 @@ public class ChartReport
private List<(string Caption, double Value)> GetData(DateTime dateTime) private List<(string Caption, double Value)> GetData(DateTime dateTime)
{ {
return _orderRepository return _orderRepository
.ReadOrders() .ReadOrders(dateForm: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.Where(x => x.DateIssue.Date == dateTime.Date) .GroupBy(x => x.MasterName, (key, group) => new {
.GroupBy(x => x.MasterId, (key, group) => new { MasterName = key,
Id = key,
Count = group.Count() Count = group.Count()
}) })
.Select(x => (x.Id.ToString(), (double)x.Count)) .Select(x => (x.MasterName.ToString(), (double)x.Count))
.ToList(); .ToList();
} }

View File

@ -44,56 +44,46 @@ internal class TableReport
private List<string[]> GetData(int masterId, string masterName, DateTime startDate, DateTime endDate) private List<string[]> GetData(int masterId, string masterName, DateTime startDate, DateTime endDate)
{ {
var orders = _orderRepository var orders = _orderRepository
.ReadOrders() .ReadOrders(startDate, endDate, masterId)
.Where(order => order.MasterId == masterId && order.DateIssue >= startDate && order.DateIssue <= endDate) .ToList();
.ToList();
if (!orders.Any()) if (!orders.Any())
return new List<string[]> { Headers }; return new List<string[]> { Headers };
var detailsData = orders var detailsData = orders
.Select(order => new .Select(order => new
{ {
MasterName = masterName, MasterName = masterName,
OrderId = order.Id,
OrderDate = order.DateIssue, OrderDate = order.DateIssue,
Details = string.Join(", ", order.OrderDetails.Select(d => $"ID: {d.DetailId} Кол-во: {d.DetailCount}")), OrderId = order.Id,
Details = string.Join(", ", order.OrderDetails.Select(d => $"Деталь: {d.DetailName} Кол-во: {d.DetailCount}")),
FullPrice = order.FullPrice FullPrice = order.FullPrice
}) })
.OrderBy(detail => detail.OrderDate)
.ToList(); .ToList();
var result = new List<string[]> { Headers }; var result = new List<string[]> { Headers };
result.AddRange(detailsData.Select(detail => new string[] result.AddRange(detailsData.Select(detail => new string[]
{ {
detail.MasterName, detail.MasterName,
detail.OrderId.ToString(), detail.OrderId.ToString(),
detail.OrderDate.ToString("dd.MM.yyyy"), detail.OrderDate.ToString("dd.MM.yyyy"),
detail.Details.ToString(), detail.Details.ToString(),
detail.FullPrice.ToString() detail.FullPrice.ToString()
})); }));
result.Add(new string[] result.Add(new string[]
{ {
"Итого", "Итого",
"", "",
"", "",
"", "",
detailsData.Sum(d => d.FullPrice).ToString() detailsData.Sum(d => d.FullPrice).ToString()
}); });
return result; return result;
} }
} }

View File

@ -64,7 +64,6 @@ public class OrderRepository : IOrderRepository
} }
} }
public void DeleteOrder(int id) public void DeleteOrder(int id)
{ {
_logger.LogInformation("Удаление объекта с Id {OrderId}", id); _logger.LogInformation("Удаление объекта с Id {OrderId}", id);
@ -90,7 +89,6 @@ public class OrderRepository : IOrderRepository
} }
public Order ReadOrderById(int id) public Order ReadOrderById(int id)
{ {
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
@ -112,26 +110,74 @@ public class OrderRepository : IOrderRepository
} }
public IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? masterId = null) public IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? masterId = null)
{ {
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
var builder = new QueryBuilder();
if (masterId.HasValue)
{
builder.AddCondition("o.MasterId = @masterId");
}
if (dateForm.HasValue)
{
builder.AddCondition("o.DateIssue >= @dateForm");
}
if (dateTo.HasValue)
{
builder.AddCondition("o.DateIssue <= @dateTo");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open(); connection.Open();
var querySelect = @" var querySelect = @$"
SELECT o.*, od.DetailId, od.DetailCount, m.MasterName as ""MasterName"" SELECT
FROM ""Order"" o o.Id AS Id,
LEFT JOIN OrderDetail od ON o.Id = od.OrderId o.OrderDate,
LEFT JOIN Master m ON m.Id = o.MasterId o.DateCompletion,
WHERE (@MasterId IS NULL OR o.MasterId = @MasterId)"; o.DateIssue,
var orders = connection.Query<TempOrderDetail>(querySelect); o.FullPrice,
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orders)); o.MasterId,
return orders.GroupBy(x => x.Id, y => y, m.MasterName AS MasterName,
(key,value) => Order.CreateOperation(value.First(), od.DetailId,
value.Select(z => OrderDetail.CreateOperation(0, z.DetailId, z.DetailCount)))).ToList(); od.DetailCount,
d.namedetail AS DetailName
FROM ""Order"" o
LEFT JOIN OrderDetail od ON o.Id = od.OrderId
LEFT JOIN Detail d ON d.Id = od.DetailId
LEFT JOIN Master m ON m.Id = o.MasterId
{builder.Build()}
ORDER BY o.DateIssue";
var orderDict = new Dictionary<int, Order>();
connection.Query<Order, OrderDetail, Order>(querySelect,
(order, orderDetail) =>
{
if (!orderDict.TryGetValue(order.Id, out var existingOrder))
{
existingOrder = order;
existingOrder.OrderDetails = new List<OrderDetail>();
orderDict.Add(existingOrder.Id, existingOrder);
}
if (orderDetail != null)
{
((List<OrderDetail>)existingOrder.OrderDetails).Add(orderDetail);
}
return existingOrder;
},
splitOn: "DetailId",
param: new { dateForm, dateTo, masterId });
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orderDict.Values));
return orderDict.Values.ToList();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRepairCompany.Repositories.Implementations;
internal class QueryBuilder
{
private readonly StringBuilder _builder;
public QueryBuilder()
{
_builder = new();
}
public QueryBuilder AddCondition(string condition)
{
if (_builder.Length > 0)
{
_builder.Append(" AND ");
}
_builder.Append(condition);
return this;
}
public string Build()
{
if (_builder.Length == 0)
{
return string.Empty;
}
return $"WHERE {_builder}";
}
}