using Infrastructure; using OpenAuth.App.BaseApp.Base; using OpenAuth.App.Interface; using OpenAuth.App.ServiceApp.Algo.Request; using OpenAuth.Repository; using OpenAuth.Repository.Domain; using SqlSugar; namespace OpenAuth.App.ServiceApp.Algo; public class AlgoInstanceServiceApp : SqlSugarBaseApp { public AlgoInstanceServiceApp(ISugarUnitOfWork unitWork, ISimpleClient repository, IAuth auth) : base(unitWork, repository, auth) { } public async Task> AddAlgoInstance(LasaAlgoInstance info) { info.Id = Guid.NewGuid().ToString(); if (await Repository.InsertAsync(info)) { return new Response { Result = true, Message = "添加成功" }; } return new Response { Result = false, Message = "添加失败" }; } public async Task> DeleteAlgoInstance(string id) { if (await Repository.DeleteByIdAsync(id)) { return new Response { Result = true, Message = "删除成功" }; } return new Response { Result = false, Message = "删除失败" }; } public async Task> UpdateAlgoInstance(LasaAlgoInstance info) { // todo 关于会更新空值问题 if (await Repository.UpdateAsync(info)) { return new Response { Result = true, Message = "修改成功" }; } return new Response { Result = false, Message = "修改失败" }; } public async Task>>> GetAlgoInstanceList(AlgoInstancePageRequest req) { RefAsync totalCount = 0; var page = await Repository.AsQueryable() .WhereIF(!string.IsNullOrEmpty(req.key), x => x.Name.Contains(req.key)) .ToPageListAsync(req.page, req.limit, totalCount); var ids = page.Select(x => x.Id).ToList(); var sql = @$"SELECT a.""Id"" as ""algoInstanceId"", l.""Name"" as ""TagName"", r.""Name"" as ""AlgoName"" FROM lasa_algoinstance a CROSS JOIN LATERAL UNNEST ( string_to_array( A.""Tags"", ',' ) ) AS TempB ( Tag ) left join lasa_modellabel l on l.""Id"" = TempB.Tag left join lasa_algorithmsrepository r on r.""Id"" = l.""PId"" WHERE a.""Id"" in ( {string.Join(",", ids.Select(id => $"'{id}'"))} ) "; var infos = await Repository.AsSugarClient().SqlQueryable(sql) .ToListAsync(); var result = page.Select(x => new { x.Id, x.Name, x.Cover, x.DisplayScheme, x.Description, x.DisplayColor, x.RecognitionX, x.RecognitionY, x.SpaceConstraint, x.ExpansionDistance, x.TemporalConstraints, x.TcStartTime, x.TcEndTime, x.FlySpeed, x.GimbalPitchDegree, x.RecognitionCoverage, x.Tags, x.AlgoIds, TagNames = infos.Where(y => y.algoInstanceId == x.Id).Select(y => y.TagName).ToArray(), AlgoNames = infos.Where(y => y.algoInstanceId == x.Id).Select(y => y.AlgoName).ToList() }); return new Response>> { Result = new PageInfo> { Items = result, Total = totalCount.Value } }; } public async Task> GetAlgoInstance(string id) { // 关于添加算法及标签信息 return new Response { Result = await Repository.GetByIdAsync(id) }; } }