Files
jinko.yikatong/Src/Dlhis.Middle/Services/Implements/CanteenSettingService.cs
2024-06-26 12:14:39 +08:00

104 lines
3.1 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Canteen.Enums;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Mapster;
namespace Dlhis.Middle.Services.Implements;
/// <summary>
/// 餐饮配置
/// </summary>
public class CanteenSettingService : ICanteenSettingService
{
private readonly IDbContext _context;
public CanteenSettingService(IDbContext context)
{
_context = context;
}
public async Task<Dictionary<CanteenSettingItemEnumType, string>> GetUserSettingAsync(string userNo, string tenantId)
{
var settings = await _context.Queryable<CanteenSettingEntity>()
.Where(x => x.Enable)
.Where(x => x.TenantId == tenantId)
.Where(x => x.UniqueType == CanteenSettingUnqiueEnumType.Person && x.UniqueValue == userNo || x.UniqueType == CanteenSettingUnqiueEnumType.Tenant)
.ToListAsync();
//从基地 -> 个人,确保下面循环做到覆盖,并且以个人配置为主
settings = settings.OrderBy(x => x.UniqueType).ToList();
var result = new Dictionary<CanteenSettingItemEnumType, string>();
settings.ForEach(x => { result[x.Item] = x.Value; });
return result;
}
public async Task<TimeOnly?> GetBusinessSwitchTimeAsync(string tenantId)
{
var setting = await _context.Queryable<CanteenSettingEntity>()
.Where(x => x.Enable)
.Where(x => x.TenantId == tenantId)
.Where(x => x.Item == CanteenSettingItemEnumType.BusinessSwitchTime)
.FirstAsync();
try
{
return TimeOnly.Parse(setting.Value);
}
catch (Exception)
{
return null;
}
}
//*********************************************** help methods ***************************************************//
/// <summary>
/// 检验参数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="type"></param>
/// <returns></returns>
private static bool IsValidFormat<T>(string value, CanteenSettingItemEnumType? type = null)
{
try
{
//判断0/1
if (type == CanteenSettingItemEnumType.AllowSendAtMeal ||
type == CanteenSettingItemEnumType.AllowRepeatInOneShift ||
type == CanteenSettingItemEnumType.AllowNoBalance ||
type == CanteenSettingItemEnumType.AllowCrossTenant)
{
var intvaule = int.Parse(value);
if (intvaule == 0 || intvaule == 1)
{
return true;
}
else
{
return false;
}
}
if (type == CanteenSettingItemEnumType.BusinessSwitchTime)
{
var timeOnly = value.Adapt<TimeOnly>();
return timeOnly.Minute == 0 && timeOnly.Second == 0;
}
//mapster
value.Adapt<T>();
}
catch (Exception)
{
return false;
}
return true;
}
}