www.pudn.com > shangdianguanlixitong.rar > consumer.cpp, change:2005-06-24,size:15201b


// consumer.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "shop.h" 
#include "consumer.h" 
 
#include <time.h> 
#include <fstream.h> 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
extern CShopApp theApp; 
///////////////////////////////////////////////////////////////////////////// 
// Cconsumer dialog 
 
struct tm *ptrstudent; 
Cconsumer::Cconsumer(CWnd* pParent /*=NULL*/) 
	: CDialog(Cconsumer::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(Cconsumer) 
	m_condition = _T(""); 
	m_consumername = _T(""); 
	m_consumerno = _T(""); 
	m_leftmoney = _T(""); 
	m_mode = _T(""); 
	m_telephone = _T(""); 
	m_address = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
 
void Cconsumer::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(Cconsumer) 
	DDX_Control(pDX, IDC_USERLIST, m_userlist); 
	DDX_Text(pDX, IDC_condition, m_condition); 
	DDX_Text(pDX, IDC_consumername, m_consumername); 
	DDX_Text(pDX, IDC_consumerno, m_consumerno); 
	DDX_Text(pDX, IDC_leftmoney, m_leftmoney); 
	DDX_CBString(pDX, IDC_mode, m_mode); 
	DDX_Text(pDX, IDC_telephone, m_telephone); 
	DDX_Text(pDX, IDC_address, m_address); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(Cconsumer, CDialog) 
	//{{AFX_MSG_MAP(Cconsumer) 
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_USERLIST, OnItemchangedUserlist) 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3) 
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4) 
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1) 
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2) 
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// Cconsumer message handlers 
 
BOOL Cconsumer::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
 
	time_t lt; 
	 lt = time(NULL); 
	 ptrstudent = localtime(<); 
	 
	// TODO: Add extra initialization here 
	::SendMessage(m_userlist.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);//当被选中时,所有子项都处于选中状态 
	//////////为列表控件添加列////////// 
 
	m_userlist.InsertColumn(0,"顾客编号",LVCFMT_LEFT,100); 
	m_userlist.InsertColumn(1,"顾客名称",LVCFMT_LEFT,100); 
    m_userlist.InsertColumn (2,"余额",LVCFMT_LEFT,100); 
	m_userlist.InsertColumn(3,"地址",LVCFMT_LEFT,100); 
	m_userlist.InsertColumn(4,"电话",LVCFMT_LEFT,100); 
	  
 
	//////////读取数据库中的信息添加到列表控件/////////// 
	displayintheuserlist(); 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void Cconsumer::displayintheuserlist() 
{ 
	//////////读取数据库中的信息添加到列表控件/////////// 
	 
 
	try 
	{ 
		 
		m_userlist.DeleteAllItems(); 
		m_pRecordset.CreateInstance("ADODB.Recordset"); 
		m_pRecordset->Open("select  *  from consumer order by consumerno",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	     
		 
	 
		//m_bSuccess = TRUE; 
		while(!m_pRecordset->adoEOF) 
		{ 
			  
			vconsumerno= m_pRecordset->GetCollect("consumerno"); 
			vconsumername= m_pRecordset->GetCollect("consumername"); 
			vleftmoney= m_pRecordset->GetCollect("leftmoney"); 
			vaddress = m_pRecordset->GetCollect("address"); 
			vtelephone= m_pRecordset->GetCollect("telephone"); 
			 
			  
			  
			 
			//this->AbleFalse(); 
	     	//this->LoadData(); 
			//this->GetDlgItem(IDC_BUTTON4)->EnableWindow(FALSE); 
			//this->GetDlgItem(IDC_BUTTON2)->EnableWindow(FALSE); 
			nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vconsumerno);//先插第一个项 
			m_userlist.SetItem(nItem,1,1,(_bstr_t)vconsumername,NULL,0,0,0); 
			m_userlist.SetItem(nItem,2,1,(_bstr_t)vleftmoney,NULL,0,0,0); 
			m_userlist.SetItem(nItem,3,1,(_bstr_t)vaddress ,NULL,0,0,0); 
			m_userlist.SetItem(nItem,4,1,(_bstr_t)vtelephone ,NULL,0,0,0); 
			  
			  
			 
 
			m_pRecordset->MoveNext(); 
		} 
	} 
	catch(_com_error e)///捕捉异常 
	{ 
		//AfxMessageBox("读取数据库失败!");///显示错误信息 
	} 
} 
 
void Cconsumer::OnItemchangedUserlist(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
if(pNMListView->uNewState&LVIS_SELECTED) 
	{ 
	//UpdateData(true); 
	//SaveData();///保存旧数据 
	m_nCurrentSel = pNMListView->iItem; 
	LoadData0();///加载新数据 
	//m_cdelitem.EnableWindow(); 
	 
	}	 
	*pResult = 0; 
} 
 
void Cconsumer::LoadData0() 
{ 
	m_pRecordset.CreateInstance("ADODB.Recordset"); 
	m_pRecordset->Open("select  *  from consumer order by consumerno",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	    
 
     
	m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));//从第一条记录移动 
	  
	vconsumerno = m_pRecordset->GetCollect("consumerno"); 
	vconsumername = m_pRecordset->GetCollect("consumername"); 
	vleftmoney = m_pRecordset->GetCollect("leftmoney"); 
	vaddress = m_pRecordset->GetCollect("address"); 
	vtelephone = m_pRecordset->GetCollect("telephone"); 
	  
	  
    UpdateData(true);//使得控件显示值 
	//m_num = vNum.lVal; 
//	m_name = (LPCTSTR)(_bstr_t)vName; 
	//m_sex = vSex.lVal; 
///	m_brithday = vBrithday.lVal; 
	//m_tel=vTel.lVal; 
	//m_addr=vAddr.lVal; 
 
 
	  
	m_consumerno= (LPCTSTR)(_bstr_t)vconsumerno; 
	m_consumername = (LPCTSTR)(_bstr_t)vconsumername; 
	m_leftmoney = (LPCTSTR)(_bstr_t)vleftmoney; 
	m_address = (LPCTSTR)(_bstr_t)vaddress; 
	m_telephone= (LPCTSTR)(_bstr_t)vtelephone; 
	  
	  
    //m_pRecordset->Move(m_nCurrentSel,_variant_t((long)adBookmarkFirst));//从第一条记录移动 
	UpdateData(FALSE);//使得控件显示值 
 
} 
 
void Cconsumer::OnButton1()  
{ 
	//bool judge; 
	//judge = false; 
 
	UpdateData(true);//把控件的值传给相应的变量 
	     if (m_consumerno == "" || m_consumername == "") 
		 { 
	   		 AfxMessageBox("顾客编号或顾客名称不能为空!!"); 
             goto tiaozhuan; 
		 } 
          
		 //UpdateData(true);//把控件的值传给相应的变量 
        //m_pRecordset1->Close(); 
		 //m_pRecordset1 = NULL; 
 
		   m_pRecordset.CreateInstance(_uuidof(Recordset));// 
	       m_pRecordset->Open("select * from consumer",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
           
		  
	try 
	   { 
		 
		    
	   	m_pRecordset->AddNew(); 
		 
		  
	    vconsumerno = m_consumerno; 
	    vconsumername = m_consumername; 
		vleftmoney = m_leftmoney; 
	    vaddress = m_address; 
		vtelephone = m_telephone; 
	      
	      
        //UpdateData(true); 
 
		  
		m_pRecordset->PutCollect("consumerno",vconsumerno); 
		m_pRecordset->PutCollect("consumername",vconsumername); 
		m_pRecordset->PutCollect("leftmoney",vleftmoney); 
		m_pRecordset->PutCollect("address",vaddress); 
		m_pRecordset->PutCollect("telephone",vtelephone); 
		  
		  
		 
 
		//UpdateData(true); 
                   
		 //m_pRecordset.CreateInstance("ADODB.Recordset"); 
		 //m_pRecordset->Open("insert into employee values(empno,empname,sex,salary,duty,deptno,birthday,address,telephone,remark)  ",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	      
        //AfxMessageBox("添加记录成功!"); 
 
		 m_pRecordset->Update(); 
		  
		 m_pRecordset->Close(); 
		 m_pRecordset = NULL; 
		  
		 //重建指针,以备下次添加记录时使用      
         //m_pRecordset1.CreateInstance(_uuidof(Recordset));// 
	     //m_pRecordset1->Open("select * from employee",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
          
         displayintheuserlist(); 
		  
         AfxMessageBox("添加记录成功!"); 
 
		 ofstream out("databf\\diary.txt",ios::app); 
	     out<<asctime(ptrstudent)<<"||..."<<"成功执行了向顾客表中添加数据:"<<"顾客编号:"<<m_consumerno<<"   顾客姓名:"<<m_consumername<<"   余额: "+m_leftmoney<<"   地址: "<<m_address<<"   电话: "<<m_telephone<<endl; 
	     out.close(); 
		  
		   
          
		  
 
 
	 
	} 
	 
	   catch(_com_error e)///捕捉异常 
	   { 
		    
		  AfxMessageBox("此顾客已存在!");///显示错误信息 
		    
	   } 
	     
 
	 
 
 
	  
     tiaozhuan:  
	   //m_vdeptno = ""; 
	  // m_vdeptname = ""; 
	   UpdateData(false); 
	// TODO: Add your control notification handler code here 
	 
} 
 
void Cconsumer::OnButton2()  
{ 
UpdateData(true); 
 
     
	if(MessageBox("确定要删除吗?",0, 
			MB_OKCANCEL|MB_DEFBUTTON2|MB_ICONWARNING)==IDOK) 
	{ 
	m_consumerno = "'"+m_consumerno + "'";  
	sql = "select * from consumer where  consumerno = "+m_consumerno ; 
	 
 
	////////////////////////////////////////////////////////////////////////////// 
	try 
	 { 
       m_pRecordset->Close(); 
       m_pRecordset = NULL; 
         m_pRecordset.CreateInstance(_uuidof(Recordset));// 
 
          
         m_pRecordset->Open( (_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
         m_pRecordset->Delete(adAffectCurrent); 
		  
		 //AfxMessageBox("删除记录成功!!"); 
 
		 displayintheuserlist(); 
 
		 AfxMessageBox("删除记录成功!!"); 
 
		 ofstream out("databf\\diary.txt",ios::app); 
	     out<<asctime(ptrstudent)<<"||..."<<"成功执行了向顾客表中删除数据:"<<"顾客编号:"<<m_consumerno<<"   顾客姓名:"<<m_consumername<<"   余额: "+m_leftmoney<<"   地址: "<<m_address<<"   电话: "<<m_telephone<<endl; 
	     out.close(); 
 
       
 
		 //m_pRecordset->Close(); 
         //m_pRecordset = NULL; 
     //  m_pRecordset.CreateInstance(_uuidof(Recordset));// 
     //  m_pRecordset->Open("select * from student",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
                
	 } 
	 catch(_com_error e)///捕捉异常 
	   { 
		  AfxMessageBox("删除记录失败!");///显示错误信息 
		 // panduan = 0; 
	   } 
	}	 
} 
 
void Cconsumer::OnButton3()  
{ 
UpdateData(true); 
 
	CString sql1,sql2,sql3,sql4; 
m_consumerno = "'"+m_consumerno + "'";  
m_consumername = "'"+m_consumername+"'"; 
m_leftmoney = "'"+m_leftmoney+"'"; 
m_address = "'"+m_address+"'"; 
m_telephone = "'"+m_telephone + "'";  
  
sql1 = " update consumer set consumername = "+m_consumername+" where  consumerno = "+m_consumerno ;  
sql2 = " update consumer set leftmoney = "+m_leftmoney+" where  consumerno = "+m_consumerno ;  
sql3 = " update consumer set address = "+m_address+" where consumerno = "+m_consumerno ;  
sql4 = " update consumer set telephone = "+m_telephone+" where  consumerno = "+m_consumerno ;  
 
 	 
  
    try 
	 { 
       m_pRecordset->Close(); 
       m_pRecordset = NULL; 
         m_pRecordset.CreateInstance(_uuidof(Recordset));// 
         m_pRecordset->Open( (_variant_t)sql1,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
         m_pRecordset->Open( (_variant_t)sql2,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
		 m_pRecordset->Open( (_variant_t)sql3,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
		 m_pRecordset->Open( (_variant_t)sql4,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
		displayintheuserlist(); 
         AfxMessageBox("修改记录成功!");  
 
		 ofstream out("databf\\diary.txt",ios::app); 
	     out<<asctime(ptrstudent)<<"||..."<<"成功执行了向顾客表中修改数据:"<<"顾客编号:"<<m_consumerno<<"   顾客姓名:"<<m_consumername<<"   余额: "+m_leftmoney<<"   地址: "<<m_address<<"   电话: "<<m_telephone<<endl; 
	     out.close(); 
		 // m_pRecordset->Open( (_variant_t)sql1,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
		  
		 //m_pRecordset->Close(); 
        // m_pRecordset = NULL; 
	  // m_pRecordset.CreateInstance(_uuidof(Recordset));// 
     //  m_pRecordset->Open("select * from student",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
                
	 } 
	 catch(_com_error e)///捕捉异常 
	   { 
		  //AfxMessageBox("删除记录失败!");///显示错误信息 
		   
	   }	 
} 
 
void Cconsumer::OnButton4()  
{ 
//顾客编号 
//顾客姓名 
//余额 
//电话 
	UpdateData(true); 
    /////////////////////////////////// 
  
	if (m_mode=="顾客编号") 
		mode="consumerno"; 
	if (m_mode=="顾客姓名") 
		mode="consumername"; 
    if (m_mode=="余额") 
		mode="leftmoney"; 
	if (m_mode=="电话") 
		mode="telephone"; 
 
	  
	////////////////////////////////// 
	  
 
	condition = m_condition; 
	  
	//if (mode1 == "" && mode2 == "") 
		//AfxMessageBox("请选择查询方式!"); 
	//else 
	if(mode == "") 
		AfxMessageBox("请选择查询方式!"); 
	 
	if(option11 == true) 
	{ 
	 sql = "select * from consumer where " + mode +" "+ option1 +" " + "'"+ condition + "'" ; 
	   
	} 
	   
	 else 
		goto label; 
	 
 
	//sql = (mode1 + option1 + condition1) + (andor + mode2 + option2 + condition2); 
	try 
	{ 
		//AfxMessageBox("请选择查询方式!"); 
		m_pRecordset->Close(); 
		m_pRecordset = NULL; 
		 
	    m_userlist.DeleteAllItems(); 
 
		m_pRecordset.CreateInstance("ADODB.Recordset"); 
 
		//m_pRecordset->MoveFirst(); 
		//m_pRecordset.CreateInstance(_uuidof(Recordset));// 
		//m_pRecordset->Open("select  *  from employeeview",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
        m_pRecordset->Open( (_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	    //AfxMessageBox("请选择查询方式!"); 
		//m_pRecordset->Close(); 
		 
	     
		//m_pRecordset->Open("select  *  from employeeview where sql",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); 
	    if(m_pRecordset->adoEOF) 
			AfxMessageBox("没有满足您查询的结果!"); 
	     
		//m_bSuccess = TRUE; 
		 
		while(!m_pRecordset->adoEOF) 
		{ 
			 
			//AfxMessageBox("请选择查询方式!"); 
			  
			vconsumerno= m_pRecordset->GetCollect("consumerno"); 
			vconsumername= m_pRecordset->GetCollect("consumername"); 
			vleftmoney= m_pRecordset->GetCollect("leftmoney"); 
			vaddress = m_pRecordset->GetCollect("address"); 
			vtelephone= m_pRecordset->GetCollect("telephone"); 
			  
			  
			 
			  
			nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vconsumerno);//先插第一个项 
			m_userlist.SetItem(nItem,1,1,(_bstr_t)vconsumername,NULL,0,0,0); 
			m_userlist.SetItem(nItem,2,1,(_bstr_t)vleftmoney,NULL,0,0,0); 
			m_userlist.SetItem(nItem,3,1,(_bstr_t)vaddress ,NULL,0,0,0); 
			m_userlist.SetItem(nItem,4,1,(_bstr_t)vtelephone ,NULL,0,0,0); 
			  
			  
			 
 
			m_pRecordset->MoveNext(); 
		} 
	} 
	catch(_com_error e)///捕捉异常 
	{ 
		//AfxMessageBox("读取数据库失败!");///显示错误信息 
	} 
     
	//对逻辑运算的符号的恢复,以供下一次的查询 。 
 label:	 //AfxMessageBox("请选择查询方式!"); 
	//option11 = false; 
	    //option22 = false; 
	    //andor00 = false; 
	//////////////////////////////////////////////////////////////////// 
                 
	 
	//mode1 = ""; 
	//mode2 = ""; 
	//option1 = ""; 
	//option2 = ""; 
	//andor = ""; 
     
   UpdateData(false); 
	 
} 
 
void Cconsumer::OnRadio1()  
{ 
option1 = "<"; 
	option11 = true;	 
} 
 
void Cconsumer::OnRadio2()  
{ 
option1 = "="; 
	option11 = true;	 
} 
 
void Cconsumer::OnRadio3()  
{ 
    option1 = ">"; 
	option11 = true;	 
}