using HMI.UI.Util;
|
using Panuon.WPF.UI;
|
using Plugin.DeviceConfig.Entity;
|
using Plugin.DeviceConfig.Model;
|
using System;
|
using System.Collections.ObjectModel;
|
using System.Linq;
|
using System.Windows;
|
|
namespace Plugin.DeviceConfig
|
{
|
/// <summary>
|
/// VersionManageModal.xaml 的交互逻辑
|
/// </summary>
|
public partial class VersionManageModal : WindowX
|
{
|
private DeviceConfigModel _model;
|
private ObservableCollection<VersionDisplayItem> _versions = new();
|
private bool _changed;
|
|
public VersionManageModal()
|
{
|
DataContext = this;
|
InitializeComponent();
|
}
|
|
/// <summary>
|
/// 打开弹窗
|
/// </summary>
|
public bool? Open(DeviceConfigModel model)
|
{
|
_model = model;
|
versionListView.ItemsSource = _versions;
|
LoadVersions();
|
return ShowDialog();
|
}
|
|
private void LoadVersions()
|
{
|
try
|
{
|
var list = _model.GetConfigVersions();
|
_versions.Clear();
|
foreach (var v in list)
|
{
|
_versions.Add(new VersionDisplayItem
|
{
|
VersionId = v.VersionId,
|
Status = v.Status,
|
Hash = v.Hash,
|
HashShort = v.Hash.Length > 16 ? v.Hash.Substring(0, 16) + "..." : v.Hash,
|
Author = v.Author,
|
Timestamp = v.Timestamp,
|
DiffDescription = v.DiffDescription,
|
});
|
}
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error($"加载版本列表失败: {ex.Message}");
|
}
|
}
|
|
#region 操作
|
|
private void PublishBtn_Click(object sender, RoutedEventArgs e)
|
{
|
try
|
{
|
var selected = versionListView.SelectedItem as VersionDisplayItem;
|
if (selected == null)
|
{
|
ToastHelper.Warning("请先选中一个版本");
|
return;
|
}
|
if (selected.Status != VersionStatus.Draft)
|
{
|
ToastHelper.Warning("只有 Draft 状态的版本可以发布");
|
return;
|
}
|
|
if (MsgBoxHelper.WarnMessage(this, $"确定要发布版本 [{selected.VersionId}] 吗?") != MessageBoxResult.OK)
|
return;
|
|
// 计算最新 Hash
|
var hash = _model.ComputeConfigHash();
|
selected.Hash = hash;
|
selected.HashShort = hash.Length > 16 ? hash.Substring(0, 16) + "..." : hash;
|
selected.Status = VersionStatus.Published;
|
selected.Timestamp = DateTime.Now;
|
|
var ver = new DeviceConfigVersion
|
{
|
VersionId = selected.VersionId,
|
Status = VersionStatus.Published,
|
Hash = hash,
|
Author = selected.Author,
|
Timestamp = DateTime.Now,
|
DiffDescription = selected.DiffDescription,
|
};
|
_model.SaveConfigVersion(ver);
|
|
_changed = true;
|
LoadVersions();
|
ToastHelper.Success($"版本 [{selected.VersionId}] 已发布");
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error($"发布失败: {ex.Message}");
|
}
|
}
|
|
private void ApplyBtn_Click(object sender, RoutedEventArgs e)
|
{
|
try
|
{
|
var selected = versionListView.SelectedItem as VersionDisplayItem;
|
if (selected == null)
|
{
|
ToastHelper.Warning("请先选中一个版本");
|
return;
|
}
|
if (selected.Status != VersionStatus.Published)
|
{
|
ToastHelper.Warning("只有 Published 状态的版本可以应用");
|
return;
|
}
|
|
// 检查当前 Effective 版本
|
var currentEffective = _versions.FirstOrDefault(v => v.Status == VersionStatus.Effective);
|
if (currentEffective != null)
|
{
|
if (MsgBoxHelper.WarnMessage(this,
|
$"当前已有生效版本 [{currentEffective.VersionId}],应用新版本将替换它。\n是否继续?") != MessageBoxResult.OK)
|
{
|
return;
|
}
|
|
// 将原 Effective 改回 Published
|
_model.UpdateVersionStatus(currentEffective.VersionId, VersionStatus.Published.ToString());
|
}
|
|
if (MsgBoxHelper.WarnMessage(this, $"确定要将 [{selected.VersionId}] 设为生效版本吗?") != MessageBoxResult.OK)
|
return;
|
|
_model.UpdateVersionStatus(selected.VersionId, VersionStatus.Effective.ToString());
|
_changed = true;
|
LoadVersions();
|
ToastHelper.Success($"版本 [{selected.VersionId}] 已设为生效版本");
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error($"应用版本失败: {ex.Message}");
|
}
|
}
|
|
private void DetailBtn_Click(object sender, RoutedEventArgs e)
|
{
|
try
|
{
|
var selected = versionListView.SelectedItem as VersionDisplayItem;
|
if (selected == null)
|
{
|
ToastHelper.Warning("请先选中一个版本");
|
return;
|
}
|
|
tbDetailHash.Text = selected.Hash;
|
tbDetailDiff.Text = selected.DiffDescription;
|
tbDetailAuthor.Text = $"{selected.Author} @ {selected.Timestamp:yyyy-MM-dd HH:mm:ss}";
|
detailPanel.Visibility = Visibility.Visible;
|
}
|
catch (Exception ex)
|
{
|
ToastHelper.Error($"加载详情失败: {ex.Message}");
|
}
|
}
|
|
private void RfhBtn_Click(object sender, RoutedEventArgs e)
|
{
|
LoadVersions();
|
ToastHelper.Success("已刷新");
|
}
|
|
private void CloseBtn_Click(object sender, RoutedEventArgs e)
|
{
|
DialogResult = _changed;
|
Close();
|
}
|
|
#endregion
|
}
|
|
/// <summary>
|
/// 版本显示项
|
/// </summary>
|
public class VersionDisplayItem
|
{
|
public string VersionId { get; set; } = string.Empty;
|
public VersionStatus Status { get; set; }
|
public string Hash { get; set; } = string.Empty;
|
public string HashShort { get; set; } = string.Empty;
|
public string Author { get; set; } = string.Empty;
|
public DateTime Timestamp { get; set; }
|
public string DiffDescription { get; set; } = string.Empty;
|
}
|
}
|