using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
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 XCommon.Log;
using XImagingXhandler.XDAL;
using Microsoft.Win32;
using XCore;
using XHandler.Class;
using HandyControl.Data;
using XCommon.Tip;
using System.CodeDom.Compiler;
using System.IO;
namespace XHandler.View.MethodProperty
{
///
/// VariableImportProperty.xaml 的交互逻辑
///
public partial class VariableImportProperty : UserControl
{
public MethodVariableImport methodVariableImport { get; set; }
public MethodVariableImport currmethodVariableImport = null;
#region 全局属性变量
string isrun = "";
string status = "";
string name = "";
string label = "";
string filePath = "";
DataTable transferDataTable = new DataTable();
int mark = 0;
#endregion
public VariableImportProperty()
{
InitializeComponent();
}
public VariableImportProperty(MethodEx method)
{
InitializeComponent();
mark = 0;
methodVariableImport = new MethodVariableImport();
methodVariableImport.name = method.method_name;
methodVariableImport.label = method.method_name;
this.DataContext = methodVariableImport;
if (method.tag != null)
{
methodVariableImport = (MethodVariableImport)method.tag;
currmethodVariableImport = methodVariableImport;
isrun = methodVariableImport.isrun;
status = methodVariableImport.status;
name = methodVariableImport.name;
label = methodVariableImport.label;
filePath = methodVariableImport.filePath;
transferDataTable = methodVariableImport.transferDataTable;
}
}
private void tbxCommandName_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (string.IsNullOrEmpty(tbxCommandName.Text))
{
xcError.Text = "命令名称不能为空";
return;
}
else
{
xcError.Text = "";
}
if (methodVariableImport != null)
{
methodVariableImport.name = tbxCommandName.Text;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void btnSelectFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "请选择Excel文件";
dlg.FileName = string.Empty;
dlg.FilterIndex = 1;
dlg.RestoreDirectory = true;
dlg.Filter = "excel files(*.xls)|*.xls|csv files(*.csv)|*.csv|All files(*.*)|*.*";
dlg.Multiselect = false;
if (!string.IsNullOrEmpty(tbExcelFile.Text.Trim()))
{
try
{
if (System.IO.Path.HasExtension(tbExcelFile.Text.Trim()))
dlg.InitialDirectory = System.IO.Path.GetDirectoryName(tbExcelFile.Text.Trim());
else
dlg.InitialDirectory = tbExcelFile.Text.Trim();
}
catch (Exception ex)
{
}
}
if (dlg.ShowDialog() == true)
{
string fullPath = dlg.FileName;
tbExcelFile.Text = fullPath;
if (methodVariableImport != null)
{
methodVariableImport.filePath = fullPath;
}
SelectFile(fullPath);
}
}
private void SelectFile(string file)
{
string ext = System.IO.Path.GetExtension(file);
DataTable transferData = null;
if (string.Compare(ext, ".csv", true) == 0)
transferData = ExcelAndCsvHelper.GetDataTableFromCsvFile(file);
else
transferData = ExcelAndCsvHelper.GetDataTableFromExcelFile(file, true);
if (transferData != null)
{
//判断表头是否符合要求
if(transferData.Columns.Contains("paraName")&& transferData.Columns.Contains("paraValue"))
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
foreach (DataRow dataRow in transferData.Rows)
{
if (provider.IsValidIdentifier(dataRow["paraName"].ToString()))
{
}
else
{
ShowTip.ShowNotice(string.Format("{0}{1}{2}", "提醒", Environment.NewLine, "表格内容列'" + dataRow["paraName"].ToString() + "'不符合规范,未能加载"), InfoType.Warning);
dgTransferFileData.ItemsSource = null;
return;
}
}
if (methodVariableImport != null)
{
methodVariableImport.transferDataTable = transferData;
}
dgTransferFileData.ItemsSource = transferData.DefaultView;
List transferFileHeaders = new TransferFileBll().GetTransferFileHeaders(transferData);
}
else
{
ShowTip.ShowNotice(string.Format("{0}{1}{2}", "提醒", Environment.NewLine, "表格头不符合规范,未能加载"), InfoType.Warning);
dgTransferFileData.ItemsSource = null;
}
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
mark += 1;
try
{
if (mark > 1)
{
return;
}
if (!string.IsNullOrEmpty(filePath))
{
tbxCommandName.Text = name;
dgTransferFileData.ItemsSource = transferDataTable.DefaultView;
tbExcelFile.Text = filePath;
if (currmethodVariableImport != null)
{
methodVariableImport = currmethodVariableImport;
}
}
else
{
if (currmethodVariableImport != null)
{
methodVariableImport = currmethodVariableImport;
}
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void tbExcelFile_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (methodVariableImport != null)
{
methodVariableImport.filePath = tbExcelFile.Text;
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
private void tbExcelFile_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (!string.IsNullOrEmpty(tbExcelFile.Text.Trim()))
{
if (System.IO.Path.HasExtension(tbExcelFile.Text.Trim()))
{
if (System.IO.File.Exists(tbExcelFile.Text.Trim()))
{
SelectFile(tbExcelFile.Text.Trim());
}
}
}
}
}
private void btnDownloadTemplate_Click(object sender, RoutedEventArgs e)
{
//弹出保存对话框
try
{
string fileDir = string.Empty;
SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "另存为";
dlg.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}.csv"; // Default file name
dlg.DefaultExt = ".xls"; // Default file extension
dlg.Filter = "excel files(*.xls)|*.xls|csv files(*.csv)|*.csv";// Filter files by extension
dlg.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
if (dlg.ShowDialog() == true)
{
string directoryBase = System.AppDomain.CurrentDomain.BaseDirectory;
string sourceFilePath = directoryBase + "TempData\\variableImport.csv";
File.Copy(sourceFilePath, dlg.FileName, true);
ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "下载保存", Environment.NewLine, dlg.FileName), InfoType.Warning);
}
}
catch (Exception ex)
{
ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "下载保存", Environment.NewLine, ""), InfoType.Warning);
LoggerHelper.ErrorLog("btnDownloadTemplate_Click ERROR:", ex);
}
}
}
}