ПИбд-22 Морозов Д.В. Лабораторная 4 #9

Closed
MorozovDanil wants to merge 4 commits from LabWork№4 into LabWork№3
9 changed files with 146 additions and 104 deletions
Showing only changes of commit c7d41abb50 - Show all commits

View File

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

View File

@ -9,6 +9,7 @@ namespace ProjectRepairCompany.Entities;
public class Order
{
[DisplayName("Номер заказа")]
public int Id { get; private set; }
[DisplayName("Дата заказа")]
@ -27,9 +28,15 @@ public class Order
public int MasterId { get; private set; }
[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,
DateTime dateIssue, IEnumerable<OrderDetail> orderDetails)
@ -45,18 +52,14 @@ public class Order
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,
OrderDate = tempOrderDetail.OrderDate,
DateCompletion = tempOrderDetail.DateCompletion,
DateIssue = tempOrderDetail.DateIssue,
FullPrice = tempOrderDetail.FullPrice,
MasterId = tempOrderDetail.MasterId,
OrderDetails = orderDetails
};
OrderDetails = orderDetails;
}
}
}

View File

@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRepairCompany.Entities;
namespace ProjectRepairCompany.Entities;
public class OrderDetail
{
public int OrderId { get; private set; }
public int DetailId { 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)
{

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

View File

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

View File

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

View File

@ -64,7 +64,6 @@ public class OrderRepository : IOrderRepository
}
}
public void DeleteOrder(int id)
{
_logger.LogInformation("Удаление объекта с Id {OrderId}", id);
@ -90,7 +89,6 @@ public class OrderRepository : IOrderRepository
}
public Order ReadOrderById(int id)
{
_logger.LogInformation("Получение всех объектов");
@ -112,26 +110,74 @@ public class OrderRepository : IOrderRepository
}
public IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? masterId = null)
{
_logger.LogInformation("Получение всех объектов");
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);
connection.Open();
var querySelect = @"
SELECT o.*, od.DetailId, od.DetailCount, m.MasterName as ""MasterName""
FROM ""Order"" o
LEFT JOIN OrderDetail od ON o.Id = od.OrderId
LEFT JOIN Master m ON m.Id = o.MasterId
WHERE (@MasterId IS NULL OR o.MasterId = @MasterId)";
var orders = connection.Query<TempOrderDetail>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orders));
return orders.GroupBy(x => x.Id, y => y,
(key,value) => Order.CreateOperation(value.First(),
value.Select(z => OrderDetail.CreateOperation(0, z.DetailId, z.DetailCount)))).ToList();
var querySelect = @$"
SELECT
o.Id AS Id,
o.OrderDate,
o.DateCompletion,
o.DateIssue,
o.FullPrice,
o.MasterId,
m.MasterName AS MasterName,
od.DetailId,
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)
{
@ -142,4 +188,4 @@ WHERE (@MasterId IS NULL OR o.MasterId = @MasterId)";
}

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}";
}
}