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

📄 product.cpp

📁 本网站是一个库存管理系统
💻 CPP
字号:
// Product.cpp: implementation of the CProduct class.

#include "stdafx.h"
#include "Stock.h"
#include "Product.h"
#include "ADOConn.h"
#include "SysLog.h"

 CProduct::CProduct() // Construction
{
	Pid = 0;
	Pname = "";
	TypeId = 0;
	Pstyle = "";
	Punit = "";
	Pprice = 0;
	Plow = 0;
	Phigh = 0;
	Valid = 0;
	AlarmDays = 0;
}

CProduct::~CProduct() // Destruction
{

}

/********************************************************/
/************************变量操作************************/
/********************************************************/

int CProduct::GetPid() // 返回产品编号
{
	return Pid;
}

void CProduct::SetPid(int iPid) // 设置产品编号
{
	Pid = iPid;
}

CString CProduct::GetPname() // 返回产品名称
{
	return Pname;
}

void CProduct::SetPname(CString cPname) // 设置产品名称
{
	Pname = cPname;
}

int CProduct::GetTypeId() // 返回产品类型编号
{
	return TypeId;
}

void CProduct::SetTypeId(int iTypeId) // 设置产品类型编号
{
	TypeId = iTypeId;
}

CString CProduct::GetPstyle() // 返回产品规格
{
	return Pstyle;
}

void CProduct::SetPstyle(CString cPstyle) // 设置产品规格
{
	Pstyle = cPstyle;
}

CString CProduct::GetPunit() // 返回计量单位
{
	return Punit;
}

void CProduct::SetPunit(CString cPunit) // 设置计量单位
{
	Punit = cPunit;
}

float CProduct::GetPprice() // 返回参考价格
{
	return Pprice;
}

void CProduct::SetPprice(float fPprice) // 设置参考价格
{
	Pprice = fPprice;
}

int CProduct::GetPlow() // 返回产品数量下限
{
	return Plow;
}

void CProduct::SetPlow(int iPlow) // 设置产品数量下限
{
	Plow = iPlow;
}

int CProduct::GetPhigh() // 返回产品数量上限
{
	return Phigh;
}

void CProduct::SetPhigh(int iPhigh) // 设置产品数量上限
{
	Phigh = iPhigh;
}

int CProduct::GetValid() // 返回有效期 (以天为单位)
{
	return Valid;
}

void CProduct::SetValid(int iValid) // 设置有效期 (以天为单位)
{
	Valid = iValid;
}

int CProduct::GetAlarmDays() // 返回在到达有效期前几天发出警告
{
	return AlarmDays;
}

void CProduct::SetAlarmDays(int iAlarmDays) // 设置在到达有效期前几天发出警告
{
	AlarmDays = iAlarmDays;
}

/************************************************************/
/*************************数据库操作*************************/
/************************************************************/

int CProduct::HaveName(CString cPname) //  判断指定的产品名称是否在数据库中 
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置SELECT语句
	_bstr_t vSQL;
	vSQL = "SELECT * FROM Product WHERE Pname='" + cPname + "'";
	// 执行SELETE语句
	_RecordsetPtr m_pRecordset;
	m_pRecordset = m_AdoConn.GetRecordSet(vSQL);

	// 返回各列的值
	if (m_pRecordset->adoEOF)
		return -1;
	else
		return 1;
	m_AdoConn.ExitConnect(); // 断开与数据库的连接
}

int CProduct::HaveType(CString cTypeId) // 判断表中是否存在指定的产品类别
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置SELECT语句
	_bstr_t vSQL;
	vSQL = "SELECT * FROM Product WHERE TypeId=" + cTypeId;
	// 执行SELETE语句
	_RecordsetPtr m_pRecordset;
	m_pRecordset = m_AdoConn.GetRecordSet(vSQL);

	// 返回各列的值
	if (m_pRecordset->adoEOF)
		return -1;
	else
		return 1;
	m_AdoConn.ExitConnect(); // 断开与数据库的连接
} 

