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

129 lines
5.2 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<WuziMulu> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<WuziMulu>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<bool> AddAsync(WuziMulu entity)
{
entity.CreatedAt = DateTime.Now;
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(WuziMulu entity)
{
entity.UpdatedAt = DateTime.Now;
return await Orm.Update<WuziMulu>().SetSource(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<WuziMulu>()
.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<PageResult<WuziMuluReadDto_View>> GetPagedAsync(long groupId, string keyword, int pageIndex, int pageSize)
{
if (pageIndex <= 0) pageIndex = 1;
if (pageSize <= 0) pageSize = 20;
var query = Orm.Select<WuziMulu>().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<WuziMuluReadDto_View>
{
Items = items,
Total = total,
PageIndex = pageIndex,
PageSize = pageSize
};
}
public async Task<WuziMuluReadDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<WuziMulu>()
.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));
/// <summary>生成新编号:分组编号 + 2 位序号</summary>
public async Task<string> GetNewCodeAsync(long groupId)
{
var group = await Orm.Select<WuziGroup>().Where(x => x.Id == groupId).FirstAsync();
var groupCode = group?.Code ?? string.Empty;
var list = await Orm.Select<WuziMulu>().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");
}
/// <summary>物资选项(供其它模块选择:名称/编号/拼音模糊匹配)</summary>
public async Task<List<WuziMuluOptionDto>> GetOptionsAsync(string keyword, int limit)
{
if (limit <= 0) limit = 200;
var query = Orm.Select<WuziMulu>().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<WuziMuluOptionDto>();
var groupIds = rows.Select(x => x.GroupId).Distinct().ToList();
var groups = await Orm.Select<WuziGroup>().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();
}
/// <summary>选项查询投影(含分组 Id)</summary>
private sealed record OptionRow(long Id, string Name, string Code, long GroupId);
}