SourceCode/Bond/Servo/ProductionLogDlg.cpp
@@ -8,457 +8,457 @@
#include "Common.h"
#include <iomanip>
#define PAGE_SIZE                  100
#define PAGE_BACKGROUND_COLOR         RGB(252, 252, 255)
// CProductionLogDlg 对话框
IMPLEMENT_DYNAMIC(CProductionLogDlg, CDialogEx)
CProductionLogDlg::CProductionLogDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_DIALOG_PRODUCTION_LOG, pParent)
{
   m_crBkgnd = PAGE_BACKGROUND_COLOR;
   m_hbrBkgnd = nullptr;
   m_pObserver = nullptr;
   m_strKeyword = "";
   m_strProductId = "";
   m_strBatchNo = "";
   m_strDeviceId = "";
   m_strOperatorName = "";
   m_strStatus = "";
   m_nCurPage = 0;
   m_nTotalPages = 0;
   m_nDateTimeFlag = 0;
   memset(m_szTimeStart, 0, sizeof(m_szTimeStart));
   memset(m_szTimeEnd, 0, sizeof(m_szTimeEnd));
   m_szTimeStart[0] = '\0';
   m_szTimeEnd[0] = '\0';
}
CProductionLogDlg::~CProductionLogDlg()
{
}
void CProductionLogDlg::InitRxWindow()
{
   /* code */
   // 订阅数据
   IRxWindows* pRxWindows = RX_GetRxWindows();
   pRxWindows->enableLog(5);
   if (m_pObserver == NULL) {
      m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
         // onNext
         pAny->addRef();
         int code = pAny->getCode();
         //if (RX_CODE_ALARM_SET == code) {
//#define PAGE_SIZE                  100
//#define PAGE_BACKGROUND_COLOR         RGB(252, 252, 255)
//
//// CProductionLogDlg 对话框
//
//IMPLEMENT_DYNAMIC(CProductionLogDlg, CDialogEx)
//
//CProductionLogDlg::CProductionLogDlg(CWnd* pParent /*=nullptr*/)
//   : CDialogEx(IDD_DIALOG_PRODUCTION_LOG, pParent)
//{
//   m_crBkgnd = PAGE_BACKGROUND_COLOR;
//   m_hbrBkgnd = nullptr;
//   m_pObserver = nullptr;
//
//   m_strKeyword = "";
//   m_strProductId = "";
//   m_strBatchNo = "";
//   m_strDeviceId = "";
//   m_strOperatorName = "";
//   m_strStatus = "";
//
//   m_nCurPage = 0;
//   m_nTotalPages = 0;
//   m_nDateTimeFlag = 0;
//
//   memset(m_szTimeStart, 0, sizeof(m_szTimeStart));
//   memset(m_szTimeEnd, 0, sizeof(m_szTimeEnd));
//   m_szTimeStart[0] = '\0';
//   m_szTimeEnd[0] = '\0';
         //}
      pAny->release();
      }, [&]() -> void {
      // onComplete
      }, [&](IThrowable* pThrowable) -> void {
      // onErrorm
      pThrowable->printf();
      });
      theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())->subscribe(m_pObserver);
   }
}
void CProductionLogDlg::Resize()
{
   CRect rcClient;
   GetClientRect(&rcClient);
   m_listCtrl.MoveWindow(12, 58, rcClient.Width() - 24, rcClient.Height() - 64);
}
void CProductionLogDlg::UpdatePageData()
{
   // 根据过滤条件加载数据(支持分页、模糊查询、时间范围)
   auto vecData = ProductionLogManager::getInstance().getFilteredSteps(
      m_strProductId,         // 产品ID
      m_strBatchNo,           // 批次号
      m_strDeviceId,          // 设备ID
      m_strOperatorName,      // 操作员
      m_strStatus,            // 状态
      m_szTimeStart,          // 起始时间
      m_szTimeEnd,            // 结束时间
      m_nCurPage,             // 当前页码
      PAGE_SIZE               // 每页条数
   );
   // 填充数据到列表控件
   FillDataToListCtrl(&m_listCtrl, vecData);
   // 更新分页控件
   UpdatePageControls();
}
void CProductionLogDlg::UpdatePageControls()
{
   // 更新分页信息
   CString strPage;
   strPage.Format(_T("第 %d 页"), m_nCurPage);
   SetDlgItemText(IDC_LABEL_PAGE_NUMBER, strPage);
   // 启用/禁用翻页按钮
   GetDlgItem(IDC_BUTTON_PREV_PAGE)->EnableWindow(m_nCurPage > 1);
   GetDlgItem(IDC_BUTTON_NEXT_PAGE)->EnableWindow(m_nCurPage < m_nTotalPages);
}
void CProductionLogDlg::FillDataToListCtrl(CListCtrl* pListCtrl, const std::vector<ProductionStep>& vecSteps)
{
   if (pListCtrl == nullptr || pListCtrl->m_hWnd == nullptr) {
      return;
   }
   // 清空当前CListCtrl中的所有项
   pListCtrl->DeleteAllItems();
   // 遍历数据并插入到CListCtrl中
   for (const auto& step : vecSteps) {
      InsertStepData(pListCtrl, step);
   }
   // 获取列数
   int nColCount = pListCtrl->GetHeaderCtrl()->GetItemCount();
   pListCtrl->SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
}
void CProductionLogDlg::InsertStepData(CListCtrl* pListCtrl, const ProductionStep& step)
{
   if (pListCtrl == nullptr || pListCtrl->m_hWnd == nullptr) {
      return;
   }
   int nRowCount = pListCtrl->GetItemCount();
   if (nRowCount >= PAGE_SIZE) {
      pListCtrl->DeleteItem(nRowCount - 1);
   }
   CString str;
   int nNewItem = pListCtrl->InsertItem(0, _T(""));
   str.Format(_T("%d"), step.nStepId);        pListCtrl->SetItemText(nNewItem, 1, str);
   pListCtrl->SetItemText(nNewItem, 2, step.strProductId.c_str());
   pListCtrl->SetItemText(nNewItem, 3, step.strBatchNo.c_str());
   str.Format(_T("%d"), step.nDeviceId);     pListCtrl->SetItemText(nNewItem, 4, str);
   pListCtrl->SetItemText(nNewItem, 5, step.strOperator.c_str());
   pListCtrl->SetItemText(nNewItem, 6, step.strStartTime.c_str());
   pListCtrl->SetItemText(nNewItem, 7, step.strEndTime.c_str());
   str.Format(_T("%d"), step.nYield);        pListCtrl->SetItemText(nNewItem, 8, str);
   str.Format(_T("%d"), step.nGoodCount);    pListCtrl->SetItemText(nNewItem, 9, str);
   str.Format(_T("%d"), step.nBadCount);     pListCtrl->SetItemText(nNewItem, 10, str);
   pListCtrl->SetItemText(nNewItem, 11, step.strStatus.c_str());
}
std::string CProductionLogDlg::getCurrentTimeString()
{
   auto now = std::chrono::system_clock::now();
   auto time_t_now = std::chrono::system_clock::to_time_t(now);
   std::tm tm_now = {};
   localtime_s(&tm_now, &time_t_now);
   std::stringstream ss;
   ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S");
   return ss.str();
}
void CProductionLogDlg::DoDataExchange(CDataExchange* pDX)
{
   DDX_Control(pDX, IDC_DATETIMEPICKER_START, m_dateTimeStart);
   DDX_Control(pDX, IDC_DATETIMEPICKER_END, m_dateTimeEnd);
   DDX_Control(pDX, IDC_LIST_PRODUCTION_LOG, m_listCtrl);
   CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CProductionLogDlg, CDialogEx)
   ON_WM_CTLCOLOR()
   ON_WM_DESTROY()
   ON_WM_CLOSE()
   ON_WM_SIZE()
   ON_CBN_SELCHANGE(IDC_COMBO_DATETIME, &CProductionLogDlg::OnCbnSelchangeComboDatetime)
   ON_BN_CLICKED(IDC_BUTTON_SEARCH, &CProductionLogDlg::OnBnClickedButtonSearch)
   ON_BN_CLICKED(IDC_BUTTON_EXPORT, &CProductionLogDlg::OnBnClickedButtonExport)
   ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CProductionLogDlg::OnBnClickedButtonPrevPage)
   ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CProductionLogDlg::OnBnClickedButtonNextPage)
