using FreeSql; using F9WebAiTest.Application.Common; using F9WebAiTest.Application.Dtos.SYS.Gongyingshangs; using F9WebAiTest.Application.Interfaces.Repositories.SYS.Gongyingshangs; using F9WebAiTest.Domain.Entities.SYS.Gongyingshangs; using F9WebAiTest.Domain.Enums.SYS.Gongyingshangs; using F9WebAiTest.Domain.Interfaces.SYS.Gongyingshangs; namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Gongyingshangs; internal class GongyingshangWriteRepository : F9RepositoryBase, IGongyingshangWriteRepository { public GongyingshangWriteRepository(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(Gongyingshang entity) { entity.CreatedAt = DateTime.Now; return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0; } public async Task UpdateAsync(Gongyingshang 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 GongyingshangReadRepository : F9RepositoryBase, IGongyingshangReadRepository { public GongyingshangReadRepository(IFreeSql fsql) : base(fsql) { } public async Task> GetPagedAsync(Gys_Type_Enum type, Gys_SaleType_Enum? saleType, 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 && x.Type == type); if (saleType.HasValue) query = query.Where(x => x.SaleType == saleType.Value); if (!string.IsNullOrWhiteSpace(keyword)) { var kw = keyword.Trim(); query = query.Where(x => x.Name.Contains(kw) || x.Pinyin.Contains(kw) || x.Lianxiren.Contains(kw) || x.Phone.Contains(kw) || x.Address.Contains(kw)); } var total = await query.CountAsync(); var items = await query .OrderBy(x => x.Name) .Page(pageIndex, pageSize) .ToListAsync(x => new GongyingshangReadDto_View(x.Id, x.Name, x.Type, "", x.BusinessGroup, "", x.SaleType, "", x.Lianxiren, x.Phone, x.Address, x.Pinyin)); var list = items .Select(x => x with { TypeName = x.Type.ToString(), Grouname = x.Group.ToString(), SaleTypeName = SaleTypeText(x.SaleType) }) .ToList(); return new PageResult { Items = list, 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 GongyingshangDto_Edit(x.Id, x.Name, x.Type, x.BusinessGroup, x.SaleType, x.Lianxiren, x.Phone, x.Address, x.Pinyin)); /// 销售类型显示文本(0 等非枚举值视为未设置) internal static string SaleTypeText(Gys_SaleType_Enum saleType) => Enum.IsDefined(typeof(Gys_SaleType_Enum), saleType) ? saleType.ToString() : "未设置"; }