134 lines
4.1 KiB
C#
134 lines
4.1 KiB
C#
using Dlhis.Basic.Api;
|
|
using Dlhis.Canteen.Setting.Services;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.User.Abstractions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Canteen.Order.Services.Implements;
|
|
|
|
public abstract class BaseOrderService
|
|
{
|
|
private readonly ICanteenSettingService _canteenSettingService;
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IDevicePermissionApi _devicePermissionApi;
|
|
private readonly ILogger _logger;
|
|
private Dictionary<CanteenSettingItemEnumType, string> _settings;
|
|
|
|
public BaseOrderService(ICanteenSettingService canteenSettingService,
|
|
IDbContext dbContext,
|
|
ICurrentUser currentUser,
|
|
IDevicePermissionApi devicePermissionApi,
|
|
ILogger logger)
|
|
{
|
|
_canteenSettingService = canteenSettingService;
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_devicePermissionApi = devicePermissionApi;
|
|
_logger = logger;
|
|
}
|
|
|
|
protected BaseOrderService(ICanteenSettingService canteenSettingService)
|
|
{
|
|
_canteenSettingService = canteenSettingService;
|
|
}
|
|
|
|
protected IDbContext DbContext => _dbContext;
|
|
|
|
protected ICurrentUser CurrentUser => _currentUser;
|
|
|
|
/// <summary>
|
|
/// 检查权限
|
|
/// </summary>
|
|
/// <param name="userNo"></param>
|
|
/// <param name="deviceUniqueNo"></param>
|
|
/// <returns></returns>
|
|
protected async Task CheckPermissionAsync(string userNo, string deviceUniqueNo)
|
|
{
|
|
var uniqueNosResult = await _devicePermissionApi.GetCanteenDeviceUniqueNosByUserNoAsync(userNo);
|
|
var uniqueNos = uniqueNosResult.Data;
|
|
|
|
uniqueNos.Contains(deviceUniqueNo).ThrowIfFalse("无消费权限");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 设备并发更新 需要抛出VersionExceptions
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="func"></param>
|
|
/// <param name="retry">并发冲突处理次数</param>
|
|
/// <param name="errorMessage"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="UserFriendlyException"></exception>
|
|
protected async Task ContinueUpdateAsync(Func<Task> func, int retry, string errorMessage = null)
|
|
{
|
|
for (int i = 0; i < retry; i++)
|
|
{
|
|
try
|
|
{
|
|
await func();
|
|
return;
|
|
}
|
|
catch (Exception ex) when (ex is VersionExceptions)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
throw new UserFriendlyException(errorMessage ?? "操作失败,请重试");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取参数配置
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="item"></param>
|
|
/// <param name="defaultValue"></param>
|
|
/// <returns></returns>
|
|
protected async Task<T> GetSettingAsync<T>(CanteenSettingItemEnumType item, T defaultValue = default)
|
|
{
|
|
if (_settings == null)
|
|
{
|
|
var subjectId = _currentUser.GetIntSubjectId();
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.IdentityUserId == subjectId).FirstAsync();
|
|
var jinkoCode = _currentUser.GetUserClaim("jinko-code") ?? string.Empty;
|
|
if (jinkoCode.IsPresent())
|
|
{
|
|
gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == jinkoCode).FirstAsync();
|
|
}
|
|
_settings = await _canteenSettingService.GetUserSettingAsync(gainfo.No);
|
|
}
|
|
|
|
|
|
if (_settings.TryGetValue(item, out var v))
|
|
{
|
|
try
|
|
{
|
|
if (int.TryParse(v, out var n))
|
|
{
|
|
return n.Adapt<T>();
|
|
}
|
|
|
|
return v.Adapt<T>();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
return defaultValue;
|
|
}
|
|
}
|