更新MybatisPlusSaasConfig中的租户ID默认值为1002;在ShiroConfig中添加MES密炼物料管理和系统分类字典的免密接口;在MesMixerMaterialController中实现密炼物料信息的免密增删改查接口,并添加相应的事件广播功能;在SysCategoryController中新增分类字典的免密查询接口;更新前端导航和菜单数据以支持密炼物料信息模块。
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
namespace YY.Admin.Services.Service.Category.Dto;
|
||||
|
||||
public class JeecgCategoryItemOutput
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string? Pid { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? HasChild { get; set; }
|
||||
public DateTime? CreateTime { get; set; }
|
||||
public DateTime? UpdateTime { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace YY.Admin.Services.Service.Category.Dto;
|
||||
|
||||
public class JeecgCategoryTreeNodeDto
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string? Pid { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public List<JeecgCategoryTreeNodeDto> Children { get; set; } = [];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace YY.Admin.Services.Service.Category.Dto;
|
||||
|
||||
public class PageJeecgCategoryItemInput
|
||||
{
|
||||
public int Page { get; set; } = 1;
|
||||
public int PageSize { get; set; } = 20;
|
||||
public string? Name { get; set; }
|
||||
public string? Code { get; set; }
|
||||
public string? Pid { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
using YY.Admin.Services.Service.Category.Dto;
|
||||
|
||||
namespace YY.Admin.Services.Service.Category;
|
||||
|
||||
public interface IJeecgCategorySyncService
|
||||
{
|
||||
Task<SqlSugarPagedList<JeecgCategoryItemOutput>> PageAsync(PageJeecgCategoryItemInput input);
|
||||
Task<int> SyncFromJeecgAsync();
|
||||
Task<List<JeecgCategoryTreeNodeDto>> LoadTreeAsync();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using SqlSugar;
|
||||
using System.Text.Json;
|
||||
using YY.Admin.Core;
|
||||
using YY.Admin.Core.Entity;
|
||||
using YY.Admin.Services.Service.Category.Dto;
|
||||
using YY.Admin.Services.Service.Jeecg;
|
||||
|
||||
namespace YY.Admin.Services.Service.Category;
|
||||
|
||||
public class JeecgCategorySyncService : IJeecgCategorySyncService, ISingletonDependency
|
||||
{
|
||||
private readonly ISqlSugarClient _dbContext;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IJeecgBackendGateway _jeecgGateway;
|
||||
|
||||
public JeecgCategorySyncService(
|
||||
ISqlSugarClient dbContext,
|
||||
IConfiguration configuration,
|
||||
IJeecgBackendGateway jeecgGateway)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_configuration = configuration;
|
||||
_jeecgGateway = jeecgGateway;
|
||||
}
|
||||
|
||||
public async Task<SqlSugarPagedList<JeecgCategoryItemOutput>> PageAsync(PageJeecgCategoryItemInput input)
|
||||
{
|
||||
var name = input.Name;
|
||||
var code = input.Code;
|
||||
var pid = input.Pid;
|
||||
var query = _dbContext.Queryable<JeecgSysCategoryItem>().ClearFilter()
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(name), x => x.Name != null && x.Name.Contains(name!))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(code), x => x.Code != null && x.Code.Contains(code!))
|
||||
.WhereIF(!string.IsNullOrWhiteSpace(pid), x => x.Pid == pid);
|
||||
|
||||
query = query.OrderBy(x => SqlFunc.IsNull(x.Code, ""))
|
||||
.OrderBy(x => SqlFunc.IsNull(x.Name, ""))
|
||||
.OrderBy(x => SqlFunc.Desc(x.UpdateTime));
|
||||
|
||||
RefAsync<int> total = 0;
|
||||
var list = await query.ToPageListAsync(input.Page, input.PageSize, total);
|
||||
var items = list.Select(x => new JeecgCategoryItemOutput
|
||||
{
|
||||
Id = x.Id,
|
||||
Pid = x.Pid,
|
||||
Name = x.Name,
|
||||
Code = x.Code,
|
||||
HasChild = x.HasChild,
|
||||
CreateTime = x.CreateTime,
|
||||
UpdateTime = x.UpdateTime
|
||||
}).ToList();
|
||||
|
||||
return new SqlSugarPagedList<JeecgCategoryItemOutput>
|
||||
{
|
||||
Page = input.Page,
|
||||
PageSize = input.PageSize,
|
||||
Total = total,
|
||||
TotalPages = input.PageSize > 0 ? (int)Math.Ceiling(total / (double)input.PageSize) : 0,
|
||||
HasNextPage = input.PageSize > 0 && input.Page < (int)Math.Ceiling(total / (double)input.PageSize),
|
||||
HasPrevPage = input.Page > 1,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<int> SyncFromJeecgAsync()
|
||||
{
|
||||
var baseUrl = _configuration.GetValue<string>("JeecgIntegration:BaseUrl")?.TrimEnd('/');
|
||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var queue = new Queue<string>();
|
||||
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
queue.Enqueue("0");
|
||||
visited.Add("0");
|
||||
var synced = 0;
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
var pid = queue.Dequeue();
|
||||
var requestUrl = $"{baseUrl}/sys/category/anon/childList?pid={Uri.EscapeDataString(pid)}";
|
||||
var json = await _jeecgGateway.ExecuteGetStringAsync(requestUrl);
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
if (!(root.TryGetProperty("success", out var successEl) && successEl.GetBoolean()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!root.TryGetProperty("result", out var listEl) || listEl.ValueKind != JsonValueKind.Array)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var row in listEl.EnumerateArray())
|
||||
{
|
||||
var id = GetString(row, "id");
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var existing = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter()
|
||||
.Where(x => x.Id == id)
|
||||
.FirstAsync();
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
existing = new JeecgSysCategoryItem { Id = id };
|
||||
}
|
||||
|
||||
existing.Pid = GetString(row, "pid");
|
||||
existing.Name = GetString(row, "name");
|
||||
existing.Code = GetString(row, "code");
|
||||
existing.HasChild = GetString(row, "hasChild");
|
||||
existing.CreateBy = GetString(row, "createBy");
|
||||
existing.CreateTime = GetDateTime(row, "createTime");
|
||||
existing.UpdateBy = GetString(row, "updateBy");
|
||||
existing.UpdateTime = GetDateTime(row, "updateTime");
|
||||
|
||||
var existsCount = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter()
|
||||
.Where(x => x.Id == existing.Id)
|
||||
.CountAsync();
|
||||
if (existsCount > 0)
|
||||
{
|
||||
await _dbContext.Updateable(existing).ExecuteCommandAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
await _dbContext.Insertable(existing).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
synced++;
|
||||
|
||||
if (existing.HasChild == "1" && !visited.Contains(existing.Id))
|
||||
{
|
||||
queue.Enqueue(existing.Id);
|
||||
visited.Add(existing.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return synced;
|
||||
}
|
||||
|
||||
public async Task<List<JeecgCategoryTreeNodeDto>> LoadTreeAsync()
|
||||
{
|
||||
return await BuildLocalTreeAsync();
|
||||
}
|
||||
|
||||
private async Task<List<JeecgCategoryTreeNodeDto>> BuildLocalTreeAsync()
|
||||
{
|
||||
var allItems = await _dbContext.Queryable<JeecgSysCategoryItem>()
|
||||
.ClearFilter()
|
||||
.ToListAsync();
|
||||
|
||||
if (allItems == null || allItems.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
var nodeMap = allItems
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x.Id))
|
||||
.ToDictionary(
|
||||
x => x.Id,
|
||||
x => new JeecgCategoryTreeNodeDto
|
||||
{
|
||||
Id = x.Id,
|
||||
Pid = x.Pid,
|
||||
Name = x.Name,
|
||||
Code = x.Code
|
||||
},
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var roots = new List<JeecgCategoryTreeNodeDto>();
|
||||
foreach (var node in nodeMap.Values)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(node.Pid) || node.Pid == "0" || !nodeMap.TryGetValue(node.Pid, out var parent))
|
||||
{
|
||||
roots.Add(node);
|
||||
continue;
|
||||
}
|
||||
parent.Children.Add(node);
|
||||
}
|
||||
|
||||
SortTreeNodes(roots);
|
||||
return roots;
|
||||
}
|
||||
|
||||
private static void SortTreeNodes(List<JeecgCategoryTreeNodeDto> nodes)
|
||||
{
|
||||
nodes.Sort((a, b) =>
|
||||
{
|
||||
var codeCompare = string.Compare(a.Code, b.Code, StringComparison.OrdinalIgnoreCase);
|
||||
return codeCompare != 0 ? codeCompare : string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
if (node.Children.Count > 0)
|
||||
{
|
||||
SortTreeNodes(node.Children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string? GetString(JsonElement row, string propertyName)
|
||||
{
|
||||
if (!row.TryGetProperty(propertyName, out var el))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (el.ValueKind == JsonValueKind.String)
|
||||
{
|
||||
return el.GetString();
|
||||
}
|
||||
return el.ToString();
|
||||
}
|
||||
|
||||
private static DateTime? GetDateTime(JsonElement row, string propertyName)
|
||||
{
|
||||
if (!row.TryGetProperty(propertyName, out var el))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (el.ValueKind == JsonValueKind.String && DateTime.TryParse(el.GetString(), out var dt))
|
||||
{
|
||||
return dt;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Web;
|
||||
using YY.Admin.Core;
|
||||
using YY.Admin.Core.Entity;
|
||||
using YY.Admin.Core.Services;
|
||||
|
||||
namespace YY.Admin.Services.Service.MixerMaterial;
|
||||
|
||||
public class MixerMaterialService : IMixerMaterialService, ISingletonDependency
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILoggerService _logger;
|
||||
|
||||
private static readonly JsonSerializerOptions _jsonOpts = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Converters = { new NullableDateTimeJsonConverter() }
|
||||
};
|
||||
|
||||
public MixerMaterialService(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IConfiguration configuration,
|
||||
ILoggerService logger)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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,
|
||||
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;
|
||||
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/list?{query}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
resp.EnsureSuccessStatusCode();
|
||||
|
||||
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 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)
|
||||
{
|
||||
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);
|
||||
if (!resp.IsSuccessStatusCode) return null;
|
||||
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (!doc.RootElement.TryGetProperty("result", out var resultEl)) return null;
|
||||
return resultEl.Deserialize<MesMixerMaterial>(_jsonOpts);
|
||||
}
|
||||
|
||||
public Task<bool> AddAsync(MesMixerMaterial material, CancellationToken ct = default)
|
||||
=> PostJsonAsync($"{BaseUrl}/mes/material/mixerMaterial/anon/add", material, ct);
|
||||
|
||||
public Task<bool> EditAsync(MesMixerMaterial material, CancellationToken ct = default)
|
||||
=> PostJsonAsync($"{BaseUrl}/mes/material/mixerMaterial/anon/edit", material, ct);
|
||||
|
||||
public async Task<bool> DeleteAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/delete?id={Uri.EscapeDataString(id)}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.DeleteAsync(url, ct).ConfigureAwait(false);
|
||||
return resp.IsSuccessStatusCode && await IsSuccessResultAsync(resp, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteBatchAsync(string ids, CancellationToken ct = default)
|
||||
{
|
||||
var url = $"{BaseUrl}/mes/material/mixerMaterial/anon/deleteBatch?ids={Uri.EscapeDataString(ids)}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.DeleteAsync(url, ct).ConfigureAwait(false);
|
||||
return resp.IsSuccessStatusCode && await IsSuccessResultAsync(resp, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<string, string>> ParseTreeOptions(JsonElement root)
|
||||
{
|
||||
var list = new List<KeyValuePair<string, string>>();
|
||||
if (root.ValueKind != JsonValueKind.Array) return list;
|
||||
foreach (var item in root.EnumerateArray())
|
||||
{
|
||||
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))
|
||||
{
|
||||
list.Add(new KeyValuePair<string, string>(text!, key!));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private async Task<bool> PostJsonAsync(string url, object body, CancellationToken ct)
|
||||
{
|
||||
var content = new StringContent(JsonSerializer.Serialize(body, _jsonOpts), Encoding.UTF8, "application/json");
|
||||
using var client = CreateClient();
|
||||
var resp = await client.PostAsync(url, content, ct).ConfigureAwait(false);
|
||||
return resp.IsSuccessStatusCode && await IsSuccessResultAsync(resp, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static async Task<bool> IsSuccessResultAsync(HttpResponseMessage resp, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
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
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NullableDateTimeJsonConverter : JsonConverter<DateTime?>
|
||||
{
|
||||
private static readonly string[] SupportedFormats =
|
||||
[
|
||||
"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)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null) return null;
|
||||
if (reader.TokenType == JsonTokenType.String)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
throw new JsonException($"无法将 JSON 值转换为 DateTime?,token={reader.TokenType}");
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
||||
{
|
||||
if (value.HasValue) writer.WriteStringValue(value.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
else writer.WriteNullValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using Prism.Events;
|
||||
using System.Text.Json;
|
||||
using YY.Admin.Core;
|
||||
using YY.Admin.Core.Events;
|
||||
using YY.Admin.Core.Services;
|
||||
|
||||
namespace YY.Admin.Services.Service.MixerMaterial;
|
||||
|
||||
public class MixerMaterialSyncCoordinator : ISingletonDependency
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly ILoggerService _logger;
|
||||
|
||||
public MixerMaterialSyncCoordinator(IEventAggregator eventAggregator, ILoggerService logger)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
_logger = logger;
|
||||
_eventAggregator.GetEvent<RemoteCommandReceivedEvent>()
|
||||
.Subscribe(OnRemoteCommand, ThreadOption.BackgroundThread);
|
||||
_eventAggregator.GetEvent<NetworkStatusChangedEvent>()
|
||||
.Subscribe(OnNetworkStatusChanged, ThreadOption.BackgroundThread);
|
||||
_logger.Information("[密炼物料推送] MixerMaterialSyncCoordinator 已启动");
|
||||
}
|
||||
|
||||
private void OnNetworkStatusChanged(NetworkStatusChangedPayload payload)
|
||||
{
|
||||
if (!payload.IsOnline) return;
|
||||
_eventAggregator.GetEvent<MixerMaterialChangedEvent>()
|
||||
.Publish(new MixerMaterialChangedPayload { Action = "reconnect" });
|
||||
}
|
||||
|
||||
private void OnRemoteCommand(RemoteCommandPayload payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = payload.CommandJson ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(json)) return;
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (!doc.RootElement.TryGetProperty("cmd", out var cmdEl)) return;
|
||||
if (!cmdEl.GetString().Equals("MIXER_MATERIAL_CHANGED", StringComparison.OrdinalIgnoreCase)) return;
|
||||
|
||||
doc.RootElement.TryGetProperty("action", out var actionEl);
|
||||
doc.RootElement.TryGetProperty("mixerMaterialId", out var idEl);
|
||||
if (idEl.ValueKind != JsonValueKind.String && doc.RootElement.TryGetProperty("id", out var id2El))
|
||||
{
|
||||
idEl = id2El;
|
||||
}
|
||||
|
||||
var changed = new MixerMaterialChangedPayload
|
||||
{
|
||||
Action = actionEl.GetString() ?? string.Empty,
|
||||
MixerMaterialId = idEl.ValueKind == JsonValueKind.String ? idEl.GetString() : null
|
||||
};
|
||||
_eventAggregator.GetEvent<MixerMaterialChangedEvent>().Publish(changed);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[密炼物料推送] 处理STOMP命令失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user