📄 returndlg.cpp
字号:
// ReturnDlg.cpp : implementation file
//
#include "stdafx.h"
#include "library.h"
#include "ReturnDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReturnDlg dialog
CReturnDlg::CReturnDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReturnDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReturnDlg)
m_BookID = _T("");
m_UserID = _T("");
m_BookName = _T("");
m_BorrowNum = 0;
m_UserName = _T("");
//}}AFX_DATA_INIT
}
void CReturnDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReturnDlg)
DDX_Control(pDX, IDC_RETURN_VIEW, m_ReturnView);
DDX_Text(pDX, IDC_BOOKID, m_BookID);
DDX_Text(pDX, IDC_USERID, m_UserID);
DDX_Text(pDX, IDC_BOOK_NAME, m_BookName);
DDX_Text(pDX, IDC_BORROW_NUM, m_BorrowNum);
DDX_Text(pDX, IDC_USER_NAME, m_UserName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReturnDlg, CDialog)
//{{AFX_MSG_MAP(CReturnDlg)
ON_BN_CLICKED(IDC_RETURN, OnReturn)
ON_NOTIFY(NM_CLICK, IDC_RETURN_VIEW, OnClickReturnView)
ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReturnDlg message handlers
BOOL CReturnDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
InitListView();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CReturnDlg::OnReturn()
{
// TODO: Add your control notification handler code here
//完成还书操作
if(CheckValid())
{
SaveData();
TextShow();
ListShow();
}
}
BOOL CReturnDlg::CheckValid()
{
//检查输入的合法性
UpdateData();
if(m_BookID.IsEmpty())
{
AfxMessageBox("书号不能为空,请输入书号!");
return false;
}
m_BorrowSet.Open();
m_BorrowSet.m_strFilter="bookid='"+m_BookID+"'";
m_BorrowSet.Requery();
m_BookSet.Open();
m_BookSet.m_strFilter="bookid='"+m_BookID+"'";
m_BookSet.Requery();
//此书不存在
if(m_BookSet.GetRecordCount()==0)
{
AfxMessageBox("您的输入有误,请重新输入!");
GetDlgItem(IDC_BOOKID)->SetFocus();
m_BorrowSet.Close();
m_BookSet.Close();
return false;
}
//此书未借出
if(m_BorrowSet.GetRecordCount()==0)
{
AfxMessageBox("此书未借出,请重新输入!");
GetDlgItem(IDC_BOOKID)->SetFocus();
m_BorrowSet.Close();
m_BookSet.Close();
return false;
}
return true;
}
void CReturnDlg::InitListView()
{
//初始化浏览列表
m_ReturnView.DeleteAllItems();
m_ReturnView.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_ReturnView.InsertColumn(0,"借书证号");
m_ReturnView.InsertColumn(1,"姓名");
m_ReturnView.InsertColumn(2,"书号");
m_ReturnView.InsertColumn(3,"书名");
m_ReturnView.InsertColumn(4,"管理员号");
m_ReturnView.InsertColumn(5,"借出日期");
m_ReturnView.InsertColumn(6,"应还日期");
m_ReturnView.SetColumnWidth(0,100);
m_ReturnView.SetColumnWidth(1,70);
m_ReturnView.SetColumnWidth(2,90);
m_ReturnView.SetColumnWidth(3,120);
m_ReturnView.SetColumnWidth(4,80);
m_ReturnView.SetColumnWidth(5,90);
m_ReturnView.SetColumnWidth(6,90);
}
void CReturnDlg::TextShow()
{
//显示基本数据
m_BorrowNum=m_UserSet.m_borrownum;
m_BookID.Empty();
UpdateData(false);
}
void CReturnDlg::ListShow()
{
//在列表中显示用户的借书纪录
int i=0;
m_BorrowSet.m_strFilter="userid='"+m_UserID+"'";
m_BorrowSet.Requery();
//当借书纪录为空时,强行返回,防止地址越界
if(m_BorrowSet.GetRecordCount()==0)
{
m_ReturnView.DeleteAllItems();
m_BookSet.Close();
m_UserSet.Close();
m_BorrowSet.Close();
return;
}
m_BorrowSet.MoveFirst();
m_ReturnView.DeleteAllItems();
while(!m_BorrowSet.IsEOF())
{
m_ReturnView.InsertItem(i,m_BorrowSet.m_userid);
m_ReturnView.SetItemText(i,1,m_UserSet.m_username);
m_ReturnView.SetItemText(i,2,m_BorrowSet.m_bookid);
m_BookSet.m_strFilter="bookid='"+m_BorrowSet.m_bookid+"'";
m_BookSet.Requery();
m_ReturnView.SetItemText(i,3,m_BookSet.m_bookname);
m_ReturnView.SetItemText(i,4,m_BorrowSet.m_manageid);
m_ReturnView.SetItemText(i,5,m_BorrowSet.m_borrowtime.Format("%Y-%m-%d"));
m_ReturnView.SetItemText(i,6,m_BorrowSet.m_returntime.Format("%Y-%m-%d"));
m_BorrowSet.MoveNext();
i++;
}
m_UserSet.Close();
m_BookSet.Close();
m_BorrowSet.Close();
}
void CReturnDlg::SaveData()
{
//将数据保存到数据库
m_UserID=m_BorrowSet.m_userid;
m_BookName=m_BookSet.m_bookname;
//将数据保存到book表
m_BookSet.Edit();
m_BookSet.m_borrowed=false;
m_BookSet.Update();
//将数据保存到user表
m_UserSet.Open();
m_UserSet.m_strFilter="userid='"+m_UserID+"'";
m_UserSet.Requery();
m_UserName=m_UserSet.m_username;
m_UserSet.Edit();
m_UserSet.m_borrownum-=1;
m_UserSet.Update();
//删除borrow表中的数据
m_BorrowSet.Delete();
}
void CReturnDlg::OnClickReturnView(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
//当鼠标单击列表中的记录时,在基本数据区中显示相应数据
POSITION pos;
pos=m_ReturnView.GetFirstSelectedItemPosition();
if(pos!=NULL)
{
int nItem=m_ReturnView.GetNextSelectedItem(pos);
m_UserID=m_ReturnView.GetItemText(nItem,0);
m_UserName=m_ReturnView.GetItemText(nItem,1);
m_BookID=m_ReturnView.GetItemText(nItem,2);
m_BookName=m_ReturnView.GetItemText(nItem,3);
m_ReturnView.SetHotItem(nItem);
UpdateData(false);
}
*pResult = 0;
}
void CReturnDlg::OnRefresh()
{
// TODO: Add your control notification handler code here
//在列表中显示所有用户的借书纪录
int i=0;
m_BorrowSet.Open();
m_BorrowSet.m_strFilter="";
m_BorrowSet.Requery();
//当借书纪录为空时,强行返回,防止地址越界
if(m_BorrowSet.GetRecordCount()==0)
{
m_ReturnView.DeleteAllItems();
m_BorrowSet.Close();
AfxMessageBox("当前借书纪录为空!");
return;
}
m_BorrowSet.MoveFirst();
if(m_BorrowSet.m_userid=="null")
m_BorrowSet.MoveNext();
if(m_BorrowSet.IsEOF())
{
m_ReturnView.DeleteAllItems();
m_BorrowSet.Close();
AfxMessageBox("当前借书纪录为空!");
return;
}
//显示第一条纪录的基本信息
m_UserID=m_BorrowSet.m_userid;
m_BookID=m_BorrowSet.m_bookid;
m_UserSet.Open();
m_UserSet.m_strFilter="userid='"+m_BorrowSet.m_userid+"'";
m_UserSet.Requery();
m_UserName=m_UserSet.m_username;
m_BorrowNum=m_UserSet.m_borrownum;
m_BookSet.Open();
m_BookSet.m_strFilter="bookid='"+m_BorrowSet.m_bookid+"'";
m_BookSet.Requery();
m_BookName=m_BookSet.m_bookname;
UpdateData(false);
//在列表中显示
m_ReturnView.DeleteAllItems();
while(!m_BorrowSet.IsEOF())
{
m_ReturnView.InsertItem(i,m_BorrowSet.m_userid);
m_UserSet.m_strFilter="userid='"+m_BorrowSet.m_userid+"'";
m_UserSet.Requery();
m_ReturnView.SetItemText(i,1,m_UserSet.m_username);
m_ReturnView.SetItemText(i,2,m_BorrowSet.m_bookid);
m_BookSet.m_strFilter="bookid='"+m_BorrowSet.m_bookid+"'";
m_BookSet.Requery();
m_ReturnView.SetItemText(i,3,m_BookSet.m_bookname);
m_ReturnView.SetItemText(i,4,m_BorrowSet.m_manageid);
m_ReturnView.SetItemText(i,5,m_BorrowSet.m_borrowtime.Format("%Y-%m-%d"));
m_ReturnView.SetItemText(i,6,m_BorrowSet.m_returntime.Format("%Y-%m-%d"));
m_BorrowSet.MoveNext();
i++;
}
m_ReturnView.SetHotItem(0);
m_UserSet.Close();
m_BookSet.Close();
m_BorrowSet.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -