Files
Migu2.0/MiGu.Server/SimpleFields/SimpleFieldService.cs
T
wei.wu 51c3fc1994 持久层重构为独立 MiGu.DB 工程并优化集成
- 新增 MiGu.DB 项目,迁移所有领域实体与枚举,统一模型约定
- 实现 Entity/Repository/UoW/Provider/Exception 等接口与实现
- 支持数据修补机制,完善 Sqlite 初始迁移与数据库管理
- Server 侧移除 EF Core 相关,依赖 MiGu.DB,PlatformPersistence 适配
- 业务服务注入 UoW/Repository,状态字段统一用 enum 及辅助类
- 统一异常处理,Controller 映射 HTTP 状态码
- 配置项与文档补充数据库启动、SchemaMode、迁移说明
- 新增 GlobalUsings.Db.cs、WmsStatusAliases.cs 简化类型引用
- 新增 HttpActorContextMiddleware 支持操作者上下文一致性
- 新增 MiGuDbContextModelSnapshot 追踪数据库结构
- 优化代码结构,解耦领域与持久层,提升扩展性与安全性
2026-07-27 14:26:04 +08:00

134 lines
5.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using MiGu.Server.Persistence;
using MiGu.DB.Domains.SimpleFields;
namespace MiGu.Server.SimpleFields;
public sealed class SimpleFieldService
{
private readonly PlatformDbContext _db;
public SimpleFieldService(PlatformDbContext db) => _db = db;
public async Task<List<SimpleField>> ListAsync(string? fieldType = null, string? carType = null, string? q = null)
{
var query = _db.SimpleFields.AsNoTracking()
.OrderBy(x => x.CarType).ThenBy(x => x.FieldType).ThenBy(x => x.Key)
.AsQueryable();
if (!string.IsNullOrWhiteSpace(fieldType))
query = query.Where(x => x.FieldType == fieldType);
if (!string.IsNullOrWhiteSpace(carType))
query = query.Where(x => x.CarType == carType);
if (!string.IsNullOrWhiteSpace(q))
{
var kw = q.Trim();
query = query.Where(x =>
x.Key.Contains(kw) ||
x.CarType.Contains(kw) ||
(x.Chinese != null && x.Chinese.Contains(kw)) ||
(x.English != null && x.English.Contains(kw)) ||
x.Other.Contains(kw) ||
x.Value.Contains(kw));
}
return await query.ToListAsync();
}
public async Task<SimpleField> SaveAsync(SimpleFieldRequest req)
{
if (string.IsNullOrWhiteSpace(req.CarType)) throw new SimpleFieldException("car_type 不能为空");
if (string.IsNullOrWhiteSpace(req.FieldType)) throw new SimpleFieldException("field_type 不能为空");
if (string.IsNullOrWhiteSpace(req.Key)) throw new SimpleFieldException("key 不能为空");
var carType = req.CarType.Trim();
var now = SimpleFieldDateTime.Now;
SimpleField entity;
if (req.Id is { } id && id != Guid.Empty)
{
entity = await _db.SimpleFields.FirstOrDefaultAsync(x => x.Id == id)
?? throw new SimpleFieldException("记录不存在");
}
else
{
var dup = await _db.SimpleFields.AnyAsync(x =>
x.CarType == carType &&
x.FieldType == req.FieldType.Trim() &&
x.Key == req.Key.Trim());
if (dup) throw new SimpleFieldException("同车型与字段类型下 key 已存在");
entity = new SimpleField { Id = Guid.NewGuid(), CreateTime = now };
_db.SimpleFields.Add(entity);
}
entity.CarType = carType;
entity.FieldType = req.FieldType.Trim();
entity.Key = req.Key.Trim();
entity.Value = req.Value?.Trim() ?? "";
entity.DataType = req.DataType?.Trim() ?? "";
entity.Chinese = req.Chinese is null ? null : req.Chinese.Trim();
entity.English = req.English is null ? null : req.English.Trim();
entity.Other = req.Other?.Trim() ?? "";
entity.IsDefault = req.IsDefault;
entity.UpdateTime = now;
if (entity.CreateTime == default) entity.CreateTime = now;
await _db.SaveChangesAsync();
return entity;
}
public async Task DeleteAsync(Guid id)
{
var entity = await _db.SimpleFields.FirstOrDefaultAsync(x => x.Id == id)
?? throw new SimpleFieldException("记录不存在");
_db.SimpleFields.Remove(entity);
await _db.SaveChangesAsync();
}
/// <summary>批量保存全部字段;ReplaceAll=true 时清空表后写入。</summary>
public async Task<int> SaveBatchAsync(SimpleFieldBatchRequest req)
{
var items = req.Items ?? new List<SimpleFieldRequest>();
if (items.Count == 0) throw new SimpleFieldException("没有可保存的字段");
if (req.ReplaceAll)
{
var all = await _db.SimpleFields.ToListAsync();
_db.SimpleFields.RemoveRange(all);
}
var now = SimpleFieldDateTime.Now;
var added = 0;
foreach (var item in items)
{
if (string.IsNullOrWhiteSpace(item.CarType) ||
string.IsNullOrWhiteSpace(item.FieldType) ||
string.IsNullOrWhiteSpace(item.Key))
continue;
_db.SimpleFields.Add(new SimpleField
{
Id = Guid.NewGuid(),
CarType = item.CarType.Trim(),
FieldType = item.FieldType.Trim(),
Key = item.Key.Trim(),
Value = item.Value?.Trim() ?? "",
DataType = item.DataType?.Trim() ?? "",
Chinese = item.Chinese is null ? null : item.Chinese.Trim(),
English = item.English is null ? null : item.English.Trim(),
Other = item.Other?.Trim() ?? "",
IsDefault = item.IsDefault,
CreateTime = now,
UpdateTime = now
});
added++;
}
await _db.SaveChangesAsync();
return added;
}
}
public sealed class SimpleFieldException : Exception
{
public SimpleFieldException(string message) : base(message) { }
}