#nullable enable using FruitBank.Common.Interfaces; using Nop.Core; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Common; using Nop.Services.Common; namespace Nop.Plugin.Misc.FruitBankPlugin.Services; public class FruitBankAttributeService(IGenericAttributeService genericAttributeService, IStoreContext storeContext) { public async Task GetGenericAttributeAsync(int entityId, string key, int storeId) { return (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)).SingleOrDefault(ga => ga.StoreId == storeId && ga.Key == key); } public async Task> GetMeasuringAttributesAsync(int entityId, int storeId) { var measuringAttributes = (await genericAttributeService.GetAttributesForEntityAsync(entityId, typeof(TEntity).Name)) .Where(ga => ga.StoreId == storeId && (ga.Key is nameof(MeasuringAttributeValues.NetWeight) or nameof(MeasuringAttributeValues.GrossWeight) or nameof(MeasuringAttributeValues.IsMeasurable))) .ToList(); return measuringAttributes; } public Task GetMeasuringAttributeValuesAsync(int entityId) => GetMeasuringAttributeValuesAsync(entityId, storeContext.GetCurrentStore().Id); public async Task GetMeasuringAttributeValuesAsync(int entityId, int storeId) { var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); if (measuringAttributes.Count == 0) return null; var measuringAttributeValues = new MeasuringAttributeValues( CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.NetWeight))), CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.GrossWeight))), CommonHelper.To(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.IsMeasurable)))); return measuringAttributeValues; } public Task IsMeasurableEntityAsync(int entityId) => IsMeasurableEntityAsync(entityId, storeContext.GetCurrentStore().Id); public async Task IsMeasurableEntityAsync(int entityId, int storeId) { var measurableAttribute = await GetGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.IsMeasurable), storeId); return measurableAttribute != null && CommonHelper.To(measurableAttribute.Value); } public Task InsertOrUpdateMeasuringAttributeValuesAsync(int entityId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) => InsertOrUpdateMeasuringAttributeValuesAsync(entityId, storeContext.GetCurrentStore().Id, measuringAttributeValues, cumulativeWeightUpdate); public async Task InsertOrUpdateMeasuringAttributeValuesAsync(int entityId, int storeId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) { if (!measuringAttributeValues.HasValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasValues() == false; entityId: {entityId}; values: {measuringAttributeValues}"); var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); var netWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.NetWeight)); if (netWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(netWeightAttribute, measuringAttributeValues.NetWeight!.Value, cumulativeWeightUpdate); else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.NetWeight), measuringAttributeValues.NetWeight!.Value, storeId); var grossWeightAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.GrossWeight)); if (grossWeightAttribute != null) await UpdateMeasuringWeightAttributeValueAsync(grossWeightAttribute, measuringAttributeValues.GrossWeight!.Value, cumulativeWeightUpdate); else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.GrossWeight), measuringAttributeValues.GrossWeight!.Value, storeId); var isMeasurableAttribute = measuringAttributes.FirstOrDefault(ma => ma.Key == nameof(MeasuringAttributeValues.IsMeasurable)); if (isMeasurableAttribute != null) await UpdateGenericAttributeAsync(isMeasurableAttribute, measuringAttributeValues.IsMeasurable!.Value); else await InsertGenericAttributeAsync(entityId, nameof(MeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId); } private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate) { await UpdateGenericAttributeAsync(genericAttribute, cumulativeWeightUpdate ? CommonHelper.To(genericAttribute.Value) + newWeightValue : newWeightValue); } public async Task DeleteAllMeasuringAttributesAsync(int entityId, int storeId) { var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); if (measuringAttributes.Count == 0) return; foreach (var measuringAttribute in measuringAttributes) { await genericAttributeService.DeleteAttributeAsync(measuringAttribute); } } public async Task InsertGenericAttributeAsync(int entityId, string key, TPropType value, int storeId) { var genericAttribute = new GenericAttribute { Key = key, EntityId = entityId, KeyGroup = typeof(TEntity).Name, Value = CommonHelper.To(value), StoreId = storeId }; await genericAttributeService.InsertAttributeAsync(genericAttribute); } public async Task UpdateGenericAttributeAsync(int entityId, string key, TPropType newValue, int storeId) { var ga = await GetGenericAttributeAsync(entityId, key, storeId); await UpdateGenericAttributeAsync(ga!, newValue); } public async Task UpdateGenericAttributeAsync(GenericAttribute genericAttribute, TPropType newValue) { genericAttribute.Value = CommonHelper.To(newValue); await genericAttributeService.UpdateAttributeAsync(genericAttribute); } public async Task DeleteGenericAttributeAsync(int entityId, string key, int storeId) { var ga = await GetGenericAttributeAsync(entityId, key, storeId); if (ga == null) return; await genericAttributeService.DeleteAttributeAsync(ga); } }