using InterfaceForward.Domain.Shared.Enum; using InterfaceForward.Application.Contracts.Dtos.Interface; using InterfaceForward.Domain.Shared; using InterfaceForward.Repositories.Interface.Entitys; using InterfaceForward.Repositories.Interface.ValueObjects; using InterfaceForward.Repositories.Parameter.VO; namespace InterfaceForward.Repositories.Interface.Services; /// /// 接口仓储实现 /// public class InterfaceRepository : BasicRepository, IInterfaceRepository, IScopedDependency { public string InterfaceGroup => "InterfaceGroup"; /// public async Task> GetInterfaceListAsync(InterfaceTreeListInputObjectValue input) { // t_interface驱动t_interface_map,数据量上来可以在t_interface_map UpStreamId字段建立索引 var query = AsQueryable() .Where(a => a.IsUpStream == input.IsUpstream) .Where(a => a.ServiceProviderId == input.ServiceProviderId); query .WhereIF(!string.IsNullOrEmpty(input.Name), a => a.Name.Contains(input.Name!)) // 默认为空,支持模糊搜索 .WhereIF(!string.IsNullOrEmpty(input.Code), a => a.Code.Contains(input.Code!)) // 默认为空,支持模糊搜索 ; if (input.IsUpstream) { return await query .OrderBy(a => a.CreateTime) .Select(a => new InterfaceTreeListOutputValueObject { Id = a.Id.SelectAll(), MapCount = SqlFunc.Subqueryable().Where(x => x.UpStreamId == a.Id && !x.IsDeleted) .DistinctCount(x => x.ServiceProviderId), }) .ToListAsync(); } return await query .OrderBy(a => a.CreateTime) .Select() .ToListAsync(); } /// public async Task> GetParameterListByIdAsync(int id, bool onlyLeafs = false) { var query = Context.Queryable() .Where(x => x.InterfaceId == id) .WhereIF(onlyLeafs, x => x.Type != ParameterType.Object && x.Type != ParameterType.Array) .OrderBy(x => x.Sort); var res = await query.Select().ToListAsync(); res.ForEach(x => { if (x.PId == 0 && x.Type == (int)ParameterType.Array && x.Name == string.Empty) x.Name = GlobalConst.RootParameterName; }); return res; } /// public async Task GetMaxCodeSequenceBySpId(int spId) { var codes = await AsQueryable() .Where(x => x.ServiceProviderId == spId && !x.IsUpStream) .Filter(null, true) .Select(x => x.Code) .ToListAsync(); var max = 0; foreach (var code in codes) { if (string.IsNullOrEmpty(code) || code.Length < 3) continue; if (int.TryParse(code.AsSpan(code.Length - 3, 3), out var sequence) && sequence > max) max = sequence; } return max; } /// public async Task> GetInterfacePaginatedListAsync(InterfacePaginatedInputObjectValue input) { // t_interface驱动t_interface_map,数据量上来可以在t_interface_map UpStreamId字段建立索引 var query = AsQueryable() .Where(a => a.IsUpStream == input.IsUpstream) .Where(a => a.ServiceProviderId == input.ServiceProviderId); var keyword = input.NameOrCode?.Trim(); query.WhereIF(input.Category != 0, a => a.Category == input.Category) // 默认“请选择”对应0,数据源在数据库字典中配置 .WhereIF(!string.IsNullOrEmpty(keyword), a => a.Name.Contains(keyword) || a.Code.Contains(keyword)) // 默认为空,支持模糊搜索 ; if (input.IsUpstream) { var res = await query.Select(a => new InterfacePaginatedOutputValueObject { Id = a.Id.SelectAll(), MapCount = SqlFunc.Subqueryable().Where(x => x.UpStreamId == a.Id && !x.IsDeleted) .DistinctCount(x => x.ServiceProviderId), }) .OrderByDescending(a => a.UpdateTime) .ToPagedListAsync(input.PageIndex, input.PageSize); return res; } return await query.Select() .OrderByDescending(a => a.UpdateTime) .ToPagedListAsync(input.PageIndex, input.PageSize); } }