175 lines
5.2 KiB
C#
175 lines
5.2 KiB
C#
using AGSS.DbSet;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
public class DictService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public DictService(ApplicationDbContext context, ICurrentUserService currentUserService)
|
|
{
|
|
_context = context;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
|
|
// 1. 获取父级列表
|
|
public async Task<List<DictItem>> GetParentsAsync(string? label)
|
|
{
|
|
var query = _context.DictItems
|
|
.Where(d => d.ParentId == null);
|
|
|
|
if (!string.IsNullOrWhiteSpace(label))
|
|
{
|
|
query = query.Where(d => d.Label.Contains(label));
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
|
|
// 2. 创建父级项
|
|
public async Task<DictItem> CreateParentAsync(string label, string value)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var newItem = new DictItem
|
|
{
|
|
Label = label,
|
|
Value = value,
|
|
CreateUserId = _currentUserService.UserId
|
|
};
|
|
|
|
_context.DictItems.Add(newItem);
|
|
await _context.SaveChangesAsync();
|
|
return newItem;
|
|
}
|
|
|
|
// 2.1 更新父级项
|
|
public async Task UpdateParentAsync(Guid uuid, string label)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var item = await _context.DictItems
|
|
.FirstOrDefaultAsync(d => d.Uuid == uuid && d.ParentId == null);
|
|
|
|
if (item == null) throw new KeyNotFoundException("字典项不存在");
|
|
|
|
item.Label = label;
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
// 3. 删除父级项(级联删除)
|
|
public async Task DeleteParentAsync(Guid uuid)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var item = await _context.DictItems
|
|
.Include(d => d.Children)
|
|
.FirstOrDefaultAsync(d => d.Uuid == uuid && d.ParentId == null);
|
|
|
|
if (item == null) throw new KeyNotFoundException("字典项不存在");
|
|
|
|
_context.DictItems.Remove(item);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
// 4. 创建子项
|
|
public async Task<DictItem> CreateChildAsync(
|
|
Guid parentId,
|
|
string parentValue,
|
|
string label,
|
|
string value,
|
|
string? labelEn = null,
|
|
string? remark = null,
|
|
string? tag = null)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var parent = await _context.DictItems
|
|
.FirstOrDefaultAsync(d => d.Uuid == parentId && d.ParentId == null);
|
|
|
|
if (parent == null) throw new KeyNotFoundException("父级字典项不存在");
|
|
|
|
var newItem = new DictItem
|
|
{
|
|
ParentId = parentId,
|
|
ParentValue = parentValue,
|
|
Label = label,
|
|
Value = value,
|
|
LabelEn = labelEn,
|
|
Remark = remark,
|
|
Tag = tag,
|
|
CreateUserId = _currentUserService.UserId
|
|
};
|
|
|
|
_context.DictItems.Add(newItem);
|
|
await _context.SaveChangesAsync();
|
|
return newItem;
|
|
}
|
|
|
|
// 4.1 更新子项
|
|
public async Task UpdateChildAsync(
|
|
Guid uuid,
|
|
string label,
|
|
string value,
|
|
string? labelEn = null,
|
|
string? remark = null,
|
|
string? tag = null)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var item = await _context.DictItems
|
|
.FirstOrDefaultAsync(d => d.Uuid == uuid && d.ParentId != null);
|
|
|
|
if (item == null) throw new KeyNotFoundException("字典项不存在");
|
|
|
|
item.Label = label;
|
|
item.Value = value;
|
|
item.LabelEn = labelEn;
|
|
item.Remark = remark;
|
|
item.Tag = tag;
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
// 5. 删除子项
|
|
public async Task DeleteChildAsync(Guid uuid)
|
|
{
|
|
if (!_currentUserService.IsAdmin)
|
|
throw new UnauthorizedAccessException("您不是管理员,无权限");
|
|
|
|
var item = await _context.DictItems
|
|
.FirstOrDefaultAsync(d => d.Uuid == uuid && d.ParentId != null);
|
|
|
|
if (item == null) throw new KeyNotFoundException("字典项不存在");
|
|
|
|
_context.DictItems.Remove(item);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
// 6. 获取子项(带标签过滤)
|
|
public async Task<List<DictItem>> GetChildrenAsync(string value, string? tag = null)
|
|
{
|
|
var query = _context.DictItems
|
|
.Where(d => d.ParentValue == value);
|
|
|
|
if (!string.IsNullOrWhiteSpace(tag))
|
|
{
|
|
// 精确标签匹配逻辑
|
|
var tags = tag.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(t => t.Trim())
|
|
.ToList();
|
|
|
|
query = query.Where(d => d.Tag != null &&
|
|
tags.All(t => d.Tag.Contains(t)));
|
|
}
|
|
|
|
return await query.ToListAsync();
|
|
}
|
|
} |