Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Domains/DataLayer/FruitBankDbContext.cs

82 lines
3.3 KiB
C#

using DocumentFormat.OpenXml.Drawing;
using FruitBank.Common.Models;
using LinqToDB;
using Mango.Nop.Core.Repositories;
using Microsoft.CodeAnalysis.CSharp.Syntax;
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<PartnerDbTable>, IShippingDbSet<ShippingDbTable>, IShippingItemDbSet<ShippingItemDbTable>, IShippingDocumentDbSet<ShippingDocumentDbTable>
{
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<Product> Products { get; set; }
public IRepository<Customer> Customers { get; set; }
public IRepository<CustomerRole> CustomerRoles { get; set; }
public IRepository<CustomerCustomerRoleMapping> CustomerRoleMappings { get; set; }
public FruitBankDbContext(INopDataProvider dataProvider, PartnerDbTable partnerDbTable, ShippingDbTable shippingDbTable, ShippingItemDbTable shippingItemDbTable,
ShippingDocumentDbTable shippingDocumentDbTable, IProductService productService, IStaticCacheManager staticCacheManager,
IRepository<Product> productRepository,
IRepository<Customer> customerRepository,
IRepository<CustomerCustomerRoleMapping> customerCustomerRoleMappingRepository,
IRepository<CustomerRole> 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<MeasuringModel> 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<Customer> 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();
}
public IQueryable<Product> GetProducts()
=> Products.Table.Where(p => !p.Deleted);
}