lab4 hopefully done
This commit is contained in:
parent
79d63202ec
commit
42762d88aa
@ -20,6 +20,8 @@ namespace ProjectPublishing.Entities
|
||||
[Browsable(false)]
|
||||
public int MaterialId { get; private set; }
|
||||
|
||||
[DisplayName("Название материала")]
|
||||
public string MaterialName { get; private set; } = string.Empty;
|
||||
public static MaterialsAdmission CreateOperation(int id, int amount, int materialId)
|
||||
{
|
||||
return new MaterialsAdmission { Id = id, Amount = amount, DateTime = DateTime.Now, MaterialId = materialId };
|
||||
|
@ -89,7 +89,11 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _contactPersonRepository.ReadContactPeople();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _contactPersonRepository.ReadContactPeople();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -89,7 +89,12 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _customerRepository.ReadCustomers();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _customerRepository.ReadCustomers();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
dataGridView.Columns["FullName"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -49,7 +49,11 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _materialAdmissionRepository.ReadMaterialsAdmission();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _materialAdmissionRepository.ReadMaterialsAdmission();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -89,7 +89,11 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _materialRepository.ReadMaterials();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _materialRepository.ReadMaterials();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -70,7 +70,11 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _orderRepository.ReadOrders();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _orderRepository.ReadOrders();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -89,7 +89,11 @@ namespace ProjectPublishing.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void loadList() => dataGridView.DataSource = _printingHouseRepository.ReadPrintingHouses();
|
||||
private void loadList()
|
||||
{
|
||||
dataGridView.DataSource = _printingHouseRepository.ReadPrintingHouses();
|
||||
dataGridView.Columns["Id"].Visible = false;
|
||||
}
|
||||
|
||||
private bool tryGetIDFromRow(out int id)
|
||||
{
|
||||
|
@ -43,9 +43,8 @@ namespace ProjectPublishing.Reports
|
||||
private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
||||
{
|
||||
return _materialRepository
|
||||
.ReadMaterialsAdmission()
|
||||
.Where(x => x.DateTime.Date == dateTime.Date)
|
||||
.GroupBy(x => x.MaterialId, (key, group) => new
|
||||
.ReadMaterialsAdmission(endDate: dateTime)
|
||||
.GroupBy(x => x.MaterialName, (key, group) => new
|
||||
{
|
||||
Id = key,
|
||||
Count = group.Sum(x => x.Amount)
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using ProjectPublishing.Repositories;
|
||||
using ProjectPublishing.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -13,8 +14,9 @@ namespace ProjectPublishing.Reports
|
||||
{
|
||||
private readonly IOrderRepository _orderRepository;
|
||||
private readonly IMaterialAdmissionRepository _materialAdmissionRepository;
|
||||
|
||||
private readonly ILogger<TableReport> _logger;
|
||||
internal static readonly string[] item = ["Материал", "Дата", "Количество поступило", "Количество заказано"];
|
||||
internal static readonly string[] item = ["Материал", "Дата", "Количество поступило", "Количество закупок"];
|
||||
public TableReport(IOrderRepository orderRepository, IMaterialAdmissionRepository materialAdmissionRepository, ILogger<TableReport> logger)
|
||||
{
|
||||
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
||||
@ -43,28 +45,39 @@ namespace ProjectPublishing.Reports
|
||||
private List<string[]> GetData(int materialId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var data = _materialAdmissionRepository
|
||||
.ReadMaterialsAdmission()
|
||||
.Where(x => x.DateTime >= startDate && x.DateTime <= endDate && x.MaterialId == materialId)
|
||||
.ReadMaterialsAdmission(startDate, endDate, materialId)
|
||||
.Select(x => new {
|
||||
MaterialId = x.MaterialId,
|
||||
MaterialName = x.MaterialName,
|
||||
Date = x.DateTime,
|
||||
CountIn = (int?)x.Amount,
|
||||
CountOut = (int?)null
|
||||
})
|
||||
.Union(
|
||||
_orderRepository
|
||||
.ReadOrders() // тут где-то
|
||||
.Where(x => x.OrderDate >= startDate && x.OrderDate <= endDate.AddDays(1) && x.Materials.Any(y => y.MaterialsId == materialId))
|
||||
.SelectMany(order => order.Materials
|
||||
.Where(m => m.MaterialsId == materialId)
|
||||
.Select(m => new
|
||||
{
|
||||
MaterialId = m.MaterialsId,
|
||||
Date = order.OrderDate,
|
||||
CountIn = (int?)null,
|
||||
CountOut = (int?)m.Amount
|
||||
})))
|
||||
.OrderBy(x => x.Date);
|
||||
CountOut = (int?)1
|
||||
}).
|
||||
ToList();
|
||||
|
||||
//var data = _materialAdmissionRepository
|
||||
// .ReadMaterialsAdmission(startDate, endDate, materialId)
|
||||
// .Select(x => new {
|
||||
// MaterialId= x.MaterialId,
|
||||
// MaterialName = x.MaterialName,
|
||||
// Date = x.DateTime,
|
||||
// CountIn = (int?)x.Amount,
|
||||
// CountOut = (int?)null
|
||||
// })
|
||||
// .Union(
|
||||
// _orderRepository
|
||||
// .ReadOrders() // тут где-то
|
||||
// .Where(x => x.OrderDate >= startDate && x.OrderDate <= endDate.AddDays(1) && x.Materials.Any(y => y.MaterialsId == materialId))
|
||||
// .SelectMany(order => order.Materials
|
||||
// .Where(m => m.MaterialsId == materialId)
|
||||
// .Select(m => new
|
||||
// {
|
||||
// MaterialId = m.MaterialsId,
|
||||
// Date = order.OrderDate,
|
||||
// CountIn = (int?)null,
|
||||
// CountOut = (int?)m.Amount
|
||||
// })))
|
||||
//.OrderBy(x => x.Date);
|
||||
_logger.LogDebug("Объединенные данные в отчет: {json}", JsonConvert.SerializeObject(data));
|
||||
return new List<string[]> { item }
|
||||
.Union(
|
||||
|
@ -9,7 +9,7 @@ namespace ProjectPublishing.Repositories
|
||||
{
|
||||
public interface IMaterialAdmissionRepository
|
||||
{
|
||||
IEnumerable<MaterialsAdmission> ReadMaterialsAdmission();
|
||||
IEnumerable<MaterialsAdmission> ReadMaterialsAdmission(DateTime? startDate = null, DateTime? endDate = null, int? materialId = null);
|
||||
|
||||
void CreateMaterialsAdmission(MaterialsAdmission materialAdmission);
|
||||
|
||||
|
@ -0,0 +1,16 @@
|
||||
using DocumentFormat.OpenXml.Office2013.Excel;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories
|
||||
{
|
||||
public interface IOrderMaterialsRepository
|
||||
{
|
||||
IEnumerable<OrderMaterials> ReadOrderMaterials(DateTime? startDate = null, DateTime? endDate = null, int? materialId = null);
|
||||
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using DocumentFormat.OpenXml.Drawing.Charts;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
@ -8,6 +9,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
@ -41,15 +43,28 @@ VALUES (@Amount, @MaterialId)";
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<MaterialsAdmission> ReadMaterialsAdmission()
|
||||
public IEnumerable<MaterialsAdmission> ReadMaterialsAdmission(DateTime? startDate = null, DateTime? endDate = null, int? materialId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (startDate.HasValue)
|
||||
{
|
||||
builder.AddCondition("ma.dateTime >= @startDate");
|
||||
}
|
||||
if (endDate.HasValue)
|
||||
{
|
||||
builder.AddCondition("ma.dateTime <= @endDate");
|
||||
}
|
||||
if (materialId.HasValue)
|
||||
{
|
||||
builder.AddCondition("ma.MaterialId = @materialId");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM MaterialAdmission";
|
||||
var materialAdmissions = connection.Query<MaterialsAdmission>(querySelect);
|
||||
var querySelect = @$"SELECT ma.*, m.Name as MaterialName FROM MaterialAdmission as ma LEFT JOIN Material as m ON m.Id = ma.materialId {builder.Build()}";
|
||||
var materialAdmissions = connection.Query<MaterialsAdmission>(querySelect, new {startDate, endDate, materialId});
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(materialAdmissions));
|
||||
return materialAdmissions;
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
using Dapper;
|
||||
using DocumentFormat.OpenXml.Office2010.Excel;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectPublishing.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public class OrderMaterialsRepository : IOrderMaterialsRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<OrderMaterialsRepository> _logger;
|
||||
|
||||
public OrderMaterialsRepository(ILogger<OrderMaterialsRepository> logger, IConnectionString connectionString)
|
||||
{
|
||||
_logger = logger;
|
||||
_connectionString = connectionString;
|
||||
}
|
||||
|
||||
public IEnumerable<OrderMaterials> ReadOrderMaterials(DateTime? startDate = null, DateTime? endDate = null, int? materialId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM OrderMaterials";
|
||||
var contactPeople = connection.Query<OrderMaterials>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contactPeople));
|
||||
return contactPeople;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public OrderMaterials ReadOrderMaterialsByIds(int orderId, int materialId)
|
||||
{
|
||||
_logger.LogInformation("Получение объекта по ID");
|
||||
_logger.LogDebug("Объект: {orderId}, {materialId}", orderId, materialId);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
var querySelect = @"
|
||||
SELECT * FROM OrderMaterials
|
||||
WHERE OrderId=@orderId AND MaterialId = @materialId";
|
||||
var orderMaterial = connection.QueryFirst<OrderMaterials>(querySelect, new { orderId, materialId });
|
||||
connection.Close();
|
||||
_logger.LogDebug("Найден объект: {json}", JsonConvert.SerializeObject(orderMaterial));
|
||||
return orderMaterial;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using DocumentFormat.OpenXml.Drawing;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
@ -97,11 +98,14 @@ LEFT JOIN OrderMaterials ma ON o.Id = ma.OrderId
|
||||
LEFT JOIN Customer c ON o.CustomerId = c.Id
|
||||
LEFT JOIN PrintingHouse ph ON o.PrintingId = ph.Id
|
||||
";
|
||||
var printingOrders = connection.Query<TempOrderMaterials>(querySelect);
|
||||
// var printingOrders = connection.Query<TempOrderMaterials>(querySelect);
|
||||
var orderDict = new Dictionary<int, List<OrderMaterials>>();
|
||||
var printingOrders = connection.Query<Order>(querySelect);
|
||||
|
||||
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(printingOrders));
|
||||
|
||||
return printingOrders.GroupBy(x => x.Id, y => y, (key, value) =>
|
||||
Order.CreateOrder(value.First(), value.Select(z => OrderMaterials.Create(0, z.MaterialsId, z.MaterialsAmount)))).ToList();
|
||||
return printingOrders.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectPublishing.Repositories.Implementations
|
||||
{
|
||||
public 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}";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user