CtrlHome.cs 9.5 KB

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