199 lines
6.9 KiB
C#
199 lines
6.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.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;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Globalization;
|
|
|
|
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>()
|
|
.LeftJoin<CanteenUserEntity>((setting, user) => setting.UniqueValue == user.UserNo)
|
|
.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, user) => 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);
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
private void Inspection(CanteenSettingSaveRequest request)
|
|
{
|
|
var value = request.Value;
|
|
|
|
var result = request.Item switch
|
|
{
|
|
//TimeOnly
|
|
CanteenSettingItemEnumType.BusinessSwitchTime => 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.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}],格式错误");
|
|
}
|
|
|
|
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)
|
|
{
|
|
var setting = await _context.Queryable<CanteenSettingEntity>()
|
|
.Where(x => x.UniqueType == CanteenSettingUnqiueEnumType.Person && (x.UniqueValue == userNo) || x.UniqueType == CanteenSettingUnqiueEnumType.Tenant)
|
|
.ToListAsync();
|
|
|
|
//从基地 -> 个人,确保下面循环做到覆盖,并且以个人配置为主
|
|
setting = setting.OrderBy(x => x.UniqueType).ToList();
|
|
|
|
var result = new Dictionary<CanteenSettingItemEnumType, string>();
|
|
setting.ForEach(x =>
|
|
{
|
|
if (result.ContainsKey(x.Item))
|
|
{
|
|
result[x.Item] = x.Value;
|
|
}
|
|
else
|
|
{
|
|
result.Add(x.Item, x.Value);
|
|
}
|
|
});
|
|
|
|
return result;
|
|
}
|
|
|
|
//*********************************************** 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.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;
|
|
}
|
|
|
|
}
|