Form1.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.OleDb;
  9. //using NPOI.SS.UserModel;
  10. using System.IO;
  11. namespace BooksManageSystem
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. DBcon con = new DBcon();
  20. List<Book> booklist;
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. reload();
  24. }
  25. private void reload()
  26. {
  27. try
  28. {
  29. booklist = new DBcon().GetBookList();
  30. fillListView(booklist);
  31. }
  32. catch (Exception err)
  33. {
  34. MessageBox.Show(err.Message + "请确保数据库文件和程序在同一个目录。");
  35. }
  36. }
  37. private void fillListView(List<Book> list)
  38. {
  39. listView1.Items.Clear();
  40. listView1.BeginUpdate();
  41. foreach (var item in list)
  42. {
  43. ListViewItem i = new ListViewItem(item.BookID.ToString());
  44. i.SubItems.Add(item.BookName);
  45. i.SubItems.Add(item.Price.ToString());
  46. i.SubItems.Add(item.OrderPrice.ToString());
  47. i.SubItems.Add(item.Count.ToString());
  48. i.SubItems.Add(item.IsStuBook?"YES":"NO");
  49. i.Tag = item;
  50. if (item.IsStuBook&& item.Count<10)
  51. {
  52. i.ForeColor = Color.Firebrick;
  53. }
  54. listView1.Items.Add(i);
  55. }
  56. listView1.EndUpdate();
  57. }
  58. private void menu_BUYBOOK_Click(object sender, EventArgs e)
  59. {
  60. BUY_BookList();
  61. }
  62. private void menu_RETURNBOOK_Click(object sender, EventArgs e)
  63. {
  64. RETURN_BOOK();
  65. }
  66. private void menu_SELLBOOK_Click(object sender, EventArgs e)
  67. {
  68. SELL_BOOK();
  69. }
  70. private void menu_LINGQU_Click(object sender, EventArgs e)
  71. {
  72. LINGQU_BOOK();
  73. }
  74. private void menu_BookLOG_Click(object sender, EventArgs e)
  75. {
  76. if (listView1.SelectedItems.Count > 1)
  77. {
  78. MessageBox.Show("只能选择一种书!");
  79. return;
  80. }
  81. if (listView1.SelectedItems.Count == 1)
  82. {
  83. var list =con.GetOPLogByID((listView1.SelectedItems[0].Tag as Book).BookID);
  84. new FrmLOGViewer(list).ShowDialog();
  85. }
  86. }
  87. private void menu_All_bookLOG_Click(object sender, EventArgs e)
  88. {
  89. new FrmLOGViewer(con.GetOPLogALL()).ShowDialog();
  90. }
  91. private void menu_exportBookList_Click(object sender, EventArgs e)
  92. {
  93. var list = con.GetBookList();
  94. StringBuilder sb = new StringBuilder();
  95. sb.AppendLine("编号\t书名\t定价\t购入价\t库存+\t学生用书");
  96. foreach (Book item in list)
  97. {
  98. sb.AppendLine(item.BookID + "\t" + item.BookName + "\t" + item.Price + "\t" + item.OrderPrice + "\t" + item.Count + "\t" + (item.IsStuBook?"YES":"NO"));
  99. }
  100. try
  101. {
  102. var fDia = new SaveFileDialog();
  103. fDia.Filter = "txt files (*.txt)|*.txt";
  104. if (fDia.ShowDialog()== DialogResult.OK)
  105. {
  106. System.IO.File.WriteAllText(fDia.FileName, sb.ToString(), Encoding.Default);
  107. MessageBox.Show("如需要Excel在Excel中操作,请全选并复制粘贴到Excel中。");
  108. System.Diagnostics.Process.Start(fDia.FileName);
  109. }
  110. }
  111. catch (Exception err)
  112. {
  113. MessageBox.Show(err.Message);
  114. }
  115. }
  116. void output()
  117. {
  118. //NPOI.HSSF.UserModel.HSSFWorkbook wb = new NPOI.HSSF.UserModel.HSSFWorkbook();
  119. //ISheet oSheet = wb.CreateSheet("leibieo");
  120. //var row = oSheet.CreateRow(1);
  121. //row.CreateCell(1).SetCellValue("编号");
  122. //row.CreateCell(2).SetCellValue("书名");
  123. //row.CreateCell(3).SetCellValue("定价");
  124. //row.CreateCell(4).SetCellValue("购入价");
  125. //row.CreateCell(5).SetCellValue("库存");
  126. //row.CreateCell(6).SetCellValue("学生用书");
  127. //var list = con.GetBookList();
  128. //int n = 2;
  129. //foreach (Book item in list)
  130. //{
  131. // row = oSheet.CreateRow(n++);
  132. // row.CreateCell(1).SetCellValue(item.BookID);
  133. // row.CreateCell(2).SetCellValue(item.BookName);
  134. // row.CreateCell(3).SetCellValue(item.Price);
  135. // row.CreateCell(4).SetCellValue(item.OrderPrice);
  136. // row.CreateCell(5).SetCellValue(item.Count);
  137. // row.CreateCell(6).SetCellValue(item.IsStuBook);
  138. //}
  139. //FileStream fs = new FileStream(@"abc.xlsx", FileMode.Create);
  140. //wb.Write(fs);
  141. //fs.Close();
  142. }
  143. void outputExcelBookList()
  144. {
  145. string fn;
  146. var fDia = new SaveFileDialog();
  147. fDia.Filter = "Excel files (*.xlsx)|*.xlsx";
  148. if (fDia.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  149. {
  150. fn = fDia.FileName;
  151. }
  152. else
  153. {
  154. return;
  155. }
  156. var file = new FileInfo(fn);
  157. if (File.Exists(file.FullName))
  158. {
  159. file.Delete();
  160. }
  161. OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage(file) ;
  162. var sheet = ep.Workbook.Worksheets.Add("库存列表");
  163. var list = con.GetBookList();
  164. int r =1;
  165. int c=1;
  166. sheet.Cells[r, c].Value = "编号";
  167. sheet.Cells[r, ++c].Value = "书名";
  168. sheet.Cells[r, ++c].Value = "定价";
  169. sheet.Cells[r, ++c].Value = "购入价";
  170. sheet.Cells[r, ++c].Value = "库存";
  171. sheet.Cells[r, ++c].Value = "学生用书";
  172. foreach (Book item in list)
  173. {
  174. c = 1;
  175. sheet.Cells[++r, c++].Value = item.BookID;
  176. sheet.Cells[r, c++].Value = item.BookName;
  177. sheet.Cells[r, c++].Value = item.Price;
  178. sheet.Cells[r, c++].Value = item.OrderPrice;
  179. sheet.Cells[r, c++].Value = item.Count;
  180. sheet.Cells[r, c++].Value = item.IsStuBook;
  181. }
  182. sheet.Cells["1:1"].Style.Font.Bold = true;
  183. sheet.Cells["A:F"].AutoFitColumns();
  184. //Console.WriteLine(sheet.Cells["1:1"].End.Row.ToString());
  185. ep.Save();
  186. ep.Dispose();
  187. }
  188. void outputExcelSellList(DateTime start,DateTime end)
  189. {//编号, 书名 , 定价, 数量, 备注信息 , 小计
  190. var fDia = new SaveFileDialog();
  191. fDia.Filter = "Excel files (*.xlsx)|*.xlsx";
  192. if (fDia.ShowDialog()== System.Windows.Forms.DialogResult.OK)
  193. {
  194. if (File.Exists(fDia.FileName))
  195. {
  196. File.Delete(fDia.FileName);
  197. }
  198. }
  199. else
  200. {
  201. return;
  202. }
  203. var dt =con.GetSellList(start,end);
  204. using (OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage())
  205. {
  206. var sheet = ep.Workbook.Worksheets.Add("Table1");
  207. for (int i = 0; i < dt.Columns.Count; i++)
  208. {
  209. sheet.Cells[1, i + 1].Value = dt.Columns[i].ColumnName;
  210. }
  211. int row = 1;
  212. foreach (DataRow r in dt.Rows)
  213. {
  214. row++;
  215. for (int i = 0; i < dt.Columns.Count; i++)
  216. {
  217. sheet.Cells[row, i + 1].Value = r[i];
  218. }
  219. }
  220. sheet.Cells["A:G"].AutoFitColumns();
  221. sheet.Cells["A1:G1"].Style.Font.Bold = true;
  222. sheet.Cells["E:E"].Style.Numberformat.Format = "yyyy-mm-dd";
  223. ep.SaveAs(new FileInfo(fDia.FileName));
  224. }
  225. }
  226. private void menu_newBook_Click(object sender, EventArgs e)
  227. {
  228. var ok= new FrmAddNewBook(null).ShowDialog();
  229. if (ok== System.Windows.Forms.DialogResult.OK)
  230. {
  231. reload();
  232. }
  233. }
  234. private void txt_Search_KeyPress(object sender, KeyPressEventArgs e)
  235. {
  236. if (e.KeyChar==13)
  237. {
  238. var list =con.GetBookListByName(txt_Search.Text.Trim());
  239. fillListView(list);
  240. e.Handled = true;
  241. }
  242. }
  243. private void menu_ExcelOutBookList_Click(object sender, EventArgs e)
  244. {
  245. outputExcelBookList();
  246. }
  247. private void menu_TotalSell_Click(object sender, EventArgs e)
  248. {
  249. FrmOutputSellList olist = new FrmOutputSellList();
  250. if (olist.ShowDialog()== System.Windows.Forms.DialogResult.OK)
  251. {
  252. outputExcelSellList(olist.Start, olist.End);
  253. }
  254. }
  255. private void menu_init_Click(object sender, EventArgs e)
  256. {
  257. new FrmClearDB().ShowDialog();
  258. }
  259. private void menu_ModifyBook_Click(object sender, EventArgs e)
  260. {
  261. if (listView1.SelectedItems.Count == 1)
  262. {
  263. var ok = new FrmAddNewBook(listView1.SelectedItems[0].Tag as Book).ShowDialog();
  264. if (ok== System.Windows.Forms.DialogResult.OK)
  265. {
  266. reload();
  267. }
  268. }
  269. else
  270. {
  271. MessageBox.Show("需要选择一项进行修改!");
  272. }
  273. }
  274. private void menu_About_Click(object sender, EventArgs e)
  275. {
  276. new FrmAbout().ShowDialog();
  277. //MessageBox.Show("H为Grace定制的图书管理!\r\n\r\n 2014年4月12日", "Grace的图书管理", MessageBoxButtons.OK, MessageBoxIcon.Information);
  278. }
  279. private void Form1_SizeChanged(object sender, EventArgs e)
  280. {
  281. //pl_ListView.Size = new Size(pl_ListView.Size.Width, this.Size.Height - he);
  282. listView1.Size = new Size(this.Size.Width - listView1.Location.X - 25 , this.Size.Height - listView1.Location.Y - 45);
  283. }
  284. private void menu_BigList_Click(object sender, EventArgs e)
  285. {
  286. try
  287. {
  288. new FrmBigListViewer().ShowDialog();
  289. }
  290. catch (Exception err)
  291. {
  292. MessageBox.Show(err.Message);
  293. }
  294. }
  295. #region 书籍变更逻辑
  296. void BUY_BOOK()
  297. {
  298. if (listView1.SelectedItems.Count != 1)
  299. {
  300. MessageBox.Show("请先选择需要操作的书籍!(能且只能选择一种书籍)");
  301. return;
  302. }
  303. if (listView1.SelectedItems.Count == 1)
  304. {
  305. var frm = new frmChange(listView1.SelectedItems[0].Tag as Book, EnumOP.购);
  306. if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  307. {
  308. try
  309. {
  310. int after = con.PurchaseBook(frm.BookID, frm.BookChangeCount, frm.NoteTag, frm.OPDateTime,0);
  311. listView1.SelectedItems[0].SubItems[4].Text = after.ToString();
  312. MessageBox.Show("变更成功!","成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  313. }
  314. catch (Exception err)
  315. {
  316. MessageBox.Show("变更失败:" + err.Message);
  317. }
  318. }
  319. }
  320. }
  321. void RETURN_BOOK()
  322. {
  323. if (listView1.SelectedItems.Count != 1)
  324. {
  325. MessageBox.Show("请先选择需要操作的书籍!(能且只能选择一种书籍)");
  326. return;
  327. }
  328. if (listView1.SelectedItems.Count == 1)
  329. {
  330. var frm = new frmChange(listView1.SelectedItems[0].Tag as Book, EnumOP.还);
  331. if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  332. {
  333. try
  334. {
  335. int after = con.ReturnBookByID(frm.BookID, frm.BookChangeCount, frm.NoteTag, frm.OPDateTime);
  336. listView1.SelectedItems[0].SubItems[4].Text = after.ToString();
  337. MessageBox.Show("变更成功!","成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  338. }
  339. catch (Exception err)
  340. {
  341. MessageBox.Show("变更失败:" + err.Message);
  342. }
  343. }
  344. }
  345. }
  346. void SELL_BOOK()
  347. {
  348. if (listView1.SelectedItems.Count != 1)
  349. {
  350. MessageBox.Show("请先选择需要操作的书籍!(能且只能选择一种书籍)");
  351. return;
  352. }
  353. if (listView1.SelectedItems.Count == 1)
  354. {
  355. var frm = new frmChange(listView1.SelectedItems[0].Tag as Book, EnumOP.售);
  356. if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  357. {
  358. try
  359. {
  360. int after = con.SellBookByID(frm.BookID, frm.BookChangeCount, frm.NoteTag, frm.OPDateTime);
  361. listView1.SelectedItems[0].SubItems[4].Text = after.ToString();
  362. MessageBox.Show("变更成功!", "成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  363. }
  364. catch (Exception err)
  365. {
  366. MessageBox.Show("变更失败:" + err.Message);
  367. }
  368. }
  369. }
  370. }
  371. void LINGQU_BOOK()
  372. {
  373. if (listView1.SelectedItems.Count != 1)
  374. {
  375. MessageBox.Show("请先选择需要操作的书籍!(能且只能选择一种书籍)");
  376. return;
  377. }
  378. if (listView1.SelectedItems.Count == 1)
  379. {
  380. var frm = new frmChange(listView1.SelectedItems[0].Tag as Book, EnumOP.领);
  381. if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  382. {
  383. try
  384. {
  385. int after = con.Lingqu(frm.BookID, frm.BookChangeCount, frm.NoteTag, frm.OPDateTime);
  386. listView1.SelectedItems[0].SubItems[4].Text = after.ToString();
  387. MessageBox.Show("变更成功!","成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  388. }
  389. catch (Exception err)
  390. {
  391. MessageBox.Show("变更失败:" + err.Message);
  392. }
  393. }
  394. }
  395. }
  396. void BUY_BookList()
  397. {
  398. try
  399. {
  400. if (booklist==null)
  401. {
  402. booklist = con.GetBookList();
  403. }
  404. var maker = new FrmBookListMaker(booklist,"购买-列表编辑器","确认购买");
  405. if (maker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  406. {
  407. //maker.BookListToBuy.Values.Count;
  408. int i = 0;
  409. foreach (var item in maker.BookListToOperate.Values)
  410. {
  411. i = i + item;
  412. }
  413. int bid = con.Blist(i, maker.OperTime);
  414. foreach (KeyValuePair<Book, int> item in maker.BookListToOperate)
  415. {
  416. con.PurchaseBook(item.Key.BookID, item.Value, maker.NoteTag, maker.OperTime, bid);
  417. }
  418. reload();
  419. MessageBox.Show("购买成功!","成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  420. }
  421. }
  422. catch (Exception err)
  423. {
  424. MessageBox.Show("操作失败,错误消息:" + err.Message);
  425. }
  426. }
  427. void Lingqu_Booklist()
  428. {
  429. try
  430. {
  431. if (booklist == null)
  432. {
  433. booklist = con.GetBookList();
  434. }
  435. var maker = new FrmBookListMaker(booklist, "领取-列表编辑器", "确认领取");
  436. if (maker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  437. {
  438. int i = 0;
  439. foreach (var item in maker.BookListToOperate.Values)
  440. {
  441. i = i + item;
  442. }
  443. foreach (KeyValuePair<Book, int> item in maker.BookListToOperate)
  444. {
  445. con.Lingqu(item.Key.BookID, item.Value, maker.NoteTag, maker.OperTime);
  446. }
  447. reload();
  448. MessageBox.Show("领取成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  449. }
  450. }
  451. catch (Exception err)
  452. {
  453. MessageBox.Show("操作失败,错误消息:" + err.Message);
  454. }
  455. }
  456. void out_sell_BookList()
  457. {
  458. try
  459. {
  460. if (booklist == null)
  461. {
  462. booklist = con.GetBookList();
  463. }
  464. var maker = new FrmBookListMaker(booklist, "销售-列表编辑器", "确认售书");
  465. if (maker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  466. {
  467. int i = 0;
  468. foreach (var item in maker.BookListToOperate.Values)
  469. {
  470. i = i + item;
  471. }
  472. foreach (KeyValuePair<Book, int> item in maker.BookListToOperate)
  473. {
  474. con.SellBookByID(item.Key.BookID, item.Value, maker.NoteTag, maker.OperTime);
  475. }
  476. reload();
  477. MessageBox.Show("售书成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  478. }
  479. }
  480. catch (Exception err)
  481. {
  482. MessageBox.Show("操作失败,错误消息:" + err.Message);
  483. }
  484. }
  485. #endregion
  486. #region ButtonCalls
  487. private void btn_ShowAll_Click(object sender, EventArgs e)
  488. {
  489. reload();
  490. }
  491. private void btn_Search_Click(object sender, EventArgs e)
  492. {
  493. var list = con.GetBookListByName(txt_Search.Text.Trim());
  494. fillListView(list);
  495. }
  496. private void btn_in_Return_Click(object sender, EventArgs e)
  497. {
  498. RETURN_BOOK();
  499. }
  500. private void btn_out_Sell_Click(object sender, EventArgs e)
  501. {
  502. SELL_BOOK();
  503. }
  504. private void btn_out_Give_Click(object sender, EventArgs e)
  505. {
  506. LINGQU_BOOK();
  507. }
  508. private void btn_OPLOGALL_Click(object sender, EventArgs e)
  509. {
  510. new FrmLOGViewer(con.GetOPLogALL()).ShowDialog();
  511. }
  512. private void btn_in_Buy_Click(object sender, EventArgs e)
  513. {
  514. BUY_BookList();
  515. }
  516. private void btn_BuyListQuery_Click(object sender, EventArgs e)
  517. {
  518. try
  519. {
  520. new FrmBigListViewer().ShowDialog();
  521. }
  522. catch (Exception err)
  523. {
  524. MessageBox.Show(err.Message);
  525. }
  526. }
  527. private void btn_out_Sell_List_Click(object sender, EventArgs e)
  528. {
  529. out_sell_BookList();
  530. }
  531. private void btn_out_Give_booklist_Click(object sender, EventArgs e)
  532. {
  533. Lingqu_Booklist();
  534. }
  535. #endregion
  536. #region 升级
  537. private void toolStripMenuItem1_Click(object sender, EventArgs e)
  538. {
  539. try
  540. {
  541. DBHelper.ExecuteNonQuery(@"Alter TABLE OPERATE ADD COLUMN BID INT");
  542. DBHelper.ExecuteNonQuery(@"CREATE TABLE BLIST(BID INT NOT NULL IDENTITY(1,1),BCOUNT INT NOT NULL,BDATE DATETIME NOT NULL)");
  543. MessageBox.Show("升级成功!");
  544. }
  545. catch (Exception)
  546. {
  547. MessageBox.Show("升级失败,可能已经升级过了。");
  548. }
  549. }
  550. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  551. {
  552. try
  553. {
  554. //DBHelper.ExecuteNonQuery(@"Alter TABLE OPERATE ADD COLUMN BID INT");
  555. DBHelper.ExecuteNonQuery(@"CREATE TABLE BOOKLIST(LID INT NOT NULL IDENTITY(1,1),BOOKSID TEXT NOT NULL,TAG TEXT NOT NULL)");
  556. MessageBox.Show("升级成功!");
  557. }
  558. catch (Exception)
  559. {
  560. MessageBox.Show("升级失败,可能已经升级过了。");
  561. }
  562. }
  563. #endregion
  564. }
  565. }