using CCDCount.DLL.AuditTrail;
using CCDCount.DLL.SqlDataClass;
using CCDCount.DLL.Tools;
using CCDCount.MODEL.AuditTrailModel;
using CCDCount.MODEL.ConfigModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;


namespace CCDCountWpf.WpfPage
{
    /// <summary>
    /// AuditTrailPage.xaml 的交互逻辑
    /// </summary>
    public partial class AuditTrailPage : Page
    {
        #region 实例
        /// <summary>
        /// 批次信息数据类
        /// </summary>
        BatchMessSqliteDataClass batchMessSqliteData = null;
        ErrorMesSqliteDataClass errorMesSqliteData = null;

        // <summary>
        /// 批次记录类(数据转PDF)
        /// </summary>
        BatchRecordClass batchRecord = new BatchRecordClass();

        FormulationRecordClass formulationRecordClass = new FormulationRecordClass();

        ValueChangeRecordClass ValueChangeRecordClass = new ValueChangeRecordClass();

        ErrorMessageRecordClass errorMessageRecordClass = new ErrorMessageRecordClass();
        #endregion

        #region 变量
        /// <summary>
        /// 时间间隔
        /// </summary>
        int TimeInterval = 0;
        string FormulationfolderPath = "";
        #endregion

        #region 窗口事件
        /// <summary>
        /// 构建方法
        /// </summary>
        public AuditTrailPage()
        {
            InitializeComponent();
            this.Height = Double.NaN;
            this.Width = Double.NaN;
            DataContext = ShowMessageBus.ShowBinding;
            InitBatchItems();
            InitTimeIntervalComBox();
            InitFormationItems();
            InitValueChangeItems();
            InitErrorMessage();
        }

