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.

287 lines
12 KiB

  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. this.button4.Enabled = false;
  33. }
  34. private void DataGridViewInit()
  35. {
  36. dataGridView1.AllowUserToResizeRows = false;
  37. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  38. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  39. }
  40. private void ExportDataToExcel(DataGridView TableName, string FileName)
  41. {
  42. try
  43. {
  44. var d = TableName.DataSource as List<ReportCountDto>;
  45. SaveFileDialog saveFileDialog = new SaveFileDialog();
  46. saveFileDialog.Title = "导出Excel文件"; //设置文件标题
  47. saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型
  48. saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序
  49. saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名
  50. saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录
  51. saveFileDialog.FileName = FileName; //设置默认文件名
  52. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  53. {
  54. string localFilePath = saveFileDialog.FileName.ToString();
  55. //数据初始化
  56. int TotalCount; //总行数
  57. int RowRead = 0; //已读行数
  58. int Percent = 0; //百分比
  59. TotalCount = TableName.Rows.Count;
  60. Stream myStream = saveFileDialog.OpenFile();
  61. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  62. StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312"));
  63. string strHeader = string.Empty;
  64. Stopwatch timer = new Stopwatch();
  65. timer.Start();
  66. try
  67. {
  68. //写入标题
  69. for (int i = 0; i < TableName.Columns.Count; i++)
  70. {
  71. if (TableName.Columns[i].Visible)
  72. {
  73. if (i > 0 && !string.IsNullOrEmpty(strHeader)) strHeader += "\t";
  74. strHeader += TableName.Columns[i].HeaderText.ToString();
  75. }
  76. }
  77. sw.WriteLine(strHeader);
  78. for (int i = 0; i < TableName.Rows.Count + 1; i++)
  79. {
  80. RowRead++;
  81. Percent = (int)(100 * RowRead / (TotalCount + 1));
  82. Application.DoEvents();
  83. string strData = string.Empty;
  84. for (int j = 0; j < TableName.Columns.Count; j++)
  85. {
  86. if (i == TableName.Rows.Count)
  87. {
  88. if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "早上消费金额")
  89. {
  90. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  91. strData += d.Sum(t => t.AM);
  92. }
  93. else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "中午消费金额")
  94. {
  95. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  96. strData += d.Sum(t => t.PM);
  97. }
  98. else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "晚上消费金额")
  99. {
  100. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  101. strData += d.Sum(t => t.AT);
  102. }
  103. else if (TableName.Columns[j].Visible && TableName.Columns[j].Name == "金额总计")
  104. {
  105. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  106. strData += d.Sum(t => t.Total);
  107. }
  108. else if (TableName.Columns[j].Visible)
  109. {
  110. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  111. strData += "-";
  112. }
  113. }
  114. else
  115. {
  116. if (i <= TableName.Rows.Count - 1)
  117. {
  118. string? res = TableName.Rows[i].Cells[j].Value?.ToString();
  119. if (TableName.Columns[j].Visible)
  120. {
  121. if (j > 0 && !string.IsNullOrEmpty(strData)) strData += "\t";
  122. strData += string.IsNullOrEmpty(res) ? "" : res;
  123. }
  124. }
  125. }
  126. }
  127. sw.WriteLine(strData);
  128. }
  129. sw.Close();
  130. myStream.Close();
  131. timer.Reset();
  132. timer.Stop();
  133. }
  134. catch (Exception ex)
  135. {
  136. MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  137. }
  138. finally
  139. {
  140. sw.Close();
  141. myStream.Close();
  142. timer.Stop();
  143. }
  144. MessageLogNotify.GetInstance.Show("数据导出成功");
  145. }
  146. }
  147. catch (Exception)
  148. {
  149. MessageBox.Show("文件已被打开,请先关闭后在导出", "提示");
  150. }
  151. }
  152. List<ReportCountDto> reportCountDtos = new List<ReportCountDto>();
  153. //查询
  154. private async void button1_Click(object sender, EventArgs e)
  155. {
  156. string TagName = this.Tag.ToString();
  157. int type = 0;
  158. if (TagName == "月餐段消费表") type = 2;
  159. else if (TagName == "记次就餐天消费表") type = 1;
  160. else if (TagName == "月餐段汇总表") type = 3;
  161. this.Invoke(() => { button1.Enabled = false; });
  162. var res = await HKLibHelper.Report(new ReportDto() { StartTime = dateTimePicker1.Value, EndTime = dateTimePicker2.Value }, type);
  163. var price = SqlLogic.GetPrice();
  164. foreach (var item in res)
  165. {
  166. item.AM = price == null ? 0 : price.AM * item.AMCount;
  167. item.PM = item.PMCount >= 20 ? 75 : price == null ? 0 : price.PM * item.PMCount;
  168. item.AT = price == null ? 0 : price.AT * item.ATCount;
  169. item.Total = item.AM + item.PM + item.AT;
  170. }
  171. reportCountDtos = res;
  172. Total = res.Count;
  173. TotalPage = Math.Ceiling(Total * 1.0 / PageSize);
  174. res = res.Skip(0).Take(PageSize).ToList();
  175. label1.Text = $"{PageIndex}/{TotalPage}";
  176. if (res != null)
  177. {
  178. if (res.Count > 0)
  179. {
  180. this.Invoke(() =>
  181. {
  182. this.button4.Enabled = true;
  183. if (type == 1)
  184. {
  185. dataGridView1.Columns[0].Visible = true;
  186. dataGridView1.Columns[1].Visible = false;
  187. dataGridView1.Columns[2].Visible = true;
  188. dataGridView1.Columns[3].Visible = false;
  189. }
  190. else if (type == 2)
  191. {
  192. dataGridView1.Columns[0].Visible = false;
  193. dataGridView1.Columns[1].Visible = false;
  194. dataGridView1.Columns[2].Visible = true;
  195. dataGridView1.Columns[3].Visible = false;
  196. }
  197. else if (type == 3)
  198. {
  199. dataGridView1.Columns[0].Visible = true;
  200. dataGridView1.Columns[1].Visible = false;
  201. dataGridView1.Columns[2].Visible = false;
  202. dataGridView1.Columns[3].Visible = true;
  203. }
  204. dataGridView1.DataSource = res;
  205. button2.Enabled = true;
  206. });
  207. MessageLogNotify.GetInstance.Show("获取表数据成功");
  208. }
  209. else if (res.Count <= 0)
  210. {
  211. MessageLogNotify.GetInstance.ShowWarning("未查询到数据");
  212. this.Invoke(() => dataGridView1.DataSource = new List<ReportCountDto>());
  213. }
  214. }
  215. else
  216. {
  217. MessageLogNotify.GetInstance.ShowError("查询失败,请输入正确的时间段");
  218. }
  219. this.Invoke(() => { button1.Enabled = true; });
  220. }
  221. //导出
  222. private void button2_Click(object sender, EventArgs e)
  223. {
  224. try
  225. {
  226. this.Invoke(() => { button2.Enabled = false; });
  227. dataGridView1.DataSource = reportCountDtos;
  228. ExportDataToExcel(dataGridView1, this.Tag.ToString());
  229. var data = reportCountDtos.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  230. dataGridView1.DataSource = data;
  231. }
  232. catch (Exception ex)
  233. {
  234. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  235. }
  236. finally
  237. {
  238. this.Invoke(() => { button2.Enabled = true; });
  239. }
  240. }
  241. private void button3_Click(object sender, EventArgs e)
  242. {
  243. if (PageIndex <= 1)
  244. {
  245. this.button3.Enabled = false;
  246. return;
  247. }
  248. PageIndex = PageIndex - 1;
  249. var data = reportCountDtos.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  250. dataGridView1.DataSource = data;
  251. this.button4.Enabled = true;
  252. label1.Text = $"{PageIndex}/{TotalPage}";
  253. }
  254. private void button4_Click(object sender, EventArgs e)
  255. {
  256. if (PageIndex >= TotalPage)
  257. {
  258. this.button4.Enabled = false;
  259. return;
  260. }
  261. PageIndex = PageIndex + 1;
  262. var data = reportCountDtos.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  263. dataGridView1.DataSource = data;
  264. this.button3.Enabled = true;
  265. label1.Text = $"{PageIndex}/{TotalPage}";
  266. }
  267. }
  268. }