From 3e68149951f631293b3b697ae47d3aa3026fd211 Mon Sep 17 00:00:00 2001 From: barhen Date: Mon, 29 May 2023 19:47:48 -0500 Subject: [PATCH] Used stored proc for GetMostPurchasedItem --- Server/Services/AnalyticalService.cs | 54 ++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/Server/Services/AnalyticalService.cs b/Server/Services/AnalyticalService.cs index 2795e05..0eb9623 100644 --- a/Server/Services/AnalyticalService.cs +++ b/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 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]!; - IEnumerable 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; + if (AuthEnums.Valid == m_tokenService.ValidateToken(token)) + { + IEnumerable accessiblebranches = m_tokenService.BranchIds(token); + + 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 result = new List(); + + 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(); } public IEnumerable GetOutOfStockItems()