#nullable enable using FruitBank.Common.Interfaces; using FruitBank.Common.Models; using Nop.Core; using Nop.Core.Domain.Common; using Nop.Services.Common; namespace Nop.Plugin.Misc.FruitBankPlugin.Services; public class FruitBankAttributeService(IGenericAttributeService genericAttributeService, IStoreContext storeContext) { private const string NET_WEIGHT_KEY = nameof(IMeasuringAttributeValues.NetWeight); private const string IS_MEASURABLE_KEY = nameof(IMeasuringAttributeValues.IsMeasurable); 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 Task GetGenericAttributeValueAsync(int entityId, string key) => GetGenericAttributeValueAsync(entityId, key, storeContext.GetCurrentStore().Id); public async Task GetGenericAttributeValueAsync(int entityId, string key, int storeId) { var ga = await GetGenericAttributeAsync(entityId, key, storeId); return ga == null ? default : CommonHelper.To(ga.Value); } 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 NET_WEIGHT_KEY or IS_MEASURABLE_KEY) .ToList(); if (measuringAttributes.Count == 0) return null; if (measuringAttributes.Count != 2) throw new Exception($"FruitBankAttributeService->GetMeasuringAttributesAsync(); measuringAttributes.Count != 2; entityId: {entityId}"); 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 == null) return null; var measuringAttributeValues = new MeasuringAttributeValues( entityId, CommonHelper.To(measuringAttributes.Single(ga => ga.Key == NET_WEIGHT_KEY).Value), CommonHelper.To(measuringAttributes.Single(ga => ga.Key == IS_MEASURABLE_KEY).Value)); 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, IS_MEASURABLE_KEY, storeId); return measurableAttribute != null && CommonHelper.To(measurableAttribute.Value); } public async Task InsertOrUpdateMeasuringAttributeValuesAsync(int entityId, double netWeight, bool isMeasurable, bool cumulativeWeightUpdate) { var measuringAttributeValues = new MeasuringAttributeValues { Id = entityId, NetWeight = netWeight, IsMeasurable = isMeasurable }; await InsertOrUpdateMeasuringAttributeValuesAsync(measuringAttributeValues, cumulativeWeightUpdate); return measuringAttributeValues; } public Task InsertOrUpdateMeasuringAttributeValuesAsync(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate) => InsertOrUpdateMeasuringAttributeValuesAsync(measuringAttributeValues, cumulativeWeightUpdate, storeContext.GetCurrentStore().Id); public async Task InsertOrUpdateMeasuringAttributeValuesAsync(IMeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate, int storeId) { if (!measuringAttributeValues.HasMeasuringValues()) throw new Exception($"FruitBankAttributeService->InsertOrUpdateMeasuringAttributeValuesAsync; measuringAttributeValues.HasMeasuringValues() == false; keyGroup: {typeof(TEntity).Name}; values: {measuringAttributeValues}"); var entityId = measuringAttributeValues.Id; var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); if (measuringAttributes == null) { await InsertGenericAttributeAsync(entityId, NET_WEIGHT_KEY, double.Round(measuringAttributeValues.NetWeight, 1), storeId); await InsertGenericAttributeAsync(entityId, IS_MEASURABLE_KEY, measuringAttributeValues.IsMeasurable, storeId); return; } await UpdateMeasuringWeightAttributeValueAsync(measuringAttributes.Single(ma => ma.Key == NET_WEIGHT_KEY), measuringAttributeValues.NetWeight, cumulativeWeightUpdate); //TODO: ezzel mi legyen? - J. //await UpdateGenericAttributeAsync(measuringAttributes.Single(ma => ma.Key == IS_MEASURABLE_KEY), measuringAttributeValues.IsMeasurable); } private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate) { await UpdateGenericAttributeAsync(genericAttribute, double.Round((cumulativeWeightUpdate ? CommonHelper.To(genericAttribute.Value) + newWeightValue : newWeightValue), 1)); } public async Task DeleteAllMeasuringAttributesAsync(int entityId, int storeId) { var measuringAttributes = await GetMeasuringAttributesAsync(entityId, storeId); if (measuringAttributes == null) return; foreach (var measuringAttribute in measuringAttributes) { await genericAttributeService.DeleteAttributeAsync(measuringAttribute); } } public Task InsertOrUpdateGenericAttributeAsync(int entityId, string key, TPropType value) => InsertOrUpdateGenericAttributeAsync(entityId, key, value, storeContext.GetCurrentStore().Id); public async Task InsertOrUpdateGenericAttributeAsync(int entityId, string key, TPropType value, int storeId) { var ga = await GetGenericAttributeAsync(entityId, key, storeId); if (ga == null) await InsertGenericAttributeAsync(entityId, key, value, storeId); else await UpdateGenericAttributeAsync(ga, value); } public Task InsertGenericAttributeAsync(int entityId, string key, TPropType value) => InsertGenericAttributeAsync(entityId, key, value, storeContext.GetCurrentStore().Id); 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 Task UpdateGenericAttributeAsync(int entityId, string key, TPropType value) => UpdateGenericAttributeAsync(entityId, key, value, storeContext.GetCurrentStore().Id); 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 Task DeleteGenericAttributeAsync(int entityId, string key) => DeleteGenericAttributeAsync(entityId, key, storeContext.GetCurrentStore().Id); 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); } }