Улучшения 2 и 3

This commit is contained in:
2024-12-19 12:11:44 +04:00
parent e029ce6f6b
commit e8372e02a0
5 changed files with 55 additions and 15 deletions

View File

@@ -22,7 +22,7 @@ namespace RealEstateTransactions.Reports
{ {
new PdfBuilder(filePath) new PdfBuilder(filePath)
.AddHeader("Доход с продажи квартир") .AddHeader("Доход с продажи квартир")
.AddPieChart("Проданные квартиры", GetData(dateTime)) .AddPieChart($"Проданные квартиры {dateTime:dd.MM.yyyy}", GetData(dateTime))
.Build(); .Build();
return true; return true;
} }
@@ -36,8 +36,7 @@ namespace RealEstateTransactions.Reports
private List<(string Caption, double Value)> GetData(DateTime dateTime) private List<(string Caption, double Value)> GetData(DateTime dateTime)
{ {
return _dealRepository return _dealRepository
.ReadDeals() .ReadDeals(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.Where(x => x.Deal_date.Date == dateTime.Date)
.Select(x => (x.Apartment_ID.ToString(), (double)x.Deal_price)) .Select(x => (x.Apartment_ID.ToString(), (double)x.Deal_price))
.ToList(); .ToList();
} }

View File

@@ -30,7 +30,7 @@ namespace RealEstateTransactions.Reports
{ {
new ExcelBuilder(filePath) new ExcelBuilder(filePath)
.AddHeader("Сводка по доходу от продажи квартиры", 0, 4) .AddHeader("Сводка по доходу от продажи квартиры", 0, 4)
.AddParagraph("за период", 0) .AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0)
.AddTable([10, 10, 10, 15], GetData(apartmentId, startDate, .AddTable([10, 10, 10, 15], GetData(apartmentId, startDate,
endDate)) endDate))
.Build(); .Build();
@@ -47,9 +47,7 @@ namespace RealEstateTransactions.Reports
endDate) endDate)
{ {
var data = _dealRepository var data = _dealRepository
.ReadDeals() .ReadDeals(startDate, endDate, apartmentId)
.Where(x => x.Deal_date >= startDate && x.Deal_date <= endDate &&
x.Apartment_ID == apartmentId)
.Select(x => new { .Select(x => new {
DealId = (int?)x.Id, Date = (DateTime?)x.Deal_date, DealPrice = (float?)x.Deal_price, ServicesPrice = (float?)null DealId = (int?)x.Id, Date = (DateTime?)x.Deal_date, DealPrice = (float?)x.Deal_price, ServicesPrice = (float?)null
}) })
@@ -67,7 +65,7 @@ namespace RealEstateTransactions.Reports
.Union( .Union(
data data
.Select(x => new string[] { .Select(x => new string[] {
x.DealId?.ToString() ?? string.Empty, x.Date?.ToString() ?? string.Empty, x.DealId?.ToString("N0") ?? string.Empty, x.Date?.ToString("dd.MM.yyyy") ?? string.Empty,
x.DealPrice?.ToString() ?? string.Empty, x.ServicesPrice?.ToString() ?? string.Empty})) x.DealPrice?.ToString() ?? string.Empty, x.ServicesPrice?.ToString() ?? string.Empty}))
.Union( .Union(
[["Всего", "", data.Sum(x => x.DealPrice ?? 0).ToString(), [["Всего", "", data.Sum(x => x.DealPrice ?? 0).ToString(),

View File

@@ -4,8 +4,8 @@ namespace RealEstateTransactions.Repositories
{ {
public interface IDealRepository public interface IDealRepository
{ {
IEnumerable<Deal> ReadDeals(DateTime? dateForm = null, DateTime? dateTo = null, IEnumerable<Deal> ReadDeals(DateTime? dateFrom = null, DateTime? dateTo = null,
int? servicesID = null, int? dealID = null); int? apartmentId = null);
void CreateDeal(Deal deal); void CreateDeal(Deal deal);

View File

@@ -69,14 +69,22 @@ namespace RealEstateTransactions.Repositories.Implementations
} }
} }
public IEnumerable<Deal> ReadDeals(DateTime? dateForm = null, DateTime? dateTo = null, public IEnumerable<Deal> ReadDeals(DateTime? dateFrom = null, DateTime? dateTo = null,
int? servicesID = null, int? dealID = null) int? apartmentId = null)
{ {
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
var builder = new QueryBuilder();
if (dateFrom.HasValue)
builder.AddCondition("de.Deal_date >= @dateFrom");
if (dateTo.HasValue)
builder.AddCondition("de.Deal_date <= @dateTo");
if (apartmentId.HasValue)
builder.AddCondition("de.Apartment_Id = @apartmentId");
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var selectQuery = @"SELECT var selectQuery = $@"SELECT
de.*, de.*,
bu.Full_name BuyerName, bu.Full_name BuyerName,
sd.Deal_Id, sd.Deal_Id,
@@ -86,7 +94,8 @@ namespace RealEstateTransactions.Repositories.Implementations
FROM Deal de FROM Deal de
LEFT JOIN Buyer bu on bu.Id = de.Buyer_Id LEFT JOIN Buyer bu on bu.Id = de.Buyer_Id
INNER JOIN Services_Deal sd on sd.Deal_Id = de.Id INNER JOIN Services_Deal sd on sd.Deal_Id = de.Id
LEFT JOIN Services ss on ss.Id = sd.Services_Id"; LEFT JOIN Services ss on ss.Id = sd.Services_Id
{builder.Build()}";
var servicesDict = new Dictionary<int, List<ServicesDeal>>(); var servicesDict = new Dictionary<int, List<ServicesDeal>>();
var deals = connection.Query<Deal, ServicesDeal, Deal>(selectQuery, var deals = connection.Query<Deal, ServicesDeal, Deal>(selectQuery,
@@ -100,7 +109,7 @@ namespace RealEstateTransactions.Repositories.Implementations
sd.Add(servicesDeal); sd.Add(servicesDeal);
return deal; return deal;
}, splitOn: "Deal_Id"); }, splitOn: "Deal_Id", param: new {dateFrom, dateTo, apartmentId});
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deals)); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(deals));
return servicesDict.Select(x => return servicesDict.Select(x =>

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RealEstateTransactions.Repositories.Implementations
{
public class QueryBuilder
{
private readonly StringBuilder _builder;
public QueryBuilder()
{
_builder = new StringBuilder();
}
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}";
}
}
}