📄 reionclass.cpp
字号:
// ReionClass.cpp : 实现文件
//
#include "stdafx.h"
#include "ProjectManage.h"
#include "ReionClass.h"
#include "ADODB.h"
// CReionClass 对话框
IMPLEMENT_DYNAMIC(CReionClass, CDialog)
CReionClass::CReionClass(CWnd* pParent /*=NULL*/)
: CDialog(CReionClass::IDD, pParent)
{
m_classname = _T("");
m_tempid = 0;
}
CReionClass::~CReionClass()
{
}
void CReionClass::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_CLASSNAME, m_oClassname); //
DDX_Control(pDX, IDC_TREE1, m_oTreeclass);//
DDX_Text(pDX, IDC_EDIT_CLASSNAME, m_classname);
DDX_Text(pDX, IDC_TEMPID, m_tempid);//
}
BEGIN_MESSAGE_MAP(CReionClass, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &CReionClass::OnBntAddSame)
ON_BN_CLICKED(IDC_BUTTON2, &CReionClass::OnBntAddDown)
ON_BN_CLICKED(IDC_BUTTON3, &CReionClass::OnBntModify)
ON_BN_CLICKED(IDC_BUTTON4, &CReionClass::OnBntDelete)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CReionClass::OnTvnSelchangedTree)
END_MESSAGE_MAP()
// CReionClass 消息处理程序
void CReionClass::OnBntAddSame()
{
// TODO: 在此添加控件通知处理程序代码
CString sql;
CString newclassname,Newpar,snewid,name;
long lNewID;
hParentItem=m_oTreeclass.GetParentItem(m_oTreeclass.GetSelectedItem ());
m_oClassname.GetWindowText(name);
lNewID=GenNewID();
snewid.Format("%d",lNewID);
Newpar=Curpar;
newclassname.Format(name,lNewID);
if(Newpar=="")
{
Newpar="0";
}
try
{
_variant_t RecordsAffected;
sql="Insert into RegionTree (mod_ID,mod_NAME,mod_seq,mod_parent) VALUES ("+snewid+",'"+newclassname+"',0,"+Newpar+")";
if(lNewID==1)
sql="Insert into RegionTree (mod_ID,mod_NAME,mod_seq,mod_parent) VALUES (1,'"+newclassname+"',0,0)";
this->m_Storage.ExecSql((_bstr_t)sql);
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("增加同级分类出错:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
m_oClassname.SetWindowText(newclassname);
m_oTreeclass.InsertItem((LPCTSTR)(_bstr_t)(newclassname), hParentItem, NULL);
}
void CReionClass::OnBntAddDown()
{
// TODO: 在此添加控件通知处理程序代码
CString sql;
CString newclassname,Newpar,snewid,name;
long lNewID;
hCurrentItem=m_oTreeclass.GetSelectedItem ();
m_oClassname.GetWindowText(name);
lNewID=GenNewID();
snewid.Format("%d",lNewID);
Newpar.Format("%d",m_tempid);
newclassname.Format(name,lNewID);
try
{
sql="Insert into RegionTree (mod_ID,mod_NAME,mod_seq,mod_parent) VALUES ('"+snewid+"','"+newclassname+"','0','"+Newpar+"')";
if(lNewID==1)
sql="Insert into RegionTree (mod_ID,mod_NAME,mod_seq,mod_parent) VALUES (1,'"+newclassname+"','0','0')";
this->m_Storage.ExecSql(_bstr_t(sql));
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("增加下级分类出错:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
m_oClassname.SetWindowText(newclassname);
m_oTreeclass.InsertItem((LPCTSTR)(_bstr_t)(newclassname), hCurrentItem, NULL);
}
void CReionClass::OnBntModify()
{
// TODO: 在此添加控件通知处理程序代码
CString sql,parent,name,sdiscount;
long discount;
UpdateData();
//检查数据完整性
if(m_classname.IsEmpty())
{
AfxMessageBox("类别名称不能为空!");
return;
}
m_oClassname.GetWindowText(name);
if(CheckRepeat(name)==TRUE)
{
AfxMessageBox("类别名称重复!请重新输入!");
return;
}
if(AfxMessageBox("确定要保存修改吗?",MB_YESNO)==IDYES)
{
sdiscount.Format("%d",discount);
try
{
_variant_t RecordsAffected;
sql.Format("Update RegionTree set mod_name='%s',mod_seq=%d where mod_ID=%d",name,discount,m_tempid);
this->m_Storage.ExecSql((_bstr_t)sql);
hCurrentItem=m_oTreeclass.GetSelectedItem ();
m_oTreeclass.SetItemText(hCurrentItem,(LPCTSTR)(_bstr_t)(name));
}
catch(_com_error e)
{
CString stemp;
stemp.Format("修改类别出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
return;
}
}
}
void CReionClass::OnBntDelete()
{
// TODO: 在此添加控件通知处理程序代码
CString sql,curID;
_RecordsetPtr m_pRecordset;
HTREEITEM hCurItem;
hCurItem=m_oTreeclass.GetSelectedItem();
sql="SELECT * FROM RegionTree where mod_name ='";
sql=sql+m_classname+"'";
if(AfxMessageBox("确定删除此类别吗?",MB_YESNO)==IDYES)
{
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pDBCon->GetActiveConnection(),true),adOpenDynamic,adLockPessimistic,adCmdText);
if(!m_pRecordset->BOF)
{
curID=VariantToCString(m_pRecordset->GetCollect("mod_id"));
if(atol(curID)==1)
{
MessageBox("不能删除ID为1的类别!",m_classname);
return;
}
if (TreeSumRecordCount(curID)==0)
{
if(CountMateriel(curID)>0)
MessageBox("此类别下已有站点,不能删除!");
else
{
m_pRecordset->Delete(adAffectCurrent);
m_oTreeclass.DeleteItem(hCurItem);
}
}
if (TreeSumRecordCount(curID)>0)
MessageBox("不能删除结点类别!",m_classname);
}
m_pRecordset->Update();
m_pRecordset->Close();
}
catch(_com_error e)
{
CString errormessage;
MessageBox("删除类别出错!",m_classname);
}
}
}
void CReionClass::AddTree()
{
TVINSERTSTRUCT tvInsert;
HTREEITEM hParent;
CString sroot;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
this->m_Storage.OpenSql("RegionTree","mod_name","where mod_id=1");
try
{
this->m_Storage.MoveFirst();
CString value;
if(!this->m_Storage.IsEof())
{
this->m_Storage.GetFieldValue(0,value);
sroot=value;
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("读取类别树出错:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
if(sroot!="")
{
//tvInsert.item.pszText = _T("站点列表");
hParent = m_oTreeclass.InsertItem(&tvInsert);
AddSubTree("0",hParent);
}
m_oTreeclass.Expand(hParent,TVE_EXPAND);
}
void CReionClass::AddSubTree(CString ParTree , HTREEITEM hPartItem)
{
HTREEITEM hCurrent;
CString sql;
CString curID;
_RecordsetPtr m_pTRecordset;
sql="SELECT * FROM RegionTree where mod_PARENT=";
sql=sql+ParTree+"";
try
{
m_pTRecordset.CreateInstance("ADODB.Recordset");
m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pDBCon->GetActiveConnection(),true),adOpenDynamic,adLockOptimistic,adCmdText);
m_pTRecordset->MoveFirst();
if(!m_pTRecordset->adoEOF)
{
while(!m_pTRecordset->adoEOF)
{
hCurrent = m_oTreeclass.InsertItem((LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("mod_NAME")), hPartItem, NULL);//Insert an item into current parent.
curID=VariantToCString(m_pTRecordset->GetCollect("mod_ID"));
if (TreeSumRecordCount(curID)>0)
AddSubTree(VariantToCString(m_pTRecordset->GetCollect("mod_ID")),hCurrent);//Recursion.
if (!m_pTRecordset->adoEOF)
m_pTRecordset->MoveNext();
}
}
m_pTRecordset->Close();
}
catch(_com_error e)
{
CString errormessage;
MessageBox("读取类别子树出错!",ParTree);
}
}
CString CReionClass::VariantToCString(VARIANT var)
{
CString strValue;
_variant_t var_t;
_bstr_t bst_t;
time_t cur_time;
CTime time_value;
COleCurrency var_currency;
switch(var.vt)
{
case VT_EMPTY:strValue=_T("");break;
case VT_UI1:strValue.Format ("%d",var.bVal);break;
case VT_I2:strValue.Format ("%d",var.iVal );break;
case VT_I4:strValue.Format ("%d",var.lVal);break;
case VT_R4:strValue.Format ("%f",var.fltVal);break;
case VT_R8:strValue.Format ("%f",var.dblVal);break;
case VT_CY:
var_currency=var;
strValue=var_currency.Format(0);
break;
case VT_BSTR:
var_t=var;
bst_t=var_t;
strValue.Format ("%s",(const char*)bst_t);
break;
case VT_NULL:strValue=_T("");break;
case VT_DATE:
cur_time=var.date;
time_value=cur_time;
strValue=time_value.Format("%A,%B%d,%Y");
break;
case VT_BOOL:strValue.Format ("%d",var.boolVal );break;
default:strValue=_T("");break;
}
return strValue;
}
int CReionClass::TreeSumRecordCount(CString strFieldValue)
{
int Sum=0;
CString sql;
_RecordsetPtr m_pRecordset;
sql="SELECT * FROM RegionTree where mod_parent =";
sql=sql+strFieldValue+"";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pDBCon->GetActiveConnection(),true),adOpenDynamic,adLockPessimistic,adCmdText);
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst ();
while(!m_pRecordset->adoEOF)
{
Sum+=1;
m_pRecordset->MoveNext ();
}
}
m_pRecordset->Close();
}
catch(_com_error e)
{
CString stemp;
stemp.Format("计算类别总数出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return Sum;
}
BOOL CReionClass::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
AddTree();
return TRUE;
// 异常: OCX 属性页应返回 FALSE
}
int CReionClass::GenNewID()
{
int NewID;
this->m_Storage.OpenSql("RegionTree","Max(mod_id)");
try
{
CString value;
this->m_Storage.GetFieldValue(0,value);
_variant_t vIndex = (long)0;
_variant_t vtemp = value;
if(vtemp.lVal>0)
NewID =(long)(_variant_t(value))+1;
else
NewID=1;
}
catch(_com_error e)
{
CString stemp;
stemp.Format("获取类别ID最大值出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return NewID;
}
bool CReionClass::CheckRepeat(CString strFieldValue)
{
bool br;
this->m_Storage.OpenSql("RegionTree","mod_name","WHERE mod_name='"+strFieldValue+"'");
if(this->m_Storage.GetFieldCount()>0)
{
br=true;
}
else
{
br=false;
}
return br;
}
int CReionClass::CountMateriel(CString sclassid)
{
int ncount;
//查询类别下站点是否存在
this->m_Storage.OpenSql("ProjectTab","count(*) as XXX","where Mod_ID="+sclassid+"");
try
{
CString value;
this->m_Storage.GetFieldValue(0,value);
ncount =(long)(_variant_t(value));
}
catch(_com_error e)
{
CString stemp;
stemp.Format("计算类别下站点数量出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return ncount;
}
void CReionClass::OnTvnSelchangedTree(NMHDR *pNMHDR, LRESULT *pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CString curtext;
HTREEITEM hCurItem;
hCurItem=m_oTreeclass.GetSelectedItem();
curtext=m_oTreeclass.GetItemText(hCurItem);
m_classname=curtext;
this->m_Storage.OpenSql("RegionTree","mod_id,mod_parent","where mod_name='"+curtext+"'");
try
{
m_tempid=0;
if(!(this->m_Storage.IsEof()))
{
CString value,svalue;
this->m_Storage.GetFieldValue(0,value);
this->m_Storage.GetFieldValue(1,svalue);
m_tempid=(long)((_variant_t)value);
Curpar=VariantToCString((_variant_t)svalue);
}
}
catch(_com_error e)
{
CString errormessage;
MessageBox("读取单选按钮出错!",m_classname);
}
UpdateData(false);
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -