Files
f9-web-ai-test/backend/F9WebAiTest.Infrastructure/FreeSqls/Repositories/SYS/YuanliaoZhuangkous/YuanliaoZhuangkouReadRepository.cs
T
2026-10-08 15:44:07 +08:00

146 lines
6.3 KiB
C#
Raw 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.Dtos.SYS.YuanliaoZhuangkous;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.YuanliaoZhuangkous;
using F9WebAiTest.Domain.Entities.SYS.WuziMulus;
using F9WebAiTest.Domain.Enums.SYS.WuziMulus;
using F9WebAiTest.Domain.Entities.SYS.YuanliaoZhuangkous;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.YuanliaoZhuangkous;
internal class YuanliaoZhuangkouReadRepository : F9RepositoryBase, IYuanliaoZhuangkouReadRepository
{
public YuanliaoZhuangkouReadRepository(IFreeSql fsql) : base(fsql) { }
public async Task<PageResult<YuanliaoZhuangkouReadDto_Table>> GetPagedAsync(int year, string chandi, string zhandian,
string jibie, string keyword, int pageIndex, int pageSize)
{
if (pageIndex <= 0) pageIndex = 1;
if (pageSize <= 0) pageSize = 20;
var query = Orm.Select<YuanliaoZhuangkou>().Where(x => x.DeletedAt == null);
if (year > 0) query = query.Where(x => x.Year == year);
if (!string.IsNullOrWhiteSpace(chandi)) query = query.Where(x => x.Chandi == chandi.Trim());
if (!string.IsNullOrWhiteSpace(zhandian)) query = query.Where(x => x.Zhandian == zhandian.Trim());
if (!string.IsNullOrWhiteSpace(jibie)) query = query.Where(x => x.Jibie == jibie.Trim());
if (!string.IsNullOrWhiteSpace(keyword))
{
var kw = keyword.Trim();
query = query.Where(x => x.Name.Contains(kw) || x.Code.Contains(kw) || x.Chandi.Contains(kw)
|| x.Zhandian.Contains(kw) || x.Canpinzhong.Contains(kw) || x.Jibie.Contains(kw));
}
var total = await query.CountAsync();
var items = await query
.OrderByDescending(x => x.Year)
.OrderBy(x => x.Code)
.Page(pageIndex, pageSize)
.ToListAsync(x => new YuanliaoZhuangkouReadDto_Table(x.Id, x.Year, x.Name, x.Code, x.Jibie,
x.Bendiwaidi, x.Chandi, x.Canpinzhong, x.WzmlId, "", x.Zhandian, x.Pici));
// 物资名称取自物资目录(sys_wuzi_mulu)
var nameMap = await GetWuziNameMapAsync(items.Select(x => x.WzmlId).Distinct().ToList());
var list = items
.Select(x => x with { WzmlName = nameMap.TryGetValue(x.WzmlId, out var name) ? name : string.Empty })
.ToList();
return new PageResult<YuanliaoZhuangkouReadDto_Table>
{
Items = list,
Total = total,
PageIndex = pageIndex,
PageSize = pageSize
};
}
public async Task<YuanliaoZhuangkouReadDto_Edit> GetByIdForEditAsync(long id)
{
var dto = await Orm.Select<YuanliaoZhuangkou>()
.Where(x => x.Id == id && x.DeletedAt == null)
.FirstAsync(x => new YuanliaoZhuangkouReadDto_Edit(x.Id, x.Year, x.Name, x.Code, x.Jibie,
x.Bendiwaidi, x.Chandi, x.Canpinzhong, x.WzmlId, "", x.Zhandian, x.Pici));
if (dto == null) return null;
var nameMap = await GetWuziNameMapAsync(new List<long> { dto.WzmlId });
return dto with { WzmlName = nameMap.TryGetValue(dto.WzmlId, out var name) ? name : string.Empty };
}
public async Task<List<string>> GetCodesByYearAsync(int year)
=> await Orm.Select<YuanliaoZhuangkou>()
.Where(x => x.Year == year && x.Code != null && x.Code != "")
.ToListAsync(x => x.Code);
/// <summary>
/// 物资(茧别)选项:取物资目录中"业务类型为原料"的分组(含其子分组,如 01 干茧、333 外购茧)下的物资。
/// 这样可避免把五金件(如捞茧爪轴)之类的物资混入茧别。
/// </summary>
public async Task<List<WuziMuluOptionDto>> GetWuziOptionsAsync(string keyword)
{
var allGroups = await Orm.Select<WuziGroup>().Where(x => x.DeletedAt == null).ToListAsync();
var rawGroupIds = new HashSet<long>(allGroups
.Where(g => g.BusinessType == WuziBusiness_Type_Enum.原料)
.Select(g => g.Id));
// 把原料分组的后代分组一并纳入
var changed = true;
while (changed)
{
changed = false;
foreach (var group in allGroups)
{
if (group.ParentId != 0 && rawGroupIds.Contains(group.ParentId) && rawGroupIds.Add(group.Id))
changed = true;
}
}
var rawGroupIdList = rawGroupIds.ToList();
var query = Orm.Select<WuziMulu>().Where(x => x.DeletedAt == null && rawGroupIdList.Contains(x.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));
}
var rows = await query
.OrderBy(x => x.Code)
.Page(1, 300)
.ToListAsync(x => new WuziRow(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();
}
public async Task<string> GetWuziNameAsync(long wzmlId)
{
if (wzmlId <= 0) return string.Empty;
var nameMap = await GetWuziNameMapAsync(new List<long> { wzmlId });
return nameMap.TryGetValue(wzmlId, out var name) ? name : string.Empty;
}
/// <summary>物资 Id → 名称(取自物资目录)</summary>
private async Task<Dictionary<long, string>> GetWuziNameMapAsync(List<long> wzmlIds)
{
var map = new Dictionary<long, string>();
if (wzmlIds == null || wzmlIds.Count == 0) return map;
var rows = await Orm.Select<WuziMulu>()
.Where(x => wzmlIds.Contains(x.Id))
.ToListAsync(x => new WuziRow(x.Id, x.Name, x.Code, x.GroupId));
foreach (var row in rows) map[row.Id] = row.Name;
return map;
}
private sealed record WuziRow(long Id, string Name, string Code, long GroupId);
}