using FruitBank.Common.Models; using Mango.Nop.Core.Repositories; using Nop.Core.Caching; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Customers; using Nop.Data; using Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer.Interfaces; using Nop.Services.Catalog; using Nop.Services.Logging; using NUglify.Helpers; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; public class FruitBankDbContext : MgDbContextBase, IPartnerDbSet, IShippingDbSet, IShippingItemDbSet, IShippingDocumentDbSet { private readonly IProductService _productService; private readonly IStaticCacheManager _staticCacheManager; public PartnerDbTable Partners { get; set; } public ShippingDbTable Shippings { get; set; } public ShippingItemDbTable ShippingItems { get; set; } public ShippingDocumentDbTable ShippingDocuments { get; set; } public IRepository Products { get; set; } public IRepository Customers { get; set; } public IRepository CustomerRoles { get; set; } public IRepository CustomerRoleMappings { get; set; } public FruitBankDbContext(INopDataProvider dataProvider, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingItemDbTable shippingItemDbTable, ShippingDocumentDbTable shippingDocumentDbTable, IProductService productService, IStaticCacheManager staticCacheManager, IRepository productRepository, IRepository customerRepository, IRepository customerCustomerRoleMappingRepository, IRepository customerRoleRepository, ILogger logger) : base(dataProvider, logger) { _productService = productService; _staticCacheManager = staticCacheManager; Partners = partnerDbTable; Shippings = shippingDbTable; ShippingItems = shippingItemDbTable; ShippingDocuments = shippingDocumentDbTable; Products = productRepository; Customers = customerRepository; CustomerRoles = customerRoleRepository; CustomerRoleMappings = customerCustomerRoleMappingRepository; } public IQueryable GetMeasuringModelByShippingId(int shippingId) { var query = from p in Partners.Table join s in Shippings.Table on p.Id equals s.PartnerId where s.Id == shippingId select new MeasuringModel(p.Name); return query; } public IQueryable GetCustormersBySystemRoleName(string systemRoleName) { if (systemRoleName.IsNullOrWhiteSpace()) throw new ArgumentException("systemRoleName.IsNullOrWhiteSpace()", nameof(systemRoleName)); var query = from c in Customers.Table join crm in CustomerRoleMappings.Table on c.Id equals crm.CustomerId join cr in CustomerRoles.Table on crm.CustomerRoleId equals cr.Id where c.Active && !c.Deleted && cr.SystemName == systemRoleName select c; return query.Distinct().OrderBy(o => o.Username); } public IQueryable GetAllProducts() => Products.Table.Where(p => !p.Deleted).OrderBy(o => o.Name); }