using FlexJobApi.Core;
using Furion.DatabaseAccessor;
using Furion.FriendlyException;
using Mapster;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlexJobApi.CommonServer.Application
{
///
/// 数据字典查询处理器
///
public class DictionaryDatasQueryHandler(
IRepository rep
) : IRequestHandler>,
IRequestHandler>>,
IRequestHandler>
{
private readonly IRepository rep = rep;
///
/// 获取数据字典分页列表数据
///
///
///
///
public Task> Handle(GetDictionaryDatasQuery request, CancellationToken cancellationToken)
{
return request.PageModel.GetPagedListAsync(
q =>
{
q = q.OrderBy(it => it.Sort).ThenBy(it => it.CreatedTime);
if (request.CategoryId.HasValue)
{
q = q.Where(it => it.CategoryId == request.CategoryId);
}
else if (request.CategoryCode.IsNotNull())
{
q = q.Where(it => it.Category.Code == request.CategoryCode);
}
else
{
throw Oops.Oh(EnumErrorCodeType.s400, "请填写类别Id或编号");
}
if (request.ParentId.HasValue)
{
q = q.Where(it => it.ParentId == request.ParentId);
}
if (request.Keywords.IsNotNull())
{
q = q.Where(it =>
it.Code.Contains(request.Keywords)
|| it.Content.Contains(request.Keywords)
|| it.Field1.Contains(request.Keywords)
|| it.Field2.Contains(request.Keywords)
|| it.Field3.Contains(request.Keywords)
|| it.Field4.Contains(request.Keywords)
|| it.Field5.Contains(request.Keywords));
}
return q;
}, cancellationToken: cancellationToken);
}
///
/// 查询数据字典选择器
///
///
///
///
public async Task>> Handle(GetDictionaryDataSelectQuery request, CancellationToken cancellationToken)
{
var models = await request.GetSelect(
it => it.Code,
it => it.Content,
q =>
{
q = q
.OrderBy(it => it.Sort)
.Where(it => !it.IsDisabled);
if (request.All != true)
{
q = q.Where(it => it.ParentId == request.ParentId);
}
if (request.MaxDeep.HasValue)
{
q = q.Where(it => it.Deep <= request.MaxDeep);
}
if (request.Keywords.IsNotNull())
{
q = q.Where(it =>
it.Code.Contains(request.Keywords)
|| it.Content.Contains(request.Keywords)
|| it.Field1.Contains(request.Keywords)
|| it.Field2.Contains(request.Keywords)
|| it.Field3.Contains(request.Keywords)
|| it.Field4.Contains(request.Keywords)
|| it.Field5.Contains(request.Keywords));
}
if (request.CategoryId.HasValue)
{
q = q.Where(it => it.CategoryId == request.CategoryId);
}
else if (request.CategoryCode.IsNotNull())
{
q = q.Where(it => it.Category.Code == request.CategoryCode);
}
else
{
throw Oops.Oh(EnumErrorCodeType.s400, "请填写类别Id或编号");
}
return q;
},
cancellationToken);
if (request.WithChildren)
{
var parents = models.Where(it => it.Data.ParentId == null).ToList();
LoopChildrens(parents, models);
return parents;
}
return models;
}
///
/// 查询地区选择器
///
///
///
///
public async Task> Handle(GetAreaSelectQuery request, CancellationToken cancellationToken)
{
var models = await Handle(new GetDictionaryDataSelectQuery
{
CategoryCode = "70",
All = true,
MaxDeep = request.MaxDeep,
WithChildren = true
}, cancellationToken);
var result = models.Adapt>();
return result;
}
///
/// 递归赋值下级
///
///
///
private void LoopChildrens(List> models, List> all)
{
foreach (var item in models)
{
item.Data.Children = all.Where(it => it.Data.ParentId == item.Data.Id).ToList();
if (item.Data.Children.IsNotNull())
{
LoopChildrens(item.Data.Children, all);
}
}
}
}
}