using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Threading.Tasks; using Abp.Application.Services.Dto; using Abp.Dependency; using Abp.Domain.Repositories; using ContractService.BaseInfo; namespace ContractService.CommonManager.Query { public class AcQueryManager : IAcQueryManager, ISingletonDependency { public AcQueryManager(IRepository repository) { Repository = repository; } private IRepository Repository { get; } /// /// 添加/修改新的查询 /// /// /// /// /// /// public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey) { await InsertOrUpdate(tKey, cKey, vKey, nKey, null); } /// /// 添加/修改新的查询 /// /// /// /// /// /// 给记录添加扩展值 /// public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey, Func fun) { var entity = await Repository.FirstOrDefaultAsync(a => a.TableKey == tKey && a.ColumnKey == cKey && a.ValueKey == vKey); var date = DateTime.Now; if (entity == null) { entity = new SysQuery() { TableKey = tKey, ColumnKey = cKey, ValueKey = vKey, NameKey = nKey, 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 tKey, string cKey, string vKey) { var item = await Repository.FirstOrDefaultAsync(a => a.TableKey == tKey && a.ColumnKey == cKey && a.ValueKey == vKey); return item; } /// /// 查询记录列表 /// /// /// /// /// public async Task> QueryItems(string tKey, string cKey, string nKey) { var list = await Repository.GetAll().Where(a => a.TableKey == tKey && a.ColumnKey == cKey && (string.IsNullOrEmpty(nKey) || a.NameKey.Contains(nKey))).OrderByDescending(a => a.UseCount).ThenByDescending(a => a.LastUseDate).ToListAsync(); return list; } /// /// 查询记录列表分页 /// /// /// /// /// /// /// public async Task> PageItems(string tKey, string cKey, string nKey, int skip, int take) { take = take == 0 ? 20 : take; var query = Repository.GetAll().Where(a => a.TableKey == tKey && a.ColumnKey == cKey && (string.IsNullOrEmpty(nKey) || a.NameKey.Contains(nKey))); 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); } } }