using System;
using System.Collections.Generic;
using System.Data;
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;
using System.Data.SQLite;
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)
{
this.LoadData();
}
private void LoadData()
{
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)
{
string macAddress = edtMachine.Text.Trim();
string plantCode = edtFactory.Text.Trim();
if (string.IsNullOrEmpty(macAddress) || string.IsNullOrEmpty(plantCode))
{
MessageBox.Show("MAC地址和工厂代码不能为空!", "提示");
return;
}
string connectionString = "Data Source=|DataDirectory|\\CNAS.db;Version=3;";
using (var connection = new SQLiteConnection(connectionString))
{
try
{
connection.Open();
// 检查是否已存在
using (var checkCommand = new SQLiteCommand(connection))
{
checkCommand.CommandText = "SELECT COUNT(*) FROM macaddress WHERE MAC_ADDRESS = @mac";
checkCommand.Parameters.AddWithValue("@mac", macAddress);
int count = Convert.ToInt32(checkCommand.ExecuteScalar());
if (count > 0)
{
MessageBox.Show("已经导入!", "提示");
return;
}
}
// 插入新记录
using (var insertCommand = new SQLiteCommand(connection))
{
insertCommand.CommandText = @"
INSERT INTO macaddress (
MAC_ADDRESS,
PLANTCODE,
CREATETIME,
FACILITY_STYLE
) VALUES (
@mac,
@plant,
@time,
@style
)";
insertCommand.Parameters.AddWithValue("@mac", macAddress);
insertCommand.Parameters.AddWithValue("@plant", plantCode);
insertCommand.Parameters.AddWithValue("@time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
insertCommand.Parameters.AddWithValue("@style", "999");
insertCommand.ExecuteNonQuery();
//AppLog.ServiceInfo("导入成功");
MessageBox.Show("导入成功...", "提示");
this.LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show($"导入失败: {ex.Message}", "提示");
//AppLog.ServiceError($"导入失败: {ex.Message}");
}
}
}
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();
}
}
}
}