fix 就餐参数配置检验调整
This commit is contained in:
@@ -50,7 +50,8 @@ public class CanteenSettingService : ICanteenSettingService
|
||||
|
||||
public async Task SaveSettingAsync(CanteenSettingSaveRequest request)
|
||||
{
|
||||
Inspection(request);//校验参数
|
||||
//校验参数
|
||||
Inspection(request);
|
||||
await CheckDuplicateSettingAsync(request);
|
||||
|
||||
if (!request.Id.HasValue)
|
||||
@@ -69,42 +70,31 @@ public class CanteenSettingService : ICanteenSettingService
|
||||
|
||||
private void Inspection(CanteenSettingSaveRequest request)
|
||||
{
|
||||
switch (request.Item)
|
||||
{
|
||||
case CanteenSettingItemEnumType.BusinessSwitchTime:
|
||||
ValidateBusinessSwitchTime(request);
|
||||
break;
|
||||
var value = request.Value;
|
||||
|
||||
case CanteenSettingItemEnumType.InitBalance:
|
||||
case CanteenSettingItemEnumType.DayCostMax:
|
||||
case CanteenSettingItemEnumType.DayMarketCostMax:
|
||||
case CanteenSettingItemEnumType.OnceCostMax:
|
||||
ValidateDecimal(request);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateBusinessSwitchTime(CanteenSettingSaveRequest request)
|
||||
{
|
||||
// 对 BusinessSwitchTime 进行时间格式验证
|
||||
if (string.IsNullOrEmpty(request.Value) || !IsValidTimeFormat(request.Value))
|
||||
var result = request.Item switch
|
||||
{
|
||||
throw new UserFriendlyException("BusinessSwitchTime 参数无效。它应该是 HH:00:00 格式");
|
||||
}
|
||||
}
|
||||
// 用于验证时间格式
|
||||
private bool IsValidTimeFormat(string time)
|
||||
{
|
||||
return DateTime.TryParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
|
||||
}
|
||||
//TimeOnly
|
||||
CanteenSettingItemEnumType.BusinessSwitchTime => IsValidFormat<TimeOnly>(value),
|
||||
|
||||
private void ValidateDecimal(CanteenSettingSaveRequest request)
|
||||
{
|
||||
// 对 InitBalance 进行 decimal 转换验证
|
||||
if (!decimal.TryParse(request.Value, out _))
|
||||
{
|
||||
throw new UserFriendlyException($"{request.Item} 参数无效。它应该是 decimal 类型");
|
||||
}
|
||||
//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.AllowSendAtMeal => IsValidFormat<bool>(value),
|
||||
CanteenSettingItemEnumType.AllowRepeatInOneShift => IsValidFormat<bool>(value),
|
||||
CanteenSettingItemEnumType.AllowCrossTenant => IsValidFormat<bool>(value),
|
||||
_ => throw new NotImplementedException(),
|
||||
};
|
||||
|
||||
result.ThrowIfFalse($"{request.Item.GetDescription()},参数值为[{value}],格式错误");
|
||||
}
|
||||
|
||||
public async Task SetEnableAsync(CanteenSettingSetEnableRequest request)
|
||||
@@ -158,4 +148,28 @@ public class CanteenSettingService : ICanteenSettingService
|
||||
|
||||
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
|
||||
{
|
||||
//mapster
|
||||
value.Adapt<T>();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user