using CnasSynchronusClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CNAS_Authorize
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private MachineAuth m_machines;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
m_machines = new MachineAuth();
m_machines.Load();
dataGrid.ItemsSource = m_machines.Page;
this.DataContext = m_machines;
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.Width < 500) return;
dataGrid.Width = this.Width - 48;
dataGrid.Height = centerPanel.Height;
//设置列宽
double colWidth = this.Width - 60;// centerPanel.Width;
for (int i = 1; i < dataGrid.Columns.Count; i++)
{
colWidth = colWidth - dataGrid.Columns[i].Width.Value;
}
if (colWidth > 0)
dataGrid.Columns[0].Width = colWidth;
edtMachine.Width = this.Width - lbMachine.Width - btnQuery.Width - 48;
edtFactory.Width = edtMachine.Width;
}
private void refreshBind()
{
this.DataContext = null;
this.DataContext = m_machines;
}
private void PrevPage_Click(object sender, RoutedEventArgs e)
{
bool ok;
List page = m_machines.PrevPage(out ok);
if (ok)
{
dataGrid.ItemsSource = page;
refreshBind();
}
}
private void NextPage_Click(object sender, RoutedEventArgs e)
{
bool ok;
List page = m_machines.NextPage(out ok);
if (ok)
{
dataGrid.ItemsSource = page;
refreshBind();
}
}
private void Query_Click(object sender, RoutedEventArgs e)
{
m_machines.ChangeFilter(edtMachine.Text.Trim(), edtFactory.Text.Trim());
dataGrid.ItemsSource = m_machines.Page;
refreshBind();
}
private void ConfirmImport_Click(object sender, RoutedEventArgs e)
{
//MySQLHelper.ExecuteNonQuery(strInsertSql, parameters);
//int iReturn = CnasDataOperationFact.CnasDataOperation().InsertDataToCNASTable(ConvertDataRowToTable(drNewTarget), targetdataBase, banlanceitem.syncParamasInfos, banlanceitem.CnasInstrumentColumn, banlanceitem.lstFixedValue);
//if (iReturn <= 0) //此时出现问题
//{
// if (iReturn == -1)
// {
// AppLog.ServiceInfo("数据库连接中断,终止本次上传。");
// break; //此时数据库连接中断,直接跳出循环,结束本次数据同步传输
// }
// else if (iReturn == -2) //等于-2表示插入准备更新时发现数据一致,不再执行
// {
// OtherCount++;
// }
// else
// {
// ErrorCount++;
// /*需要转换一下,否则会将整个dt全部写入日志*/
// lstError.Add(GlobalCommonOperation.ConvertDataRowToTable(drNewTarget).Rows[0]);
// }
// bSuccess = false;
//}
}
private void DownloadActivationCode_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var machine = button.DataContext as Machine;
if (machine != null)
{
try
{
// 生成激活码
string activationCode = GenerateActivationCode(machine.MachineCode);
// 创建保存文件对话框
var saveFileDialog = new Microsoft.Win32.SaveFileDialog
{
FileName = $"激活码_{machine.MachineCode}_{DateTime.Now:yyyyMMdd}",
DefaultExt = ".txt",
Filter = "文本文件|*.txt"
};
if (saveFileDialog.ShowDialog() == true)
{
// 将激活码写入文件
System.IO.File.WriteAllText(saveFileDialog.FileName, activationCode);
MessageBox.Show("激活码已成功保存!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
// 更新状态
machine.IsGenerated = true;
refreshBind();
}
}
catch (Exception ex)
{
MessageBox.Show($"下载激活码失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private string GenerateActivationCode(string machineCode)
{
// 这里实现激活码生成算法
// 示例:简单的加密算法,实际应用中应该使用更安全的加密方式
using (var md5 = System.Security.Cryptography.MD5.Create())
{
// 加入时间戳使得生成的激活码具有时效性
string input = machineCode + DateTime.Now.ToString("yyyyMMdd");
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// 转换为16进制字符串
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
// 每4个字符添加一个分隔符,提高可读性
if ((i + 1) % 4 == 0 && i != hashBytes.Length - 1)
{
sb.Append("-");
}
}
return sb.ToString();
}
}
}
}