- 移除 LegacyStatusNormalizationMigrator,仅保留 AreaLayoutModeDefaultMigrator - 删除 WmsDispatchStatus 枚举、常量和全局 using - 统一所有 DbContext 注入为 MiGuDbContext,移除 PlatformDbContext - 服务实体操作统一用 IEditableRepository<T>,移除本地实现 - 移除 WmsService 的 MigrateLegacyAsync 方法 - 精简接口模型,移除部分字段和请求体 - 前端保存容器时 status 字段取自已有数据,移除写死默认值 - 更新文档,去除旧说明,统一数据库初始化流程
134 lines
5.0 KiB
C#
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 MiGuDbContext _db;
|
|
|
|
public SimpleFieldService(MiGuDbContext 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) { }
|
|
}
|