FlightEditor.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using DataSimulation.Repostory;
  2. using DataSimulation.Repostory.EFContext;
  3. using DataSimulation.Repostory.Model;
  4. using DevExpress.Utils.About;
  5. using DevExpress.XtraEditors.Controls;
  6. using DevExpress.XtraEditors.DXErrorProvider;
  7. using DxHelper;
  8. using ExtensionsDev;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Entity;
  14. using System.Data.Entity.Migrations;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. namespace DataSimulation.Forms.EditForms
  21. {
  22. public partial class FlightEditor : DevExpress.XtraEditors.XtraForm
  23. {
  24. public SimulationInfo info;
  25. private List<SimulationInfo> simulationInfos;
  26. public FlightEditor(List<SimulationInfo> simulationInfos)
  27. {
  28. InitializeComponent();
  29. this.Text = "添加航迹";
  30. info = new SimulationInfo();
  31. this.simulationInfos = simulationInfos;
  32. this.StartPosition = FormStartPosition.CenterParent;
  33. }
  34. public FlightEditor(List<SimulationInfo> simulationInfos,SimulationInfo info)
  35. : this(simulationInfos)
  36. {
  37. this.Text = "编辑航迹";
  38. this.info = info;
  39. }
  40. private void Editor_Load(object sender, EventArgs e)
  41. {
  42. mapControl.UseDefalutOptions()
  43. .UseClearAll()
  44. .UseDistanceLine()
  45. .UseMarkDot()
  46. .UseExportImg()
  47. .UseExportXlsx()
  48. .UseExportCsv()
  49. .SetMapLayerType(null);
  50. txtFlightName.EditValueChanged += TxtFlightName_EditValueChanged;
  51. txtSpeed.EditValueChanged += TxtSpeed_EditValueChanged;
  52. }
  53. private void TxtSpeed_EditValueChanged(object sender, EventArgs e)
  54. {
  55. double speed;
  56. if (!double.TryParse(txtSpeed.Text, out speed))
  57. {
  58. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(Km/s)");
  59. return;
  60. }
  61. if (speed <= 0)
  62. {
  63. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0Km/s");
  64. return;
  65. }
  66. dxErrorProvider.SetError(txtSpeed, string.Empty);
  67. }
  68. private void TxtFlightName_EditValueChanged(object sender, EventArgs e)
  69. {
  70. string Name = txtFlightName.Text.Trim();
  71. if (string.IsNullOrEmpty(Name))
  72. {
  73. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  74. return;
  75. }
  76. if (simulationInfos.Any(m => m.SimulationName == Name))
  77. {
  78. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  79. return;
  80. }
  81. dxErrorProvider.SetError(txtFlightName, string.Empty);
  82. }
  83. private bool Validators()
  84. {
  85. dxErrorProvider.ClearErrors();
  86. string Name = txtFlightName.Text.Trim();
  87. if (string.IsNullOrEmpty(Name))
  88. {
  89. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  90. return false;
  91. }
  92. if (simulationInfos.Any(m => m.SimulationName == Name))
  93. {
  94. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  95. return false;
  96. }
  97. double speed;
  98. if (!double.TryParse(txtSpeed.Text, out speed))
  99. {
  100. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(Km/s)");
  101. return false;
  102. }
  103. if (speed <= 0)
  104. {
  105. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0Km/s");
  106. return false;
  107. }
  108. return true;
  109. }
  110. private async void btnSave_Click(object sender, EventArgs e)
  111. {
  112. if (!Validators())
  113. {
  114. return;
  115. }
  116. try
  117. {
  118. var fligths = mapControl.GetFlightLine<FlightInfo>();
  119. var fligth = fligths.First();
  120. SimulationInfo simulationInfo = new SimulationInfo();
  121. simulationInfo.SimulationName = fligth.FlightName;
  122. simulationInfo.SimulationSpeed = fligth.Speed;
  123. using (SimulationPartContext db = SimulationPartContext.GetContext(simulationInfo.CreateTime))
  124. {
  125. var sinfo = db.SimulationInfos.Add(simulationInfo);
  126. await db.SaveChangesAsync();
  127. var points = fligth.flights.Select(f => new SimulationPonit()
  128. {
  129. SimulationId = sinfo.ID,
  130. SimulationLon = f.FlightLon,
  131. SimulationLat = f.FlightLat,
  132. });
  133. db.SimulationPonits.AddRange(points);
  134. await db.SaveChangesAsync();
  135. }
  136. this.DialogResult = DialogResult.OK;
  137. }
  138. catch (Exception ex)
  139. {
  140. Serilog.Log.Error(ex, $"{this.Text}异常");
  141. DxHelper.MsgBoxHelper.ShowError($"{this.Text}异常");
  142. }
  143. }
  144. private void btnFlight_Click(object sender, EventArgs e)
  145. {
  146. if (!Validators())
  147. {
  148. return;
  149. }
  150. try
  151. {
  152. string Name = txtFlightName.Text.Trim();
  153. double speed = Convert.ToDouble(txtSpeed.Text);
  154. mapControl.DrawFlightLine(Name, speed);
  155. }
  156. catch (Exception ex)
  157. {
  158. Serilog.Log.Error(ex, $"绘制航迹异常");
  159. DxHelper.MsgBoxHelper.ShowError($"绘制航迹异常");
  160. }
  161. }
  162. }
  163. }