CtrlHome.cs 9.8 KB

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