using FreeSql; using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.SYS.WuziMulus; using F9WebAiTest.Application.Dtos.SYS.YuanliaoZhuangkous; using F9WebAiTest.Application.Interfaces.Repositories.SYS.YuanliaoZhuangkous; using F9WebAiTest.Domain.Entities.SYS.WuziMulus; using F9WebAiTest.Domain.Enums.SYS.WuziMulus; using F9WebAiTest.Domain.Entities.SYS.YuanliaoZhuangkous; namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.YuanliaoZhuangkous; internal class YuanliaoZhuangkouReadRepository : F9RepositoryBase, IYuanliaoZhuangkouReadRepository { public YuanliaoZhuangkouReadRepository(IFreeSql fsql) : base(fsql) { } public async Task> GetPagedAsync(int year, string chandi, string zhandian, string jibie, string keyword, int pageIndex, int pageSize) { if (pageIndex <= 0) pageIndex = 1; if (pageSize <= 0) pageSize = 20; var query = Orm.Select().Where(x => x.DeletedAt == null); if (year > 0) query = query.Where(x => x.Year == year); if (!string.IsNullOrWhiteSpace(chandi)) query = query.Where(x => x.Chandi == chandi.Trim()); if (!string.IsNullOrWhiteSpace(zhandian)) query = query.Where(x => x.Zhandian == zhandian.Trim()); if (!string.IsNullOrWhiteSpace(jibie)) query = query.Where(x => x.Jibie == jibie.Trim()); if (!string.IsNullOrWhiteSpace(keyword)) { var kw = keyword.Trim(); query = query.Where(x => x.Name.Contains(kw) || x.Code.Contains(kw) || x.Chandi.Contains(kw) || x.Zhandian.Contains(kw) || x.Canpinzhong.Contains(kw) || x.Jibie.Contains(kw)); } var total = await query.CountAsync(); var items = await query .OrderByDescending(x => x.Year) .OrderBy(x => x.Code) .Page(pageIndex, pageSize) .ToListAsync(x => new YuanliaoZhuangkouReadDto_Table(x.Id, x.Year, x.Name, x.Code, x.Jibie, x.Bendiwaidi, x.Chandi, x.Canpinzhong, x.WzmlId, "", x.Zhandian, x.Pici)); // 物资名称取自物资目录(sys_wuzi_mulu) var nameMap = await GetWuziNameMapAsync(items.Select(x => x.WzmlId).Distinct().ToList()); var list = items .Select(x => x with { WzmlName = nameMap.TryGetValue(x.WzmlId, out var name) ? name : string.Empty }) .ToList(); return new PageResult { Items = list, Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task GetByIdForEditAsync(long id) { var dto = await Orm.Select() .Where(x => x.Id == id && x.DeletedAt == null) .FirstAsync(x => new YuanliaoZhuangkouReadDto_Edit(x.Id, x.Year, x.Name, x.Code, x.Jibie, x.Bendiwaidi, x.Chandi, x.Canpinzhong, x.WzmlId, "", x.Zhandian, x.Pici)); if (dto == null) return null; var nameMap = await GetWuziNameMapAsync(new List { dto.WzmlId }); return dto with { WzmlName = nameMap.TryGetValue(dto.WzmlId, out var name) ? name : string.Empty }; } public async Task> GetCodesByYearAsync(int year) => await Orm.Select() .Where(x => x.Year == year && x.Code != null && x.Code != "") .ToListAsync(x => x.Code); /// /// 物资(茧别)选项:取物资目录中"业务类型为原料"的分组(含其子分组,如 01 干茧、333 外购茧)下的物资。 /// 这样可避免把五金件(如捞茧爪轴)之类的物资混入茧别。 /// public async Task> GetWuziOptionsAsync(string keyword) { var allGroups = await Orm.Select().Where(x => x.DeletedAt == null).ToListAsync(); var rawGroupIds = new HashSet(allGroups .Where(g => g.BusinessType == WuziBusiness_Type_Enum.原料) .Select(g => g.Id)); // 把原料分组的后代分组一并纳入 var changed = true; while (changed) { changed = false; foreach (var group in allGroups) { if (group.ParentId != 0 && rawGroupIds.Contains(group.ParentId) && rawGroupIds.Add(group.Id)) changed = true; } } var rawGroupIdList = rawGroupIds.ToList(); var query = Orm.Select().Where(x => x.DeletedAt == null && rawGroupIdList.Contains(x.GroupId)); if (!string.IsNullOrWhiteSpace(keyword)) { var kw = keyword.Trim(); query = query.Where(x => x.Name.Contains(kw) || x.Code.Contains(kw) || x.PinyinName.Contains(kw)); } var rows = await query .OrderBy(x => x.Code) .Page(1, 300) .ToListAsync(x => new WuziRow(x.Id, x.Name, x.Code, x.GroupId)); if (rows.Count == 0) return new List(); var groupIds = rows.Select(x => x.GroupId).Distinct().ToList(); var groups = await Orm.Select().Where(x => groupIds.Contains(x.Id)).ToListAsync(); var groupNameMap = groups.ToDictionary(x => x.Id, x => x.Name); return rows .Select(x => new WuziMuluOptionDto(x.Id, x.Name, x.Code, groupNameMap.TryGetValue(x.GroupId, out var name) ? name : string.Empty)) .ToList(); } public async Task GetWuziNameAsync(long wzmlId) { if (wzmlId <= 0) return string.Empty; var nameMap = await GetWuziNameMapAsync(new List { wzmlId }); return nameMap.TryGetValue(wzmlId, out var name) ? name : string.Empty; } /// 物资 Id → 名称(取自物资目录) private async Task> GetWuziNameMapAsync(List wzmlIds) { var map = new Dictionary(); if (wzmlIds == null || wzmlIds.Count == 0) return map; var rows = await Orm.Select() .Where(x => wzmlIds.Contains(x.Id)) .ToListAsync(x => new WuziRow(x.Id, x.Name, x.Code, x.GroupId)); foreach (var row in rows) map[row.Id] = row.Name; return map; } private sealed record WuziRow(long Id, string Name, string Code, long GroupId); }