完善基础数据

This commit is contained in:
yanpeng200212
2026-10-08 15:44:07 +08:00
parent 8430ba0452
commit 2ffb83830c
95 changed files with 37759 additions and 5 deletions
@@ -0,0 +1,92 @@
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<Gongyingshang> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<Gongyingshang>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<bool> AddAsync(Gongyingshang entity)
{
entity.CreatedAt = DateTime.Now;
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(Gongyingshang entity)
{
entity.UpdatedAt = DateTime.Now;
return await Orm.Update<Gongyingshang>().SetSource(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<Gongyingshang>()
.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<PageResult<GongyingshangReadDto_View>> 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<Gongyingshang>().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<GongyingshangReadDto_View>
{
Items = list,
Total = total,
PageIndex = pageIndex,
PageSize = pageSize
};
}
public async Task<GongyingshangDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<Gongyingshang>()
.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));
/// <summary>销售类型显示文本(0 等非枚举值视为未设置)</summary>
internal static string SaleTypeText(Gys_SaleType_Enum saleType)
=> Enum.IsDefined(typeof(Gys_SaleType_Enum), saleType) ? saleType.ToString() : "未设置";
}
@@ -0,0 +1,28 @@
using FreeSql;
using F9WebAiTest.Application.Dtos.SYS.Registries;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Registries;
using F9WebAiTest.Domain.Entities.SYS.Registries;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Registries;
internal class RegistryReadRepository : F9RepositoryBase, IRegistryReadRepository
{
public RegistryReadRepository(IFreeSql fsql) : base(fsql) { }
public async Task<List<RegistryReadDto_View>> GetAllAsync()
=> await Orm.Select<Registry>()
.Where(x => x.DeletedAt == null)
.OrderBy(x => x.Category)
.OrderBy(x => x.KeyName)
.ToListAsync(x => new RegistryReadDto_View(x.Id, x.Category, x.KeyName, x.Value, x.DataType, x.Description, x.IsActive));
public async Task<RegistryReadDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<Registry>()
.Where(x => x.Id == id && x.DeletedAt == null)
.FirstAsync(x => new RegistryReadDto_Edit(x.Id, x.Category, x.KeyName, x.Value, x.DataType, x.Description, x.IsActive));
public async Task<RegistryReadDto_Edit> GetByCategoryAndKeyAsync(string category, string keyName)
=> await Orm.Select<Registry>()
.Where(x => x.DeletedAt == null && x.Category == category && x.KeyName == keyName)
.FirstAsync(x => new RegistryReadDto_Edit(x.Id, x.Category, x.KeyName, x.Value, x.DataType, x.Description, x.IsActive));
}
@@ -0,0 +1,37 @@
using FreeSql;
using F9WebAiTest.Domain.Entities.SYS.Registries;
using F9WebAiTest.Domain.Interfaces.SYS.Registries;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Registries;
internal class RegistryWriteRepository : F9RepositoryBase, IRegistryWriteRepository
{
public RegistryWriteRepository(IFreeSql fsql) : base(fsql) { }
public async Task<Registry> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<Registry>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<Registry> GetByCategoryAndKeyAsync(string category, string keyName)
=> await Orm.Select<Registry>()
.Where(x => x.DeletedAt == null && x.Category == category && x.KeyName == keyName)
.FirstAsync();
public async Task<bool> AddAsync(Registry registry)
{
registry.CreatedAt = DateTime.Now;
return await Orm.Insert(registry).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(Registry registry)
{
registry.UpdatedAt = DateTime.Now;
return await Orm.Update<Registry>().SetSource(registry).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<Registry>()
.Set(x => x.IsDeleted, true)
.Set(x => x.DeletedAt, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync() > 0;
}
@@ -0,0 +1,90 @@
using FreeSql;
using F9WebAiTest.Application.Common;
using F9WebAiTest.Application.Dtos.SYS.WorkTypes;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.WorkTypes;
using F9WebAiTest.Domain.Entities.SYS.Departments;
using F9WebAiTest.Domain.Entities.SYS.WorkTypes;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.WorkTypes;
internal class WorkTypeReadRepository : F9RepositoryBase, IWorkTypeReadRepository
{
public WorkTypeReadRepository(IFreeSql fsql) : base(fsql) { }
public async Task<PageResult<WorkTypeReadDto_View>> GetPagedAsync(int pageIndex, int pageSize, string keyword)
{
if (pageIndex <= 0) pageIndex = 1;
if (pageSize <= 0) pageSize = 20;
var query = Orm.Select<WorkType>().Where(x => x.DeletedAt == null);
if (!string.IsNullOrWhiteSpace(keyword))
{
var kw = keyword.Trim();
query = query.Where(x => x.WorkName.Contains(kw) || x.BizType.Contains(kw) || x.SccbFenlei.Contains(kw));
}
var total = await query.CountAsync();
var items = await query
.OrderBy(x => x.SortOrder)
.OrderBy(x => x.Id)
.Page(pageIndex, pageSize)
.ToListAsync(x => new WorkTypeReadDto_View(x.Id, x.WorkName, x.SortOrder, x.BizType, x.SccbFenlei, x.IsActive, ""));
var nameMap = await GetDepartmentNameMapAsync(items.Select(x => x.Id).ToList());
var list = items
.Select(x => x with { DepartmentNames = nameMap.TryGetValue(x.Id, out var names) ? names : string.Empty })
.ToList();
return new PageResult<WorkTypeReadDto_View>
{
Items = list,
Total = total,
PageIndex = pageIndex,
PageSize = pageSize
};
}
public async Task<WorkTypeReadDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<WorkType>()
.Where(x => x.Id == id && x.DeletedAt == null)
.FirstAsync(x => new WorkTypeReadDto_Edit(x.Id, x.WorkName, x.SortOrder, x.BizType, x.SccbFenlei, x.IsActive));
public async Task<List<WorkTypeDepartmentReadDto_Cache>> GetDepartmentsAsync(long workTypeId)
=> await Orm.Select<WorkTypeDepartment>()
.Where(x => x.WorkTypeId == workTypeId)
.OrderBy(x => x.AssignedAt)
.ToListAsync(x => new WorkTypeDepartmentReadDto_Cache(x.Id, x.WorkTypeId, x.DepartmentId));
public async Task<List<WorkTypeRoleReadDto_Cache>> GetRolesAsync(long workTypeId)
=> await Orm.Select<WorkTypeRole>()
.Where(x => x.WorkTypeId == workTypeId)
.OrderBy(x => x.AssignedAt)
.ToListAsync(x => new WorkTypeRoleReadDto_Cache(x.Id, x.WorkTypeId, x.RoleId));
/// <summary>工种 Id → 所属部门名称串(顿号分隔)</summary>
private async Task<Dictionary<long, string>> GetDepartmentNameMapAsync(List<long> workTypeIds)
{
var map = new Dictionary<long, string>();
if (workTypeIds == null || workTypeIds.Count == 0) return map;
var relations = await Orm.Select<WorkTypeDepartment>()
.Where(x => workTypeIds.Contains(x.WorkTypeId))
.ToListAsync();
if (relations.Count == 0) return map;
var depIds = relations.Select(x => x.DepartmentId).Distinct().ToList();
var deps = await Orm.Select<Department>().Where(x => depIds.Contains(x.Id)).ToListAsync();
var depNameMap = deps.ToDictionary(x => x.Id, x => x.Name);
foreach (var group in relations.GroupBy(x => x.WorkTypeId))
{
var names = group
.Select(r => depNameMap.TryGetValue(r.DepartmentId, out var name) ? name : null)
.Where(name => !string.IsNullOrEmpty(name))
.ToList();
map[group.Key] = string.Join("、", names);
}
return map;
}
}
@@ -0,0 +1,51 @@
using FreeSql;
using F9WebAiTest.Domain.Entities.SYS.WorkTypes;
using F9WebAiTest.Domain.Interfaces.SYS.WorkTypes;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.WorkTypes;
/// <summary>工种-部门关联仓储(关联表用物理删除,避免唯一索引与历史数据冲突)</summary>
internal class WorkTypeDepartmentRepository : F9RepositoryBase, IWorkTypeDepartmentRepository
{
public WorkTypeDepartmentRepository(IFreeSql fsql) : base(fsql) { }
public async Task<WorkTypeDepartment> GetAsync(long workTypeId, long departmentId)
=> await Orm.Select<WorkTypeDepartment>()
.Where(x => x.WorkTypeId == workTypeId && x.DepartmentId == departmentId)
.FirstAsync();
public async Task<bool> AddAsync(WorkTypeDepartment entity)
{
entity.CreatedAt = DateTime.Now;
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> HardDeleteAsync(long id)
=> await Orm.Delete<WorkTypeDepartment>().Where(x => x.Id == id).ExecuteAffrowsAsync() > 0;
public async Task<bool> HardDeleteByWorkTypeIdAsync(long workTypeId)
=> await Orm.Delete<WorkTypeDepartment>().Where(x => x.WorkTypeId == workTypeId).ExecuteAffrowsAsync() > 0;
}
/// <summary>工种-角色关联仓储</summary>
internal class WorkTypeRoleRepository : F9RepositoryBase, IWorkTypeRoleRepository
{
public WorkTypeRoleRepository(IFreeSql fsql) : base(fsql) { }
public async Task<WorkTypeRole> GetAsync(long workTypeId, long roleId)
=> await Orm.Select<WorkTypeRole>()
.Where(x => x.WorkTypeId == workTypeId && x.RoleId == roleId)
.FirstAsync();
public async Task<bool> AddAsync(WorkTypeRole entity)
{
entity.CreatedAt = DateTime.Now;
return await Orm.Insert(entity).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> HardDeleteAsync(long id)
=> await Orm.Delete<WorkTypeRole>().Where(x => x.Id == id).ExecuteAffrowsAsync() > 0;
public async Task<bool> HardDeleteByWorkTypeIdAsync(long workTypeId)
=> await Orm.Delete<WorkTypeRole>().Where(x => x.WorkTypeId == workTypeId).ExecuteAffrowsAsync() > 0;
}
@@ -0,0 +1,39 @@
using FreeSql;
using F9WebAiTest.Domain.Entities.SYS.WorkTypes;
using F9WebAiTest.Domain.Interfaces.SYS.WorkTypes;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.WorkTypes;
internal class WorkTypeWriteRepository : F9RepositoryBase, IWorkTypeWriteRepository
{
public WorkTypeWriteRepository(IFreeSql fsql) : base(fsql) { }
public async Task<WorkType> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<WorkType>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<bool> AddAsync(WorkType workType)
{
workType.CreatedAt = DateTime.Now;
return await Orm.Insert(workType).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(WorkType workType)
{
workType.UpdatedAt = DateTime.Now;
return await Orm.Update<WorkType>().SetSource(workType).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<WorkType>()
.Set(x => x.IsDeleted, true)
.Set(x => x.DeletedAt, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync() > 0;
public async Task<bool> ActiveOrDeactive(long id, bool active)
=> await Orm.Update<WorkType>()
.Set(x => x.IsActive, active)
.Set(x => x.UpdatedAt, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync() > 0;
}
@@ -0,0 +1,78 @@
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");
}
}
@@ -0,0 +1,128 @@
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);
}
@@ -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);
}
@@ -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;
}
@@ -0,0 +1,65 @@
using FreeSql;
using F9WebAiTest.Application.Common;
using F9WebAiTest.Application.Dtos.SYS.Zq;
using F9WebAiTest.Application.Interfaces.Repositories.SYS.Zq;
using F9WebAiTest.Domain.Entities.SYS.Zq;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Zq;
internal class ZhangqiReadRepository : F9RepositoryBase, IZhangqiReadRepository
{
public ZhangqiReadRepository(IFreeSql fsql) : base(fsql) { }
public async Task<PageResult<ZhangqiReadDto_View>> GetPagedAsync(int? year, string keyword, int pageIndex, int pageSize)
{
if (pageIndex <= 0) pageIndex = 1;
if (pageSize <= 0) pageSize = 20;
var query = Orm.Select<Zhangqi>().Where(x => x.DeletedAt == null);
if (year.HasValue && year.Value > 0) query = query.Where(x => x.Year == year.Value);
// 关键字:纯数字时按年度/月份匹配
if (!string.IsNullOrWhiteSpace(keyword) && int.TryParse(keyword.Trim(), out var num))
query = query.Where(x => x.Year == num || x.Month == num);
var total = await query.CountAsync();
var items = await query
.OrderByDescending(x => x.Year)
.OrderByDescending(x => x.Month)
.Page(pageIndex, pageSize)
.ToListAsync(x => new ZhangqiReadDto_View(x.Id, x.Year, x.Month, x.StartDate, x.EndDate, x.IsZhaZhang));
return new PageResult<ZhangqiReadDto_View>
{
Items = items,
Total = total,
PageIndex = pageIndex,
PageSize = pageSize
};
}
public async Task<ZhangqiReadDto_Edit> GetByIdForEditAsync(long id)
=> await Orm.Select<Zhangqi>()
.Where(x => x.Id == id && x.DeletedAt == null)
.FirstAsync(x => new ZhangqiReadDto_Edit(x.Id, x.Year, x.Month, x.StartDate, x.EndDate, x.IsZhaZhang));
public async Task<ZhangqiReadDto_Edit> GetByDateAsync(DateTime date)
=> await Orm.Select<Zhangqi>()
.Where(x => x.DeletedAt == null && x.StartDate <= date && x.EndDate >= date)
.OrderByDescending(x => x.StartDate)
.FirstAsync(x => new ZhangqiReadDto_Edit(x.Id, x.Year, x.Month, x.StartDate, x.EndDate, x.IsZhaZhang));
public async Task<bool> GetIsZhaZhangAsync(int year, int month)
{
var list = await Orm.Select<Zhangqi>()
.Where(x => x.DeletedAt == null && x.Year == year && x.Month == month)
.ToListAsync();
return list.Any(x => x.IsZhaZhang);
}
public async Task<bool> GetIsZhaZhangByDateAsync(DateTime date)
{
var dto = await GetByDateAsync(date);
return dto != null && dto.IsZhaZhang;
}
}
@@ -0,0 +1,39 @@
using FreeSql;
using F9WebAiTest.Domain.Entities.SYS.Zq;
using F9WebAiTest.Domain.Interfaces.SYS.Zq;
namespace F9WebAiTest.Infrastructure.FreeSqls.Repositories.SYS.Zq;
internal class ZhangqiWriteRepository : F9RepositoryBase, IZhangqiWriteRepository
{
public ZhangqiWriteRepository(IFreeSql fsql) : base(fsql) { }
public async Task<Zhangqi> GetByIdAsync(long id, CancellationToken cancellationToken)
=> await Orm.Select<Zhangqi>().Where(x => x.Id == id && x.DeletedAt == null).FirstAsync(cancellationToken);
public async Task<bool> AddAsync(Zhangqi zhangqi)
{
zhangqi.CreatedAt = DateTime.Now;
return await Orm.Insert(zhangqi).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> UpdateAsync(Zhangqi zhangqi)
{
zhangqi.UpdatedAt = DateTime.Now;
return await Orm.Update<Zhangqi>().SetSource(zhangqi).ExecuteAffrowsAsync() > 0;
}
public async Task<bool> SoftDeleteAsync(long id)
=> await Orm.Update<Zhangqi>()
.Set(x => x.IsDeleted, true)
.Set(x => x.DeletedAt, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync() > 0;
public async Task<bool> SetZhaZhangAsync(long id, bool isZhaZhang)
=> await Orm.Update<Zhangqi>()
.Set(x => x.IsZhaZhang, isZhaZhang)
.Set(x => x.UpdatedAt, DateTime.Now)
.Where(x => x.Id == id)
.ExecuteAffrowsAsync() > 0;
}