C3031
2026-01-16 28ddc75eadee6c73da2b955e320132fdbecc88de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using LB_SmartVision.CSV;
using LB_SmartVision.SQL;
using LB_SmartVisionCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace LB_SmartVision.Forms.Pages.HistoricalData
{
    public partial class HistoricalDataEditPage : UserControl
    {
        public Action<string, LogInfoType> LogInfo;
        // 保存原始数据的列表
        private List<DataGridViewRow> originalRows = new List<DataGridViewRow>();
 
        private int row2OriginalHeight;
        private int row3OriginalHeight;
 
        List<RecordProductData> RecordProductDatas = new List<RecordProductData>();
        public HistoricalDataEditPage()
        {
            Name = "HistoricalDataEditPage";
            Text = "历史数据查询";
 
            InitializeComponent();
 
            // 保存原始行高度
            row2OriginalHeight = (int)tableLayoutPanel3.RowStyles[1].Height;
            row3OriginalHeight = (int)tableLayoutPanel3.RowStyles[2].Height;
 
            InitializeDataGridView();
            InitializeComboBox();
        }
 
        /// <summary>
        /// 数据显示表格初始化
        /// </summary>
        private void InitializeDataGridView()
        {
            this.dataGridViewHD.DataSource = RecordProductDatas;
 
            dataGridViewHD.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
 
            // 禁止编辑单元格(可选)
            dataGridViewHD.ReadOnly = true;
 
            // 允许多行选择(可选)
            dataGridViewHD.MultiSelect = false;
 
            // 显示行标题(可选)
            dataGridViewHD.RowHeadersVisible = true;
        }
 
        /// <summary>
        /// 选择依据下拉框初始化
        /// </summary>
        private void InitializeComboBox()
        {
            comboBoxSearchBasis.Items.Add("日期");
            comboBoxSearchBasis.Items.Add("SN号");
 
            // 设置默认选择项
            comboBoxSearchBasis.SelectedIndex = 0;
        }
 
        /// <summary>
        /// 下拉框控制选项视图变换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxSearchBasis_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
 
            if (cb.SelectedIndex == 0)
            {
                ShowRow2();
            }
            else if (cb.SelectedIndex == 1)
            {
                ShowRow3();
            }
        }
 
        private void ShowRow2()
        {
            // 显示Row2(恢复高度)
            tableLayoutPanel3.RowStyles[1].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[1].Height = row2OriginalHeight;
 
            // 隐藏Row3(高度设为0)
            tableLayoutPanel3.RowStyles[2].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[2].Height = 0;
 
            // 显示Row2中的控件
            foreach (Control ctrl in tableLayoutPanel3.Controls)
            {
                int row = tableLayoutPanel3.GetRow(ctrl);
                if (row == 1)
                    ctrl.Visible = true;
                else if (row == 2)
                    ctrl.Visible = false;
            }
        }
 
        private void ShowRow3()
        {
            // 隐藏Row2(高度设为0)
            tableLayoutPanel3.RowStyles[1].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[1].Height = 0;
 
            // 显示Row3(恢复高度)
            tableLayoutPanel3.RowStyles[2].SizeType = SizeType.Absolute;
            tableLayoutPanel3.RowStyles[2].Height = row3OriginalHeight;
 
            // 显示Row3中的控件
            foreach (Control ctrl in tableLayoutPanel3.Controls)
            {
                int row = tableLayoutPanel3.GetRow(ctrl);
                if (row == 1)
                    ctrl.Visible = false;
                else if (row == 2)
                    ctrl.Visible = true;
            }
        }
 
        private void dateTimePickerStart_ValueChanged(object sender, EventArgs e)
        {
            dateTimePickerEnd.MinDate = dateTimePickerStart.Value;
        }
        public string OpenFileForSave()
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                // 设置默认文件名和扩展名
                saveFileDialog.FileName = $"历史数据_{DateTime.Now:yyyyMMdd_HHmmss}";
                saveFileDialog.DefaultExt = "xlsx";
 
                // 设置文件过滤器
                saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx|" +
                                        "CSV文件 (*.csv)|*.csv|" +
                                        "文本文件 (*.txt)|*.txt|" +
                                        "所有文件 (*.*)|*.*";
 
                saveFileDialog.Title = "选择文件保存位置";
                saveFileDialog.RestoreDirectory = true;  // 记住上次打开的目录
                saveFileDialog.OverwritePrompt = true;   // 覆盖时提示
 
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string filePathHD = saveFileDialog.FileName;
                    return filePathHD;
                }
            }
 
            return null;
        }
        private void btnHisDataExport_Click(object sender, EventArgs e)
        {
            CsvDataHelperRecordProductData.SaveToCsv(OpenFileForSave(), RecordProductDatas);
        }
        
        private void btnHisDataFind_Click(object sender, EventArgs e)
        {
            
            DateTime startDate = dateTimePickerStart.Value.Date;
            DateTime endDate = dateTimePickerEnd.Value.Date;
 
            switch(comboBoxSearchBasis.SelectedIndex)
            {
                //依据日期查询
                case 0:
                    RecordProductDatas = RecordProductDataRepository.GetRecordsByTimeRange(DateTime.Parse(this.dateTimePickerStart.Value.ToString()), DateTime.Parse(this.dateTimePickerEnd.Value.ToString()));
                    break;
                //依据SN号查询
                case 1:
                    if(string.IsNullOrEmpty(textBoxSN.Text))
                    {
                        MessageBox.Show("请填写SN号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        RecordProductDatas = RecordProductDataRepository.GetRecordsByProductNumber(textBoxSN.Text);
                    }
                        break;
            }
 
            this.dataGridViewHD.DataSource = RecordProductDatas;
            this.dataGridViewHD.AutoGenerateColumns = true;
 
        }
 
    }
}