Skip to content

Commit

Permalink
PT-8665: add Merged Prices Service (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Sep 15, 2022
1 parent 8e9ae33 commit 6dc66da
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/VirtoCommerce.PricingModule.Core/Model/MergedPrice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.PricingModule.Core.Model
{
public class MergedPrice : Entity
{
public string Currency { get; set; }

public string PricelistId { get; set; }

public string ProductId { get; set; }

public decimal? Sale { get; set; }

public decimal List { get; set; }

public int MinQuantity { get; set; }

public MergedPriceState State { get; set; }
}
}
23 changes: 23 additions & 0 deletions src/VirtoCommerce.PricingModule.Core/Model/MergedPriceGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace VirtoCommerce.PricingModule.Core.Model
{
public class MergedPriceGroup
{
public string ProductId { get; set; }

public string ProductName { get; set; }

public string ProductCode { get; set; }

public string ProductImgSrc { get; set; }

public int GroupPricesCount { get; set; }

public MergedPriceState GroupState { get; set; }

public decimal? MinSalePrice { get; set; }
public decimal? MaxSalePrice { get; set; }

public decimal MinListPrice { get; set; }
public decimal MaxListPrice { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace VirtoCommerce.PricingModule.Core.Model
{
public enum MergedPriceState
{
Base = 0,
New = 1,
Updated = 2,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.PricingModule.Core.Model.Search
{
public class MergedPriceGroupSearchResult : GenericSearchResult<MergedPriceGroup>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.PricingModule.Core.Model.Search
{
public class MergedPriceSearchCriteria : SearchCriteriaBase
{
public bool All { get; set; }

public string BasePriceListId { get; set; }

public string PriorityPriceListId { get; set; }

public List<string> ProductIds { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using VirtoCommerce.Platform.Core.Common;

namespace VirtoCommerce.PricingModule.Core.Model.Search
{
public class MergedPriceSearchResult : GenericSearchResult<MergedPrice>
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading.Tasks;
using VirtoCommerce.PricingModule.Core.Model.Search;

namespace VirtoCommerce.PricingModule.Core.Services
{
public interface IMergedPriceSearchService
{
Task<MergedPriceSearchResult> SearchGroupPricesAsync(MergedPriceSearchCriteria criteria);

Task<MergedPriceGroupSearchResult> SearchGroupsAsync(MergedPriceSearchCriteria criteria);
}
}
34 changes: 34 additions & 0 deletions src/VirtoCommerce.PricingModule.Data/Model/MergedPriceEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.PricingModule.Core.Model;

namespace VirtoCommerce.PricingModule.Data.Model
{
public class MergedPriceEntity : Entity
{
public decimal? Sale { get; set; }

public decimal List { get; set; }

public string ProductId { get; set; }

public decimal MinQuantity { get; set; }

public string PricelistId { get; set; }

public int State { get; set; }

public MergedPrice ToModel(MergedPrice model)
{
model.Id = Id;

model.List = List;
model.MinQuantity = (int)MinQuantity;
model.PricelistId = PricelistId;
model.ProductId = ProductId;
model.Sale = Sale;
model.State = (MergedPriceState)State;

return model;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using VirtoCommerce.PricingModule.Core.Model;

namespace VirtoCommerce.PricingModule.Data.Model
{
public class MergedPriceGroupEntity
{
public string ProductId { get; set; }

public int GroupPricesCount { get; set; }

public int GroupState { get; set; }

public decimal? MinSalePrice { get; set; }
public decimal? MaxSalePrice { get; set; }

public decimal MinListPrice { get; set; }
public decimal MaxListPrice { get; set; }

public MergedPriceGroup ToModel(MergedPriceGroup model)
{
model.ProductId = ProductId;
model.GroupPricesCount = GroupPricesCount;
model.GroupState = (MergedPriceState)GroupState;
model.MinSalePrice = MinSalePrice;
model.MaxSalePrice = MaxSalePrice;
model.MinListPrice = MinListPrice;
model.MaxListPrice = MaxListPrice;

return model;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public interface IPricingRepository : IRepository
Task DeletePricesAsync(IEnumerable<string> ids);
Task DeletePricelistsAsync(IEnumerable<string> ids);
Task DeletePricelistAssignmentsAsync(IEnumerable<string> ids);

IQueryable<MergedPriceEntity> GetMergedPrices(string basePriceListId, string priorityPriceListId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<PricelistAssignmentEntity>().HasOne(x => x.Pricelist).WithMany(x => x.Assignments).IsRequired().HasForeignKey(x => x.PricelistId);
modelBuilder.Entity<PricelistAssignmentEntity>().Property(x => x.Id).HasMaxLength(128).ValueGeneratedOnAdd();

// ugly hack because EFCore removed ultra useful DbQuery type in 3.0
modelBuilder.Entity<MergedPriceEntity>().HasNoKey().ToView("empty");

base.OnModelCreating(modelBuilder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,61 @@ public Task DeletePricelistAssignmentsAsync(IEnumerable<string> ids)
return ExecuteSqlCommandAsync("DELETE FROM PricelistAssignment WHERE Id IN ({0})", ids);
}

/// <summary>
/// Readonly only DBSet, do not try to insert/update/delete anything here
/// </summary>
public IQueryable<MergedPriceEntity> GetMergedPrices(string basePriceListId, string priorityPriceListId)
{
var command = GetSearchMergedPricesCommand(basePriceListId, priorityPriceListId);
var query = DbContext.Set<MergedPriceEntity>().FromSqlRaw(command.Text, command.Parameters.ToArray());
return query;
}

#region Raw queries
private static Command GetSearchMergedPricesCommand(string basePriceListId, string priorityPriceListId)
{
var template = @"
select a.* from
(select p.Id, p.ProductId, p.List, p.Sale, p.MinQuantity, p.PricelistId, 2 as [State]
FROM Price AS p
JOIN Price AS b
on p.ProductId = b.ProductId and p.MinQuantity = b.MinQuantity
WHERE b.PricelistId = @basePriceListId AND p.PricelistId = @priorityPriceListId
union
select Id, ProductId, List, Sale, MinQuantity, PricelistId, 0 as [State]
from Price c
where c.PricelistId = @basePriceListId and NOT EXISTS
(
select p.ProductId, p.MinQuantity
FROM Price AS p
JOIN Price AS b
on p.ProductId = c.ProductId and p.MinQuantity = c.MinQuantity
WHERE b.PricelistId = @basePriceListId AND p.PricelistId = @priorityPriceListId
)
union
select Id, ProductId, List, Sale, MinQuantity, PricelistId, 1 as [State]
from Price s
where s.PricelistId = @priorityPriceListId and NOT EXISTS
(
select p.ProductId, p.MinQuantity
FROM Price AS p
JOIN Price AS b
on b.ProductId = s.ProductId and b.MinQuantity = s.MinQuantity
WHERE b.PricelistId = @basePriceListId AND p.PricelistId = @priorityPriceListId
)) a";

var basePriceListIdParam = new SqlParameter("@basePriceListId", basePriceListId);
var priorityPriceListIdParam = new SqlParameter("@priorityPriceListId", priorityPriceListId);

return new Command
{
Text = template,
Parameters = new List<object> { basePriceListIdParam, priorityPriceListIdParam }
};
}
#endregion

#region Commands
protected virtual Task ExecuteSqlCommandAsync(string commandTemplate, IEnumerable<string> parameterValues)
{
if (parameterValues?.Count() > 0)
Expand Down Expand Up @@ -96,5 +150,6 @@ protected class Command
public string Text { get; set; }
public IEnumerable<object> Parameters { get; set; }
}
#endregion
}
}
Loading

0 comments on commit 6dc66da

Please sign in to comment.