📄 borrowdlg.cpp
字号:
// BorrowDlg.cpp : implementation file
//
#include "stdafx.h"
#include "libmis.h"
#include "BorrowDlg.h"
#include "LendDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBorrowDlg dialog
CBorrowDlg::CBorrowDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBorrowDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBorrowDlg)
m_strVal1 = _T("");
m_strVal2 = _T("");
//}}AFX_DATA_INIT
}
void CBorrowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBorrowDlg)
DDX_Control(pDX, IDC_LIST1, m_cList);
DDX_Text(pDX, IDC_EDIT1, m_strVal1);
DDX_Text(pDX, IDC_EDIT2, m_strVal2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBorrowDlg, CDialog)
//{{AFX_MSG_MAP(CBorrowDlg)
ON_BN_CLICKED(IDC_BUTSEARCH, OnButsearch)
ON_BN_CLICKED(ID_LEND, OnLend)
ON_BN_CLICKED(ID_RETURN, OnReturn)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBorrowDlg message handlers
BOOL CBorrowDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString strSql;
m_cList.SetExtendedStyle (LVS_EX_TRACKSELECT|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_cList.InsertColumn (0,"读者ID",LVCFMT_CENTER,150);
m_cList.InsertColumn (1,"书ID",LVCFMT_CENTER,170);
m_cList.InsertColumn (2,"日期",LVCFMT_CENTER,200);
try
{
if (!m_pConnection.CreateInstance(__uuidof(Connection)))
m_pConnection->Open("Provider=Microsoft.JET.OLEDB.4.0;Data Source=lib.mdb","","",adModeUnknown);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
try
{
m_pRecordset1.CreateInstance(__uuidof(Recordset));
m_pRecordset2.CreateInstance(__uuidof(Recordset));
strSql="SELECT * FROM borrow";
m_pRecordset1->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
int nIndex=0;
while (!m_pRecordset1->adoEOF)
{
m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset1->GetCollect ("readerid")));
m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset1->GetCollect ("bookid")));
m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset1->GetCollect ("date")));
nIndex++;
m_pRecordset1->MoveNext ();
}
m_pRecordset1->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return FALSE;
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CBorrowDlg::OnButsearch()
{
// TODO: Add your control notification handler code here
UpdateData ();
m_strVal1.TrimLeft ();
m_strVal1.TrimRight ();
m_strVal2.TrimLeft ();
m_strVal2.TrimRight ();
CString strSql="SELECT * FROM borrow WHERE readerid LIKE '%"+m_strVal1+"%'"+
"AND bookid LIKE '%"+m_strVal2+="%'";
try
{
m_pRecordset1->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_cList.DeleteAllItems ();
int nIndex=0;
while (!m_pRecordset1->adoEOF)
{
m_cList.InsertItem (nIndex,(_bstr_t)(m_pRecordset1->GetCollect ("readerid")));
m_cList.SetItemText (nIndex,1,(_bstr_t)(m_pRecordset1->GetCollect ("bookid")));
m_cList.SetItemText (nIndex,2,(_bstr_t)(m_pRecordset1->GetCollect ("date")));
nIndex++;
m_pRecordset1->MoveNext ();
}
m_pRecordset1->Close ();
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
}
void CBorrowDlg::OnLend()
{
// TODO: Add your control notification handler code here
CLendDlg dlg;
dlg.m_tTime=CTime::GetCurrentTime();
if (IDOK==dlg.DoModal ())
{
CString strSql;
try
{
strSql="SELECT * FROM reader WHERE id='"+dlg.m_strReaderId+"'";
m_pRecordset1->Open ((_variant_t)strSql,m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
if (!strcmp ((_bstr_t)m_pRecordset1->GetCollect ("valid"),"无效"))
{
MessageBox ("读者证件无效!","");
return;
}
m_pRecordset1->Close ();
m_pRecordset1->Open ("SELECT * FROM borrow",m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset1->AddNew ();
m_pRecordset1->PutCollect ("readerid",(_variant_t)dlg.m_strReaderId);
m_pRecordset1->PutCollect ("bookid",(_variant_t)dlg.m_strBookId);
m_pRecordset1->PutCollect ("date",(_variant_t)dlg.m_tTime.Format ("%y-%m-%d"));
strSql="SELECT * FROM book WHERE id='"+dlg.m_strBookId+"'";
m_pRecordset2->Open ((_variant_t)strSql,m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
_variant_t vNum=m_pRecordset2->GetCollect ("num");
if (vNum.intVal>=1)
{
vNum.intVal--;
m_pRecordset2->PutCollect ("num",vNum);
m_pRecordset1->Update();
m_pRecordset2->Update ();
m_pRecordset1->Close ();
m_pRecordset2->Close ();
}
else
{
AfxMessageBox ("已无此书!");
return;
}
}
catch (_com_error e)
{
if (3105==e.WCode ())
MessageBox ("同一种书每人只能借一本!");
else
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
}
return;
}
if (6==MessageBox ("借书成功,是否继续","成功",MB_ICONQUESTION|MB_YESNO))
OnLend ();
}
}
void CBorrowDlg::OnReturn()
{
// TODO: Add your control notification handler code here
int nIndex;
CString strSql,strBookId,strReaderId;
try
{
if (!m_cList.GetFirstSelectedItemPosition ())
return;
if (2==MessageBox ("确认要还书吗?","还书",MB_OKCANCEL|MB_ICONWARNING))
return;
for (nIndex=m_cList.GetItemCount ()-1;nIndex>=0;nIndex--)
{
if (m_cList.GetItemState (nIndex,LVIS_SELECTED))
{
////以下删除borrow表记录及修改book表记录
//m_pRecordset1-----borrow表
//m_pRecordset2-----book表
strReaderId=m_cList.GetItemText (nIndex,0);
strBookId=m_cList.GetItemText (nIndex,1);
strSql="SELECT * FROM borrow WHERE readerid='"+strReaderId
+"' AND bookid='"+strBookId+"'";
m_pRecordset1->Open (_variant_t(strSql),m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset1->Delete (adAffectCurrent);
strSql="SELECT * FROM book WHERE id='"+strBookId+"'";
m_pRecordset2->Open ((_variant_t)strSql,m_pConnection.GetInterfacePtr (),
adOpenStatic,adLockOptimistic,adCmdText);
_variant_t vNum=m_pRecordset2->GetCollect ("num");
vNum.intVal++;
m_pRecordset2->PutCollect ("num",vNum);
m_pRecordset1->Update();
m_pRecordset2->Update ();
m_pRecordset1->Close ();
m_pRecordset2->Close ();
m_cList.DeleteItem (nIndex);
////以下检查读者有效性
//m_pRecordset1-----borrow表
//m_pRecordset2-----reader表
strSql="SELECT * FROM borrow WHERE readerid='"+strReaderId+"'";
m_pRecordset1->Open (_variant_t(strSql),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
strSql="SELECT * FROM reader WHERE id='"+strReaderId+"'";
m_pRecordset2->Open (_variant_t(strSql),_variant_t((IDispatch*)m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset2->PutCollect ("valid","有效");
m_pRecordset2->Update ();
COleDateTime ot,curT;
COleDateTimeSpan ts;
while (!m_pRecordset1->adoEOF)
{
ot=m_pRecordset1->GetCollect ("date");
curT=curT.GetCurrentTime ();
ts=curT-ot;
if (ts.GetDays ()>30) //仍有超期图书,将读者置为无效
{
m_pRecordset2->PutCollect ("valid","无效");
m_pRecordset2->Update ();
break;
}
m_pRecordset1->MoveNext ();
}
m_pRecordset2->Close ();
m_pRecordset1->Close ();
}
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format ("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage ());
AfxMessageBox (errormessage);
return;
}
}
void CBorrowDlg::OnDestroy()
{
m_pConnection->Close ();
CDialog::OnDestroy();
// TODO: Add your message handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -