Files
2026-10-08 15:44:07 +08:00

79 lines
3.3 KiB
C#

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<WuziGroup> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<WuziGroup>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<bool> AddAsync(WuziGroup entity)
{
entity.CreatedAt = DateTime.Now;
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(WuziGroup entity)
{
entity.UpdatedAt = DateTime.Now;
return await Orm.Update<WuziGroup>().SetSource(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<WuziGroup>()
.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<List<WuziGroupReadDto_View>> GetAllAsync()
=> await Orm.Select<WuziGroup>()
.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<WuziGroupReadDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<WuziGroup>()
.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<bool> ExistChildAsync(long id)
=> await Orm.Select<WuziGroup>().Where(x => x.DeletedAt == null && x.ParentId == id).AnyAsync();
public async Task<bool> ExistMuluAsync(long id)
=> await Orm.Select<WuziMulu>().Where(x => x.DeletedAt == null && x.GroupId == id).AnyAsync();
/// <summary>生成新编号:一级 = 最大编号 + 1;下级 = 父级编号 + 2 位序号</summary>
public async Task<string> GetNewCodeAsync(long parentId)
{
var all = await Orm.Select<WuziGroup>().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");
}
}