⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookadddlg.cpp

📁 自己编写的迷你图书馆的完整程序.VC6MFC编写的,Windows界面.数据采用了Sqlserver2000.压缩包中已包括了数据库.先附加数据库再运行.
💻 CPP
字号:
// BookAddDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BookLib.h"
#include "BookAddDlg.h"
#include "TypeAddDlg.h"
#include "ProviderAddDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBookAddDlg dialog


CBookAddDlg::CBookAddDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBookAddDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBookAddDlg)
	m_isbn = _T("");
	m_name = _T("");
	m_type = -1;
	m_subtype = -1;
	m_author = _T("");
	m_pub = _T("");
	m_ver = _T("");
	m_price = 0.0f;
	m_prov = -1;
	m_remark = _T("");
	m_qty = 0;
	//}}AFX_DATA_INIT
}


void CBookAddDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBookAddDlg)
	DDX_Control(pDX, IDC_CMB_PROV, m_cmb_prov);
	DDX_Control(pDX, IDC_CMB_SUBTYPE, m_cmb_subtype);
	DDX_Control(pDX, IDC_CMB_TYPE, m_cmb_type);
	DDX_Text(pDX, IDC_EDIT_ISBN, m_isbn);
	DDV_MaxChars(pDX, m_isbn, 13);
	DDX_Text(pDX, IDC_EDIT_NAME, m_name);
	DDV_MaxChars(pDX, m_name, 50);
	DDX_CBIndex(pDX, IDC_CMB_TYPE, m_type);
	DDX_CBIndex(pDX, IDC_CMB_SUBTYPE, m_subtype);
	DDX_Text(pDX, IDC_EDIT_AUTHOR, m_author);
	DDV_MaxChars(pDX, m_author, 20);
	DDX_CBString(pDX, IDC_CMB_PUB, m_pub);
	DDV_MaxChars(pDX, m_pub, 30);
	DDX_Text(pDX, IDC_EDIT_VER, m_ver);
	DDV_MaxChars(pDX, m_ver, 20);
	DDX_Text(pDX, IDC_EDIT_PRICE, m_price);
	DDX_CBIndex(pDX, IDC_CMB_PROV, m_prov);
	DDX_Text(pDX, IDC_EDIT_REMARK, m_remark);
	DDV_MaxChars(pDX, m_remark, 100);
	DDX_Text(pDX, IDC_EDIT_QTY, m_qty);
	//}}AFX_DATA_MAP
}

void CBookAddDlg::InitCmb()
{
	m_cmb_type.ResetContent();
	m_cmb_prov.ResetContent();

	CDStrs typeFields,provFields;
	g_adoDB.ExecuteQuery("Select distinct 类别名称 from 书籍类别表",typeFields);
	for(int i=0;i<typeFields.size();i++)
	{
		CStrs strs=typeFields[i];
		m_cmb_type.AddString(strs[0]);
	}
	g_adoDB.ExecuteQuery("Select 名称 from 供应商表",provFields);
	for(i=0;i<provFields.size();i++)
	{
		CStrs strs=provFields[i];
		m_cmb_prov.AddString(strs[0]);
	}
}

CString CBookAddDlg::GetBookID()
{
	CString strBookID;

	return strBookID;

}

BEGIN_MESSAGE_MAP(CBookAddDlg, CDialog)
	//{{AFX_MSG_MAP(CBookAddDlg)
	ON_BN_CLICKED(IDC_BTN_NEW_TYPE, OnBtnNewType)
	ON_BN_CLICKED(IDC_BTN_NEW_PROV, OnBtnNewProv)
	ON_CBN_SELCHANGE(IDC_CMB_TYPE, OnSelchangeCmbType)
	ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBookAddDlg message handlers

void CBookAddDlg::OnBtnNewType() 
{
	CTypeAddDlg dlg;
	dlg.DoModal();
	InitCmb();
}

void CBookAddDlg::OnBtnNewProv() 
{
	CProviderAddDlg dlg;
	dlg.DoModal();
	InitCmb();
}

void CBookAddDlg::OnSelchangeCmbType() 
{
	CString type;
	m_cmb_type.GetLBText(m_cmb_type.GetCurSel(),type);

	m_cmb_subtype.ResetContent();
	CString sql;
	sql.Format("Select distinct 子类名称 from 书籍类别表 where 类别名称='%s'",type);
	CDStrs subtypeFields;
	g_adoDB.ExecuteQuery(sql,subtypeFields);
	for(int i=0;i<subtypeFields.size();i++)
	{
		CStrs strs=subtypeFields[i];
		m_cmb_subtype.AddString(strs[0]);
	}
}

