| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 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<SysQuery> repository)
- {
- Repository = repository;
- }
- private IRepository<SysQuery> Repository { get; }
- /// <summary>
- /// 添加/修改新的查询
- /// </summary>
- /// <param name="tKey"></param>
- /// <param name="cKey"></param>
- /// <param name="vKey"></param>
- /// <param name="nKey"></param>
- /// <returns></returns>
- public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey)
- {
- await InsertOrUpdate(tKey, cKey, vKey, nKey, null);
- }
- /// <summary>
- /// 添加/修改新的查询
- /// </summary>
- /// <param name="tKey"></param>
- /// <param name="cKey"></param>
- /// <param name="vKey"></param>
- /// <param name="nKey"></param>
- /// <param name="fun">给记录添加扩展值</param>
- /// <returns></returns>
- public async Task InsertOrUpdate(string tKey, string cKey, string vKey, string nKey,
- Func<SysQuery, SysQuery> 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);
- }
- }
- /// <summary>
- /// 查询指定记录
- /// </summary>
- /// <param name="tKey"></param>
- /// <param name="cKey"></param>
- /// <param name="vKey"></param>
- /// <returns></returns>
- public async Task<SysQuery> QueryItem(string tKey, string cKey, string vKey)
- {
- var item = await Repository.FirstOrDefaultAsync(a =>
- a.TableKey == tKey && a.ColumnKey == cKey && a.ValueKey == vKey);
- return item;
- }
- /// <summary>
- /// 查询记录列表
- /// </summary>
- /// <param name="tKey"></param>
- /// <param name="cKey"></param>
- /// <param name="nKey"></param>
- /// <returns></returns>
- public async Task<List<SysQuery>> 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;
- }
- /// <summary>
- /// 查询记录列表分页
- /// </summary>
- /// <param name="tKey"></param>
- /// <param name="cKey"></param>
- /// <param name="nKey"></param>
- /// <param name="skip"></param>
- /// <param name="take"></param>
- /// <returns></returns>
- public async Task<PagedResultDto<SysQuery>> 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<SysQuery>(total, list);
- }
- }
- }
|