新增物料类型处理逻辑,确保在保存和编辑称重记录时自动设置默认物料类型。更新前端表单以支持密炼物料的选择和显示,优化用户体验。添加分类字典和数据字典的事件广播功能,增强系统的实时数据同步能力。
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SqlSugar;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
@@ -14,7 +16,12 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ISqlSugarClient _dbContext;
|
||||
private readonly ILoggerService _logger;
|
||||
private readonly SemaphoreSlim _syncLock = new(1, 1);
|
||||
private readonly object _cacheLock = new();
|
||||
private readonly string _cacheFilePath;
|
||||
private List<MesMixerMaterial> _localCache = [];
|
||||
|
||||
private static readonly JsonSerializerOptions _jsonOpts = new()
|
||||
{
|
||||
@@ -26,36 +33,158 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
public MixerMaterialService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IConfiguration configuration,
|
||||
ISqlSugarClient dbContext,
|
||||
ILoggerService logger)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
|
||||
var appDataDir = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
"YY.Admin", "sync-cache");
|
||||
Directory.CreateDirectory(appDataDir);
|
||||
_cacheFilePath = Path.Combine(appDataDir, "mixer-material-cache.json");
|
||||
|
||||
_ = LoadCacheAsync();
|
||||
}
|
||||
|
||||
private string BaseUrl => (_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
||||
private HttpClient CreateClient() => _httpClientFactory.CreateClient("JeecgApi");
|
||||
|
||||
public async Task<MixerMaterialPageResult> PageAsync(
|
||||
int pageNo,
|
||||
int pageSize,
|
||||
string? materialCode = null,
|
||||
string? materialName = null,
|
||||
string? erpCode = null,
|
||||
string? majorCategoryId = null,
|
||||
string? minorCategoryId = null,
|
||||
private async Task LoadCacheAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_cacheFilePath)) return;
|
||||
var json = await File.ReadAllTextAsync(_cacheFilePath).ConfigureAwait(false);
|
||||
var list = JsonSerializer.Deserialize<List<MesMixerMaterial>>(json, _jsonOpts);
|
||||
if (list != null)
|
||||
lock (_cacheLock) { _localCache = list; }
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 加载本地缓存失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveCacheAsync(List<MesMixerMaterial> list)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(list, _jsonOpts);
|
||||
await File.WriteAllTextAsync(_cacheFilePath, json).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 保存本地缓存失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SyncFromRemoteAsync(CancellationToken ct = default)
|
||||
{
|
||||
if (!await _syncLock.WaitAsync(0, ct).ConfigureAwait(false)) return;
|
||||
try
|
||||
{
|
||||
var all = new List<MesMixerMaterial>();
|
||||
var pageNo = 1;
|
||||
const int pageSize = 500;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var qs = HttpUtility.ParseQueryString(string.Empty);
|
||||
qs["pageNo"] = pageNo.ToString();
|
||||
qs["pageSize"] = pageSize.ToString();
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/list?{qs}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
if (!resp.IsSuccessStatusCode) break;
|
||||
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (!doc.RootElement.TryGetProperty("result", out var resultEl)) break;
|
||||
var records = resultEl.TryGetProperty("records", out var recEl)
|
||||
? recEl.Deserialize<List<MesMixerMaterial>>(_jsonOpts) ?? []
|
||||
: [];
|
||||
|
||||
all.AddRange(records);
|
||||
if (records.Count < pageSize) break;
|
||||
pageNo++;
|
||||
}
|
||||
|
||||
if (all.Count > 0)
|
||||
{
|
||||
lock (_cacheLock) { _localCache = all; }
|
||||
await SaveCacheAsync(all).ConfigureAwait(false);
|
||||
_logger.Information($"[密炼物料] 同步完成,共 {all.Count} 条");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 远程同步失败:{ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_syncLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public Task<MixerMaterialPageResult> PageAsync(
|
||||
int pageNo, int pageSize,
|
||||
string? materialCode = null, string? materialName = null, string? erpCode = null,
|
||||
string? majorCategoryId = null, string? minorCategoryId = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var query = HttpUtility.ParseQueryString(string.Empty);
|
||||
query["pageNo"] = pageNo.ToString();
|
||||
query["pageSize"] = pageSize.ToString();
|
||||
if (!string.IsNullOrWhiteSpace(materialCode)) query["materialCode"] = materialCode;
|
||||
if (!string.IsNullOrWhiteSpace(materialName)) query["materialName"] = materialName;
|
||||
if (!string.IsNullOrWhiteSpace(erpCode)) query["erpCode"] = erpCode;
|
||||
if (!string.IsNullOrWhiteSpace(majorCategoryId)) query["majorCategoryId"] = majorCategoryId;
|
||||
if (!string.IsNullOrWhiteSpace(minorCategoryId)) query["minorCategoryId"] = minorCategoryId;
|
||||
List<MesMixerMaterial> cache;
|
||||
lock (_cacheLock) { cache = _localCache; }
|
||||
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/list?{query}";
|
||||
if (cache.Count > 0)
|
||||
return Task.FromResult(PageFromCache(cache, pageNo, pageSize,
|
||||
materialCode, materialName, erpCode, majorCategoryId, minorCategoryId));
|
||||
|
||||
return PageFromRemoteAsync(pageNo, pageSize,
|
||||
materialCode, materialName, erpCode, majorCategoryId, minorCategoryId, ct);
|
||||
}
|
||||
|
||||
private static MixerMaterialPageResult PageFromCache(
|
||||
List<MesMixerMaterial> cache, int pageNo, int pageSize,
|
||||
string? materialCode, string? materialName, string? erpCode,
|
||||
string? majorCategoryId, string? minorCategoryId)
|
||||
{
|
||||
var q = cache.AsEnumerable();
|
||||
if (!string.IsNullOrWhiteSpace(materialCode))
|
||||
q = q.Where(x => x.MaterialCode?.Contains(materialCode, StringComparison.OrdinalIgnoreCase) == true);
|
||||
if (!string.IsNullOrWhiteSpace(materialName))
|
||||
q = q.Where(x => x.MaterialName?.Contains(materialName, StringComparison.OrdinalIgnoreCase) == true);
|
||||
if (!string.IsNullOrWhiteSpace(erpCode))
|
||||
q = q.Where(x => x.ErpCode?.Contains(erpCode, StringComparison.OrdinalIgnoreCase) == true);
|
||||
if (!string.IsNullOrWhiteSpace(majorCategoryId))
|
||||
q = q.Where(x => x.MajorCategoryId == majorCategoryId);
|
||||
if (!string.IsNullOrWhiteSpace(minorCategoryId))
|
||||
q = q.Where(x => x.MinorCategoryId == minorCategoryId);
|
||||
|
||||
var filtered = q.ToList();
|
||||
var records = filtered.Skip((pageNo - 1) * pageSize).Take(pageSize).ToList();
|
||||
return new MixerMaterialPageResult(records, filtered.Count, pageNo, pageSize);
|
||||
}
|
||||
|
||||
private async Task<MixerMaterialPageResult> PageFromRemoteAsync(
|
||||
int pageNo, int pageSize,
|
||||
string? materialCode, string? materialName, string? erpCode,
|
||||
string? majorCategoryId, string? minorCategoryId,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var qs = HttpUtility.ParseQueryString(string.Empty);
|
||||
qs["pageNo"] = pageNo.ToString();
|
||||
qs["pageSize"] = pageSize.ToString();
|
||||
if (!string.IsNullOrWhiteSpace(materialCode)) qs["materialCode"] = materialCode;
|
||||
if (!string.IsNullOrWhiteSpace(materialName)) qs["materialName"] = materialName;
|
||||
if (!string.IsNullOrWhiteSpace(erpCode)) qs["erpCode"] = erpCode;
|
||||
if (!string.IsNullOrWhiteSpace(majorCategoryId)) qs["majorCategoryId"] = majorCategoryId;
|
||||
if (!string.IsNullOrWhiteSpace(minorCategoryId)) qs["minorCategoryId"] = minorCategoryId;
|
||||
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/list?{qs}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
resp.EnsureSuccessStatusCode();
|
||||
@@ -63,13 +192,18 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var result = doc.RootElement.GetProperty("result");
|
||||
var records = result.GetProperty("records").Deserialize<List<MesMixerMaterial>>(_jsonOpts) ?? new();
|
||||
var records = result.GetProperty("records").Deserialize<List<MesMixerMaterial>>(_jsonOpts) ?? [];
|
||||
var total = result.TryGetProperty("total", out var totalEl) ? totalEl.GetInt64() : records.Count;
|
||||
return new MixerMaterialPageResult(records, total, pageNo, pageSize);
|
||||
}
|
||||
|
||||
public async Task<MesMixerMaterial?> GetByIdAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
List<MesMixerMaterial> cache;
|
||||
lock (_cacheLock) { cache = _localCache; }
|
||||
var local = cache.FirstOrDefault(x => x.Id == id);
|
||||
if (local != null) return local;
|
||||
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/queryById?id={Uri.EscapeDataString(id)}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
@@ -105,43 +239,75 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
|
||||
public async Task<List<KeyValuePair<string, string>>> GetMajorCategoryOptionsAsync(CancellationToken ct = default)
|
||||
{
|
||||
var url = $"{BaseUrl}/sys/category/anon/loadTreeData?pcode=XSLMES_MATERIAL";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
if (!resp.IsSuccessStatusCode) return [];
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var arr = doc.RootElement.TryGetProperty("result", out var resultEl) ? resultEl : doc.RootElement;
|
||||
return ParseTreeOptions(arr);
|
||||
try
|
||||
{
|
||||
var root = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => x.Code == "XSLMES_MATERIAL").FirstAsync();
|
||||
if (root == null) return [];
|
||||
|
||||
var majors = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => x.Pid == root.Id).OrderBy(x => x.Code).ToListAsync();
|
||||
|
||||
return majors.Where(x => !string.IsNullOrWhiteSpace(x.Id))
|
||||
.Select(x => new KeyValuePair<string, string>(x.Name ?? x.Code ?? x.Id, x.Id))
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 加载大类选项失败:{ex.Message}");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<KeyValuePair<string, string>>> GetMinorCategoryOptionsAsync(string majorCategoryId, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(majorCategoryId)) return [];
|
||||
var url = $"{BaseUrl}/sys/category/anon/loadTreeData?pid={Uri.EscapeDataString(majorCategoryId)}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
if (!resp.IsSuccessStatusCode) return [];
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var arr = doc.RootElement.TryGetProperty("result", out var resultEl) ? resultEl : doc.RootElement;
|
||||
return ParseTreeOptions(arr);
|
||||
try
|
||||
{
|
||||
var minors = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => x.Pid == majorCategoryId).OrderBy(x => x.Code).ToListAsync();
|
||||
|
||||
return minors.Where(x => !string.IsNullOrWhiteSpace(x.Id))
|
||||
.Select(x => new KeyValuePair<string, string>(x.Name ?? x.Code ?? x.Id, x.Id))
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 加载小类选项失败:{ex.Message}");
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<string, string>> ParseTreeOptions(JsonElement root)
|
||||
public async Task<List<MaterialCategoryNode>> GetMaterialCategoryTreeAsync(CancellationToken ct = default)
|
||||
{
|
||||
var list = new List<KeyValuePair<string, string>>();
|
||||
if (root.ValueKind != JsonValueKind.Array) return list;
|
||||
foreach (var item in root.EnumerateArray())
|
||||
try
|
||||
{
|
||||
var text = item.TryGetProperty("title", out var titleEl) ? titleEl.GetString() : null;
|
||||
var key = item.TryGetProperty("key", out var keyEl) ? keyEl.GetString() : null;
|
||||
if (!string.IsNullOrWhiteSpace(text) && !string.IsNullOrWhiteSpace(key))
|
||||
var root = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => x.Code == "XSLMES_MATERIAL").FirstAsync();
|
||||
if (root == null) return [];
|
||||
|
||||
var majors = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => x.Pid == root.Id).OrderBy(x => x.Code).ToListAsync();
|
||||
if (majors.Count == 0) return [];
|
||||
|
||||
var majorIds = majors.Select(x => x.Id).ToList();
|
||||
var allMinors = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter().Where(x => majorIds.Contains(x.Pid!)).OrderBy(x => x.Code).ToListAsync();
|
||||
|
||||
return majors.Select(major =>
|
||||
{
|
||||
list.Add(new KeyValuePair<string, string>(text!, key!));
|
||||
}
|
||||
var minorNodes = allMinors
|
||||
.Where(m => m.Pid == major.Id)
|
||||
.Select(m => new MaterialCategoryNode(m.Id, m.Name, m.Code, []))
|
||||
.ToList();
|
||||
return new MaterialCategoryNode(major.Id, major.Name, major.Code, minorNodes);
|
||||
}).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料] 加载分类树失败:{ex.Message}");
|
||||
return [];
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private async Task<bool> PostJsonAsync(string url, object body, CancellationToken ct)
|
||||
@@ -158,32 +324,20 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
{
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (doc.RootElement.TryGetProperty("code", out var code))
|
||||
{
|
||||
return code.GetInt32() == 200;
|
||||
}
|
||||
if (doc.RootElement.TryGetProperty("success", out var success))
|
||||
{
|
||||
return success.GetBoolean();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (doc.RootElement.TryGetProperty("code", out var code)) return code.GetInt32() == 200;
|
||||
if (doc.RootElement.TryGetProperty("success", out var success)) return success.GetBoolean();
|
||||
return true;
|
||||
}
|
||||
catch { return true; }
|
||||
}
|
||||
|
||||
private sealed class NullableDateTimeJsonConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
private static readonly string[] SupportedFormats =
|
||||
private static readonly string[] Formats =
|
||||
[
|
||||
"yyyy-MM-dd HH:mm:ss",
|
||||
"yyyy-MM-dd HH:mm:ss.fff",
|
||||
"yyyy-MM-ddTHH:mm:ss",
|
||||
"yyyy-MM-ddTHH:mm:ss.fff",
|
||||
"yyyy-MM-ddTHH:mm:ssZ",
|
||||
"yyyy-MM-ddTHH:mm:ss.fffZ"
|
||||
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.fff",
|
||||
"yyyy-MM-ddTHH:mm:ss", "yyyy-MM-ddTHH:mm:ss.fff",
|
||||
"yyyy-MM-ddTHH:mm:ssZ", "yyyy-MM-ddTHH:mm:ss.fffZ"
|
||||
];
|
||||
|
||||
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
@@ -193,14 +347,9 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
{
|
||||
var raw = reader.GetString();
|
||||
if (string.IsNullOrWhiteSpace(raw)) return null;
|
||||
if (DateTime.TryParseExact(raw, SupportedFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out var exact))
|
||||
{
|
||||
return exact;
|
||||
}
|
||||
if (DateTime.TryParse(raw, out var fallback))
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
if (DateTime.TryParseExact(raw, Formats, System.Globalization.CultureInfo.InvariantCulture,
|
||||
System.Globalization.DateTimeStyles.AssumeLocal, out var exact)) return exact;
|
||||
if (DateTime.TryParse(raw, out var fallback)) return fallback;
|
||||
}
|
||||
throw new JsonException($"无法将 JSON 值转换为 DateTime?,token={reader.TokenType}");
|
||||
}
|
||||
@@ -212,4 +361,3 @@ public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user