END_MESSAGE_MAP()
// CProductionLogDlg 消息处理程序
BOOL CProductionLogDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   InitRxWindow();
   // 初始化时间范围选择
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
   pComboBox->AddString(_T("不限"));
   pComboBox->AddString(_T("今天"));
   pComboBox->AddString(_T("七天内"));
   pComboBox->AddString(_T("本月"));
   pComboBox->AddString(_T("今年"));
   pComboBox->AddString(_T("自定义"));
   pComboBox->SetCurSel(0);
   m_dateTimeStart.EnableWindow(FALSE);
   m_dateTimeEnd.EnableWindow(FALSE);
   // 读取列宽配置
   CString strIniFile, strItem;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
   int width[12] = { 0, 60, 100, 100, 70, 100, 140, 140, 60, 60, 60, 80 };
   for (int i = 0; i < 12; ++i) {
      strItem.Format(_T("Col_%d_Width"), i);
      width[i] = GetPrivateProfileInt("ProductionListCtrl", strItem, width[i], strIniFile);
   }
   // 初始化列表控件
   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PRODUCTION_LOG);
   DWORD dwStyle = pListCtrl->GetExtendedStyle();
   dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
   pListCtrl->SetExtendedStyle(dwStyle);
   HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
   ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
   pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, width[0]);
   pListCtrl->InsertColumn(1, _T("步骤ID"), LVCFMT_LEFT, width[1]);
   pListCtrl->InsertColumn(2, _T("产品ID"), LVCFMT_LEFT, width[2]);
   pListCtrl->InsertColumn(3, _T("批次号"), LVCFMT_LEFT, width[3]);
   pListCtrl->InsertColumn(4, _T("设备ID"), LVCFMT_LEFT, width[4]);
   pListCtrl->InsertColumn(5, _T("操作员"), LVCFMT_LEFT, width[5]);
   pListCtrl->InsertColumn(6, _T("开始时间"), LVCFMT_LEFT, width[6]);
   pListCtrl->InsertColumn(7, _T("结束时间"), LVCFMT_LEFT, width[7]);
   pListCtrl->InsertColumn(8, _T("产量"), LVCFMT_LEFT, width[8]);
   pListCtrl->InsertColumn(9, _T("良品数"), LVCFMT_LEFT, width[9]);
   pListCtrl->InsertColumn(10, _T("不良品数"), LVCFMT_LEFT, width[10]);
   pListCtrl->InsertColumn(11, _T("状态"), LVCFMT_LEFT, width[11]);
   // 初始化分页数据
   int totalRecords = ProductionLogManager::getInstance().getTotalStepCount(
      m_strProductId, m_strBatchNo, m_strDeviceId, m_strOperatorName,
      m_strStatus, m_szTimeStart, m_szTimeEnd);
   m_nTotalPages = (totalRecords + PAGE_SIZE - 1) / PAGE_SIZE;
   m_nCurPage = 1;
   Resize();
   UpdatePageData();
   return TRUE;  // return TRUE unless you set the focus to a control
   // 异常: OCX 属性页应返回 FALSE
}
HBRUSH CProductionLogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
   if (nCtlColor == CTLCOLOR_STATIC) {
      pDC->SetBkColor(m_crBkgnd);
   }
   if (m_hbrBkgnd == nullptr) {
      m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
   }
   return m_hbrBkgnd;
}
void CProductionLogDlg::OnDestroy()
{
   CDialogEx::OnDestroy();
   // 保存列宽
   CString strIniFile, strItem, strTemp;
   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
   CHeaderCtrl* pHeader = m_listCtrl.GetHeaderCtrl();
   for (int i = 0; i < pHeader->GetItemCount(); ++i) {
      RECT rect;
      pHeader->GetItemRect(i, &rect);
      strItem.Format(_T("Col_%d_Width"), i);
      strTemp.Format(_T("%d"), rect.right - rect.left);
      WritePrivateProfileString("ProductionListCtrl", strItem, strTemp, strIniFile);
   }
   if (m_hbrBkgnd != nullptr) {
      ::DeleteObject(m_hbrBkgnd);
   }
   if (m_pObserver != NULL) {
      m_pObserver->unsubscribe();
      m_pObserver = NULL;
   }
}
void CProductionLogDlg::OnClose()
{
   ShowWindow(SW_HIDE);
   //GetParent()->PostMessage(ID_MSG_ALARMDLG_HIDE, 0, 0);
   CDialogEx::OnClose();
}
void CProductionLogDlg::OnSize(UINT nType, int cx, int cy)
{
   CDialogEx::OnSize(nType, cx, cy);
   if (GetDlgItem(IDC_LIST_PRODUCTION_LOG) == nullptr) return;
   Resize();
}
void CProductionLogDlg::OnCbnSelchangeComboDatetime()
{
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
   int nIndex = pComboBox->GetCurSel();
   int nCount = pComboBox->GetCount();
   m_dateTimeStart.EnableWindow(nIndex == nCount - 1);
   m_dateTimeEnd.EnableWindow(nIndex == nCount - 1);
}
void CProductionLogDlg::OnBnClickedButtonSearch()
{
   // 获取关键字
   CString cstrKeyword;
   GetDlgItemText(IDC_EDIT_KEYWORD, cstrKeyword);
   m_strKeyword = CT2A(cstrKeyword);
   // 获取日期
   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
   m_nDateTimeFlag = pComboBox->GetCurSel();
   if (m_nDateTimeFlag == 0) {
      memset(m_szTimeStart, 0, sizeof(m_szTimeStart));
      memset(m_szTimeEnd, 0, sizeof(m_szTimeEnd));
      m_szTimeStart[0] = '\0';
      m_szTimeEnd[0] = '\0';
   }
   else {
      CTime time = CTime::GetCurrentTime();
      if (m_nDateTimeFlag == 1) {
         // 今天
         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d 00:00:00", time.GetYear(), time.GetMonth(), time.GetDay());
         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
      }
      else if (m_nDateTimeFlag == 2) {
         // 7天内
         CTime time2 = time - CTimeSpan(7, 0, 0, 0);
         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d 00:00:00", time2.GetYear(), time2.GetMonth(), time2.GetDay());
         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
      }
      else if (m_nDateTimeFlag == 3) {
         // 本月
         sprintf_s(m_szTimeStart, 64, "%d-%02d-01 00:00:00", time.GetYear(), time.GetMonth());
         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
      }
      else if (m_nDateTimeFlag == 4) {
         // 今年
         sprintf_s(m_szTimeStart, 64, "%d-01-01 00:00:00", time.GetYear());
         sprintf_s(m_szTimeEnd, 64, "%d-12-31 23:59:59", time.GetYear());
      }
      else if (m_nDateTimeFlag == 5) {
         // 自定义
         SYSTEMTIME t1, t2;
         m_dateTimeStart.GetTime(&t1);
         m_dateTimeEnd.GetTime(&t2);
         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d %02d:%02d:%02d",
            t1.wYear, t1.wMonth, t1.wDay, t1.wHour, t1.wMinute, t1.wSecond);
         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d %02d:%02d:%02d",
            t2.wYear, t2.wMonth, t2.wDay, t2.wHour, t2.wMinute, t2.wSecond);
      }
   }
   // 计算总页数
   int totalRecords = ProductionLogManager::getInstance().getTotalStepCount(
      m_strProductId, m_strBatchNo, m_strDeviceId, m_strOperatorName,
      m_strStatus, m_szTimeStart, m_szTimeEnd);
   m_nTotalPages = (totalRecords + PAGE_SIZE - 1) / PAGE_SIZE;
   m_nCurPage = 1;
   UpdatePageData();  // 调用分页更新函数
}
void CProductionLogDlg::OnBnClickedButtonExport()
{
   CFileDialog fileDialog(FALSE, "csv", "", OFN_HIDEREADONLY, "CSV Files (*.csv)|*.csv||");
   if (fileDialog.DoModal() != IDOK) {
      return;
   }
   CStdioFile file;
   if (!file.Open(fileDialog.GetPathName(), CFile::modeCreate | CFile::modeWrite | CFile::typeText)) {
      CString err;
      err.Format(_T("无法创建文件: %s"), fileDialog.GetPathName());
      AfxMessageBox(err);
      return;
   }
   const int MAX_COLS = 32;
   char szItem[256] = { 0 };
   HDITEM hdItem[MAX_COLS];
   for (int i = 0; i < MAX_COLS; i++) {
      hdItem[i].pszText = szItem;
      hdItem[i].cchTextMax = 256;
      hdItem[i].mask = HDI_TEXT | HDI_WIDTH;
   }
   // 获取列数
   CHeaderCtrl* pHeader = m_listCtrl.GetHeaderCtrl();
   int nSubItemCount = min(pHeader->GetItemCount(), MAX_COLS);
   // 表头
   CString strHeader;
   for (int i = 0; i < nSubItemCount; i++) {
      pHeader->GetItem(i, &hdItem[i]);
      if (hdItem[i].cxy > 0) {
         if (!strHeader.IsEmpty()) strHeader += ",";
         strHeader += CString(hdItem[i].pszText);
      }
   }
   strHeader += "\n";
   file.WriteString(strHeader);
   // 表格内容
   int nItemCount = m_listCtrl.GetItemCount();
   for (int i = 0; i < nItemCount; i++) {
      CStringArray arrRow;
      for (int j = 0; j < nSubItemCount; j++) {
         if (hdItem[j].cxy > 0) {
            CString strText = m_listCtrl.GetItemText(i, j);
            strText.Replace(_T("* "), _T(""));
            // 如果字段中含逗号,包裹双引号
            if (strText.Find(',') != -1) {
               strText = _T("\"") + strText + _T("\"");
            }
            arrRow.Add(strText);
         }
      }
      CString strRow;
      for (int k = 0; k < arrRow.GetCount(); ++k) {
         if (k > 0) strRow += ",";
         strRow += arrRow[k];
      }
      strRow += "\n";
      file.WriteString(strRow);
   }
   file.Close();
}
void CProductionLogDlg::OnBnClickedButtonPrevPage()
{
   // 点击上一页
   m_nCurPage--;
   UpdatePageData();  // 调用分页更新函数
}
void CProductionLogDlg::OnBnClickedButtonNextPage()
{
   // 点击下一页
   m_nCurPage++;
   UpdatePageData();  // 调用分页更新函数
}
//
//CProductionLogDlg::~CProductionLogDlg()
//{
//}
//
//void CProductionLogDlg::InitRxWindow()
//{
//   /* code */
//   // 订阅数据
//   IRxWindows* pRxWindows = RX_GetRxWindows();
//   pRxWindows->enableLog(5);
//   if (m_pObserver == NULL) {
//      m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
//         // onNext
//         pAny->addRef();
//         int code = pAny->getCode();
//
//         //if (RX_CODE_ALARM_SET == code) {
//         //}
//
//      pAny->release();
//      }, [&]() -> void {
//      // onComplete
//      }, [&](IThrowable* pThrowable) -> void {
//      // onErrorm
//      pThrowable->printf();
//      });
//
//      theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())->subscribe(m_pObserver);
//   }
//}
//
//void CProductionLogDlg::Resize()
//{
//   CRect rcClient;
//   GetClientRect(&rcClient);
//
//   m_listCtrl.MoveWindow(12, 58, rcClient.Width() - 24, rcClient.Height() - 64);
//}
//
//void CProductionLogDlg::UpdatePageData()
//{
//   // 根据过滤条件加载数据(支持分页、模糊查询、时间范围)
//   auto vecData = ProductionLogManager::getInstance().getFilteredSteps(
//      m_strProductId,         // 产品ID
//      m_strBatchNo,           // 批次号
//      m_strDeviceId,          // 设备ID
//      m_strOperatorName,      // 操作员
//      m_strStatus,            // 状态
//      m_szTimeStart,          // 起始时间
//      m_szTimeEnd,            // 结束时间
//      m_nCurPage,             // 当前页码
//      PAGE_SIZE               // 每页条数
//   );
//
//   // 填充数据到列表控件
//   FillDataToListCtrl(&m_listCtrl, vecData);
//
//   // 更新分页控件
//   UpdatePageControls();
//}
//
//void CProductionLogDlg::UpdatePageControls()
//{
//   // 更新分页信息
//   CString strPage;
//   strPage.Format(_T("第 %d 页"), m_nCurPage);
//   SetDlgItemText(IDC_LABEL_PAGE_NUMBER, strPage);
//
//   // 启用/禁用翻页按钮
//   GetDlgItem(IDC_BUTTON_PREV_PAGE)->EnableWindow(m_nCurPage > 1);
//   GetDlgItem(IDC_BUTTON_NEXT_PAGE)->EnableWindow(m_nCurPage < m_nTotalPages);
//}
//
//void CProductionLogDlg::FillDataToListCtrl(CListCtrl* pListCtrl, const std::vector<ProductionStep>& vecSteps)
//{
//   if (pListCtrl == nullptr || pListCtrl->m_hWnd == nullptr) {
//      return;
//   }
//
//   // 清空当前CListCtrl中的所有项
//   pListCtrl->DeleteAllItems();
//
//   // 遍历数据并插入到CListCtrl中
//   for (const auto& step : vecSteps) {
//      InsertStepData(pListCtrl, step);
//   }
//
//   // 获取列数
//   int nColCount = pListCtrl->GetHeaderCtrl()->GetItemCount();
//   pListCtrl->SetColumnWidth(nColCount - 1, LVSCW_AUTOSIZE_USEHEADER);
//}
//
//void CProductionLogDlg::InsertStepData(CListCtrl* pListCtrl, const ProductionStep& step)
//{
//   if (pListCtrl == nullptr || pListCtrl->m_hWnd == nullptr) {
//      return;
//   }
//
//   int nRowCount = pListCtrl->GetItemCount();
//   if (nRowCount >= PAGE_SIZE) {
//      pListCtrl->DeleteItem(nRowCount - 1);
//   }
//
//   CString str;
//   int nNewItem = pListCtrl->InsertItem(0, _T(""));
//   str.Format(_T("%d"), step.nStepId);        pListCtrl->SetItemText(nNewItem, 1, str);
//   pListCtrl->SetItemText(nNewItem, 2, step.strProductId.c_str());
//   pListCtrl->SetItemText(nNewItem, 3, step.strBatchNo.c_str());
//   str.Format(_T("%d"), step.nDeviceId);     pListCtrl->SetItemText(nNewItem, 4, str);
//   pListCtrl->SetItemText(nNewItem, 5, step.strOperator.c_str());
//   pListCtrl->SetItemText(nNewItem, 6, step.strStartTime.c_str());
//   pListCtrl->SetItemText(nNewItem, 7, step.strEndTime.c_str());
//   str.Format(_T("%d"), step.nYield);        pListCtrl->SetItemText(nNewItem, 8, str);
//   str.Format(_T("%d"), step.nGoodCount);    pListCtrl->SetItemText(nNewItem, 9, str);
//   str.Format(_T("%d"), step.nBadCount);     pListCtrl->SetItemText(nNewItem, 10, str);
//   pListCtrl->SetItemText(nNewItem, 11, step.strStatus.c_str());
//}
//
//std::string CProductionLogDlg::getCurrentTimeString()
//{
//   auto now = std::chrono::system_clock::now();
//   auto time_t_now = std::chrono::system_clock::to_time_t(now);
//
//   std::tm tm_now = {};
//   localtime_s(&tm_now, &time_t_now);
//
//   std::stringstream ss;
//   ss << std::put_time(&tm_now, "%Y-%m-%d %H:%M:%S");
//   return ss.str();
//}
//
//void CProductionLogDlg::DoDataExchange(CDataExchange* pDX)
//{
//   DDX_Control(pDX, IDC_DATETIMEPICKER_START, m_dateTimeStart);
//   DDX_Control(pDX, IDC_DATETIMEPICKER_END, m_dateTimeEnd);
//   DDX_Control(pDX, IDC_LIST_PRODUCTION_LOG, m_listCtrl);
//   CDialogEx::DoDataExchange(pDX);
//}
//
//BEGIN_MESSAGE_MAP(CProductionLogDlg, CDialogEx)
//   ON_WM_CTLCOLOR()
//   ON_WM_DESTROY()
//   ON_WM_CLOSE()
//   ON_WM_SIZE()
//   ON_CBN_SELCHANGE(IDC_COMBO_DATETIME, &CProductionLogDlg::OnCbnSelchangeComboDatetime)
//   ON_BN_CLICKED(IDC_BUTTON_SEARCH, &CProductionLogDlg::OnBnClickedButtonSearch)
//   ON_BN_CLICKED(IDC_BUTTON_EXPORT, &CProductionLogDlg::OnBnClickedButtonExport)
//   ON_BN_CLICKED(IDC_BUTTON_PREV_PAGE, &CProductionLogDlg::OnBnClickedButtonPrevPage)
//   ON_BN_CLICKED(IDC_BUTTON_NEXT_PAGE, &CProductionLogDlg::OnBnClickedButtonNextPage)
//END_MESSAGE_MAP()
//
//// CProductionLogDlg 消息处理程序
//BOOL CProductionLogDlg::OnInitDialog()
//{
//   CDialogEx::OnInitDialog();
//   InitRxWindow();
//
//   // 初始化时间范围选择
//   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
//   pComboBox->AddString(_T("不限"));
//   pComboBox->AddString(_T("今天"));
//   pComboBox->AddString(_T("七天内"));
//   pComboBox->AddString(_T("本月"));
//   pComboBox->AddString(_T("今年"));
//   pComboBox->AddString(_T("自定义"));
//   pComboBox->SetCurSel(0);
//
//   m_dateTimeStart.EnableWindow(FALSE);
//   m_dateTimeEnd.EnableWindow(FALSE);
//
//   // 读取列宽配置
//   CString strIniFile, strItem;
//   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
//   int width[12] = { 0, 60, 100, 100, 70, 100, 140, 140, 60, 60, 60, 80 };
//   for (int i = 0; i < 12; ++i) {
//      strItem.Format(_T("Col_%d_Width"), i);
//      width[i] = GetPrivateProfileInt("ProductionListCtrl", strItem, width[i], strIniFile);
//   }
//
//   // 初始化列表控件
//   CListCtrl* pListCtrl = (CListCtrl*)GetDlgItem(IDC_LIST_PRODUCTION_LOG);
//   DWORD dwStyle = pListCtrl->GetExtendedStyle();
//   dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
//   pListCtrl->SetExtendedStyle(dwStyle);
//
//   HIMAGELIST imageList = ImageList_Create(24, 24, ILC_COLOR24, 1, 1);
//   ListView_SetImageList(pListCtrl->GetSafeHwnd(), imageList, LVSIL_SMALL);
//
//   pListCtrl->InsertColumn(0, _T(""), LVCFMT_RIGHT, width[0]);
//   pListCtrl->InsertColumn(1, _T("步骤ID"), LVCFMT_LEFT, width[1]);
//   pListCtrl->InsertColumn(2, _T("产品ID"), LVCFMT_LEFT, width[2]);
//   pListCtrl->InsertColumn(3, _T("批次号"), LVCFMT_LEFT, width[3]);
//   pListCtrl->InsertColumn(4, _T("设备ID"), LVCFMT_LEFT, width[4]);
//   pListCtrl->InsertColumn(5, _T("操作员"), LVCFMT_LEFT, width[5]);
//   pListCtrl->InsertColumn(6, _T("开始时间"), LVCFMT_LEFT, width[6]);
//   pListCtrl->InsertColumn(7, _T("结束时间"), LVCFMT_LEFT, width[7]);
//   pListCtrl->InsertColumn(8, _T("产量"), LVCFMT_LEFT, width[8]);
//   pListCtrl->InsertColumn(9, _T("良品数"), LVCFMT_LEFT, width[9]);
//   pListCtrl->InsertColumn(10, _T("不良品数"), LVCFMT_LEFT, width[10]);
//   pListCtrl->InsertColumn(11, _T("状态"), LVCFMT_LEFT, width[11]);
//
//   // 初始化分页数据
//   int totalRecords = ProductionLogManager::getInstance().getTotalStepCount(
//      m_strProductId, m_strBatchNo, m_strDeviceId, m_strOperatorName,
//      m_strStatus, m_szTimeStart, m_szTimeEnd);
//   m_nTotalPages = (totalRecords + PAGE_SIZE - 1) / PAGE_SIZE;
//   m_nCurPage = 1;
//
//   Resize();
//   UpdatePageData();
//
//   return TRUE;  // return TRUE unless you set the focus to a control
//   // 异常: OCX 属性页应返回 FALSE
//}
//
//HBRUSH CProductionLogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
//{
//   HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
//
//   if (nCtlColor == CTLCOLOR_STATIC) {
//      pDC->SetBkColor(m_crBkgnd);
//   }
//
//   if (m_hbrBkgnd == nullptr) {
//      m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
//   }
//
//   return m_hbrBkgnd;
//}
//
//void CProductionLogDlg::OnDestroy()
//{
//   CDialogEx::OnDestroy();
//
//   // 保存列宽
//   CString strIniFile, strItem, strTemp;
//   strIniFile.Format(_T("%s\\configuration.ini"), (LPCTSTR)theApp.m_strAppDir);
//   CHeaderCtrl* pHeader = m_listCtrl.GetHeaderCtrl();
//   for (int i = 0; i < pHeader->GetItemCount(); ++i) {
//      RECT rect;
//      pHeader->GetItemRect(i, &rect);
//      strItem.Format(_T("Col_%d_Width"), i);
//      strTemp.Format(_T("%d"), rect.right - rect.left);
//      WritePrivateProfileString("ProductionListCtrl", strItem, strTemp, strIniFile);
//   }
//
//   if (m_hbrBkgnd != nullptr) {
//      ::DeleteObject(m_hbrBkgnd);
//   }
//
//   if (m_pObserver != NULL) {
//      m_pObserver->unsubscribe();
//      m_pObserver = NULL;
//   }
//}
//
//void CProductionLogDlg::OnClose()
//{
//   ShowWindow(SW_HIDE);
//   //GetParent()->PostMessage(ID_MSG_ALARMDLG_HIDE, 0, 0);
//   CDialogEx::OnClose();
//}
//
//void CProductionLogDlg::OnSize(UINT nType, int cx, int cy)
//{
//   CDialogEx::OnSize(nType, cx, cy);
//   if (GetDlgItem(IDC_LIST_PRODUCTION_LOG) == nullptr) return;
//   Resize();
//}
//
//void CProductionLogDlg::OnCbnSelchangeComboDatetime()
//{
//   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
//   int nIndex = pComboBox->GetCurSel();
//   int nCount = pComboBox->GetCount();
//   m_dateTimeStart.EnableWindow(nIndex == nCount - 1);
//   m_dateTimeEnd.EnableWindow(nIndex == nCount - 1);
//}
//
//void CProductionLogDlg::OnBnClickedButtonSearch()
//{
//   // 获取关键字
//   CString cstrKeyword;
//   GetDlgItemText(IDC_EDIT_KEYWORD, cstrKeyword);
//   m_strKeyword = CT2A(cstrKeyword);
//
//   // 获取日期
//   CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO_DATETIME);
//   m_nDateTimeFlag = pComboBox->GetCurSel();
//   if (m_nDateTimeFlag == 0) {
//      memset(m_szTimeStart, 0, sizeof(m_szTimeStart));
//      memset(m_szTimeEnd, 0, sizeof(m_szTimeEnd));
//      m_szTimeStart[0] = '\0';
//      m_szTimeEnd[0] = '\0';
//   }
//   else {
//      CTime time = CTime::GetCurrentTime();
//      if (m_nDateTimeFlag == 1) {
//         // 今天
//         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d 00:00:00", time.GetYear(), time.GetMonth(), time.GetDay());
//         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
//      }
//      else if (m_nDateTimeFlag == 2) {
//         // 7天内
//         CTime time2 = time - CTimeSpan(7, 0, 0, 0);
//         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d 00:00:00", time2.GetYear(), time2.GetMonth(), time2.GetDay());
//         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
//      }
//      else if (m_nDateTimeFlag == 3) {
//         // 本月
//         sprintf_s(m_szTimeStart, 64, "%d-%02d-01 00:00:00", time.GetYear(), time.GetMonth());
//         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d 23:59:59", time.GetYear(), time.GetMonth(), time.GetDay());
//      }
//      else if (m_nDateTimeFlag == 4) {
//         // 今年
//         sprintf_s(m_szTimeStart, 64, "%d-01-01 00:00:00", time.GetYear());
//         sprintf_s(m_szTimeEnd, 64, "%d-12-31 23:59:59", time.GetYear());
//      }
//      else if (m_nDateTimeFlag == 5) {
//         // 自定义
//         SYSTEMTIME t1, t2;
//         m_dateTimeStart.GetTime(&t1);
//         m_dateTimeEnd.GetTime(&t2);
//
//         sprintf_s(m_szTimeStart, 64, "%d-%02d-%02d %02d:%02d:%02d",
//            t1.wYear, t1.wMonth, t1.wDay, t1.wHour, t1.wMinute, t1.wSecond);
//         sprintf_s(m_szTimeEnd, 64, "%d-%02d-%02d %02d:%02d:%02d",
//            t2.wYear, t2.wMonth, t2.wDay, t2.wHour, t2.wMinute, t2.wSecond);
//      }
//   }
//
//   // 计算总页数
//   int totalRecords = ProductionLogManager::getInstance().getTotalStepCount(
//      m_strProductId, m_strBatchNo, m_strDeviceId, m_strOperatorName,
//      m_strStatus, m_szTimeStart, m_szTimeEnd);
//   m_nTotalPages = (totalRecords + PAGE_SIZE - 1) / PAGE_SIZE;
//   m_nCurPage = 1;
//
//   UpdatePageData();  // 调用分页更新函数
//}
//
//void CProductionLogDlg::OnBnClickedButtonExport()
//{
//   CFileDialog fileDialog(FALSE, "csv", "", OFN_HIDEREADONLY, "CSV Files (*.csv)|*.csv||");
//   if (fileDialog.DoModal() != IDOK) {
//      return;
//   }
//
//   CStdioFile file;
//   if (!file.Open(fileDialog.GetPathName(), CFile::modeCreate | CFile::modeWrite | CFile::typeText)) {
//      CString err;
//      err.Format(_T("无法创建文件: %s"), fileDialog.GetPathName());
//      AfxMessageBox(err);
//      return;
//   }
//
//   const int MAX_COLS = 32;
//   char szItem[256] = { 0 };
//   HDITEM hdItem[MAX_COLS];
//
//   for (int i = 0; i < MAX_COLS; i++) {
//      hdItem[i].pszText = szItem;
//      hdItem[i].cchTextMax = 256;
//      hdItem[i].mask = HDI_TEXT | HDI_WIDTH;
//   }
//
//   // 获取列数
//   CHeaderCtrl* pHeader = m_listCtrl.GetHeaderCtrl();
//   int nSubItemCount = min(pHeader->GetItemCount(), MAX_COLS);
//
//   // 表头
//   CString strHeader;
//   for (int i = 0; i < nSubItemCount; i++) {
//      pHeader->GetItem(i, &hdItem[i]);
//      if (hdItem[i].cxy > 0) {
//         if (!strHeader.IsEmpty()) strHeader += ",";
//         strHeader += CString(hdItem[i].pszText);
//      }
//   }
//   strHeader += "\n";
//   file.WriteString(strHeader);
//
//   // 表格内容
//   int nItemCount = m_listCtrl.GetItemCount();
//   for (int i = 0; i < nItemCount; i++) {
//      CStringArray arrRow;
//      for (int j = 0; j < nSubItemCount; j++) {
//         if (hdItem[j].cxy > 0) {
//            CString strText = m_listCtrl.GetItemText(i, j);
//            strText.Replace(_T("* "), _T(""));
//            // 如果字段中含逗号,包裹双引号
//            if (strText.Find(',') != -1) {
//               strText = _T("\"") + strText + _T("\"");
//            }
//            arrRow.Add(strText);
//         }
//      }
//
//      CString strRow;
//      for (int k = 0; k < arrRow.GetCount(); ++k) {
//         if (k > 0) strRow += ",";
//         strRow += arrRow[k];
//      }
//      strRow += "\n";
//      file.WriteString(strRow);
//   }
//
//   file.Close();
//}
//
//void CProductionLogDlg::OnBnClickedButtonPrevPage()
//{
//   // 点击上一页
//   m_nCurPage--;
//   UpdatePageData();  // 调用分页更新函数
//}
//
//void CProductionLogDlg::OnBnClickedButtonNextPage()
//{
//   // 点击下一页
//   m_nCurPage++;
//   UpdatePageData();  // 调用分页更新函数
//}