fix CanteenSettingItemEnumType参数校验
This commit is contained in:
@@ -2,11 +2,15 @@
|
||||
using Dlhis.Canteen.Setting.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Canteen.Enums;
|
||||
using Kurisu.AspNetCore.UnifyResultAndValidation;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Dlhis.Canteen.Setting.Services.Implements;
|
||||
|
||||
@@ -46,6 +50,7 @@ public class CanteenSettingService : ICanteenSettingService
|
||||
|
||||
public async Task SaveSettingAsync(CanteenSettingSaveRequest request)
|
||||
{
|
||||
Inspection(request);//校验参数
|
||||
await CheckDuplicateSettingAsync(request);
|
||||
|
||||
if (!request.Id.HasValue)
|
||||
@@ -62,6 +67,48 @@ public class CanteenSettingService : ICanteenSettingService
|
||||
}
|
||||
}
|
||||
|
||||
private void Inspection(CanteenSettingSaveRequest request)
|
||||
{
|
||||
switch (request.Item)
|
||||
{
|
||||
case CanteenSettingItemEnumType.BusinessSwitchTime:
|
||||
ValidateBusinessSwitchTime(request);
|
||||
break;
|
||||
|
||||
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))
|
||||
{
|
||||
throw new UserFriendlyException("BusinessSwitchTime 参数无效。它应该是 HH:00:00 格式");
|
||||
}
|
||||
}
|
||||
// 用于验证时间格式
|
||||
private bool IsValidTimeFormat(string time)
|
||||
{
|
||||
return DateTime.TryParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
|
||||
}
|
||||
|
||||
private void ValidateDecimal(CanteenSettingSaveRequest request)
|
||||
{
|
||||
// 对 InitBalance 进行 decimal 转换验证
|
||||
if (!decimal.TryParse(request.Value, out _))
|
||||
{
|
||||
throw new UserFriendlyException("InitBalance 参数无效。它应该是 decimal 类型");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task SetEnableAsync(CanteenSettingSetEnableRequest request)
|
||||
{
|
||||
var entity = await _context.Queryable<CanteenSettingEntity>().FirstAsync(x => x.Id == request.Id);
|
||||
|
||||
Reference in New Issue
Block a user