Mango.Nop.Plugins/Nop.Plugin.Misc.AIPlugin/Services/FruitBankAttributeService.cs

122 lines
6.9 KiB
C#

#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<GenericAttribute?> GetGenericAttributeAsync<TEntity>(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<List<GenericAttribute>> GetMeasuringAttributesAsync<TEntity>(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<MeasuringAttributeValues?> GetMeasuringAttributeValuesAsync<TEntity>(int entityId)
=> GetMeasuringAttributeValuesAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id);
public async Task<MeasuringAttributeValues?> GetMeasuringAttributeValuesAsync<TEntity>(int entityId, int storeId)
{
var measuringAttributes = await GetMeasuringAttributesAsync<TEntity>(entityId, storeId);
if (measuringAttributes.Count == 0) return null;
var measuringAttributeValues = new MeasuringAttributeValues(
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.NetWeight))),
CommonHelper.To<double?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.GrossWeight))),
CommonHelper.To<bool?>(measuringAttributes.FirstOrDefault(ga => ga.Key == nameof(MeasuringAttributeValues.IsMeasurable))));
return measuringAttributeValues;
}
public Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId) => IsMeasurableEntityAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id);
public async Task<bool> IsMeasurableEntityAsync<TEntity>(int entityId, int storeId)
{
var measurableAttribute = await GetGenericAttributeAsync<TEntity>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), storeId);
return measurableAttribute != null && CommonHelper.To<bool>(measurableAttribute.Value);
}
public Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(int entityId, MeasuringAttributeValues measuringAttributeValues, bool cumulativeWeightUpdate)
=> InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(entityId, storeContext.GetCurrentStore().Id, measuringAttributeValues, cumulativeWeightUpdate);
public async Task InsertOrUpdateMeasuringAttributeValuesAsync<TEntity>(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<TEntity>(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<TEntity, double>(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<TEntity, double>(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<TEntity, bool>(entityId, nameof(MeasuringAttributeValues.IsMeasurable), measuringAttributeValues.IsMeasurable!.Value, storeId);
}
private async Task UpdateMeasuringWeightAttributeValueAsync(GenericAttribute genericAttribute, double newWeightValue, bool cumulativeWeightUpdate)
{
await UpdateGenericAttributeAsync(genericAttribute, cumulativeWeightUpdate ? CommonHelper.To<double>(genericAttribute.Value) + newWeightValue : newWeightValue);
}
public async Task DeleteAllMeasuringAttributesAsync<TEntity>(int entityId, int storeId)
{
var measuringAttributes = await GetMeasuringAttributesAsync<TEntity>(entityId, storeId);
if (measuringAttributes.Count == 0) return;
foreach (var measuringAttribute in measuringAttributes)
{
await genericAttributeService.DeleteAttributeAsync(measuringAttribute);
}
}
public async Task InsertGenericAttributeAsync<TEntity, TPropType>(int entityId, string key, TPropType value, int storeId)
{
var genericAttribute = new GenericAttribute
{
Key = key,
EntityId = entityId,
KeyGroup = typeof(TEntity).Name,
Value = CommonHelper.To<string>(value),
StoreId = storeId
};
await genericAttributeService.InsertAttributeAsync(genericAttribute);
}
public async Task UpdateGenericAttributeAsync<TEntity, TPropType>(int entityId, string key, TPropType newValue, int storeId)
{
var ga = await GetGenericAttributeAsync<TEntity>(entityId, key, storeId);
await UpdateGenericAttributeAsync(ga!, newValue);
}
public async Task UpdateGenericAttributeAsync<TPropType>(GenericAttribute genericAttribute, TPropType newValue)
{
genericAttribute.Value = CommonHelper.To<string>(newValue);
await genericAttributeService.UpdateAttributeAsync(genericAttribute);
}
public async Task DeleteGenericAttributeAsync<TEntity>(int entityId, string key, int storeId)
{
var ga = await GetGenericAttributeAsync<TEntity>(entityId, key, storeId);
if (ga == null) return;
await genericAttributeService.DeleteAttributeAsync(ga);
}
}