This commit is contained in:
xyy
2026-05-08 20:20:24 +08:00
parent bd43d195cd
commit 4072f3bb5c
6 changed files with 409 additions and 286 deletions

View File

@@ -3,6 +3,7 @@ using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data.SQLite;
using System.IO;
using System.Windows;
@@ -18,6 +19,8 @@ namespace ShanghaiEnvironmentalTechnology
}
public partial class ReportWindow5 : Window
{
private readonly string _lang = ConfigurationManager.AppSettings["Language"] ?? "zh-CN";
private const int PageSize = 10;
private int currentPage = 1;
private int totalRecords = 0;
@@ -119,7 +122,10 @@ namespace ShanghaiEnvironmentalTechnology
private void UpdatePageInfo()
{
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
if (_lang == "en-US")
PageInfo.Text = $"Page {currentPage} / {Math.Ceiling((double)totalRecords / PageSize)} | Total: {totalRecords} records";
else
PageInfo.Text = $"第 {currentPage} 页 / 共 {Math.Ceiling((double)totalRecords / PageSize)} 页 {totalRecords}条记录";
}
private void PreviousPage_Click(object sender, RoutedEventArgs e)
@@ -148,37 +154,48 @@ namespace ShanghaiEnvironmentalTechnology
this.Background = brush;
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement element && element.Tag is int id)
{
var result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除",
MessageBoxButton.YesNo, MessageBoxImage.Warning);
MessageBoxResult result;
if (_lang == "en-US")
result = MessageBox.Show($"Delete record ID: {id} ?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
else
result = MessageBox.Show($"确定要删除ID为 {id} 的记录吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
DeleteRecordFromDb(id);
// 刷新当前页数据
LoadData(_currentStartDate, _currentEndDate);
}
}
}
// 新增:批量删除功能
private void BatchDeleteButton_Click(object sender, RoutedEventArgs e)
{
var selectedIds = _records.Where(r => r.IsSelected).Select(r => r.Id).ToList();
if (!selectedIds.Any())
{
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
if (_lang == "en-US")
MessageBox.Show("Please select records to delete", "Tip", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show("请先选择要删除的记录", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
var result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除",
MessageBoxButton.YesNo, MessageBoxImage.Warning);
MessageBoxResult result;
if (_lang == "en-US")
result = MessageBox.Show($"Delete {selectedIds.Count} selected records?", "Confirm Batch Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning);
else
result = MessageBox.Show($"确定要删除选中的 {selectedIds.Count} 条记录吗?", "确认批量删除", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
BatchDeleteFromDb(selectedIds);
// 刷新当前页数据
LoadData(_currentStartDate, _currentEndDate);
}
}
@@ -221,7 +238,6 @@ namespace ShanghaiEnvironmentalTechnology
private void Export_Click(object sender, RoutedEventArgs e)
{
DateTime? startDate = null;
if (DateTime.TryParse(StartDatePicker.SelectedDate?.ToString(), out DateTime tempStartDate))
{
@@ -229,30 +245,35 @@ namespace ShanghaiEnvironmentalTechnology
}
DateTime? endDate = EndDatePicker.SelectedDate;
if (endDate.HasValue)
{
endDate = endDate.Value.Date.AddDays(1).AddTicks(-1);
}
// 步骤2读取CO2表数据
List<CO2Record> co2Records = ReadCO2RecordsFromDatabase(startDate, endDate);
if (co2Records == null || !co2Records.Any())
List<CO2Record> records = ReadCO2RecordsFromDatabase(startDate, endDate);
if (records == null || !records.Any())
{
MessageBox.Show("CO2表中无数据无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
if (_lang == "en-US")
MessageBox.Show("No CO2 data to export", "Tip", MessageBoxButton.OK, MessageBoxImage.Warning);
else
MessageBox.Show("CO2表中无数据无法导出", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// 步骤3导出Excel
bool exportSuccess = ExportCO2RecordsToExcel(co2Records);
if (exportSuccess)
bool success = ExportCO2RecordsToExcel(records);
if (success)
{
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
if (_lang == "en-US")
MessageBox.Show("Export successful!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
else
MessageBox.Show("数据已成功导出到Excel", "成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show("Excel导出失败请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
if (_lang == "en-US")
MessageBox.Show("Export failed! File may be in use.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
else
MessageBox.Show("Excel导出失败请检查文件是否被占用", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
@@ -267,32 +288,16 @@ namespace ShanghaiEnvironmentalTechnology
using (SQLiteConnection conn = new SQLiteConnection(CSConstant.DbConnectionString))
{
conn.Open();
// 构建查询语句,根据开始时间和结束时间进行筛选
string query = "SELECT BeginCO2, EndCO2, CO2Added, RecordTime FROM CO2 WHERE 1=1";
if (startDate != null)
{
query += " AND RecordTime >= @StartDate";
}
if (endDate != null)
{
query += " AND RecordTime <= @EndDate";
}
if (startDate != null) query += " AND RecordTime >= @StartDate";
if (endDate != null) query += " AND RecordTime <= @EndDate";
query += " ORDER BY RecordTime";
using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
{
if (startDate != null)
{
cmd.Parameters.AddWithValue("@StartDate", startDate);
}
if (endDate != null)
{
cmd.Parameters.AddWithValue("@EndDate", endDate);
}
if (startDate != null) cmd.Parameters.AddWithValue("@StartDate", startDate);
if (endDate != null) cmd.Parameters.AddWithValue("@EndDate", endDate);
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
@@ -300,10 +305,10 @@ namespace ShanghaiEnvironmentalTechnology
{
records.Add(new CO2Record
{
BeginCO2 = reader.GetDouble(0), // 开始CO2浓度%
EndCO2 = reader.GetDouble(1), // 终CO2浓度%
CO2Added = reader.GetDouble(2), // CO2浓度相对增加%
RecordTime = reader.GetDateTime(3) // 时间
BeginCO2 = reader.GetDouble(0),
EndCO2 = reader.GetDouble(1),
CO2Added = reader.GetDouble(2),
RecordTime = reader.GetDateTime(3)
});
}
}
@@ -313,7 +318,10 @@ namespace ShanghaiEnvironmentalTechnology
}
catch (Exception ex)
{
MessageBox.Show($"读取CO2表失败{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
if (_lang == "en-US")
MessageBox.Show($"Failed to read CO2 data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
else
MessageBox.Show($"读取CO2表失败{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
@@ -336,7 +344,7 @@ namespace ShanghaiEnvironmentalTechnology
// 用户取消选择则返回
bool? result = saveDialog.ShowDialog(); if (!(result ?? false)) return false;
// EPPlus 许可设置(.NET 8 必须显式设置)
ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // 非商业用途