📄 buystock.cpp
字号:
// BuyStock.cpp : implementation file
//
#include "stdafx.h"
#include "StockSystem.h"
#include "BuyStock.h"
#include "AccountTreeView.h"
#include "PersonFrame.h"
#include "StockSystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBuyStock dialog
CBuyStock::CBuyStock(CWnd* pParent /*=NULL*/)
: CDialog(CBuyStock::IDD, pParent)
{
//{{AFX_DATA_INIT(CBuyStock)
m_remark = _T("");
m_stcokcode = _T("");
m_buyamount = 0;
m_buysprice = 0.0;
m_canbuyamount = 0;
m_currentmoney = 0.0;
m_fare = 0.0;
m_allfare = 0.0;
//}}AFX_DATA_INIT
}
void CBuyStock::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBuyStock)
DDX_Control(pDX, IDC_BUYSTOCK_MARTTYPE, m_marttype);
DDX_Control(pDX, IDC_BUYSTOCK_DEALDATE, m_dealdate);
DDX_Control(pDX, IDC_BUTSTOCK_STOCKCODE, m_stockcode_c);
DDX_Text(pDX, IDC_BUGSTOCK_REMARK, m_remark);
DDX_Text(pDX, IDC_BUTSTOCK_STOCKCODE, m_stcokcode);
DDX_Text(pDX, IDC_BUYSTOCK_BUYAMOUNT, m_buyamount);
DDX_Text(pDX, IDC_BUYSTOCK_BUYSPRICE, m_buysprice);
DDX_Text(pDX, IDC_BUYSTOCK_CANBUYAMOUNT, m_canbuyamount);
DDX_Text(pDX, IDC_BUYSTOCK_CURRENTMONEY, m_currentmoney);
DDX_Text(pDX, IDC_BUYSTOCK_FARE, m_fare);
DDX_Text(pDX, IDC_BUYSTOCK_ALLFARE, m_allfare);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBuyStock, CDialog)
//{{AFX_MSG_MAP(CBuyStock)
ON_BN_CLICKED(IDC_BTN_OK, OnBtnOk)
ON_BN_CLICKED(IDC_BTN_CANCLE, OnBtnCancle)
ON_WM_CLOSE()
ON_EN_KILLFOCUS(IDC_BUTSTOCK_STOCKCODE, OnKillfocusButstockStockcode)
ON_EN_CHANGE(IDC_BUYSTOCK_BUYSPRICE, OnChangeBuystockBuysprice)
ON_EN_CHANGE(IDC_BUYSTOCK_BUYAMOUNT, OnChangeBuystockBuyamount)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBuyStock message handlers
void CBuyStock::OnOK()
{
}
void CBuyStock::OnCancel()
{
}
//买入股票
void CBuyStock::OnBtnOk()
{
UpdateData();
if (m_stcokcode.GetLength() != 6)
{
MessageBox("股票代码必须为6位","提示");
return;
}
if (m_allfare > m_currentmoney)
{
MessageBox("账户余额不够,不能买入","提示");
return;
}
//数据都正确后,把数据插入数据库
CStockSystemApp *pApp = (CStockSystemApp *)AfxGetApp();
CAdoRecordSet record;
CString command;
CString account_id;
//先从账户表里面获取当前账户的ID
command.Format("select account_id from tb_account where name='%s'",currentuser);
pApp->m_pConnection.GetRecordSet(command, record);
record.GetValue("account_id", account_id);
//获得股票名称,从TB_STOCK_BASE_INFO表
CString stock_name;
command.Empty();
command.Format("select stock_for_short from tb_stock_base_info where stock_spec_code='%s'", m_stcokcode);
pApp->m_pConnection.GetRecordSet(command, record);
record.GetValue("stock_for_short", stock_name);
//算出成本价格
double m_costprice = m_buysprice + m_fare/m_buyamount;
//获取市场类型
CString mart_type;
if (m_marttype.GetCurSel() == 0)
{
mart_type = "上海";
}
else
{
mart_type = "深圳";
}
//把买入股票的信息写入数据库
pApp->m_pConnection.AddStockInfoToTB_SUMMARYLIST(account_id, m_stcokcode,stock_name,
m_buysprice,m_costprice, m_buyamount, m_buysprice, m_allfare);
//把交易信息写入到交易,明细表TB_DEALDETAIL
// pApp->m_pConnection.AddStockInfoToTB_DEALDETAIL()
//保存当前的买入信息,写入结构体
CString time;
CTime m_thetime;
m_thetime = CTime::GetCurrentTime();
time = m_thetime.Format("%Y%M%D%H%M%S");
StockDealList deallist;
deallist.cost_price = m_costprice;
deallist.date = time;
deallist.mart_value = m_buysprice * m_buyamount;
deallist.payoff_odds = -1;
deallist.price = m_buysprice;
deallist.profit_lost = 0 - m_allfare;
deallist.stock_amount = m_buyamount;
deallist.stockcode = m_stcokcode;
deallist.stockname = stock_name;
deallist.deal_mart = mart_type;
//保存到VECT
pApp->StockDealListVect.push_back(deallist);
//更新list控件,显示当前交易的股票信息
CString textstr;
CPersonFrame *pframe = (CPersonFrame *)GetParent();
CMyListView *the_list =(CMyListView *)(pframe->m_splitterwnd.GetPane(0,1));
CListCtrl& m_list = the_list->GetListCtrl();
m_list.DeleteAllItems();
int len = pApp->StockDealListVect.size();
for (int i = 0; i<len; i++)
{
StockDealList listbuf;
listbuf = pApp->StockDealListVect[i];
textstr.Format("%d", i+1);
m_list.InsertItem(i, textstr);
textstr.Empty();
textstr.Format("%s", listbuf.stockcode);
m_list.SetItemText(i, 1, textstr);
textstr.Empty();
textstr.Format("%s", listbuf.stockname);
m_list.SetItemText(i, 2, textstr);
textstr.Empty();
textstr.Format("%d", listbuf.stock_amount);
m_list.SetItemText(i, 3, textstr);
textstr.Empty();
textstr.Format("%.2f", listbuf.mart_value);
m_list.SetItemText(i, 4, textstr);
textstr.Empty();
textstr.Format("%.2f", listbuf.cost_price);
m_list.SetItemText(i, 5, textstr);
textstr.Empty();
textstr.Format("%.2f", listbuf.price);
m_list.SetItemText(i, 6, textstr);
textstr.Empty();
textstr.Format("%.2f", listbuf.profit_lost);
m_list.SetItemText(i, 7, textstr);
textstr.Empty();
textstr.Format("%.2f", listbuf.payoff_odds);
m_list.SetItemText(i, 8, textstr);
textstr.Empty();
textstr.Format("%s", listbuf.date);
m_list.SetItemText(i, 9, textstr);
textstr.Empty();
textstr.Format("%s", listbuf.deal_mart);
m_list.SetItemText(i, 10, textstr);
textstr.Empty();
}
//清空数据
m_remark.Empty();
m_stcokcode.Empty();
m_buyamount = 0;
m_buysprice = 0;
m_canbuyamount = 0;
m_currentmoney = 0;
m_fare = 0;
m_allfare = 0;
//更新用户的账户余额
CDialog::OnOK();
}
void CBuyStock::OnBtnCancle()
{
CDialog::OnCancel();
}
void CBuyStock::OnClose()
{
CDialog::OnCancel();
CDialog::OnClose();
}
BOOL CBuyStock::OnInitDialog()
{
CDialog::OnInitDialog();
//设置市场类型初始化
m_marttype.SetCurSel(0);
//设置对话框TITLE
CPersonFrame *pframe = (CPersonFrame *)GetParent();
CAccountTreeView *mytree =(CAccountTreeView *)(pframe->m_splitterwnd.GetPane(0,0));
mytree->GetCurrentUser(currentuser);
m_title.Format("买入股票 - [%s]", currentuser);
SetWindowText(m_title);
//设置日期为当天日期,不能改变
CTime time;
time = CTime::GetCurrentTime();
m_dealdate.SetTime(&time);
//读取数据文件,显示出帐户当前所拥有的现金
CStockSystemApp *pApp = (CStockSystemApp *)AfxGetApp();
int len = pApp->UserInfoVect.size();
for (int i=0; i< len; i++)
{
if (pApp->UserInfoVect[i].username == currentuser)
{
m_currentmoney = pApp->UserInfoVect[i].balance;
break;
}
}
UpdateData(FALSE);
return TRUE;
}
void CBuyStock::OnKillfocusButstockStockcode()
{
UpdateData();
int id = GetFocus()->GetDlgCtrlID();
if (id != 0 && id != IDC_BTN_OK && id !=IDC_BTN_CANCLE && m_stcokcode.GetLength() != 6)
{
// MessageBox("股票代码必须为6位!");
GetDlgItem(IDC_BUTSTOCK_STOCKCODE)->SetFocus();
return;
}
}
//当用户输入的价格变化的时候,相应的可买数量也要变化
void CBuyStock::OnChangeBuystockBuysprice()
{
UpdateData();
m_canbuyamount = INT(m_currentmoney/m_buysprice);
UpdateData(FALSE);
}
void CBuyStock::OnChangeBuystockBuyamount()
{
//输入买入数量的时候,获得当前的数据
//算出费用支出,总计开支等
UpdateData();
if (m_buyamount > m_canbuyamount)
{
MessageBox("账户金额不够,买入数量必须小于可买数量","提示");
return;
}
m_fare = m_buyamount * 1.36; //费用支出
m_allfare = m_fare + m_buyamount * m_buysprice;
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -