// SellRuleDlg.cpp: 实现文件
|
//
|
#include "common/pch.h"
|
#include "FloatTrade.h"
|
#include "SellRuleDlg.h"
|
#include "../common/NetworkApi.h"
|
#include "../common/StringUtil.h"
|
#include "../common/JsonUtil.h"
|
#include "afxdialogex.h"
|
#include <thread>
|
|
|
// SellRuleDlg 对话框
|
|
IMPLEMENT_DYNAMIC(SellRuleDlg, CDialogEx)
|
|
|
SellRuleDlg::SellRuleDlg(CString code, CString volume, CWnd* pParent /*=nullptr*/)
|
: CDialogEx(IDD_DIALOG_SELL_RULE, pParent)
|
{
|
this->initCode = code;
|
this->initVolume = volume;
|
}
|
|
SellRuleDlg::~SellRuleDlg()
|
{
|
}
|
|
void SellRuleDlg::DoDataExchange(CDataExchange* pDX)
|
{
|
CDialogEx::DoDataExchange(pDX);
|
DDX_Control(pDX, IDC_STATIC_ID, labelID);
|
DDX_Control(pDX, IDC_EDIT_CODE, editCode);
|
DDX_Control(pDX, IDC_EDIT_BUY1_VOLUME, editBuy1Volume);
|
DDX_Control(pDX, IDC_EDIT_SELL_VOLUME, editSellVolume);
|
DDX_Control(pDX, IDC_EDIT_END_TIME, editEndTime);
|
DDX_Control(pDX, IDC_BUTTON_CLEAR, btnClear);
|
DDX_Control(pDX, IDC_BUTTON_DELETE, btnDel);
|
DDX_Control(pDX, IDC_BUTTON_ADD, btnAdd);
|
DDX_Control(pDX, IDC_LIST_RULES, listRules);
|
DDX_Control(pDX, IDC_EDIT_BUY1_PRICE, editBuy1Price);
|
DDX_Control(pDX, IDC_COMBO_SELL_PRICE_TYPE, comboSellPriceType);
|
}
|
|
|
BEGIN_MESSAGE_MAP(SellRuleDlg, CDialogEx)
|
ON_BN_CLICKED(IDC_BUTTON_CLEAR, &SellRuleDlg::OnBnClickedButtonClear)
|
ON_BN_CLICKED(IDC_BUTTON_DELETE, &SellRuleDlg::OnBnClickedButtonDelete)
|
ON_BN_CLICKED(IDC_BUTTON_ADD, &SellRuleDlg::OnBnClickedButtonAdd)
|
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_RULES, &SellRuleDlg::OnLvnItemchangedListRules)
|
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST_RULES, &SellRuleDlg::OnNMCustomdrawListRules)
|
END_MESSAGE_MAP()
|
|
|
// SellRuleDlg 消息处理程序
|
|
|
BOOL SellRuleDlg::OnInitDialog()
|
{
|
CDialogEx::OnInitDialog();
|
|
if (this->initCode) {
|
editCode.SetWindowTextW(this->initCode);
|
}
|
if (this->initVolume) {
|
editSellVolume.SetWindowTextW(this->initVolume);
|
}
|
editEndTime.SetWindowTextW(L"14:56:55");
|
initView();
|
std::thread t1(requestListRules, this);
|
t1.detach();
|
return TRUE; // return TRUE unless you set the focus to a control
|
// 异常: OCX 属性页应返回 FALSE
|
}
|
|
|
|
void SellRuleDlg::OnBnClickedButtonClear()
|
{
|
labelID.SetWindowTextW(L"");
|
editBuy1Volume.SetWindowTextW(L"");
|
editCode.SetWindowTextW(L"");
|
editSellVolume.SetWindowTextW(L"");
|
editEndTime.SetWindowTextW(L"14:56:55");
|
}
|
|
|
void SellRuleDlg::initView()
|
{
|
//初始化列表
|
listRules.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES| WS_HSCROLL);
|
listRules.ModifyStyle(LVS_SINGLESEL, LVS_REPORT);
|
listRules.SetRowHeigt(24);
|
|
CRect rect;
|
listRules.GetClientRect(&rect);
|
|
int unitSize = (rect.Width() - 5) / 7.5f;
|
|
listRules.InsertColumn(0, _T("代码"), LVCFMT_LEFT, unitSize*1, 0);
|
listRules.InsertColumn(1, _T("买1量(手)"), LVCFMT_LEFT, unitSize * 1.1, 1);
|
listRules.InsertColumn(2, _T("买1价"), LVCFMT_LEFT, unitSize * 1, 2);
|
listRules.InsertColumn(3, _T("卖量(股)"), LVCFMT_LEFT, unitSize * 1.1, 3);
|
listRules.InsertColumn(4, _T("卖价类型"), LVCFMT_LEFT, unitSize * 1.2, 4);
|
listRules.InsertColumn(5, _T("结束时间"), LVCFMT_LEFT, unitSize * 1.3, 5);
|
listRules.InsertColumn(6, _T("状态"), LVCFMT_LEFT, unitSize * 0.8, 6);
|
// listRules.InsertColumn(7, _T("ID"), LVCFMT_LEFT, 50, 7);
|
// listRules.InsertColumn(8, _T("创建时间"), LVCFMT_LEFT, 50, 8);
|
listRules.AutoColumn();
|
|
list<string> types = get_price_types();
|
for (list<string>::iterator e = types.begin(); e != types.end();++e) {
|
comboSellPriceType.AddString(CString((*e).c_str()));
|
}
|
comboSellPriceType.SetCurSel(0);
|
}
|
|
void SellRuleDlg::showDetail(SellRule rule) {
|
labelID.SetWindowTextW(rule.id_);
|
editBuy1Volume.SetWindowTextW(CString(std::to_wstring(rule.buy1_volume).c_str()));
|
editBuy1Price.SetWindowTextW(rule.buy1_price);
|
editCode.SetWindowTextW(rule.code);
|
editSellVolume.SetWindowTextW(CString(std::to_wstring(rule.sell_volume).c_str()));
|
comboSellPriceType.SetCurSel(rule.sell_price_type);
|
editEndTime.SetWindowTextW(rule.end_time);
|
}
|
|
void SellRuleDlg::requestListRules(SellRuleDlg* app)
|
{
|
string result = NetworkApi::list_sell_rules();
|
auto doc = JsonUtil::parseUTF16(result);
|
if (doc.IsObject()) {
|
if (doc[L"code"].GetInt() == 0) {
|
auto array = doc[L"data"].GetArray();
|
list<SellRule> rules;
|
for (int i = 0; i < array.Size(); i++) {
|
SellRule rule;
|
rule.code = array[i][L"code"].GetString();
|
rule.id_ = array[i][L"id_"].GetString();
|
rule.buy1_volume = array[i][L"buy1_volume"].GetInt() / 100;
|
rule.buy1_price = array[i][L"buy1_price"].GetString();
|
rule.sell_volume = array[i][L"sell_volume"].GetInt();
|
rule.sell_price_type = array[i][L"sell_price_type"].GetInt();
|
rule.create_time = array[i][L"create_time"].GetString();
|
rule.excuted = array[i][L"excuted"].GetInt();
|
rule.end_time = array[i][L"end_time"].GetString();
|
rules.push_back(rule);
|
}
|
app->listRules.SetRedraw(false);
|
app->listRules.DeleteAllItems();
|
app->ruleList = rules;
|
int index = 0;
|
list<string> TYPES = get_price_types();
|
for (list<SellRule>::iterator ele = app->ruleList.begin(); ele != app->ruleList.end(); ++ele) {
|
SellRule rule = *ele;
|
app->listRules.InsertItem(index, rule.id_);
|
app->listRules.SetItemText(index, 0, rule.code);
|
app->listRules.SetItemText(index, 1, std::to_wstring(rule.buy1_volume).c_str());
|
app->listRules.SetItemText(index, 2, rule.buy1_price);
|
app->listRules.SetItemText(index, 3, std::to_wstring(rule.sell_volume).c_str());
|
list<string>::iterator its = TYPES.begin();
|
std::advance(its, rule.sell_price_type);
|
string price_type = *its;
|
app->listRules.SetItemText(index, 4, CString(price_type.c_str()));
|
app->listRules.SetItemText(index, 5, rule.end_time);
|
app->listRules.SetItemText(index, 6, rule.excuted ? L"已执行" : L"未执行");
|
//app->listRules.SetItemText(index, 7, rule.id_);
|
//app->listRules.SetItemText(index, 8, rule.create_time);
|
if (rule.excuted) {
|
app->listRules.SetItemData(index, COLOR_RED);
|
}
|
index++;
|
}
|
|
}
|
app->listRules.SetRedraw(true);
|
}
|
else {
|
AfxMessageBox(doc[L"msg"].GetString());
|
}
|
}
|
|
|
|
void SellRuleDlg::OnBnClickedButtonDelete()
|
{
|
CString id;
|
this->labelID.GetWindowTextW(id);
|
if (id.GetLength() < 1) {
|
AfxMessageBox(L"尚未选中代码");
|
return;
|
}
|
string result = NetworkApi::del_sell_rule(id);
|
rapidjson::GenericDocument<rapidjson::UTF16<>> doc = JsonUtil::parseUTF16(result);
|
if (doc[L"code"].GetInt() == 0) {
|
MessageBox(L"删除成功", L"提示", MB_TASKMODAL);
|
requestListRules(this);
|
}
|
else {
|
AfxMessageBox(doc[L"msg"].GetString());
|
}
|
}
|
|
|
void SellRuleDlg::OnBnClickedButtonAdd()
|
{
|
CString id;
|
CString code;
|
CString buy1Volume;
|
CString buy1Price;
|
CString sellVolume;
|
int sellPriceType = comboSellPriceType.GetCurSel();
|
CString endTime;
|
this->labelID.GetWindowTextW(id);
|
this->editBuy1Volume.GetWindowTextW(buy1Volume);
|
this->editBuy1Price.GetWindowTextW(buy1Price);
|
this->editCode.GetWindowTextW(code);
|
this->editSellVolume.GetWindowTextW(sellVolume);
|
this->editEndTime.GetWindowTextW(endTime);
|
if (id.GetLength() > 0) {
|
AfxMessageBox(L"请先清除数据");
|
return;
|
}
|
if (code.GetLength() != 6) {
|
AfxMessageBox(L"代码输入不正确");
|
return;
|
}
|
if (buy1Volume.GetLength() < 1) {
|
AfxMessageBox(L"请输入买1量");
|
return;
|
}
|
if (sellVolume.GetLength() < 1) {
|
AfxMessageBox(L"请输入卖量");
|
return;
|
}
|
|
if (endTime.GetLength() < 1) {
|
AfxMessageBox(L"请输入生效截止时间");
|
return;
|
}
|
|
int buy1Volume_int = stoi(StringUtil::cstring2String(buy1Volume));
|
|
rapidjson::StringBuffer buf;
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buf);
|
writer.StartObject();
|
writer.Key("code");
|
writer.String(StringUtil::cstring2String(code).c_str());
|
writer.Key("buy1_volume");
|
writer.Int(buy1Volume_int * 100);
|
|
writer.Key("buy1_price");
|
writer.String(StringUtil::cstring2String(buy1Price).c_str());
|
|
writer.Key("sell_volume");
|
writer.Int(stoi(StringUtil::cstring2String(sellVolume)));
|
|
writer.Key("sell_price_type");
|
writer.Int(sellPriceType);
|
|
writer.Key("end_time");
|
writer.String(StringUtil::cstring2String(endTime).c_str());
|
writer.EndObject();
|
const char* json_content = buf.GetString();
|
string result = NetworkApi::add_sell_rule(JsonUtil::parseUTF8(json_content));
|
rapidjson::GenericDocument<rapidjson::UTF16<>> doc = JsonUtil::parseUTF16(result);
|
if (doc[L"code"].GetInt() == 0) {
|
MessageBox(L"添加成功", L"提示", MB_TASKMODAL);
|
requestListRules(this);
|
}
|
else {
|
AfxMessageBox(doc[L"msg"].GetString());
|
}
|
}
|
|
void SellRuleDlg::OnLvnItemchangedListRules(NMHDR* pNMHDR, LRESULT* pResult)
|
{
|
NMLISTVIEW* pLV = reinterpret_cast<NMLISTVIEW*>(pNMHDR);
|
// 检查是否为行选中事件,且为选中状态,避免重复触发
|
if ((pLV->uChanged & LVIF_STATE) && (pLV->uNewState & LVIS_SELECTED))
|
{
|
int rowIndex = pLV->iItem;
|
std::list<SellRule>::iterator ele = ruleList.begin();
|
std::advance(ele, rowIndex);
|
SellRule rule = *ele;
|
showDetail(rule);
|
}
|
*pResult = 0;
|
}
|
|
|
void SellRuleDlg::OnNMCustomdrawListRules(NMHDR* pNMHDR, LRESULT* pResult)
|
{
|
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
|
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
|
// TODO: 在此添加控件通知处理程序代码
|
|
NMCUSTOMDRAW nmCustomDraw = pLVCD->nmcd;
|
|
if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
|
{
|
*pResult = CDRF_NOTIFYITEMDRAW;
|
}
|
else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
|
{
|
//整行处理
|
if (COLOR_RED == nmCustomDraw.lItemlParam)
|
{
|
pLVCD->clrText = RGB(206, 38, 26);
|
}
|
else if (COLOR_DEFAULT == nmCustomDraw.lItemlParam)
|
{
|
|
pLVCD->clrText = RGB(96, 96, 96);
|
}
|
*pResult = CDRF_NOTIFYSUBITEMDRAW;
|
}//这上面的都是默认的不用理
|
else if ((CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage)
|
{
|
*pResult = 0;
|
// 暂时不处理每一列
|
return;
|
}
|
|
}
|
|
std::list<string> SellRuleDlg::get_price_types()
|
{
|
std::list<string> types;
|
types.push_back("价格笼子");
|
types.push_back("跌停价");
|
types.push_back("涨停价");
|
types.push_back("现价");
|
types.push_back("买5价");
|
return types;
|
}
|