using FreeSql;
namespace CloudGaming.Api.Admin.ApplicationServices.Systems;
///
/// 数据字典服务
///
public class SysDictionaryService(IServiceProvider serviceProvider)
: ApplicationService(serviceProvider)
{
///
/// 获取列表数据
///
///
///
public async Task> FindListAsync(SysDictionary search)
{
var query = (from sysDictionary in Repository.Select
from sysDictionaryParent in Repository.Select.Where(w => w.Id == sysDictionary.ParentId)
.DefaultIfEmpty()
select new
{
t1 = sysDictionary,
t2 = sysDictionaryParent
})
//.WhereIf(search?.ParentId == 0 || search?.ParentId == null, w => w.t1.ParentId == null || w.t1.ParentId == 0)
//.WhereIf(search?.ParentId != 0 && search?.ParentId != null, w => w.t1.ParentId == search.ParentId)
.WhereIf(!string.IsNullOrWhiteSpace(search?.Name), a => a.t1.Name.Contains(search.Name))
.OrderBy(w => w.t1.Sort)
.Select(w => new SysDictionaryTreeDto
{
Sort = w.t1.Sort,
Code = w.t1.Code,
Name = w.t1.Name,
Value = w.t1.Value,
ParentId = w.t1.ParentId,
ParentName = w.t2.Name,
LastModificationTime = w.t1.LastModificationTime,
CreationTime = w.t1.CreationTime,
Id = w.t1.Id,
})
;
return await query.ToListAsync();
}
///
/// 保存数据
///
///
///
public override async Task SaveFormAsync(SysDictionary form)
{
if (await Repository.AnyAsync(w => w.Code == form.Code && w.Id != form.Id))
{
throw MessageBox.Show("编码已存在,请勿重复插入");
}
await Repository.InsertOrUpdateAsync(form);
}
///
/// 获取字典树
///
///
public async Task> GetDictionaryTreeAsync()
{
var allDictionary = await Repository.ToListAllAsync();
return CreateDictionaryTree(0, allDictionary);
}
///
/// 创建字典树
///
///
private List CreateDictionaryTree(int id, List allDictionary)
{
List result = new();
var data = new List();
if (id == 0)
{
data = allDictionary
.Where(w => w.ParentId == null || w.ParentId == 0)
.ToList()
;
}
else
{
data = allDictionary
.Where(w => w.ParentId == id)
.ToList()
;
}
foreach (var item in data)
{
var model = item.MapTo();
model.Children = CreateDictionaryTree(item.Id, allDictionary);
result.Add(model);
}
return result;
}
///
/// 所有字典集合 1小时缓存
///
///
//[Cacheable(CacheDuration = 60 * 60)]
public async Task> GetDictionaryAllAsync()
{
return await Repository.SelectNoTracking
.Select(w => new SysDictionaryDto
{
Key = w.Id,
Code = w.Code,
Name = w.Name,
Value = w.Value,
ParentId = w.ParentId,
Sort=w.Sort
})
.ToListAsync();
}
///
/// 根据编码获取下级字典
///
///
///
public async Task> GetDictionaryByCodeAsync(string code)
{
if (string.IsNullOrWhiteSpace(code))
{
throw MessageBox.Show("参数Code是空!");
}
var data = await GetDictionaryAllAsync();
var parentData = data.FirstOrDefault(w => w.Code == code) ?? throw MessageBox.Show($"编码“{code}”对应找不到字典数据!");
return data.Where(w => w.ParentId == parentData.Key).OrderBy(it=>it.Sort).ToList();
}
///
/// 根据 Code 获取 字典集
///
///
///
public async Task> GetDictionaryTreeByCodeAsync(string code)
{
if (string.IsNullOrWhiteSpace(code))
{
throw MessageBox.Show("参数Code是空!");
}
var dictionary = await Repository.FindAsync(w => w.Code == code);
if (dictionary == null) return default;
var dictionarys = await Repository.Select.Where(w => w.ParentId == dictionary.Id).ToListAsync();
if (!dictionarys.Any()) return default;
var result = new List();
return CreateDictionaryTree(dictionary.Id, dictionarys);
}
///
///
///
///
///
///
public async Task> GetDictionaryByProjectCodeAsync(string tenant, string code)
{
if (string.IsNullOrWhiteSpace(tenant))
{
MessageBox.Show("参数Code是空!");
}
//拿到租户
var dictionary = await Repository.FindAsync(w => w.Code == tenant);
if (dictionary == null) return default;
//拿到租户的子标签
//var dictionarys = await _defaultRepository.Select.Where(w => w.ParentId == dictionary.Id).OrderBy(it => it.Sort).ToListAsync();
//if (dictionarys.Count == 0) return default;
var list = await Repository.SelectNoTracking.ToListAsync();
//dictionarys.ForEach(_dictionary =>
//{
//});
//var result = new List();
return CreateDictionaryTree(dictionary.Id, list);
}
}