UserDlg.cpp
上传用户:sztwq510
上传日期:2013-06-26
资源大小:4045k
文件大小:13k
源码类别:

酒店行业

开发平台:

Java

  1. // UserDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "UserDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CUserDlg dialog
  13. extern CMyPosApp theApp;
  14. CUserDlg::CUserDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CUserDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CUserDlg)
  18. m_Listuser = _T("");
  19. m_datebirth = _T("");
  20. m_duty = _T("");
  21. m_jiguan = _T("");
  22. m_logname = _T("");
  23. m_username = _T("");
  24. m_number = _T("");
  25. m_upwd = _T("");
  26. m_sex = _T("");
  27. //}}AFX_DATA_INIT
  28. }
  29. void CUserDlg::DoDataExchange(CDataExchange* pDX)
  30. {
  31. CDialog::DoDataExchange(pDX);
  32. //{{AFX_DATA_MAP(CUserDlg)
  33. DDX_Control(pDX, IDC_CHECK1, m_oCheck1);
  34. DDX_Control(pDX, IDC_CHECK2, m_oCheck2);
  35. DDX_Control(pDX, IDC_CHECK3, m_oCheck3);
  36. DDX_Control(pDX, IDC_CHECK4, m_oCheck4);
  37. DDX_Control(pDX, IDC_CHECK5, m_oCheck5);
  38. DDX_Control(pDX, IDC_CHECK6, m_oCheck6);
  39. DDX_Control(pDX, IDC_CHECK7, m_oCheck7);
  40. DDX_Control(pDX, IDC_CHECK8, m_oCheck8);
  41. DDX_Control(pDX, IDC_CHECK9, m_oCheck9);
  42. DDX_Control(pDX, IDC_EDIT_USERID, m_oUserid);
  43. DDX_Control(pDX, IDC_EDIT_DISCOUNT, m_oDiscount);
  44. DDX_Control(pDX, IDC_EDIT_NUMBER, m_oNumber);
  45. DDX_Control(pDX, IDC_EDIT_LOGNAME, m_oLogname);
  46. DDX_Control(pDX, IDC_EDIT_JIGUAN, m_oJiguan);
  47. DDX_Control(pDX, IDC_EDIT_DUTY, m_oDuty);
  48. DDX_Control(pDX, IDC_EDIT_DATEBIRTH, m_oDatebirth);
  49. DDX_Control(pDX, IDC_EDIT_PWD, m_oPwd);
  50. DDX_Control(pDX, IDC_EDIT_USERNAME, m_oUsername);
  51. DDX_Control(pDX, IDC_COMBO_SEX, m_oSex);
  52. DDX_Control(pDX, IDC_LIST_USER, m_oListuser);
  53. DDX_LBString(pDX, IDC_LIST_USER, m_Listuser);
  54. DDX_Text(pDX, IDC_EDIT_DATEBIRTH, m_datebirth);
  55. DDX_Text(pDX, IDC_EDIT_DUTY, m_duty);
  56. DDX_Text(pDX, IDC_EDIT_JIGUAN, m_jiguan);
  57. DDX_Text(pDX, IDC_EDIT_LOGNAME, m_logname);
  58. DDX_Text(pDX, IDC_EDIT_USERNAME, m_username);
  59. DDX_Text(pDX, IDC_EDIT_NUMBER, m_number);
  60. DDX_Text(pDX, IDC_EDIT_PWD, m_upwd);
  61. DDX_CBString(pDX, IDC_COMBO_SEX, m_sex);
  62. //}}AFX_DATA_MAP
  63. }
  64. BEGIN_MESSAGE_MAP(CUserDlg, CDialog)
  65. //{{AFX_MSG_MAP(CUserDlg)
  66. ON_BN_CLICKED(IDC_BUTTON_UADD, OnButtonUadd)
  67. ON_BN_CLICKED(IDC_BUTTON_UDEL, OnButtonUdel)
  68. ON_BN_CLICKED(IDC_BUTTON_USAVE, OnButtonUsave)
  69. ON_LBN_SELCHANGE(IDC_LIST_USER, OnSelchangeListUser)
  70. //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CUserDlg message handlers
  74. BOOL CUserDlg::OnInitDialog() 
  75. {
  76. CDialog::OnInitDialog();
  77. ReadToListbox();
  78. m_oListuser.SelectString(-1, theApp.name);//设置选中项
  79. OnSelchangeListUser();
  80. return TRUE;  // return TRUE unless you set the focus to a control
  81.               // EXCEPTION: OCX Property Pages should return FALSE
  82. }
  83. void CUserDlg::OnButtonUadd() 
  84. {
  85. //Clear the edit and combobox content.
  86. m_oDatebirth.SetWindowText("");
  87. m_oDuty.SetWindowText("");
  88. m_oJiguan.SetWindowText("");
  89. m_oLogname.SetWindowText("");
  90. m_oUsername.SetWindowText("");
  91. m_oNumber.SetWindowText("");
  92. m_oPwd.SetWindowText("");
  93. m_oDiscount.SetWindowText("");
  94. m_oSex.SetCurSel(0);
  95. m_oCheck1.SetCheck(0);
  96. m_oCheck2.SetCheck(0);
  97. m_oCheck3.SetCheck(0);
  98. m_oCheck4.SetCheck(0);
  99. m_oCheck5.SetCheck(0);
  100. m_oCheck6.SetCheck(0);
  101. m_oCheck7.SetCheck(0);
  102. m_oCheck8.SetCheck(0);
  103. m_oCheck9.SetCheck(0);
  104. lNewID=GenNewID();
  105. // m_oListuser.SetCurSel(-1);
  106. }
  107. void CUserDlg::OnButtonUdel() 
  108. {
  109. CString sql, stemp;
  110. int iOption;
  111. _RecordsetPtr m_pRecordset;
  112. iOption = m_oListuser.GetCurSel();
  113. if(iOption>=0)
  114. {
  115. m_oListuser.GetText(iOption, stemp);//得到选中项的文本
  116. sql="select * from USERS where NAME='"+stemp+"'";
  117. try
  118. m_pRecordset.CreateInstance("ADODB.Recordset");
  119. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  120. if(AfxMessageBox("要删除此用户吗?",MB_YESNO)==IDYES)
  121. {
  122. CString slogid=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("LOGINID");
  123. if(slogid=="Admin")
  124. {
  125. MessageBox("不能删除系统管理员!");
  126. return;
  127. }
  128. m_pRecordset->Delete(adAffectCurrent);///删除当前记录
  129. m_oListuser.DeleteString(iOption);
  130. OnButtonUadd();//Clear the edit text.
  131. }
  132. else
  133. return;
  134. m_pRecordset->Update();   
  135. }
  136. catch(_com_error e)///捕捉异常
  137. {
  138. CString temp;
  139. temp.Format("删除用户出错:%s",e.ErrorMessage());
  140. AfxMessageBox(temp);
  141. return;
  142. }
  143. }
  144. }
  145. void CUserDlg::OnButtonUsave() 
  146. {
  147. CString sql="select * from USERS";
  148. CString temp,snewid,spower;
  149. CString name,sdiscount;
  150. _RecordsetPtr m_pRecordset;
  151. UpdateData();
  152. //检查数据完整性
  153. if(m_username.IsEmpty()||m_logname.IsEmpty()||m_upwd.IsEmpty())
  154. {
  155. AfxMessageBox("请输入姓名、登录名称和密码!");
  156. return;
  157. }
  158. m_oUsername.GetWindowText(name);//Get the current text in edit.
  159. if(AfxMessageBox("保存修改吗?",MB_YESNO)==IDYES)
  160. {
  161. int sex=m_oSex.GetCurSel();
  162. if(sex==0) m_sex="0";
  163. if(sex==1) m_sex="1";
  164. m_oDiscount.GetWindowText(sdiscount);
  165. if(m_oCheck1.GetCheck()==1)
  166. spower="[Basedoc]";
  167. if(m_oCheck2.GetCheck()==1)
  168. spower=spower+"[Stat]";
  169. if(m_oCheck3.GetCheck()==1)
  170. spower=spower+"[Sysmain]";
  171. if(m_oCheck4.GetCheck()==1)
  172. spower=spower+"[RF]";
  173. if(m_oCheck5.GetCheck()==1)
  174. spower=spower+"[DelBill]";
  175. if(m_oCheck6.GetCheck()==1)
  176. spower=spower+"[CallBill]";
  177. if(m_oCheck7.GetCheck()==1)
  178. spower=spower+"[Deldata]";
  179. if(m_oCheck8.GetCheck()==1)
  180. spower=spower+"[Hang]";
  181. if(m_oCheck9.GetCheck()==1)
  182. spower=spower+"[Recheck]";
  183. if(lNewID>atol(suserid))
  184. {
  185. snewid.Format("%d",lNewID);
  186. if(bRepeat(name)==TRUE)
  187. {
  188. AfxMessageBox("用户名重复,请重新输入!");
  189. return;
  190. }
  191. try
  192. m_pRecordset.CreateInstance("ADODB.Recordset");
  193. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  194.    m_pRecordset->AddNew();//insert the current into database.
  195. m_pRecordset->PutCollect("ID",_variant_t(snewid));
  196. m_pRecordset->PutCollect("NAME",_variant_t(m_username));
  197. m_pRecordset->PutCollect("LOGINID",_variant_t(m_logname));
  198. m_pRecordset->PutCollect("PSD",_variant_t(m_upwd));
  199. m_pRecordset->PutCollect("TITLE",_variant_t(m_duty));
  200. m_pRecordset->PutCollect("SEX",_variant_t(m_sex));
  201. m_pRecordset->PutCollect("DATEBIRTH",_variant_t(m_datebirth));
  202. m_pRecordset->PutCollect("HOME",_variant_t(m_jiguan));
  203. m_pRecordset->PutCollect("EMPLOYNUMBER",_variant_t(m_number));
  204. m_pRecordset->PutCollect("DISCOUNT",_variant_t(sdiscount));
  205. m_pRecordset->PutCollect("POWER",_variant_t(spower));
  206. m_pRecordset->Update();//保存到库中    
  207. m_oListuser.AddString(m_username);
  208. m_oListuser.SelectString(-1,m_username);
  209. m_pRecordset->Close();
  210. UpdateData(FALSE);
  211. }
  212. catch(_com_error e)///捕捉异常
  213. {
  214. CString temp;
  215. temp.Format("保存用户资料出错:%s",e.ErrorMessage());
  216. AfxMessageBox(temp);
  217. return;
  218. }
  219. }
  220. else
  221. {
  222. _variant_t RecordsAffected;
  223. if(sdiscount=="")
  224. sdiscount="0";
  225. sql="Update USERS set NAME='"+m_username+
  226. "',LOGINID='"+m_logname+
  227. "',PSD='"+m_upwd+
  228. "',TITLE='"+m_duty+
  229. "',SEX='"+m_sex+
  230. "',DATEBIRTH='"+m_datebirth+
  231. "',HOME='"+m_jiguan+ 
  232. "',EMPLOYNUMBER='"+m_number+
  233. "',DISCOUNT="+sdiscount+
  234. ",POWER='"+spower+"' where ID="+suserid+"";
  235. try
  236. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  237. ReadToListbox();
  238. UpdateData(FALSE);
  239. }
  240. catch(_com_error e)///捕捉异常
  241. {
  242. CString temp;
  243. temp.Format("修改用户资料出错:%s",e.ErrorMessage());
  244. AfxMessageBox(temp);
  245. return;
  246. }
  247. }
  248. }
  249. }
  250. void CUserDlg::OnSelchangeListUser() //Read selected user info into controls.
  251. {
  252. CString stemp,sql,sex,spower;
  253. int iOption; 
  254. _RecordsetPtr m_pRecordset;
  255. m_oCheck1.SetCheck(0);
  256. m_oCheck2.SetCheck(0);
  257. m_oCheck3.SetCheck(0);
  258. m_oCheck4.SetCheck(0);
  259. m_oCheck5.SetCheck(0);
  260. m_oCheck6.SetCheck(0);
  261. m_oCheck7.SetCheck(0);
  262. m_oCheck8.SetCheck(0);
  263. m_oCheck9.SetCheck(0);
  264. iOption = m_oListuser.GetCurSel();
  265. m_oListuser.GetText(iOption, stemp);//得到选中项的文本
  266. sql="select * from USERS where NAME='"+stemp+"'";
  267. try
  268. {
  269. m_pRecordset.CreateInstance("ADODB.Recordset");
  270. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  271. lNewID=0;
  272. //Read selected user info into controls.
  273. if(!m_pRecordset->adoEOF)
  274. {
  275. m_oUserid.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID"));
  276. m_oUserid.GetWindowText(suserid);
  277. m_oPwd.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PSD"));
  278. m_oUsername.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
  279. m_oDuty.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TITLE"));
  280. m_oDatebirth.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("DATEBIRTH"));
  281. m_oJiguan.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("HOME"));
  282. m_oNumber.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("EMPLOYNUMBER"));
  283. m_oDiscount.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("DISCOUNT"));
  284. CString slogid=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("LOGINID");
  285. if(slogid=="Admin")
  286. m_oLogname.SetReadOnly(TRUE);
  287. else
  288. m_oLogname.SetReadOnly(FALSE);
  289. m_oLogname.SetWindowText(slogid);
  290. sex=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SEX");//Set sex combobox string.
  291. if(sex=="0")
  292. m_oSex.SetCurSel(0);
  293. if(sex=="1")
  294. m_oSex.SetCurSel(1);
  295. spower=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("POWER");
  296. if(spower.Find("Basedoc")>0)
  297. m_oCheck1.SetCheck(1);
  298. if(spower.Find("Stat")>0)
  299. m_oCheck2.SetCheck(1);
  300. if(spower.Find("Sysmain")>0)
  301. m_oCheck3.SetCheck(1);
  302. if(spower.Find("RF")>0)
  303. m_oCheck4.SetCheck(1);
  304. if(spower.Find("DelBill")>0)
  305. m_oCheck5.SetCheck(1);
  306. if(spower.Find("CallBill")>0)
  307. m_oCheck6.SetCheck(1);
  308. if(spower.Find("Deldata")>0)
  309. m_oCheck7.SetCheck(1);
  310. if(spower.Find("Hang")>0)
  311. m_oCheck8.SetCheck(1);
  312. if(spower.Find("Recheck")>0)
  313. m_oCheck9.SetCheck(1);
  314. }
  315. m_pRecordset->Close();
  316. }
  317. catch(_com_error e)///捕捉异常
  318. {
  319. CString temp;
  320. temp.Format("显示用户信息出错:%s",e.ErrorMessage());
  321. AfxMessageBox(temp);
  322. return;
  323. }
  324. }
  325. long CUserDlg::GenNewID()
  326. {
  327. CString sql;
  328. long NewID;
  329. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  330. sql="SELECT Max(ID) FROM USERS";
  331. try
  332. m_pRecordset.CreateInstance("ADODB.Recordset");
  333. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  334. _variant_t vIndex = (long)0;//How to get field value in only 1 record and 1 field condition.
  335. _variant_t vtemp = m_pRecordset->GetCollect(vIndex);
  336. if(vtemp.lVal>0) 
  337. NewID =(long)(m_pRecordset->GetCollect(vIndex))+1;///取得第一个字段的值(MAX ID)加1后放入id变量.
  338. else
  339. NewID=1;
  340. m_pRecordset->Close();
  341. }
  342. catch(_com_error e)///捕捉异常
  343. {
  344. CString stemp;
  345. stemp.Format("获得ID最大值出错:%s",e.ErrorMessage());
  346. AfxMessageBox(stemp);
  347. }
  348. return NewID;
  349. }
  350. BOOL CUserDlg::bRepeat(CString strFieldValue)
  351. {
  352. CString sql;
  353. BOOL br;
  354. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  355. sql="SELECT * FROM USERS WHERE NAME='";
  356. sql=sql+strFieldValue+"'";
  357. try
  358. m_pRecordset.CreateInstance("ADODB.Recordset");
  359. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  360. if(m_pRecordset->adoEOF)
  361. br=FALSE;
  362. if(!m_pRecordset->adoEOF)
  363. br=TRUE;
  364. m_pRecordset->Close();
  365. }
  366. catch(_com_error e)///捕捉异常
  367. {
  368. CString stemp;
  369. stemp.Format("查找用户名出错:%s",e.ErrorMessage());
  370. AfxMessageBox(stemp);
  371. }
  372. return br;
  373. }
  374. void CUserDlg::ReadToListbox()
  375. {
  376. _RecordsetPtr m_pRecordset;
  377. //Read user name from database into listbox。
  378. m_oListuser.ResetContent();
  379. CString sql="select * from USERS";
  380. try
  381. {
  382. m_pRecordset.CreateInstance("ADODB.Recordset");
  383. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  384. while(!m_pRecordset->adoEOF)
  385. {
  386. m_oListuser.AddString((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
  387. m_pRecordset->MoveNext();
  388. }
  389. m_pRecordset->Close();
  390. }
  391. catch(_com_error e)///捕捉异常
  392. {
  393. CString temp;
  394. temp.Format("列表框读取用户名出错:%s",e.ErrorMessage());
  395. AfxMessageBox(temp);
  396. }
  397. }