117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
using Dlhis.Canteen.Setting.Request;
|
|
using Dlhis.Canteen.Setting.Response;
|
|
using Dlhis.Entity.Basic;
|
|
using Dlhis.Entity.Canteen.Enums;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.Core.DataAccess.Dto;
|
|
using Kurisu.SqlSugar.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
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>()
|
|
.LeftJoin<CanteenUserEntity>((setting, user) => setting.UniqueValue == user.UserId)
|
|
.WhereIF(request.UniqueType.HasValue, setting => setting.UniqueType == request.UniqueType)
|
|
.WhereIF(request.Item.HasValue, setting => setting.Item == request.Item)
|
|
.WhereIF(request.Search.IsPresent(), setting => setting.UniqueValue.Contains(request.Search))
|
|
.Select((setting, user) => new CanteenSettingResponse
|
|
{
|
|
UniqueType = setting.UniqueType,
|
|
UniqueValue = setting.UniqueValue,
|
|
//UniqueValueDesc = user.UserName,
|
|
Catalog = setting.Catalog,
|
|
Enable = setting.Enable,
|
|
Item = setting.Item,
|
|
Value = setting.Value
|
|
})
|
|
.ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task SaveSettingAsync(CanteenSettingSaveRequest 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 userId)
|
|
{
|
|
var setting = await _context.Queryable<CanteenSettingEntity>()
|
|
.Where(x => x.UniqueType == CanteenSettingUnqiueEnumType.Person && (x.UniqueValue == userId) || 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("配置项重复,请检查.");
|
|
}
|
|
}
|