using FreeSql; 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 WuziGroupWriteRepository : F9RepositoryBase, IWuziGroupWriteRepository { public WuziGroupWriteRepository(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(WuziGroup entity) { entity.CreatedAt = DateTime.Now; return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0; } public async Task UpdateAsync(WuziGroup 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 WuziGroupReadRepository : F9RepositoryBase, IWuziGroupReadRepository { public WuziGroupReadRepository(IFreeSql fsql) : base(fsql) { } public async Task> GetAllAsync() => await Orm.Select() .Where(x => x.DeletedAt == null) .OrderBy(x => x.SortOrder) .OrderBy(x => x.Code) .ToListAsync(x => new WuziGroupReadDto_View(x.Id, x.BusinessType, x.DepartmentType, x.Code, x.Name, x.ParentId, x.SortOrder, x.Description)); public async Task GetByIdForEditAsync(long id) => await Orm.Select() .Where(x => x.Id == id && x.DeletedAt == null) .FirstAsync(x => new WuziGroupReadDto_Edit(x.Id, x.BusinessType, x.DepartmentType, x.Code, x.Name, x.ParentId, x.SortOrder, x.Description)); public async Task ExistChildAsync(long id) => await Orm.Select().Where(x => x.DeletedAt == null && x.ParentId == id).AnyAsync(); public async Task ExistMuluAsync(long id) => await Orm.Select().Where(x => x.DeletedAt == null && x.GroupId == id).AnyAsync(); /// 生成新编号:一级 = 最大编号 + 1;下级 = 父级编号 + 2 位序号 public async Task GetNewCodeAsync(long parentId) { var all = await Orm.Select().Where(x => x.DeletedAt == null).ToListAsync(); var parentCode = string.Empty; if (parentId > 0) parentCode = all.FirstOrDefault(x => x.Id == parentId)?.Code ?? string.Empty; var maxSeq = 0; foreach (var group in all.Where(x => x.ParentId == parentId)) { var code = group.Code ?? string.Empty; var suffix = parentCode.Length > 0 && code.StartsWith(parentCode) ? code.Substring(parentCode.Length) : code; if (int.TryParse(suffix, out var seq) && seq > maxSeq) maxSeq = seq; } return parentCode + (maxSeq + 1).ToString("00"); } }