// ReCharge.cpp : implementation file
//

#include "stdafx.h"
#include "LYFZSendMsg.h"
#include "ReCharge.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ReCharge dialog
ReCharge::ReCharge(CWnd* pParent /*=NULL*/)
	: CDialog(ReCharge::IDD, pParent)
{
	//{{AFX_DATA_INIT(ReCharge)
	m_account = _T("");
	m_name = _T("");
	m_money = _T("");
	m_count = _T("");
	m_price = _T("");
	//}}AFX_DATA_INIT

}

void ReCharge::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ReCharge)
	DDX_Control(pDX, IDC_EDITcount, m_editnum2);
	DDX_Control(pDX, IDC_EDITmoney, m_editnum1);
	DDX_Text(pDX, IDC_EDITaccount, m_account);
	DDX_Text(pDX, IDC_EDITname, m_name);
	DDX_Text(pDX, IDC_EDITmoney, m_money);
	DDX_Text(pDX, IDC_EDITcount, m_count);
	DDX_Text(pDX, IDC_EDITprice, m_price);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(ReCharge, CDialog)
	//{{AFX_MSG_MAP(ReCharge)
	ON_EN_CHANGE(IDC_EDITmoney, OnChangeEDITmoney)
	ON_EN_CHANGE(IDC_EDITcount, OnChangeEDITcount)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ReCharge message handlers

BOOL ReCharge::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
/*	CRecordset myset(&g_db);
	CString	sql="select ReCharge from ReCharge"; 
	myset.Open (CRecordset::forwardOnly, sql);
	myset.GetFieldValue ("ReCharge", m_oldReCharge);
	myset.Close();
	this->CenterWindow (g_pMainWnd);*/
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void ReCharge::OnChangeEDITmoney() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	UpdateData();
	if(atof(m_count))
	{
		m_price.Format ("%0.4f", atof(m_money)/atof(m_count));
	}
	else
		m_price="0";
	UpdateData(false);
	// TODO: Add your control notification handler code here
	
}

void ReCharge::OnChangeEDITcount() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	OnChangeEDITmoney();
	// TODO: Add your control notification handler code here
	
}

void ReCalAccount(CString account)
{
	CString	sql;
	if(1)
	{
		
	MyLock lock("xiaoaccessdbmsg");
    CRecordset myset(&g_db);
	CString count1,count2,count3;
	

#ifdef SQLSERVER_VERSION
		sql="select sum(cast([count] as float)) as cot from recharge where account='"+account+"'"; 
#else
		sql="select sum([count]) as cot from recharge where account='"+account+"'"; 
#endif


	myset.Open (CRecordset::forwardOnly, sql);
	if(!myset.IsEOF())
	myset.GetFieldValue ("cot", count1);
	myset.Close();
	
	 
#ifdef SQLSERVER_VERSION
	sql="select sum(cast([msgcount] as float)) as cot from sendreg where account='"+account+"'";
#else
	sql="select sum([msgcount]) as cot from sendreg where account='"+account+"'";
#endif

	myset.Open (CRecordset::forwardOnly, sql);
	if(!myset.IsEOF())
		myset.GetFieldValue ("cot", count2);
	myset.Close();

	
#ifdef SQLSERVER_VERSION
	sql="select sum(cast([msgcount2] as float)) as cot from sendreg where account='"+account+"'";
#else
	sql="select sum([msgcount2]) as cot from sendreg where account='"+account+"'";
#endif

	myset.Open (CRecordset::forwardOnly, sql);
	if(!myset.IsEOF())
		myset.GetFieldValue ("cot", count3);
	myset.Close();

//	sql.Format ("update clientinfo set balance='%d' where account='%s'", atoi(count1)-min(atoi(count2),atoi(count3)), account);
	sql.Format ("update clientinfo set balance='%d' where account='%s'", atoi(count1)-atoi(count2), account);

	}

    MyExecuteSQL(&g_db, sql);
}

void ReCharge::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	m_count.TrimLeft ();
	m_count.TrimRight ();
	m_money.TrimLeft ();
	m_money.TrimRight ();
 	if(atoi(m_count)==0)
	{
		AfxMessageBox("������������Ϊ0!", MB_ICONINFORMATION);
		return;
	}
	CString timestamp=CTime::GetCurrentTime ().Format ("%Y-%m-%d");
    CString sql;
	sql="insert into recharge([account],[money],[count],[timestamp]) values('"+m_account+"','"+m_money+"','"+m_count+"','"+timestamp+"')";
//	MyLock lock("msgaccessdb");
    MyExecuteSQL(&g_db, sql);
    ReCalAccount(m_account);
	AfxMessageBox("��ֵ�ɹ�!", MB_ICONINFORMATION);

	sql="delete from nullbalance where [account]='"+m_account+"'";
    MyExecuteSQL(&g_db, sql);
	CDialog::OnOK();
}