📄 cpglview.cpp
字号:
// cpglView.cpp : implementation of the CCpglView class
//
#include "stdafx.h"
#include "cpgl.h"
#include "ccSet.h"
#include "cpSet.h"
#include "mainSet.h"
#include "cpglDoc.h"
#include "cpglView.h"
#include "DlgBuy.h"
#include "DlgBack.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCpglView
IMPLEMENT_DYNCREATE(CCpglView, CRecordView)
BEGIN_MESSAGE_MAP(CCpglView, CRecordView)
//{{AFX_MSG_MAP(CCpglView)
ON_BN_CLICKED(IDC_BtnSearch, OnBtnSearch)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_BtnBuy, OnBtnBuy)
ON_NOTIFY(NM_CLICK, IDC_Info, OnClickInfo)
ON_BN_CLICKED(IDC_BtnBack, OnBtnBack)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCpglView construction/destruction
CCpglView::CCpglView()
: CRecordView(CCpglView::IDD)
{
//{{AFX_DATA_INIT(CCpglView)
m_ccSet=NULL;
m_cpSet = NULL;
m_mainSet=NULL;
m_EtDes = _T("");
m_EtDate = _T("");
m_EtNum = _T("");
m_EtCPH = _T("");
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CCpglView::~CCpglView()
{
}
void CCpglView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCpglView)
DDX_Control(pDX, IDC_Info, m_InfoList);
DDX_Text(pDX, IDC_EtDes, m_EtDes);
DDX_Text(pDX, IDC_Etdate, m_EtDate);
DDX_Text(pDX, IDC_EtNum, m_EtNum);
DDX_Text(pDX, IDC_EtCPH, m_EtCPH);
//}}AFX_DATA_MAP
}
BOOL CCpglView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CCpglView::OnInitialUpdate()
{
m_ccSet = &GetDocument()->m_mccSet;
m_cpSet = &GetDocument()->m_mcpSet;
m_mainSet = &GetDocument()->m_mmainSet;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//界面颜色
m_Brush=CreateSolidBrush(RGB(0,100,100));
//初始化m_InfoList
m_InfoList.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_InfoList.InsertColumn(0,"序号",LVCFMT_LEFT,40);
m_InfoList.InsertColumn(1,"车次",LVCFMT_LEFT,60);
m_InfoList.InsertColumn(2,"发车日期",LVCFMT_LEFT,90);
m_InfoList.InsertColumn(3,"发车时间",LVCFMT_LEFT,90);
m_InfoList.InsertColumn(4,"票价",LVCFMT_LEFT,60);
m_InfoList.InsertColumn(5,"剩余票数",LVCFMT_LEFT,70);
//获取最后的车票号
cph=0; //初始化
m_mainSet->Open();
if(m_mainSet->GetRecordCount())
{
m_mainSet->MoveLast();
cph=atoi(m_mainSet->m_cph.Right(4));
}
m_mainSet->Close();
}
/////////////////////////////////////////////////////////////////////////////
// CCpglView diagnostics
#ifdef _DEBUG
void CCpglView::AssertValid() const
{
CRecordView::AssertValid();
}
void CCpglView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CCpglDoc* CCpglView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCpglDoc)));
return (CCpglDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCpglView database support
CRecordset* CCpglView::OnGetRecordset()
{
return m_cpSet;
}
/////////////////////////////////////////////////////////////////////////////
// CCpglView message handlers
void CCpglView::OnBtnSearch()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
BOOL Flage=FALSE;
if(!m_cpSet->IsOpen())
m_cpSet->Open();
int number=20;
CString cc,pj;
if(m_cpSet->GetRecordCount()!=0)
{
m_cpSet->MoveFirst();
m_InfoList.DeleteAllItems();
do{
if(strcmp(m_cpSet->m_zd,m_EtDes))
m_cpSet->MoveNext();
else
{
Flage=TRUE;
pj.Format("%.2f",m_cpSet->m_pj);
cc=m_cpSet->m_cc;
GetTime(cc,pj); //传参,调用GetTime函数来确定发车时间
m_ccSet->Close();
break;
}
}while(!m_cpSet->IsEOF());
m_cpSet->MoveFirst();
if(Flage==FALSE)
MessageBox("没有你想要的记录!","提示",MB_OK);
}
else
MessageBox("对不起!数据库里没用存储任何数据...","敬告",MB_OK);
}
HBRUSH CCpglView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return m_Brush;
}
void CCpglView::GetTime(CString cc,CString pj)
{
int i=0,col=1,k=0,total=0;
CString s,fcsj;
m_ccSet->Open(); //打开记录集
while(!m_ccSet->IsEOF())
{
m_ccSet->MoveNext();
total++; //统计记录数量
}
do{
s.Format("%d",col);
if(m_ccSet->IsOpen())
m_ccSet->Close(); //如果已经打开了,就暂时关闭
m_ccSet->Open();
m_ccSet->Move(k); //跳转到第k条记录
if(!strcmp(m_ccSet->m_cc,cc))
{
fcsj=m_ccSet->m_fcsj;
//向控件中写入查询到的数据集合
m_InfoList.InsertItem(i,s);
m_InfoList.SetItemText(i,1,m_cpSet->m_cc);
m_InfoList.SetItemText(i,2,m_EtDate);
m_InfoList.SetItemText(i,3,m_ccSet->m_fcsj);
m_InfoList.SetItemText(i,4,pj);
if(m_ccSet->IsOpen())
m_ccSet->Close(); //在调用函数GetNumer()之前一定要保证m_ccSet是关闭的
GetNumber(i,cc,m_EtDate,fcsj);
col++;
i++;
}
k++;
}while(k<total);
}
void CCpglView::GetNumber(int i,CString cc,CString fcDate,CString fcTime)
{
int num=20;
CString strNum;
m_mainSet->Open();
if(m_mainSet->GetRecordCount())
do{
if(!strcmp(m_mainSet->m_cc,cc)&&!strcmp(m_mainSet->m_fcrq,fcDate)&&!strcmp(m_mainSet->m_fcsj,fcTime)&&(m_mainSet->m_cph.Left(1)!='A'))
num--; //统计剩余票数
m_mainSet->MoveNext();
}while(!m_mainSet->IsEOF());
strNum.Format("%d",num);
m_InfoList.SetItemText(i,5,strNum);//将剩余票数写入到控件中
m_mainSet->Close();
}
void CCpglView::OnBtnBuy()
{
// TODO: Add your control notification handler code here
if(p_cc=="")
MessageBox("请选择需要购买的车票","提示",MB_OK);
else
{
CDlgBuy dlg; //创建对话框
CString strMoney,strcph; //总票价和车票号
int i,num; //数量
UpdateData(); //更新以获得文本框中内容
dlg.m_ShowCC=p_cc; //得到车次信息
dlg.m_ShowDate=p_fcrq; //得到发车日期
dlg.m_ShowTime=p_fcsj; //得到发车时间
dlg.m_ShowNum=m_EtNum; //获取数量
num=atoi(m_EtNum);
strMoney.Format("%.2f", p_pj*num); //计算总票价
dlg.m_ShowMoney=strMoney; //得到总票价
if(dlg.DoModal()==IDOK)
{
m_mainSet->Open();
//逐个插入购买的票务信息
for(i=0;i<num;i++)
{
cph++;
m_mainSet->AddNew();
strcph.Format("%04d",cph);
strcph="CPH"+strcph;
m_mainSet->m_cph=strcph;
m_mainSet->m_cc=p_cc;
m_mainSet->m_fcrq=p_fcrq;
m_mainSet->m_fcsj=p_fcsj;
m_mainSet->Update();
m_mainSet->Requery();
m_mainSet->MoveLast();
}
m_mainSet->Close();
}
}
}
void CCpglView::OnClickInfo(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
//单击的时候获取需要的票务信息
*pResult = 0;
int row=m_InfoList.GetSelectionMark();
p_cc=m_InfoList.GetItemText(row,1);
p_fcrq=m_InfoList.GetItemText(row,2);
p_fcsj=m_InfoList.GetItemText(row,3);
p_pj= (float)atof((char *)(LPTSTR)(LPCTSTR)m_InfoList.GetItemText(row,4));//CString转float
}
void CCpglView::OnBtnBack()
{
// TODO: Add your control notification handler code here
CString strMoney,strcc;
UpdateData();
if(m_mainSet->IsOpen())
m_mainSet->Close();
if(m_EtCPH.IsEmpty())
{
MessageBox("车票号不能为空!");
return;
}
CDlgBack dlg;
m_mainSet->m_strFilter.Format("cph='%s'",m_EtCPH); //设置过滤条件
m_mainSet->Open();
dlg.m_BackCPH=m_EtCPH;
strcc=m_mainSet->m_cc;
m_mainSet->Close();
if(m_cpSet->IsOpen())
m_cpSet->Close();
m_cpSet->m_strFilter.Format("cc='%s'",strcc);
m_cpSet->Open();
strMoney.Format("%.2f",(0.8*m_cpSet->m_pj));
dlg.m_BackMoney=strMoney;
m_cpSet->m_strFilter="";
m_cpSet->Close();
m_mainSet->Open();
if(dlg.DoModal()==IDOK)
{
m_mainSet->Edit(); //更新记录集
m_mainSet->m_cph="A"+m_mainSet->m_cph; //退票后在车票号前加上一个'A'标识
m_mainSet->Update();
m_mainSet->m_strFilter=""; //取消过滤条件
m_mainSet->Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -