using Newtonsoft.Json.Linq; using PTMedicalInsurance.Business; using PTMedicalInsurance.Common; using PTMedicalInsurance.FormSetter; using PTMedicalInsurance.Helper; using PTMedicalInsurance.Variables; using Sunny.UI; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Serialization; namespace PTMedicalInsurance.Forms { public partial class PreAndInProcessAnalysisLogQueryForm : Form { private PreAndInProcessAnalysisLogService logService = new PreAndInProcessAnalysisLogService(); private GridViewSetter grdSetter = new GridViewSetter(); public PreAndInProcessAnalysisLogQueryForm() { InitializeComponent(); } private void PreAndInProcessAnalysisLogQueryForm_Load(object sender, EventArgs e) { try { // 初始化日期范围 dtpStartDate.Value = DateTime.Now.AddDays(-7); dtpEndDate.Value = DateTime.Now; // 设置界面样式 this.StartPosition = FormStartPosition.CenterParent; // 初始化表格列 InitializeDataGridViewColumns(); } catch (Exception ex) { MessageBox.Show($"初始化界面失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void InitializeDataGridViewColumns() { dgvLogs.Columns.Clear(); // 添加列定义 var columns = new[] { new { HeaderText = "日期", DataPropertyName = "InfDate", Width = 150 }, new { HeaderText = "时间", DataPropertyName = "InfTime", Width = 150 }, new { HeaderText = "科室", DataPropertyName = "DeptName", Width = 120 }, new { HeaderText = "操作员", DataPropertyName = "OpterName", Width = 100 }, new { HeaderText = "病人姓名", DataPropertyName = "PatnName", Width = 100 }, new { HeaderText = "项目名称", DataPropertyName = "ItemName", Width = 150 }, new { HeaderText = "违规分类", DataPropertyName = "VolaBhvrType", Width = 100 }, new { HeaderText = "限制类型", DataPropertyName = "WarnType", Width = 100 }, new { HeaderText = "后续操作", DataPropertyName = "DspoWay", Width = 100 }, new { HeaderText = "数量", DataPropertyName = "Quantity", Width = 80 }, new { HeaderText = "金额", DataPropertyName = "Amount", Width = 100 }, new { HeaderText = "交易代码", DataPropertyName = "TradeCode", Width = 80 }, new { HeaderText = "触发场景", DataPropertyName = "TriggerScene", Width = 80 }, new { HeaderText = "违规规则", DataPropertyName = "RuleName", Width = 150 }, new { HeaderText = "违规金额", DataPropertyName = "VolaAmt", Width = 100 } }; foreach (var col in columns) { DataGridViewColumn newColumn = new DataGridViewTextBoxColumn(); newColumn.HeaderText = col.HeaderText; newColumn.DataPropertyName = col.DataPropertyName; newColumn.Width = col.Width; dgvLogs.Columns.Add(newColumn); } dgvLogs.AutoGenerateColumns = false; } private void btnQuery_Click(object sender, EventArgs e) { try { Cursor = Cursors.WaitCursor; btnQuery.Enabled = false; // 获取查询条件 string startDate = dtpStartDate.Value.ToString("yyyy-MM-dd HH:mm:ss"); string endDate = dtpEndDate.Value.ToString("yyyy-MM-dd HH:mm:ss"); string operatorName = txtOperatorName.Text.Trim(); string patientName = txtPatientName.Text.Trim(); string itemName = txtItemName.Text.Trim(); // 调用查询服务 string errMsg; DataTable dt = logService.QueryLogs(startDate, endDate, operatorName, patientName, itemName, out errMsg); if (dt == null) { if (!string.IsNullOrEmpty(errMsg)) { MessageBox.Show($"查询失败: {errMsg}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("查询失败,请稍后重试", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } dgvLogs.DataSource = null; lblRecordCount.Text = "查询结果:0 条记录"; return; } // 显示查询结果 dgvLogs.DataSource = dt; lblRecordCount.Text = $"查询结果:{dt.Rows.Count} 条记录"; // 计算合计金额 CalculateTotals(dt); } catch (Exception ex) { MessageBox.Show($"查询异常: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor = Cursors.Default; btnQuery.Enabled = true; } } private void CalculateTotals(DataTable dt) { try { decimal totalAmount = 0; decimal totalViolationAmount = 0; int totalRecords = dt.Rows.Count; foreach (DataRow row in dt.Rows) { // 计算总金额 if (row["Amount"] != DBNull.Value && decimal.TryParse(row["Amount"].ToString(), out decimal amount)) { totalAmount += amount; } // 计算违规金额 if (row["ViolationAmount"] != DBNull.Value && decimal.TryParse(row["ViolationAmount"].ToString(), out decimal violationAmount)) { totalViolationAmount += violationAmount; } } lblTotalAmount.Text = $"总金额:{totalAmount:F2} 元"; lblTotalViolationAmount.Text = $"违规金额:{totalViolationAmount:F2} 元"; lblRecordCount.Text = $"查询结果:{totalRecords} 条记录"; } catch (Exception ex) { Global.writeLog($"计算合计金额异常: {ex.Message}"); } } private void btnExport_Click(object sender, EventArgs e) { try { if (dgvLogs.DataSource == null) { MessageBox.Show("请先查询数据再进行导出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } DataTable dt = (DataTable)dgvLogs.DataSource; if (dt.Rows.Count == 0) { MessageBox.Show("没有数据可以导出", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // 弹出保存文件对话框 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件 (*.xlsx)|*.xlsx|Excel文件 (*.xls)|*.xls|所有文件 (*.*)|*.*"; saveFileDialog.FilterIndex = 1; saveFileDialog.RestoreDirectory = true; saveFileDialog.FileName = $"事前事中分析日志_{DateTime.Now:yyyyMMddHHmmss}"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { Cursor = Cursors.WaitCursor; btnExport.Enabled = false; try { //// 导出到Excel //string fileName = ExportToExcel.DataTabletoExcel(dt, saveFileDialog.FileName); //if (!string.IsNullOrEmpty(fileName)) //{ // MessageBox.Show($"数据已成功导出到:{fileName}", "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information); // // 更新导出状态 // UpdateExportStatus(dt); //} //else //{ // MessageBox.Show("导出失败,请稍后重试", "导出失败", MessageBoxButtons.OK, MessageBoxIcon.Error); //} // 导出到Excel ExportToExcel.ExportDataGridViewToExcel(dgvLogs, saveFileDialog.FileName); MessageBox.Show($"数据已成功导出到:{saveFileDialog.FileName}", "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"导出异常: {ex.Message}", "导出失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor = Cursors.Default; btnExport.Enabled = true; } } } catch (Exception ex) { MessageBox.Show($"导出异常: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UpdateExportStatus(DataTable dt) { try { // 这里可以实现更新数据库中的导出状态 // 例如:标记已导出的记录 Global.writeLog($"已导出 {dt.Rows.Count} 条日志记录"); // 在实际项目中,这里应该调用服务更新数据库中的导出状态 // logService.UpdateExportStatus(logIds, DateTime.Now, Global.user.Name); } catch (Exception ex) { Global.writeLog($"更新导出状态异常: {ex.Message}"); } } private void btnClear_Click(object sender, EventArgs e) { // 清空查询条件 txtOperatorName.Text = ""; txtPatientName.Text = ""; txtItemName.Text = ""; } private void dgvLogs_CellClick(object sender, DataGridViewCellEventArgs e) { try { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { // 可以在这里实现查看详细信息的逻辑 // 例如:点击某行时显示详细信息 var selectedRow = dgvLogs.Rows[e.RowIndex]; string logId = selectedRow.Cells["ID"]?.Value?.ToString(); if (!string.IsNullOrEmpty(logId)) { // 可以在这里显示详细信息对话框 // ShowLogDetailDialog(logId); } } } catch (Exception ex) { Global.writeLog($"点击表格单元格异常: {ex.Message}"); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }