CtrlHistoryTask.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using DataSimulation.Repostory.Model;
  2. using DevExpress.Utils.Html;
  3. using DevExpress.XtraEditors;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Windows.Forms;
  12. using DataSimulation.Repostory.EFContext;
  13. using DataSimulation.Forms.EditForms;
  14. using DataSimulation.Repostory;
  15. using System.Threading.Tasks;
  16. using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
  17. using ExtensionsDev;
  18. namespace DataSimulation.Forms.UserControl
  19. {
  20. public partial class CtrlHistoryTask : DevExpress.XtraEditors.XtraUserControl
  21. {
  22. List<TaskInfo> list = new List<TaskInfo>();
  23. List<TaskInfo> listAllHistory = new List<TaskInfo>();
  24. public CtrlHistoryTask()
  25. {
  26. InitializeComponent();
  27. }
  28. private async void CtrlHistoryTask_Load(object sender, EventArgs e)
  29. {
  30. try
  31. {
  32. gridHistoryTask.Init<TaskInfo>().UseSort().UseFilter().UseMultiSelect().UseRowNumber();
  33. gridHistoryTask.DataSource = list;
  34. var taskListCache = await TaskCache.GetAllAsync();
  35. listAllHistory = taskListCache.Where(w => w.isHistory).ToList();
  36. list.AddRange(listAllHistory);
  37. gridView1.RefreshData();
  38. txtStartTime.UseDefault();
  39. txtEndTime.UseDefault();
  40. }
  41. catch (Exception ex)
  42. {
  43. Serilog.Log.Error(ex, "查询历史任务信息异常");
  44. DxHelper.MsgBoxHelper.ShowError("查询历史任务信息异常");
  45. }
  46. }
  47. private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
  48. {
  49. if (gridView1.GetSelectedRows().Any())
  50. {
  51. btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
  52. }
  53. else
  54. {
  55. btnDel.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
  56. }
  57. popupMenu1.ShowPopup(MousePosition);
  58. }
  59. private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  60. {
  61. try
  62. {
  63. if (XtraMessageBox.Show("确认删除选择的历史任务信息?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
  64. {
  65. return;
  66. }
  67. var ids = gridView1.GetSelectedRows();
  68. foreach (var id in ids)
  69. {
  70. var info = gridView1.GetRow(id) as TaskInfo;
  71. using (SimulationPartContext db = SimulationPartContext.GetContext(info.CreateTime))
  72. {
  73. var items = db.TaskInfos.Where(p => p.ID == id);
  74. if (items == null) return;
  75. db.TaskInfos.RemoveRange(items);
  76. await db.SaveChangesAsync();
  77. }
  78. }
  79. gridView1.DeleteSelectedRows();
  80. }
  81. catch (Exception ex)
  82. {
  83. Serilog.Log.Error(ex, "删除历史任务信息异常");
  84. DxHelper.MsgBoxHelper.ShowError("删除历史任务信息异常");
  85. }
  86. }
  87. private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
  88. {
  89. if (gridView1.RowCount == 0)
  90. {
  91. string txt = "暂无历史任务";
  92. var s = e.Appearance.CalcTextSize(e.Cache, txt, e.Bounds.Width).ToSize();
  93. var x = (e.Bounds.Width - s.Width) / 2;
  94. var y = (e.Bounds.Height - s.Height) / 2;
  95. e.Appearance.ForeColor = Color.Gray;
  96. e.Appearance.DrawString(e.Cache, txt, new Rectangle(x, y, s.Width, s.Height));
  97. }
  98. }
  99. private async void btnSearch_Click(object sender, EventArgs e)
  100. {
  101. var res = listAllHistory;
  102. if (!string.IsNullOrEmpty(txtTaskName.Text))
  103. {
  104. res = res.Where(w => w.TaskName.Contains(txtTaskName.Text)).ToList();
  105. }
  106. if (txtStartTime.DateTime != DateTime.MinValue)
  107. {
  108. res = res.Where(w => w.CreateTime >= txtStartTime.DateTime).ToList();
  109. }
  110. if (txtEndTime.DateTime != DateTime.MinValue)
  111. {
  112. res = res.Where(w => w.CreateTime <= txtEndTime.DateTime).ToList();
  113. }
  114. list.Clear();
  115. list.AddRange(res);
  116. gridView1.RefreshData();
  117. }
  118. }
  119. }