完善基础数据
This commit is contained in:
+145
@@ -0,0 +1,145 @@
|
||||
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);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
using FreeSql;
|
||||
using F9WebAiTest.Domain.Entities.SYS.YuanliaoZhuangkous;
|
||||
using F9WebAiTest.Domain.Interfaces.SYS.YuanliaoZhuangkous;
|
||||
|
||||
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.YuanliaoZhuangkous;
|
||||
|
||||
internal class YuanliaoZhuangkouWriteRepository : F9RepositoryBase, IYuanliaoZhuangkouWriteRepository
|
||||
{
|
||||
public YuanliaoZhuangkouWriteRepository(IFreeSql fsql) : base(fsql) { }
|
||||
|
||||
public async Task<YuanliaoZhuangkou> GetByIdAsync(long id, CancellationToken cancellationToken)
|
||||
=> await Orm.Select<YuanliaoZhuangkou>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
|
||||
|
||||
public async Task<bool> AddAsync(YuanliaoZhuangkou entity)
|
||||
{
|
||||
entity.CreatedAt = DateTime.Now;
|
||||
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync(YuanliaoZhuangkou entity)
|
||||
{
|
||||
entity.UpdatedAt = DateTime.Now;
|
||||
return await Orm.Update<YuanliaoZhuangkou>().SetSource(entity).ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<bool> SoftDeleteAsync(long id)
|
||||
=> await Orm.Update<YuanliaoZhuangkou>()
|
||||
.Set(x => x.IsDeleted, true)
|
||||
.Set(x => x.DeletedAt, DateTime.Now)
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteAffrowsAsync() > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user