97 lines
3.1 KiB
C#
97 lines
3.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.Utils.Extensions;
|
|
using Kurisu.Core.User.Abstractions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Canteen.Order.Services;
|
|
|
|
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 ILogger Logger => _logger;
|
|
|
|
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>
|
|
/// 获取参数配置
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="tenantId"></param>
|
|
/// <param name="item"></param>
|
|
/// <param name="defaultValue"></param>
|
|
/// <returns></returns>
|
|
protected async Task<T> GetSettingAsync<T>(string tenantId, 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, tenantId);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|