using FreeSql; using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.SYS.WuziMulus; using F9WebAiTest.Application.Interfaces.Repositories.SYS.WuziMulus; using F9WebAiTest.Domain.Entities.SYS.WuziMulus; using F9WebAiTest.Domain.Interfaces.SYS.WuziMulus; namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.WuziMulus; internal class WuziMuluWriteRepository : F9RepositoryBase, IWuziMuluWriteRepository { public WuziMuluWriteRepository(IFreeSql fsql) : base(fsql) { } public async Task GetByIdAsync(long id, CancellationToken cancellationToken) => await Orm.Select().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken); public async Task AddAsync(WuziMulu entity) { entity.CreatedAt = DateTime.Now; return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0; } public async Task UpdateAsync(WuziMulu entity) { entity.UpdatedAt = DateTime.Now; return await Orm.Update().SetSource(entity).ExecuteAffrowsAsync() > 0; } public async Task SoftDeleteAsync(long id) => await Orm.Update() .Set(x => x.IsDeleted, true) .Set(x => x.DeletedAt, DateTime.Now) .Where(x => x.Id == id) .ExecuteAffrowsAsync() > 0; } internal class WuziMuluReadRepository : F9RepositoryBase, IWuziMuluReadRepository { public WuziMuluReadRepository(IFreeSql fsql) : base(fsql) { } public async Task> GetPagedAsync(long groupId, 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 (groupId > 0) query = query.Where(x => x.GroupId == 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) || x.Guige.Contains(kw) || x.Xinghao.Contains(kw)); } var total = await query.CountAsync(); var items = await query .OrderBy(x => x.SortOrder) .OrderBy(x => x.Code) .Page(pageIndex, pageSize) .ToListAsync(x => new WuziMuluReadDto_View(x.Id, x.GroupId, x.Name, x.Code, x.Unit, x.Guige, x.Xinghao, x.PinyinName, x.Kucunyujing, x.SortOrder, x.IsYujing, x.SaleRate)); return new PageResult { Items = items, Total = total, PageIndex = pageIndex, PageSize = pageSize }; } public async Task GetByIdForEditAsync(long id) => await Orm.Select() .Where(x => x.Id == id && x.DeletedAt == null) .FirstAsync(x => new WuziMuluReadDto_Edit(x.Id, x.GroupId, x.Name, x.Code, x.Unit, x.Guige, x.Xinghao, x.PinyinName, x.Kucunyujing, x.SortOrder, x.IsYujing, x.SaleRate)); /// 生成新编号:分组编号 + 2 位序号 public async Task GetNewCodeAsync(long groupId) { var group = await Orm.Select().Where(x => x.Id == groupId).FirstAsync(); var groupCode = group?.Code ?? string.Empty; var list = await Orm.Select().Where(x => x.DeletedAt == null && x.GroupId == groupId).ToListAsync(); var maxSeq = 0; foreach (var item in list) { var code = item.Code ?? string.Empty; var suffix = groupCode.Length > 0 && code.StartsWith(groupCode) ? code.Substring(groupCode.Length) : code; if (int.TryParse(suffix, out var seq) && seq > maxSeq) maxSeq = seq; } return groupCode + (maxSeq + 1).ToString("00"); } /// 物资选项(供其它模块选择:名称/编号/拼音模糊匹配) public async Task> GetOptionsAsync(string keyword, int limit) { if (limit <= 0) limit = 200; var query = Orm.Select().Where(x => x.DeletedAt == null); 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, limit) .ToListAsync(x => new OptionRow(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(); } /// 选项查询投影(含分组 Id) private sealed record OptionRow(long Id, string Name, string Code, long GroupId); }