        /// <summary>
        /// 批号时间筛选选择时间-最小时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BatchMinTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BatchMinTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (BatchMaxTime.SelectedDate == null)
                {
                    InitBatchItemsByMinTime((DateTime)BatchMinTime.SelectedDate);
                }
                else
                {
                    InitBatchItems((DateTime)BatchMinTime.SelectedDate, (DateTime)BatchMaxTime.SelectedDate);
                }
            }
        }

        /// <summary>
        /// 批号时间筛选选择时间-最大时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BatchMaxTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BatchMaxTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (BatchMinTime.SelectedDate == null)
                {
                    InitBatchItemsByMaxTime((DateTime)BatchMaxTime.SelectedDate);
                }
                else
                {
                    InitBatchItems((DateTime)BatchMinTime.SelectedDate, (DateTime)BatchMaxTime.SelectedDate);
                }
            }
        }

        /// <summary>
        /// 批号选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BatchNumComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (BatchNumComBox.SelectedItem == null || BatchNumComBox.SelectedItem.ToString() == "")
            {
                return;
            }
            if (TimeInterval == 0) return;
            string LoadPath = GenerateBatchPDF();
            BatchLoadPdf(LoadPath);
        }

        /// <summary>
        /// 时间间隔选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimeIntervalComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (TimeIntervalComBox.SelectedItem != null)
            {
                // 获取显示的文本
                var selectedText = TimeIntervalComBox.Text;

                // 获取选中的值
                var selectedValue = Convert.ToInt32(TimeIntervalComBox.SelectedValue?.ToString());

                TimeInterval = selectedValue;

                if (BatchNumComBox.SelectedItem == null || BatchNumComBox.SelectedItem.ToString() == "")
                {
                    return;
                }
                if (TimeInterval == 0) return;
                string LoadPath = GenerateBatchPDF();
                BatchLoadPdf(LoadPath);
            }
        }

        /// <summary>
        /// 批号记录按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BatchRecordBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            BatchRecordGrid.Visibility = System.Windows.Visibility.Visible;
            FormulationRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ValueChangeRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ErrorRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 配方记录按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormulationRecordBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            BatchRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            FormulationRecordGrid.Visibility = System.Windows.Visibility.Visible;
            ValueChangeRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ErrorRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 值改变记录按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValueChangeRecordBtd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            BatchRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            FormulationRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ValueChangeRecordGrid.Visibility = System.Windows.Visibility.Visible;
            ErrorRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
        }

        /// <summary>
        /// 错误记录按钮点击事件
        /// </summary>
        private void ErrorRecordBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            BatchRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            FormulationRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ValueChangeRecordGrid.Visibility = System.Windows.Visibility.Collapsed;
            ErrorRecordGrid.Visibility = System.Windows.Visibility.Visible;
        }

        /// <summary>
        /// 配方选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormulationRecordComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (FormulationRecordComBox.SelectedItem == null || FormulationRecordComBox.SelectedItem.ToString() == "")
            {
                return;
            }
            FormulationfolderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Formulations\\{FormulationRecordComBox.SelectedItem.ToString()}.xml";
            string pdfFilePath = GenerateFormulationPDF();
            FormulationLoadPdf(pdfFilePath);
        }

        /// <summary>
        /// 值改变记录最小时间选择框选择事件
        /// </summary>
        private void ValueChangeRecordMinTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ValueChangeRecordMinTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (ValueChangeRecordMaxTime.SelectedDate == null)
                {
                    InitValueChangeItemsByMinTime((DateTime)ValueChangeRecordMinTime.SelectedDate);
                }
                else
                {
                    InitValueChangeItems((DateTime)ValueChangeRecordMinTime.SelectedDate, (DateTime)ValueChangeRecordMaxTime.SelectedDate);
                }
            }
        }

        /// <summary>
        /// 值改变记录最大时间选择框选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValueChangeRecordMaxTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ValueChangeRecordMaxTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (ValueChangeRecordMinTime.SelectedDate == null)
                {
                    InitValueChangeItemsByMaxTime((DateTime)ValueChangeRecordMaxTime.SelectedDate);
                }
                else
                {
                    InitValueChangeItems((DateTime)ValueChangeRecordMinTime.SelectedDate, (DateTime)ValueChangeRecordMaxTime.SelectedDate);
                }
            }
        }

        /// <summary>
        /// 值改变记录选择框选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValueChangeRecordComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ValueChangeDateComBox.SelectedItem == null || ValueChangeDateComBox.SelectedItem.ToString() == "")
            {
                return;
            }
            string LoadPath = GenerateValueChangePDF();
            ValueChangeLoadPdf(LoadPath);
        }

        /// <summary>
        /// 异常信息选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ErrorMessageRecordComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ErrorMessageDateComBox.SelectedItem == null || ErrorMessageDateComBox.SelectedItem.ToString() == "")
            {
                return;
            }
            string LoadPath = GenerateErrorMessagePDF();
            ErrorMessageLoadPdf(LoadPath);
        }

        /// <summary>
        /// 异常信息时间选择事件-最小时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ErrorMessageRecordMinTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ErrorMessageRecordMinTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (ErrorMessageRecordMaxTime.SelectedDate == null)
                {
                    InitErrorMessageByMinTime((DateTime)ErrorMessageRecordMinTime.SelectedDate);
                }
                else
                {
                    InitErrorMessage((DateTime)ErrorMessageRecordMinTime.SelectedDate, (DateTime)ErrorMessageRecordMaxTime.SelectedDate);
                }
            }
        }

        /// <summary>
        /// 异常信息时间选择事件-最大时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ErrorMessageRecordMaxTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ErrorMessageRecordMaxTime.SelectedDate == null)
            {
                return;
            }
            else
            {
                if (ErrorMessageRecordMinTime.SelectedDate == null)
                {
                    InitErrorMessageByMaxTime((DateTime)ErrorMessageRecordMaxTime.SelectedDate);
                }
                else
                {
                    InitErrorMessage((DateTime)ErrorMessageRecordMinTime.SelectedDate, (DateTime)ErrorMessageRecordMaxTime.SelectedDate);
                }
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 初始化批次列表
        /// </summary>
        private void InitBatchItems()
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.BatchItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.BatchItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                BatchNumComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化批号列表
        /// </summary>
        private void InitBatchItems(DateTime Mintime, DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime && f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.BatchItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.BatchItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                BatchNumComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化批号列表
        /// </summary>
        private void InitBatchItemsByMinTime(DateTime Mintime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.BatchItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.BatchItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                BatchNumComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化批号列表
        /// </summary>
        private void InitBatchItemsByMaxTime(DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.BatchItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.BatchItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                BatchNumComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化时间间隔下拉框
        /// </summary>
        private void InitTimeIntervalComBox()
        {
            // 数据源
            var items = new List<KeyValuePair<string, int>>
            {
                new KeyValuePair<string, int>("1秒", 1),
                new KeyValuePair<string, int>("5秒", 5),
                new KeyValuePair<string, int>("10秒", 10),
                new KeyValuePair<string, int>("30秒",30),
                new KeyValuePair<string, int>("1分", 60),
                new KeyValuePair<string, int>("5分", 300),
            };
            TimeIntervalComBox.ItemsSource = items;
            TimeIntervalComBox.DisplayMemberPath = "Key";
            TimeIntervalComBox.SelectedValuePath = "Value";
            TimeIntervalComBox.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化配方列表
        /// </summary>
        private void InitFormationItems()
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Formulations";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 获取文件列表（包含子目录）
                string[] files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
                ShowMessageBus.ShowBinding.FormulationItems.Clear();
                foreach (string file in files)
                {
                    ShowMessageBus.ShowBinding.FormulationItems.Add(Path.GetFileNameWithoutExtension(file));
                }
                if (ShowMessageBus.ShowBinding.FormulationName != null && ShowMessageBus.ShowBinding.FormulationName != string.Empty)
                {
                    try
                    {
                        FormulationRecordComBox.SelectedItem = ShowMessageBus.ShowBinding.FormulationName;
                    }
                    catch { }
                }
            }
            catch
            { }
        }

        /// <summary>
        /// 生成批记录PDF
        /// </summary>
        /// <returns></returns>
        private string GenerateBatchPDF()
        {
            string BatchNumber = BatchNumComBox.SelectedItem.ToString();
            //string LoadPath = $"{AppDomain.CurrentDomain.BaseDirectory}PDF\\BatchData.PDF";
            batchMessSqliteData = new BatchMessSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData\\BatchData_{BatchNumber}.db");
            var batchMess = batchMessSqliteData.GetAllBatchMessage();
            var ShowtableMessage = batchMess.Where((item, index) => index % TimeInterval == 0).ToList();
            List<string> ColSpan = new List<string>()
            {
                "批次号",
                "料筒震盘速度",
                "过滤震台速度",
                "计数震台速度",
                "装瓶数量",
                "装瓶速度",
                "记录时间"
            };
            batchRecord.GetRecordLogo(Properties.Resources.Logo);
            return batchRecord.BatchRecordToPDFReColspan(ShowtableMessage, ColSpan);
        }

        /// <summary>
        /// 生成配方PDF
        /// </summary>
        /// <returns></returns>
        private string GenerateFormulationPDF()
        {
            string BatchNumber = BatchNumComBox.SelectedItem.ToString();
            FormulationConfigClass formulationConfigClass = null;
            formulationConfigClass = XmlStorage.DeserializeFromXml<FormulationConfigClass>(FormulationfolderPath);
            formulationRecordClass.GetRecordLogo(Properties.Resources.Logo);
            List<string> RowSpanNames = new List<string>()
            {
                "配方名称",
                "二值化阈值",
                "允许物体中断的最大连续行数",
                "合格物体的最长长度",
                "合格物体的最短长度",
                "判定标准识别码",
                "合格物体的最大面积",
                "合格物体的最小面积",
                "噪声过滤阈值",
                "通道数量",
                "曝光时间",
                "采集行频",
                "设备名称",
                "速度模式运行速度",
                "点动速度",
                "瓶装设定值",
                "中转阀打开速度",
                "中转阀打开时间",
                "罐装减速值",
                "缓存减速值",
                "缓存计数延迟时间",
                "缓存停机值",
                "罐装料筒震台高速值",
                "罐装过滤震台高速值",
                "罐装计数震台高速值",
                "罐装料筒震台减速值",
                "罐装过滤震台减速值",
                "罐装计数震台减速值",
                "缓存料筒震台高速值",
                "缓存过滤震台高速值",
                "缓存计数震台高速值",
                "缓存料筒震台减速值",
                "缓存过滤震台减速值",
                "缓存计数震台减速值",
                "阀门打开延迟",
                "回零偏移值",
                "回零偏移速度",
                "中转阀关闭速度",
                "中转阀打开位置",
                "中转阀关闭位置",
                "气阀打开延迟",
                "装瓶停机值",
                "装瓶停机时间",
                "送瓶轮运行速度",
                "送瓶轮点动速度",
                "送瓶轮回零偏移值",
                "送瓶轮回零速度",
                "送瓶轮位置长度",
                "送瓶轮暂停时间",
                "下料延时"
            };
            return formulationRecordClass.FormulationRecordToPDFReRowspan(formulationConfigClass, RowSpanNames);
        }

        /// <summary>
        /// 加载PDF
        /// </summary>
        /// <param name="pdfFilePath"></param>
        private async void BatchLoadPdf(string pdfFilePath)
        {
            // 加载 PDF 文件
            await BatchRecordWebBrowser.EnsureCoreWebView2Async(null);
            BatchRecordWebBrowser.CoreWebView2.Navigate(pdfFilePath);
        }

        /// <summary>
        /// 加载PDF
        /// </summary>
        /// <param name="pdfFilePath"></param>
        private async void FormulationLoadPdf(string pdfFilePath)
        {
            // 加载 PDF 文件
            await FormulationRecordWebBrowser.EnsureCoreWebView2Async(null);
            FormulationRecordWebBrowser.CoreWebView2.Navigate(pdfFilePath);
        }

        /// <summary>
        /// 加载PDF
        /// </summary>
        /// <param name="pdfFilePath"></param>
        private async void ValueChangeLoadPdf(string pdfFilePath)
        {
            // 加载 PDF 文件
            await ValueChangeRecordWebBrowser.EnsureCoreWebView2Async(null);
            ValueChangeRecordWebBrowser.CoreWebView2.Navigate(pdfFilePath);
        }

        /// <summary>
        /// 加载PDF
        /// </summary>
        /// <param name="pdfFilePath"></param>
        private async void ErrorMessageLoadPdf(string pdfFilePath)
        {
            // 加载 PDF 文件
            await ErrorMessageRecordWebBrowser.EnsureCoreWebView2Async(null);
            ErrorMessageRecordWebBrowser.CoreWebView2.Navigate(pdfFilePath);
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitValueChangeItems()
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ValueChangeItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ValueChangeItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ValueChangeDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitValueChangeItems(DateTime Mintime, DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime && f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ValueChangeItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ValueChangeItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ValueChangeDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitValueChangeItemsByMinTime(DateTime Mintime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ValueChangeItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ValueChangeItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ValueChangeDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitValueChangeItemsByMaxTime(DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ValueChangeItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ValueChangeItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ValueChangeDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 生成值更改记录PDF
        /// </summary>
        /// <returns></returns>
        private string GenerateValueChangePDF()
        {
            string ValueChangeValue = ValueChangeDateComBox.SelectedItem.ToString();
            errorMesSqliteData = new ErrorMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage\\FaultMessage_{ValueChangeValue}.db");
            var ValueChangeMess = errorMesSqliteData.GetValueChangeMessage();
            List<string> ColSpan = new List<string>()
            {
                "信息类别",
                "信息类型",
                "错误信息",
                "记录时间",
                "操作员ID",
            };
            ValueChangeRecordClass.GetRecordLogo(Properties.Resources.Logo);
            return ValueChangeRecordClass.ValueChangeRecordToPDFReColspan(ValueChangeMess, ColSpan);
            //return ValueChangeRecordClass.ValueChangeRecordToPDF(ValueChangeMess);
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitErrorMessage()
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ErrorMessItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ErrorMessItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ErrorMessageDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitErrorMessage(DateTime Mintime, DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime && f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ErrorMessItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ErrorMessItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ErrorMessageDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitErrorMessageByMinTime(DateTime Mintime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime > Mintime).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ErrorMessItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ErrorMessItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ErrorMessageDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 初始化异常数据列表
        /// </summary>
        private void InitErrorMessageByMaxTime(DateTime MaxTime)
        {
            string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage";
            if (!Directory.Exists(folderPath))
            {
                // 创建文件夹
                Directory.CreateDirectory(folderPath);
            }
            try
            {
                // 使用 DirectoryInfo 获取文件并按修改时间排序
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);

                // 按照修改时间排序（最新的在前）
                var sortedFiles = files.Where(f => f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
                ShowMessageBus.ShowBinding.ErrorMessItems.Clear();
                foreach (FileInfo file in sortedFiles)
                {
                    ShowMessageBus.ShowBinding.ErrorMessItems.Add(Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
                }
                ErrorMessageDateComBox.SelectedIndex = 0;
            }
            catch
            { }
        }

        /// <summary>
        /// 生成值更改记录PDF
        /// </summary>
        /// <returns></returns>
        private string GenerateErrorMessagePDF()
        {
            string ValueChangeValue = ErrorMessageDateComBox.SelectedItem.ToString();
            errorMesSqliteData = new ErrorMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\FaultMessage\\FaultMessage_{ValueChangeValue}.db");
            var ValueChangeMess = errorMesSqliteData.GetErrorMessage();
            List<string> ColSpan = new List<string>()
            {
                "信息类别",
                "信息类型",
                "错误信息",
                "信息路径",
                "记录时间",
                "操作员ID",
            };
            errorMessageRecordClass.GetRecordLogo(Properties.Resources.Logo);
            return errorMessageRecordClass.ErrorMessageRecordToPDFReColspan(ValueChangeMess, ColSpan);
            //return ValueChangeRecordClass.ValueChangeRecordToPDF(ValueChangeMess);
        }
        #endregion
    }
}
