You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReportFormPage.cs 9.2 KiB

2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using HKLib.Dto;
  2. using HKLib.Interfaces;
  3. using HKLib.SQLHelper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace HKCardManager.UserPages
  15. {
  16. public partial class ReportFormPage : UserControl
  17. {
  18. private int PageSize = 10;
  19. private int Total;
  20. private int PageIndex;
  21. private double TotalPage;
  22. public ReportFormPage()
  23. {
  24. InitializeComponent();
  25. dataGridView1.AutoGenerateColumns = false;
  26. button2.Enabled = false;
  27. DataGridViewInit();
  28. PageIndex = 1;
  29. TotalPage = 0;
  30. Total = 0;
  31. this.button3.Enabled = false;
  32. }
  33. private void DataGridViewInit()
  34. {
  35. dataGridView1.AllowUserToResizeRows = false;
  36. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  37. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  38. }
  39. private void ExportDataToExcel(DataGridView TableName, string FileName)
  40. {
  41. SaveFileDialog saveFileDialog = new SaveFileDialog();
  42. saveFileDialog.Title = "导出Excel文件"; //设置文件标题
  43. saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型
  44. saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序
  45. saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名
  46. saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录
  47. saveFileDialog.FileName = FileName; //设置默认文件名
  48. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  49. {
  50. string localFilePath = saveFileDialog.FileName.ToString();
  51. //数据初始化
  52. int TotalCount; //总行数
  53. int RowRead = 0; //已读行数
  54. int Percent = 0; //百分比
  55. TotalCount = TableName.Rows.Count;
  56. Stream myStream = saveFileDialog.OpenFile();
  57. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  58. StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312"));
  59. string strHeader = string.Empty;
  60. Stopwatch timer = new Stopwatch();
  61. timer.Start();
  62. try
  63. {
  64. //写入标题
  65. for (int i = 0; i < TableName.Columns.Count; i++)
  66. {
  67. if (TableName.Columns[i].Visible)
  68. {
  69. if (i > 0 && !string.IsNullOrEmpty(strHeader)) strHeader += "\t";
  70. strHeader += TableName.Columns[i].HeaderText.ToString();
  71. }
  72. }
  73. sw.WriteLine(strHeader);
  74. for (int i = 0; i < TableName.Rows.Count; i++)
  75. {
  76. RowRead++;
  77. Percent = (int)(100 * RowRead / TotalCount);
  78. Application.DoEvents();
  79. string strData = string.Empty;
  80. for (int j = 0; j < TableName.Columns.Count; j++)
  81. {
  82. string? res = TableName.Rows[i].Cells[j].Value?.ToString();
  83. if (TableName.Columns[j].Visible)
  84. {
  85. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  86. strData += string.IsNullOrEmpty(res) ? "" : res;
  87. }
  88. }
  89. sw.WriteLine(strData);
  90. }
  91. sw.Close();
  92. myStream.Close();
  93. timer.Reset();
  94. timer.Stop();
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  99. }
  100. finally
  101. {
  102. sw.Close();
  103. myStream.Close();
  104. timer.Stop();
  105. }
  106. MessageLogNotify.GetInstance.Show("数据导出成功");
  107. }
  108. }
  109. List<ReportCountDto> reportCountDtos = new List<ReportCountDto>();
  110. //查询
  111. private async void button1_Click(object sender, EventArgs e)
  112. {
  113. string TagName = this.Tag.ToString();
  114. int type = 0;
  115. if (TagName == "月餐段消费表") type = 2;
  116. else if (TagName == "记次就餐天消费表") type = 1;
  117. else if (TagName == "月餐段汇总表") type = 3;
  118. this.Invoke(() => { button1.Enabled = false; });
  119. var res = await HKLibHelper.Report(new ReportDto() { StartTime = dateTimePicker1.Value, EndTime = dateTimePicker2.Value }, type);
  120. var price = SqlLogic.GetPrice();
  121. foreach (var item in res)
  122. {
  123. item.AM = price == null ? 0 : price.AM * item.AMCount;
  124. item.PM = item.PMCount >= 20 ? 75 : price == null ? 0 : price.PM * item.PMCount;
  125. item.AT = price == null ? 0 : price.AT * item.ATCount;
  126. item.Total = item.AM + item.PM + item.AT;
  127. }
  128. reportCountDtos = res;
  129. Total = res.Count;
  130. TotalPage = Math.Ceiling(Total * 1.0 / PageSize);
  131. res = res.Skip(0).Take(PageSize).ToList();
  132. label1.Text = $"{PageIndex}/{TotalPage}";
  133. if (res != null)
  134. {
  135. if (res.Count > 0)
  136. {
  137. this.Invoke(() =>
  138. {
  139. if (type == 1)
  140. {
  141. dataGridView1.Columns[0].Visible = true;
  142. dataGridView1.Columns[1].Visible = false;
  143. dataGridView1.Columns[2].Visible = true;
  144. dataGridView1.Columns[3].Visible = false;
  145. }
  146. else if (type == 2)
  147. {
  148. dataGridView1.Columns[0].Visible = false;
  149. dataGridView1.Columns[1].Visible = false;
  150. dataGridView1.Columns[2].Visible = true;
  151. dataGridView1.Columns[3].Visible = false;
  152. }
  153. else if (type == 3)
  154. {
  155. dataGridView1.Columns[0].Visible = true;
  156. dataGridView1.Columns[1].Visible = false;
  157. dataGridView1.Columns[2].Visible = false;
  158. dataGridView1.Columns[3].Visible = true;
  159. }
  160. dataGridView1.DataSource = res;
  161. button2.Enabled = true;
  162. });
  163. MessageLogNotify.GetInstance.Show("获取表数据成功");
  164. }
  165. else if (res.Count <= 0)
  166. {
  167. MessageLogNotify.GetInstance.ShowWarning("未查询到数据");
  168. this.Invoke(() => dataGridView1.DataSource = new List<ReportCountDto>());
  169. }
  170. }
  171. else
  172. {
  173. MessageLogNotify.GetInstance.ShowError("查询失败,请输入正确的时间段");
  174. }
  175. this.Invoke(() => { button1.Enabled = true; });
  176. }
  177. //导出
  178. private void button2_Click(object sender, EventArgs e)
  179. {
  180. try
  181. {
  182. this.Invoke(() => { button2.Enabled = false; });
  183. ExportDataToExcel(dataGridView1, this.Tag.ToString());
  184. }
  185. catch (Exception ex)
  186. {
  187. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  188. }
  189. finally
  190. {
  191. this.Invoke(() => { button2.Enabled = true; });
  192. }
  193. }
  194. private void button3_Click(object sender, EventArgs e)
  195. {
  196. if (PageIndex <= 1)
  197. {
  198. this.button3.Enabled = false;
  199. return;
  200. }
  201. PageIndex = PageIndex - 1;
  202. var data = reportCountDtos.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  203. dataGridView1.DataSource = data;
  204. this.button4.Enabled = true;
  205. label1.Text = $"{PageIndex}/{TotalPage}";
  206. }
  207. private void button4_Click(object sender, EventArgs e)
  208. {
  209. if (PageIndex >= TotalPage)
  210. {
  211. this.button4.Enabled = false;
  212. return;
  213. }
  214. PageIndex = PageIndex + 1;
  215. var data = reportCountDtos.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  216. dataGridView1.DataSource = data;
  217. this.button3.Enabled = true;
  218. label1.Text = $"{PageIndex}/{TotalPage}";
  219. }
  220. }
  221. }