using Nop.Core; using Nop.Core.Caching; using Nop.Data; using Nop.Plugin.Shipping.FixedByWeightByTotal.Domain; namespace Nop.Plugin.Shipping.FixedByWeightByTotal.Services; /// /// Represents service shipping by weight service implementation /// public class ShippingByWeightByTotalService : IShippingByWeightByTotalService { #region Fields protected readonly FixedByWeightByTotalSettings _pluginSettings; protected readonly IRepository _sbwtRepository; protected readonly IShortTermCacheManager _shortTermCacheManager; protected readonly IStaticCacheManager _staticCacheManager; #endregion #region Ctor public ShippingByWeightByTotalService(FixedByWeightByTotalSettings pluginSettings, IRepository sbwtRepository, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager) { _pluginSettings = pluginSettings; _sbwtRepository = sbwtRepository; _shortTermCacheManager = shortTermCacheManager; _staticCacheManager = staticCacheManager; } #endregion #region Utilities /// /// Get filtered shipping by weight records /// /// Shipping method identifier /// Store identifier /// Warehouse identifier /// Country identifier /// State identifier /// Zip postal code /// /// A task that represents the asynchronous operation /// The task result contains the list of the shipping by weight record /// private async Task> GetRecordsAsync(int shippingMethodId, int storeId, int warehouseId, int countryId, int stateProvinceId, string zip) { var rez = await _shortTermCacheManager.GetAsync(async ()=> await _sbwtRepository.GetAllAsync(async query => { var data = _pluginSettings.LoadAllRecord ? (await _shortTermCacheManager.GetAsync(async () => await _sbwtRepository.GetAllAsync(q => q), FixedByWeightByTotalDefaults.ShippingByWeightByTotalCacheKey, null, null, null, null, null, null)).AsQueryable() : query; //filter by shipping method data = shippingMethodId <= 0 ? data : data = data.Where(sbw => sbw.ShippingMethodId == shippingMethodId); //filter by store data = storeId == 0 ? data : data.Where(r => r.StoreId == storeId || r.StoreId == 0); //filter by warehouse data = warehouseId == 0 ? data : data.Where(r => r.WarehouseId == warehouseId || r.WarehouseId == 0); //filter by country data = countryId == 0 ? data : data.Where(r => r.CountryId == countryId || r.CountryId == 0); //filter by state/province data = stateProvinceId == 0 ? data : data.Where(r => r.StateProvinceId == stateProvinceId || r.StateProvinceId == 0); zip = zip?.Trim() ?? string.Empty; //filter by zip data = string.IsNullOrEmpty(zip) ? data : data.Where(r => string.IsNullOrEmpty(r.Zip) || r.Zip.Equals(zip)); data = data.OrderBy(sbw => sbw.StoreId) .ThenBy(sbw => sbw.CountryId) .ThenBy(sbw => sbw.StateProvinceId) .ThenBy(sbw => sbw.Zip) .ThenBy(sbw => sbw.ShippingMethodId) .ThenBy(sbw => sbw.WeightFrom) .ThenBy(sbw => sbw.OrderSubtotalFrom); return data; }), FixedByWeightByTotalDefaults.ShippingByWeightByTotalCacheKey, shippingMethodId, storeId, warehouseId, countryId, stateProvinceId, zip); return rez; } #endregion #region Methods /// /// Filter Shipping Weight Records /// /// Shipping method identifier /// Store identifier /// Warehouse identifier /// Country identifier /// State identifier /// Zip postal code /// Weight /// Order subtotal /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the list of the shipping by weight record /// public virtual async Task> FindRecordsAsync(int shippingMethodId, int storeId, int warehouseId, int countryId, int stateProvinceId, string zip, decimal? weight, decimal? orderSubtotal, int pageIndex, int pageSize) { //filter by weight var existingRates = (await GetRecordsAsync(shippingMethodId, storeId, warehouseId, countryId, stateProvinceId, zip)) .Where(sbw => !weight.HasValue || weight >= sbw.WeightFrom && weight <= sbw.WeightTo); //filter by order subtotal existingRates = !orderSubtotal.HasValue ? existingRates : existingRates.Where(sbw => orderSubtotal >= sbw.OrderSubtotalFrom && orderSubtotal <= sbw.OrderSubtotalTo); //sort from particular to general, more particular cases will be the first existingRates = existingRates .OrderBy(r => r.StoreId == 0) .ThenBy(r => r.WarehouseId == 0) .ThenBy(r => r.CountryId == 0) .ThenBy(r => r.StateProvinceId == 0) .ThenBy(r => string.IsNullOrEmpty(r.Zip)); var records = new PagedList(existingRates.ToList(), pageIndex, pageSize); return records; } /// /// Get a shipping by weight record by passed parameters /// /// Shipping method identifier /// Store identifier /// Warehouse identifier /// Country identifier /// State identifier /// Zip postal code /// Weight /// Order subtotal /// /// A task that represents the asynchronous operation /// The task result contains the shipping by weight record /// public virtual async Task FindRecordsAsync(int shippingMethodId, int storeId, int warehouseId, int countryId, int stateProvinceId, string zip, decimal weight, decimal orderSubtotal) { var foundRecords = await FindRecordsAsync(shippingMethodId, storeId, warehouseId, countryId, stateProvinceId, zip, weight, orderSubtotal, 0, int.MaxValue); return foundRecords.FirstOrDefault(); } /// /// Get a shipping by weight record by identifier /// /// Record identifier /// /// A task that represents the asynchronous operation /// The task result contains the shipping by weight record /// public virtual async Task GetByIdAsync(int shippingByWeightRecordId) { return await _sbwtRepository.GetByIdAsync(shippingByWeightRecordId); } /// /// Insert the shipping by weight record /// /// Shipping by weight record /// A task that represents the asynchronous operation public virtual async Task InsertShippingByWeightRecordAsync(ShippingByWeightByTotalRecord shippingByWeightRecord) { await _sbwtRepository.InsertAsync(shippingByWeightRecord, false); await _staticCacheManager.RemoveByPrefixAsync(FixedByWeightByTotalDefaults.ShippingByWeightByTotalCachePrefix); } /// /// Update the shipping by weight record /// /// Shipping by weight record /// A task that represents the asynchronous operation public virtual async Task UpdateShippingByWeightRecordAsync(ShippingByWeightByTotalRecord shippingByWeightRecord) { await _sbwtRepository.UpdateAsync(shippingByWeightRecord, false); await _staticCacheManager.RemoveByPrefixAsync(FixedByWeightByTotalDefaults.ShippingByWeightByTotalCachePrefix); } /// /// Delete the shipping by weight record /// /// Shipping by weight record /// A task that represents the asynchronous operation public virtual async Task DeleteShippingByWeightRecordAsync(ShippingByWeightByTotalRecord shippingByWeightRecord) { await _sbwtRepository.DeleteAsync(shippingByWeightRecord, false); await _staticCacheManager.RemoveByPrefixAsync(FixedByWeightByTotalDefaults.ShippingByWeightByTotalCachePrefix); } #endregion }