Browse Source

Used stored proc for GetMostPurchasedItem

BISK2023-2-performance-analysis-feature
Benjamin Arhen 2 years ago
parent
commit
3e68149951
  1. 52
      Server/Services/AnalyticalService.cs

52
Server/Services/AnalyticalService.cs

@ -1,8 +1,12 @@
using Biskilog_Accounting.Server.POSModels; using Biskilog_Accounting.Server.POSModels;
using Biskilog_Accounting.Shared.CustomModels; using Biskilog_Accounting.Shared.CustomModels;
using Biskilog_Accounting.Shared.Enums;
using Biskilog_Accounting.Shared.Interfaces; using Biskilog_Accounting.Shared.Interfaces;
using Biskilog_Accounting.Shared.POSModels; using Biskilog_Accounting.Shared.POSModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers; using Microsoft.Net.Http.Headers;
using MySqlConnector;
using System.Collections.Generic;
using System.Data.Entity; using System.Data.Entity;
namespace Biskilog_Accounting.Server.Services namespace Biskilog_Accounting.Server.Services
@ -90,23 +94,43 @@ namespace Biskilog_Accounting.Server.Services
public IEnumerable<MostPurchasedItem> GetMostPurchasedItem(DateTime a_start, DateTime a_end) public IEnumerable<MostPurchasedItem> GetMostPurchasedItem(DateTime a_start, DateTime a_end)
{ {
//TODO either rewrite query or increase memory on server to deal with comparison mode performance issue
string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!; string token = m_httpContext.Request.Headers[HeaderNames.Authorization]!;
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
{
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token); IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
var items = (from s in m_context.Tblcarts using (var command = m_context.Database.GetDbConnection().CreateCommand())
join p in m_context.Tblproducts on s.Id equals p.Pcode {
where s.Date >= a_start && s.Date <= a_end && s.BranchId == accessiblebranches.First() command.CommandText = "CALL GetMostPurchasedItems(@p0, @p1, @p2, @p3)";
group s by p into g command.Parameters.Add(new MySqlParameter("@p0", a_start));
orderby g.Count() descending command.Parameters.Add(new MySqlParameter("@p1", a_end));
select new MostPurchasedItem command.Parameters.Add(new MySqlParameter("@p2", string.Join(", ", accessiblebranches.ToArray())));
{ command.Parameters.Add(new MySqlParameter("@p3", 50));
ProductId = g.Key.Pcode,
ProductName = g.Key.ProductName, m_context.Database.OpenConnection();
NbrTimesSold = g.Count(),
Revenue = g.Sum(s => s.Price) using (var reader = command.ExecuteReader())
}).Take(50).ToList(); {
return items; List<MostPurchasedItem> result = new List<MostPurchasedItem>();
while (reader.Read())
{
MostPurchasedItem item = new MostPurchasedItem
{
ProductId = reader.GetString(0),
ProductName = reader.GetString(1),
NbrTimesSold = reader.GetInt32(2),
Revenue = reader.GetDecimal(3)
};
result.Add(item);
}
return result;
}
}
}
return new List<MostPurchasedItem>();
} }
public IEnumerable<ProductItem> GetOutOfStockItems() public IEnumerable<ProductItem> GetOutOfStockItems()

Loading…
Cancel
Save