using FruitBank.Common.Entities; using LinqToDB; using Mango.Nop.Data.Repositories; using Nop.Core.Caching; using Nop.Core.Configuration; using Nop.Core.Events; using Nop.Data; using Mango.Nop.Core.Loggers; namespace Nop.Plugin.Misc.FruitBankPlugin.Domains.DataLayer; public class ShippingDbTable : MgDbTableBase { public ShippingDbTable(IEventPublisher eventPublisher, INopDataProvider dataProvider, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, AppSettings appSettings) : base(eventPublisher, dataProvider, shortTermCacheManager, staticCacheManager, appSettings) { } public override IOrderedQueryable GetAll() => base.GetAll().OrderBy(s => s.ShippingDate); public IQueryable GetAll(bool loadRelations) { return loadRelations ? GetAll() .LoadWith(s => s.ShippingDocuments).ThenLoad(sd => sd.ShippingItems).ThenLoad(si => si.ShippingItemPallets) .LoadWith(s => s.ShippingDocuments).ThenLoad(sd => sd.ShippingItems).ThenLoad(si => si.Pallet) .LoadWith(s => s.ShippingDocuments).ThenLoad(sd => sd.ShippingItems).ThenLoad(si => si.ProductDto).ThenLoad(prod => prod.GenericAttributes) .LoadWith(s => s.ShippingDocuments).ThenLoad(sd => sd.ShippingDocumentToFiles).ThenLoad(sdtof => sdtof.ShippingDocumentFile) .LoadWith(s => s.ShippingDocuments).ThenLoad(sd => sd.Partner) : GetAll(); } public IQueryable GetAllNotMeasured(bool loadRelations, int lastDaysCount) => GetAll(loadRelations).Where(s => !s.IsAllMeasured || s.MeasuredDate == null || s.MeasuredDate >= DateTime.Now.AddDays(-lastDaysCount)); public Task GetByIdAsync(int id, bool loadRelations) => GetAll(loadRelations).FirstOrDefaultAsync(s => s.Id == id); protected override void OnUpdate(Shipping entity) { if (entity is { IsAllMeasured: true, MeasuredDate: null }) entity.MeasuredDate = DateTime.Now; base.OnUpdate(entity); } }