using Abp.Application.Services.Dto; using Abp.Dependency; using Abp.Domain.Repositories; using Microsoft.EntityFrameworkCore; using VberZero.BaseSystem; namespace VberZero.DomainService.AutoCompletes; public class AutoCompleteQueryManager : VzDomainServiceBase, IAutoCompleteQueryManager, ISingletonDependency { public AutoCompleteQueryManager(IRepository repository) { Repository = repository; } private IRepository Repository { get; } /// /// 添加/修改新的查询 /// /// /// /// /// public async Task InsertOrUpdate(string codeKey, string value, string name) { await InsertOrUpdate(codeKey, value, name, null); } /// /// 添加/修改新的查询 /// /// /// /// /// 给记录添加扩展值 /// public async Task InsertOrUpdate(string codeKey, string value, string name, Func fun) { var entity = await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.ValueKey == value); var date = DateTime.Now; if (entity == null) { entity = new SysAutoCompleteQuery() { CodeKey = codeKey, ValueKey = value, NameKey = name, UseCount = 1, LastUseDate = date }; fun?.Invoke(entity); await Repository.InsertAsync(entity); } else { entity.UseCount++; entity.LastUseDate = date; fun?.Invoke(entity); await Repository.UpdateAsync(entity); } } /// /// 查询指定记录 /// /// /// /// public async Task QueryItem(string codeKey, string value) { var item = await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.ValueKey == value); return item; } /// /// 查询记录列表 /// /// /// /// public async Task> QueryItems(string codeKey, string name) { var list = await Repository.GetAll().Where(a => a.CodeKey == codeKey && (string.IsNullOrEmpty(name) || a.NameKey.Contains(name))).OrderByDescending(a => a.UseCount).ThenByDescending(a => a.LastUseDate).ToListAsync(); return list; } /// /// 查询记录列表分页 /// /// /// /// /// /// public async Task> PageItems(string codeKey, string keywords, int skip, int take) { take = take == 0 ? 20 : take; var query = Repository.GetAll().Where(a => a.CodeKey == codeKey && (string.IsNullOrEmpty(keywords) || a.NameKey.Contains(keywords))); var total = await query.CountAsync(); var list = await query.OrderByDescending(a => a.UseCount).ThenByDescending(a => a.LastUseDate).Skip(skip).Take(take).ToListAsync(); return new PagedResultDto(total, list); } }