void CProduct::sql_insert() // 插入新的产品记录
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置INSERT语句
	CString strTypeId;
	strTypeId.Format("%d", TypeId);
	CString strPrice;
	strPrice.Format("%f", Pprice);
	CString strPlow;
	strPlow.Format("%d", Plow);
	CString strPhigh;
	strPhigh.Format("%d", Phigh);
	CString strValid;
	strValid.Format("%d", Valid);
	CString strAlarm;
	strAlarm.Format("%d", AlarmDays);

	_bstr_t vSQL;
	vSQL = "INSERT INTO Product (Pname, TypeId, Pstyle, Punit, Pprice, Plow, Phigh, Valid, AlarmDays) VALUES('" 
		+ Pname + "'," + strTypeId + ",'" + Pstyle + "','" + Punit + "'," + strPrice + "," + strPlow + ","
		+ strPhigh + "," + strValid + "," + strAlarm + ")";

	m_AdoConn.ExecuteSQL(vSQL);	// 执行INSERT语句
	m_AdoConn.ExitConnect(); // 断开与数据库的连接

	// 保存日志
	CSysLog log;
	log.SetLogType(7);
	log.SetTitle("添加产品信息");
	log.SetBody("产品名称:" + Pname);
	log.sql_insert();
}
	
void CProduct::sql_update(CString cPid) // 修改指定的产品记录
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置UPDATE语句
	CString strTypeId;
	strTypeId.Format("%d", TypeId);
	CString strPrice;
	strPrice.Format("%f", Pprice);
	CString strPlow;
	strPlow.Format("%d", Plow);
	CString strPhigh;
	strPhigh.Format("%d", Phigh);
	CString strValid;
	strValid.Format("%d", Valid);
	CString strAlarm;
	strAlarm.Format("%d", AlarmDays);
	
	_bstr_t vSQL;
	vSQL = "UPDATE Product SET Pname='" + Pname + "', Pstyle='" 
		+ Pstyle + "', Punit='" + Punit + "', Pprice=" 
		+ strPrice + ", Plow=" + strPlow + ", Phigh=" + strPhigh
		+ ", Valid=" + strValid + ", AlarmDays=" + strAlarm
		+ " WHERE Pid=" + cPid;

	m_AdoConn.ExecuteSQL(vSQL);	// 执行UPDATE语句
	m_AdoConn.ExitConnect(); // 断开与数据库的连接

	// 保存日志
	CSysLog log;
	log.SetLogType(7);
	log.SetTitle("修改产品信息");
	log.SetBody("产品名称:" + Pname);
	log.sql_insert();
}	

void CProduct::sql_delete(CString cPid) // 删除指定的产品记录
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置DELETE语句
	_bstr_t vSQL;
	vSQL = "DELETE FROM Product WHERE Pid=" + cPid;

	m_AdoConn.ExecuteSQL(vSQL);	// 执行DELETE语句
	m_AdoConn.ExitConnect(); // 断开与数据库的连接

	// 保存日志
	CSysLog log;
	log.SetLogType(7);
	log.SetTitle("删除产品信息");
	log.SetBody("产品名称:" + Pname);
	log.sql_insert();
}

void CProduct::GetData(CString cPid) //读取所有字段值
{
	// 连接数据库
	ADOConn m_AdoConn;
	m_AdoConn.OnInitADOConn();
	// 设置SELECT语句
	_bstr_t vSQL;
	vSQL = "SELECT * FROM Product WHERE Pid=" + cPid;
	// 执行SELETE语句
	_RecordsetPtr m_pRecordset;
	m_pRecordset = m_AdoConn.GetRecordSet(vSQL);

	// 返回各列的值
	if (m_pRecordset->adoEOF)
		CProduct();
	else
	{
		Pid = atoi(cPid);
		Pname = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pname");
		TypeId = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TypeId"));
		Pstyle = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pstyle");
		Punit = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Punit");
		Pprice = atof((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Pprice"));
		Plow = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Plow"));
		Phigh = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Phigh"));
		Valid = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Valid"));
		AlarmDays = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("AlarmDays"));
	}

	m_AdoConn.ExitConnect(); // 断开与数据库的连接
}

⌨️ 快捷键说明

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