254 lines
8.9 KiB
C#
254 lines
8.9 KiB
C#
using Dlhis.Canteen.Setting.Request;
|
|
using Dlhis.Canteen.Setting.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Canteen.Setting.Services.Implements;
|
|
|
|
/// <summary>
|
|
/// 餐饮配置
|
|
/// </summary>
|
|
public class CanteenSettingService : ICanteenSettingService
|
|
{
|
|
private readonly IDbContext _context;
|
|
|
|
public CanteenSettingService(IDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<Pagination<CanteenSettingResponse>> GetPageAsync(CanteenSettingPageRequest request)
|
|
{
|
|
var page = await _context.Queryable<CanteenSettingEntity>()
|
|
.WhereIF(request.UniqueType.HasValue, setting => setting.UniqueType == request.UniqueType)
|
|
.WhereIF(request.Item.HasValue, setting => setting.Item == request.Item)
|
|
.WhereIF(request.Search.IsPresent(), setting => setting.Catalog.Contains(request.Search))
|
|
.Select((setting) => new CanteenSettingResponse
|
|
{
|
|
Id = setting.Id,
|
|
UniqueType = setting.UniqueType,
|
|
UniqueValue = setting.UniqueValue,
|
|
Catalog = setting.Catalog,
|
|
Enable = setting.Enable,
|
|
Item = setting.Item,
|
|
Value = setting.Value
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
var userNos = page.Data.Where(x => x.UniqueType == CanteenSettingUnqiueEnumType.Person).Select(x => x.UniqueValue).Distinct().ToList();
|
|
var gainfos = await _context.Queryable<GainfoEntity>().Where(x => userNos.Contains(x.No)).ToListAsync();
|
|
page.Data.ForEach(x => { x.UniqueValueDesc = gainfos.FirstOrDefault(item => item.No == x.UniqueValue)?.Name; });
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task SaveSettingAsync(CanteenSettingSaveRequest request)
|
|
{
|
|
//校验参数
|
|
Inspection(request);
|
|
await CheckDuplicateSettingAsync(request);
|
|
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<CanteenSettingEntity>();
|
|
await _context.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _context.Queryable<CanteenSettingEntity>().FirstAsync(x => x.Id == request.Id);
|
|
request.Adapt(entity);
|
|
|
|
await _context.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
|
|
public async Task SetEnableAsync(CanteenSettingSetEnableRequest request)
|
|
{
|
|
var entity = await _context.Queryable<CanteenSettingEntity>().FirstAsync(x => x.Id == request.Id);
|
|
entity.ThrowIfNull("配置不存在,无法修改状态");
|
|
|
|
entity.Enable = request.Enable;
|
|
await _context.UpdateAsync(entity);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<T> GetSettingAsync<T>(CanteenSettingItemEnumType item, string factory, string userNo, T defaultValue = default)
|
|
{
|
|
var settings = await GetUserSettingAsync(userNo, factory);
|
|
|
|
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;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<DateTime> GetCostDateAsync(string factory, DateTime costDateTime)
|
|
{
|
|
var switchTime = await GetSettingAsync<TimeOnly?>(CanteenSettingItemEnumType.BusinessSwitchTime, factory, string.Empty);
|
|
|
|
var costDate = costDateTime.Date;
|
|
|
|
if (!switchTime.HasValue) return costDate;
|
|
|
|
var beginOfDay = costDateTime.Date;
|
|
|
|
var setTime = beginOfDay.Add(switchTime.Value.ToTimeSpan());
|
|
if (costDateTime < setTime)
|
|
costDate = costDateTime.Date.AddDays(-1);
|
|
|
|
return costDate;
|
|
}
|
|
|
|
//*********************************************** help methods ***************************************************//
|
|
|
|
private async Task CheckDuplicateSettingAsync(CanteenSettingSaveRequest request)
|
|
{
|
|
//Tenant只有一条数据, Person对应UniqueValue唯一 , Catalog不作为唯一判断
|
|
|
|
var exists = await _context.Queryable<CanteenSettingEntity>()
|
|
.Where(x => x.UniqueType == request.UniqueType)
|
|
.Where(x => x.Catalog == request.Catalog)
|
|
.Where(x => x.Item == request.Item)
|
|
.WhereIF(request.UniqueType != CanteenSettingUnqiueEnumType.Tenant, x => x.UniqueValue == request.UniqueValue)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfTrue("配置项重复,请检查.");
|
|
}
|
|
|
|
|
|
/// <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;
|
|
}
|
|
|
|
|
|
private static void Inspection(CanteenSettingSaveRequest request)
|
|
{
|
|
var value = request.Value;
|
|
|
|
var result = request.Item switch
|
|
{
|
|
//TimeOnly
|
|
CanteenSettingItemEnumType.BusinessSwitchTime => IsValidFormat<TimeOnly>(value, request.Item),
|
|
CanteenSettingItemEnumType.BookingDeadline => IsValidFormat<TimeOnly>(value, request.Item),
|
|
|
|
//int
|
|
CanteenSettingItemEnumType.DayEVoucherUseCountMax => IsValidFormat<int>(value),
|
|
|
|
//decimal
|
|
CanteenSettingItemEnumType.OnceCostMax => IsValidFormat<decimal>(value),
|
|
CanteenSettingItemEnumType.DayCostMax => IsValidFormat<decimal>(value),
|
|
CanteenSettingItemEnumType.DayMarketCostMax => IsValidFormat<decimal>(value),
|
|
CanteenSettingItemEnumType.Subsidies => IsValidFormat<decimal>(value),
|
|
CanteenSettingItemEnumType.InitBalance => IsValidFormat<decimal>(value),
|
|
|
|
//bool
|
|
CanteenSettingItemEnumType.AllowNoBalance => IsValidFormat<bool>(value, request.Item),
|
|
CanteenSettingItemEnumType.AllowSendAtMeal => IsValidFormat<bool>(value, request.Item),
|
|
CanteenSettingItemEnumType.AllowRepeatInOneShift => IsValidFormat<bool>(value, request.Item),
|
|
CanteenSettingItemEnumType.AllowCrossTenant => IsValidFormat<bool>(value, request.Item),
|
|
_ => throw new NotImplementedException(),
|
|
};
|
|
|
|
result.ThrowIfFalse($"{request.Item.GetDescription()},参数值为[{value}],格式错误");
|
|
}
|
|
} |