Used stored proc for GetMostPurchasedItem #5

Merged
barhen merged 1 commits from BISK2023-2-performance-analysis-feature into dev 2 years ago
  1. 52
      Server/Services/AnalyticalService.cs

52
Server/Services/AnalyticalService.cs

@ -1,8 +1,12 @@
using Biskilog_Accounting.Server.POSModels;
using Biskilog_Accounting.Shared.CustomModels;
using Biskilog_Accounting.Shared.Enums;
using Biskilog_Accounting.Shared.Interfaces;
using Biskilog_Accounting.Shared.POSModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Net.Http.Headers;
using MySqlConnector;
using System.Collections.Generic;
using System.Data.Entity;
namespace Biskilog_Accounting.Server.Services
@ -90,23 +94,43 @@ namespace Biskilog_Accounting.Server.Services
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]!;
if (AuthEnums.Valid == m_tokenService.ValidateToken(token))
{
IEnumerable<string> accessiblebranches = m_tokenService.BranchIds(token);
var items = (from s in m_context.Tblcarts
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()
group s by p into g
orderby g.Count() descending
select new MostPurchasedItem
{
ProductId = g.Key.Pcode,
ProductName = g.Key.ProductName,
NbrTimesSold = g.Count(),
Revenue = g.Sum(s => s.Price)
}).Take(50).ToList();
return items;
using (var command = m_context.Database.GetDbConnection().CreateCommand())
{
command.CommandText = "CALL GetMostPurchasedItems(@p0, @p1, @p2, @p3)";
command.Parameters.Add(new MySqlParameter("@p0", a_start));
command.Parameters.Add(new MySqlParameter("@p1", a_end));
command.Parameters.Add(new MySqlParameter("@p2", string.Join(", ", accessiblebranches.ToArray())));
command.Parameters.Add(new MySqlParameter("@p3", 50));
m_context.Database.OpenConnection();
using (var reader = command.ExecuteReader())
{
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()

Loading…
Cancel
Save