void CBookAddDlg::OnBtnAdd() 
{
	if(!UpdateData()) return;
	if(m_isbn.IsEmpty())
	{
		MessageBox("请输入ISBN号!","错误");
		return;
	}
	if(m_name.IsEmpty())
	{
		MessageBox("请输入书名!","错误");
		return;
	}
	if(m_type==-1)
	{
		MessageBox("请选择类别!","错误");
		return;
	}
	if(m_subtype==-1)
	{
		MessageBox("请选择子类别!","错误");
		return;
	}
	if(m_author.IsEmpty())
	{
		MessageBox("请输入作者姓名!","错误");
		return;
	}
	if(m_pub.IsEmpty())
	{
		MessageBox("请选择或输入出版社名称!","错误");
		return;
	}
	if(m_price<0)
	{
		MessageBox("价格输入有误!","错误");
		return;
	}
	if(m_qty<1)
	{
		MessageBox("数量输入有误!","错误");
		return;
	}
	if(m_prov==-1)
	{
		MessageBox("请选择供应商!","错误");
		return;
	}

	CString sql;
	CString strProvName;
	m_cmb_prov.GetLBText(m_cmb_prov.GetCurSel(),strProvName);
	sql.Format("select providerID from 供应商表 where 名称='%s'",strProvName);
	CString strProvid;
	g_adoDB.ExecuteQueryValue(sql,strProvid);

	CString newBookid,strMaxBookid,prompt;
	sql.Format("select max(bookID) from 书籍编号表 where ISBN='%s'",m_isbn);
	g_adoDB.ExecuteQueryValue(sql,strMaxBookid);
	if(strMaxBookid.IsEmpty())
	{
		CString strType,strSubType;
		m_cmb_type.GetLBText(m_cmb_type.GetCurSel(),strType);
		m_cmb_subtype.GetLBText(m_cmb_subtype.GetCurSel(),strSubType);
		sql.Format("select 类别代码,子类代码,typeID from 书籍类别表 where 类别名称='%s' and 子类名称='%s'"
			,strType,strSubType);
		CDStrs codeFields;
		g_adoDB.ExecuteQuery(sql,codeFields);
		CStrs strs=codeFields[0];
		CString strTypeCode,strSubTypeCode,strTypeid;
		strTypeCode=strs[0];//类别代码
		strSubTypeCode=strs[1];//子类代码
		strTypeid=strs[2];//类别编号(ID)

		sql.Format("Select bookID from 书籍编号表 where bookID like '%s-%s%%' order by bookID",strTypeCode,strSubTypeCode);
		CDStrs bookidFields;
		g_adoDB.ExecuteQuery(sql,bookidFields);
		if(bookidFields.size()!=0)
		{
			strs=bookidFields[bookidFields.size()-1];
			strMaxBookid=strs[0];
			int start=FindNth(strMaxBookid,"-",2)+1;
			CString strNewMaxN1=Int2Str(atoi(strMaxBookid.Mid(start,3))+1);
			if(strNewMaxN1.GetLength()<3) strNewMaxN1="0"+strNewMaxN1;//最多差两个"0",补足3位
			if(strNewMaxN1.GetLength()<3) strNewMaxN1="0"+strNewMaxN1;
			newBookid=strMaxBookid.Left(start)+strNewMaxN1;
		}else{
			newBookid=strTypeCode+"-"+strSubTypeCode+"-001";
		}

		//插入书籍信息表
		sql.Format("Insert into 书籍信息表 values('%s','%s',%d,'%s','%s','%s',%.2f,%d,%d,%d,'%s')"
			,m_isbn,m_name,atoi(strTypeid),m_author,m_pub,m_ver,m_price,m_qty,m_qty,atoi(strProvid),m_remark);
		g_adoDB.Execute(sql);

		//插入书籍编号表
		for(int i=0;i<m_qty;i++)
		{
			sql.Format("Insert into 书籍编号表 values('%s','%s')",newBookid+"-"+Int2Str(i+1),m_isbn);
			g_adoDB.Execute(sql);
		}

		if(m_qty==1)
			prompt=newBookid+"-1";
		else
			prompt=newBookid+"-1.."+Int2Str(m_qty);
		MessageBox("新书编号: "+prompt);

	}else{
		if(AfxMessageBox("该ISBN已经存在,是否要增加数量?",MB_OKCANCEL)!=IDOK)
			return;
		int idx=FindNth(strMaxBookid,"-",3);
		newBookid=strMaxBookid.Left(idx);
		int n=atoi(strMaxBookid.Mid(idx+1,strMaxBookid.GetLength()-idx-1));
		
		//修改书籍信息表
		sql.Format("Update 书籍信息表 set 数量=数量+%d,剩余数量=剩余数量+%d",m_qty,m_qty);
		g_adoDB.Execute(sql);

		//插入书籍编号表
		for(int i=n+1;i<=n+m_qty;i++)
		{
			sql.Format("Insert into 书籍编号表 values('%s','%s')",newBookid+"-"+Int2Str(i),m_isbn);
			g_adoDB.Execute(sql);
		}

		prompt=newBookid+"-"+Int2Str(n+1)+".."+Int2Str(n+m_qty);
		MessageBox("新书编号: "+prompt);
	}

	OnCancel();	
}

BOOL CBookAddDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	InitCmb();
	
	return TRUE;  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -