www.pudn.com > duanxinfangwei.rar > FormView_MakeNewCode.cpp


// FormView_MakeNewCode.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "smspreventforgery.h" 
#include "FormView_MakeNewCode.h" 
#include "Dialog_MakeCodeSet.h" 
#include "DbInterface.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFormView_MakeNewCode 
 
IMPLEMENT_DYNCREATE(CFormView_MakeNewCode, CListView) 
 
CFormView_MakeNewCode::CFormView_MakeNewCode() 
{ 
	::srand((unsigned)time(NULL)); 
} 
 
CFormView_MakeNewCode::~CFormView_MakeNewCode() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CFormView_MakeNewCode, CListView) 
	//{{AFX_MSG_MAP(CFormView_MakeNewCode) 
	ON_WM_TIMER() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFormView_MakeNewCode drawing 
 
void CFormView_MakeNewCode::OnDraw(CDC* pDC) 
{ 
	CDocument* pDoc = GetDocument(); 
	// TODO: add draw code here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFormView_MakeNewCode diagnostics 
 
#ifdef _DEBUG 
void CFormView_MakeNewCode::AssertValid() const 
{ 
	CListView::AssertValid(); 
} 
 
void CFormView_MakeNewCode::Dump(CDumpContext& dc) const 
{ 
	CListView::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CFormView_MakeNewCode message handlers 
 
 
 
void CFormView_MakeNewCode::OnInitialUpdate()  
{ 
	CListView::OnInitialUpdate(); 
	 
	/* 改变列表风格 */ 
	CListCtrl &listctrl = GetListCtrl(); 
 
	listctrl.ModifyStyle(0, LVS_REPORT); 
	listctrl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); 
 
	listctrl.InsertColumn(1, "防伪号", LVCFMT_LEFT, 150); 
 
	/* 显示设置窗口 */ 
	SetMakeCodePara(); 
	 
} 
 
/********************************************************************** 
 * 函数名:	SetMakeCodePara 
 * 功  能:  设定生成防伪码条件 
 * 参  数:  void 
 * 返回值:  void 
 *********************************************************************/ 
void CFormView_MakeNewCode::SetMakeCodePara() 
{ 
	CDialog_MakeCodeSet dlg; 
	if(IDOK == dlg.DoModal()) 
	{ 
		m_codenum		= dlg.m_out_codenum; 
		m_file			= dlg.m_out_file; 
		m_codeLen		= dlg.m_out_codeLen; 
		m_productId		= dlg.m_out_productId; 
		m_useLetter		= dlg.m_out_useLetter;	 
		m_useNum		= dlg.m_out_useNum; 
 
		// 设定定时器 
		SetTimer(STARTMAKECODE, 500, 0); 
	} 
} 
 
/********************************************************************** 
 * 函数名:	OnTimer 
 * 功  能:  定时事件 
 * 参  数:  UINT 
 * 返回值:  void 
 *********************************************************************/ 
void CFormView_MakeNewCode::OnTimer(UINT nIDEvent)  
{ 
	if(STARTMAKECODE == nIDEvent) 
	{ 
		KillTimer(STARTMAKECODE); 
		MakeCode(); 
	} 
	 
	CListView::OnTimer(nIDEvent); 
} 
 
/********************************************************************** 
 * 函数名:	MakeCode 
 * 功  能:  生成防伪号并写入数据库及显示 
 * 参  数:  void 
 * 返回值:  void 
 *********************************************************************/ 
void CFormView_MakeNewCode::MakeCode() 
{ 
	CString singleCode;			//单个防伪号 
	CString elementList;		//组成元素 
	CDbInterface db;			//数据库操作接口 
	bool	existFlag;			//存在标志 
	CString gentime;			//写入批号 
	std::list codelist;//号码列表 
	CListCtrl &listctrl = GetListCtrl(); 
 
	/* 生成组成元素 */ 
	if(m_useNum && !m_useLetter) 
	{ 
		elementList.Format("%s", "0123456789"); 
	} 
	else if(!m_useNum && m_useLetter) 
	{ 
		elementList.Format("%s", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
	} 
	else 
	{ 
		elementList.Format("%s", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
	} 
 
	/* 生成批号 */ 
	CTime current = CTime::GetCurrentTime(); 
	gentime.Format("%s", current.Format("%Y%m%d%H%M%S")); 
 
	/* 生成指定个数防伪号并写入数据库及显示 */ 
	codelist.clear(); 
	for(int currentCodeNum = 0; currentCodeNum < m_codenum; currentCodeNum++) 
	{ 
		/* 取得防伪号 */ 
		do 
		{ 
			singleCode.Empty(); 
			singleCode = GetRandString(elementList, m_codeLen); 
			 
			existFlag = false; 
			if(!db.CheckCodeExist(singleCode, existFlag)) 
			{ 
				MessageBox("数据库操作错误!", "错误", MB_OK | MB_ICONERROR); 
				goto ENDFLAG; 
			} 
		}while(existFlag); 
 
		/* 防伪号写入数据库 */ 
		CODEINFO codeinfo; 
		codeinfo.code = singleCode; 
		codeinfo.gentime = gentime; 
		codeinfo.productId = m_productId; 
		codeinfo.outflag = OUTFACTORY; 
		codeinfo.stateflag = OKCODE; 
		codeinfo.querytimes = 0; 
		if(!db.AppendNewCode(codeinfo)) 
		{ 
			currentCodeNum--; 
		} 
		else 
		{ 
			//加入新防伪号列表 
			codelist.push_back(singleCode); 
			//显示新防伪号 
			listctrl.InsertItem(LVIF_TEXT | LVIF_STATE, 0, 0, 0, 0, 0, 0); 
			listctrl.SetItemText(0, 0, codeinfo.code); 
			PeekAndPump(); 
		} 
		 
	}// end of for(int currentCodeNum = 0; currentCodeNum < m_codenum; currentCodeNum++) 
 
ENDFLAG: 
 
	/* 写入导出文件 */ 
	ExportCodeFile(codelist); 
 
	return; 
} 
 
/********************************************************************** 
 * 函数名:	GetRandString 
 * 功  能:  按指定字符及长度生成随机字符串 
 * 参  数:  CString							组成元素列表 
 *       :  int   
 * 返回值:  void 
 *********************************************************************/ 
CString CFormView_MakeNewCode::GetRandString(CString elementlist, int len) 
{ 
	CString randString;		//生成的随机串 
	int		elementlistLen;	//合法字符个数 
	char	singleChar;		//随机取得的单个字符 
	int		randNum;		//随机数 
 
	randString.Empty(); 
	elementlistLen = elementlist.GetLength(); 
 
	for(int loop = 0; loop < len; loop++) 
	{ 
		randNum = ::rand()%elementlistLen; 
		singleChar = elementlist.GetAt(randNum); 
		randString += singleChar; 
	} 
 
	return randString; 
} 
 
/********************************************************************** 
 * 函数名:	exportCodeFile 
 * 功  能:  导出防伪号文件 
 * 参  数:  std::list					号码列表 
 *       :  int   
 * 返回值:  void 
 *********************************************************************/ 
void CFormView_MakeNewCode::ExportCodeFile(std::list codelist) 
{ 
	CFile exportFile; 
	CString singleCode; 
	CString splitFlag; 
 
	splitFlag = SPLITFLAG; 
 
	if(!exportFile.Open(m_file, CFile::modeCreate | CFile::modeWrite)) 
	{ 
		MessageBox("导出防伪号文件错误!", "错误", MB_OK | MB_ICONERROR); 
		return; 
	} 
 
	while(codelist.size() > 0) 
	{ 
		singleCode = codelist.front(); 
		codelist.pop_front(); 
 
		exportFile.Write(singleCode, singleCode.GetLength()); 
		exportFile.Write(splitFlag, splitFlag.GetLength()); 
	} 
 
	exportFile.Close(); 
} 
 
/********************************************************************** 
 * 函数名:	PeekAndPump 
 * 功  能:	 
 * 参  数:  void 
 * 返回值:	BOOL				 
 *									 
 **********************************************************************/ 
BOOL CFormView_MakeNewCode::PeekAndPump() 
{ 
	static MSG msg; 
	while(::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) 
	{ 
		if(!AfxGetApp()->PumpMessage()){ 
			::PostQuitMessage(0); 
			return FALSE; 
		} 
	} 
	return TRUE; 
}