CtrlHome.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using DataSimulation.Forms.EditForms;
  2. using DataSimulation.Repostory;
  3. using DataSimulation.Repostory.EFContext;
  4. using DataSimulation.Repostory.Model;
  5. using DevExpress.Utils;
  6. using DevExpress.Utils.About;
  7. using DevExpress.XtraBars.Customization;
  8. using DevExpress.XtraEditors.ButtonsPanelControl;
  9. using DxHelper;
  10. using ExtensionsDev;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.ComponentModel.DataAnnotations;
  15. using System.Data;
  16. using System.Data.Entity;
  17. using System.Drawing;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Reflection;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Windows.Forms;
  24. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  25. namespace DataSimulation.Forms.UserControl
  26. {
  27. public partial class CtrlHome : DevExpress.XtraEditors.XtraUserControl
  28. {
  29. List<TaskInfo> taskList = new List<TaskInfo>();
  30. public CtrlHome()
  31. {
  32. InitializeComponent();
  33. gridControl.Init<TaskInfo>().UseFilter();
  34. layoutControl1.UseDefault();
  35. mapControl.UseDefalutOptions()
  36. .UseClearAll()
  37. .UseDistanceLine()
  38. .UseMarkDot()
  39. .UseExportImg()
  40. .UseExportXlsx()
  41. .UseExportCsv()
  42. .SetMapLayerType(null)
  43. .UseDrawRect(rect =>
  44. {
  45. (double startLon, double startLat, double centerLon, double centerLat, double endLon, double endLat, double lonRange, double latRange) = rect;
  46. });
  47. }
  48. private async void CtrlHome_Load(object sender, EventArgs e)
  49. {
  50. try
  51. {
  52. var yearDirs = Directory.EnumerateDirectories(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DbPart")).OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name));//年目录,倒叙排列
  53. foreach (var yearDir in yearDirs)
  54. {
  55. //每一天的db文件,倒序排列
  56. var dayFiles = Directory.EnumerateFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, yearDir), "*.db").OrderByDescending(p => Convert.ToInt32(new DirectoryInfo(p).Name.Substring(0, 4)));
  57. foreach (var dayFile in dayFiles)
  58. {
  59. using (SimulationPartContext db = SimulationPartContext.GetContext(dayFile))
  60. {
  61. taskList.AddRange(await db.TaskInfos.Where(w => !w.isHistory).ToListAsync());
  62. var runningList = await db.TaskInfos.Where(p => p.TaskState == EnumTaskState.Running && !p.isHistory).ToListAsync();
  63. foreach (var item in runningList)
  64. {
  65. item.TaskState = EnumTaskState.Stopped;
  66. }
  67. await db.SaveChangesAsync();
  68. }
  69. }
  70. }
  71. gridControl.DataSource = taskList;
  72. gridView.Columns[nameof(TaskInfo.TaskName)].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
  73. this.svgImageCollection1.Add("Stopped", SvgHelper.CreateCycle("#1E8B58"));
  74. this.svgImageCollection1.Add("Running", SvgHelper.CreateCycle("#2E8B57"));
  75. }
  76. catch (Exception)
  77. {
  78. throw;
  79. }
  80. }
  81. private async void layoutControlGroup_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e)
  82. {
  83. var txt = (e.Button as GroupBoxButton).Caption;
  84. if (txt == "新建任务")
  85. {
  86. try
  87. {
  88. TaskEditor frm = new TaskEditor();
  89. if (frm.ShowDialog() != DialogResult.OK) return;
  90. var addItem = frm.info;
  91. // var taskListCache = await TaskCache.GetAllAsync();
  92. if (taskList.ToList().Any(p => p.TaskName == addItem.TaskName))
  93. {
  94. DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
  95. return;
  96. }
  97. using (SimulationPartContext db = SimulationPartContext.GetContext(DateTime.Now))
  98. {
  99. db.TaskInfos.Add(addItem);
  100. await db.SaveChangesAsync();
  101. }
  102. taskList.Insert(0, addItem);
  103. gridView.RefreshData();
  104. gridView.FocusedRowHandle = 0;
  105. }
  106. catch (Exception ex)
  107. {
  108. Serilog.Log.Error(ex, "保存任务异常");
  109. DxHelper.MsgBoxHelper.ShowError("保存任务异常");
  110. }
  111. }
  112. else if (txt == "编辑任务")
  113. {
  114. try
  115. {
  116. var editItem = gridView.GetFocusedRow() as TaskInfo;
  117. TaskEditor frm = new TaskEditor(editItem);
  118. if (frm.ShowDialog() != DialogResult.OK) return;
  119. editItem = frm.info;
  120. var taskListCache = await TaskCache.GetAllAsync();
  121. if (taskListCache.ToList().Any(p => p.TaskName == editItem.TaskName))
  122. {
  123. DxHelper.MsgBoxHelper.ShowWarning("任务名称重复");
  124. return;
  125. }
  126. using (SimulationPartContext db = SimulationPartContext.GetContext(editItem.CreateTime))
  127. {
  128. var find = db.TaskInfos.Where(p => p.ID == editItem.ID).ToList().FirstOrDefault();
  129. if (find == null) return;
  130. find.TaskName = editItem.TaskName;
  131. find.SimulationType = editItem.SimulationType;
  132. find.UpdateTime = DateTime.Now;
  133. find.Freq = editItem.Freq;
  134. find.Band = editItem.Band;
  135. find.MainSat = editItem.MainSat;
  136. if (find.SimulationType == EnumSimulationType.X3TwoDto)
  137. {
  138. find.Adja1Sat = editItem.Adja1Sat;
  139. find.Adja2Sat = editItem.Adja2Sat;
  140. }
  141. await db.SaveChangesAsync();
  142. }
  143. gridView.RefreshData();
  144. }
  145. catch (Exception ex)
  146. {
  147. Serilog.Log.Error(ex, "保存任务异常");
  148. DxHelper.MsgBoxHelper.ShowError("保存任务异常");
  149. }
  150. }
  151. else
  152. {
  153. try
  154. {
  155. var tsk = gridView.GetFocusedRow() as TaskInfo;
  156. if (tsk == null) return;
  157. if (!DxHelper.MsgBoxHelper.ShowConfirm($"删除任务[{tsk.TaskName}]?"))
  158. return;
  159. using (SimulationPartContext db = SimulationPartContext.GetContext(tsk.CreateTime))
  160. {
  161. var item = db.TaskInfos.FirstOrDefault(p => p.ID == tsk.ID);
  162. if (item == null) return;
  163. db.TaskInfos.Remove(item);
  164. await db.SaveChangesAsync();
  165. }
  166. gridView.DeleteSelectedRows();
  167. }
  168. catch (Exception ex)
  169. {
  170. Serilog.Log.Error(ex, "删除任务异常");
  171. DxHelper.MsgBoxHelper.ShowError("删除任务异常");
  172. }
  173. }
  174. }
  175. private void gridView_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
  176. {
  177. if (e.Column.FieldName == nameof(TaskInfo.Freq))
  178. {
  179. e.DisplayText = $"{(long)e.Value / 1e6}MHz";
  180. }
  181. else if (e.Column.FieldName == nameof(TaskInfo.Band))
  182. {
  183. e.DisplayText = $"{(long)e.Value / 1e6}MHz";
  184. }
  185. }
  186. private async void btnStart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  187. {
  188. var tsk = gridView.GetFocusedRow() as TaskInfo;
  189. if (e.Item.Caption == "启动任务")
  190. {
  191. try
  192. {
  193. if (tsk.TaskState == EnumTaskState.Running)
  194. {
  195. DxHelper.MsgBoxHelper.ShowWarning($"该任务正在执行!");
  196. return;
  197. }
  198. using (SimulationPartContext db = SimulationPartContext.GetContext(tsk.CreateTime))
  199. {
  200. var item = await db.TaskInfos.FirstOrDefaultAsync(p => p.ID == tsk.ID);
  201. item.TaskState = EnumTaskState.Running;
  202. item.isHistory = true;
  203. //gridView.DeleteSelectedRows();
  204. await db.SaveChangesAsync();
  205. Serilog.Log.Information($"用户启动了任务,ID={tsk.ID}");
  206. }
  207. }
  208. catch (Exception ex)
  209. {
  210. Serilog.Log.Error(ex, "启动任务异常");
  211. DxHelper.MsgBoxHelper.ShowError($"启动任务异常.{ex.Message}");
  212. }
  213. }
  214. }
  215. }
  216. }