using Newtonsoft.Json; using NPOI.SS.Formula.Functions; using System.Collections.Generic; namespace MiaoYu.Api.Admin.ApplicationServices.Systems; /// /// 数据字典服务 /// public class SysDictionaryService : ApplicationService> { public SysDictionaryService(IRepository defaultRepository) : base(defaultRepository) { } /// /// 获取全部数据 /// /// public async Task> FindListAsync() { var list = await _defaultRepository.SelectNoTracking.ToListAsync(); var l = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(list)); return l; } /// /// 获取列表数据 /// /// /// public async Task> FindListAsync(SysDictionary search) { var query = (from sysDictionary in _defaultRepository.Select from sysDictionaryParent in _defaultRepository.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 SysDictionaryDto { 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, ExtendValue = w.t1.ExtendValue, ProjectCode = w.t1.ProjectCode, }) ; return await query.ToListAsync(); } /// /// 根据id数组删除 /// /// /// public async Task DeleteListAsync(List ids) { await _defaultRepository.DeleteByIdsAsync(ids); } /// /// 查询表单数据 /// /// /// public async Task> FindFormAsync(int id) { var res = new Dictionary(); var form = await _defaultRepository.FindByIdAsync(id); res[nameof(id)] = id == 0 ? "" : id; res[nameof(form)] = form.NullSafe(); return res; } /// /// 保存数据 /// /// /// public async Task SaveFormAsync(SysDictionary form) { if (await _defaultRepository.AnyAsync(w => w.Code == form.Code && w.Id != form.Id)) { MessageBox.Show("编码已存在,请勿重复插入"); } await _defaultRepository.InsertOrUpdateAsync(form); } /// /// 获取字典树 /// /// public async Task> GetDictionaryTreeAsync() { var allDictionary = await _defaultRepository.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; } /// /// 根据 Code 获取 字典集 /// /// /// public async Task> GetDictionaryByCodeAsync(string code) { if (string.IsNullOrWhiteSpace(code)) { MessageBox.Show("参数Code是空!"); } var dictionary = await _defaultRepository.FindAsync(w => w.Code == code); if (dictionary == null) return default; var dictionarys = await _defaultRepository.Select.Where(w => w.ParentId == dictionary.Id).OrderBy(it => it.Sort).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 _defaultRepository.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 _defaultRepository.SelectNoTracking.ToListAsync(); //dictionarys.ForEach(_dictionary => //{ //}); //var result = new List(); return CreateDictionaryTree(dictionary.Id, list); } }