FlightEditor.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. this.simulationInfos = simulationInfos;
  31. this.StartPosition = FormStartPosition.CenterParent;
  32. }
  33. public FlightEditor(List<SimulationInfo> simulationInfos, SimulationInfo info)
  34. : this(simulationInfos)
  35. {
  36. this.Text = "编辑航迹";
  37. this.info = info;
  38. }
  39. private async void Editor_Load(object sender, EventArgs e)
  40. {
  41. mapControl.UseDefalutOptions()
  42. .UseClearAll()
  43. .UseDistanceLine()
  44. .UseMarkDot()
  45. .UseExportImg()
  46. .UseExportXlsx()
  47. .UseExportCsv()
  48. .UseExportFlightLine()
  49. .SetMapLayerType(null);
  50. txtFlightName.EditValueChanged += TxtFlightName_EditValueChanged;
  51. txtSpeed.EditValueChanged += TxtSpeed_EditValueChanged;
  52. if (this.Text == "编辑航迹" && info != null)
  53. {
  54. this.txtFlightName.Text = info.SimulationName;
  55. this.txtSpeed.Text = $"{info.SimulationSpeed}";
  56. var points = await SimulationCache.GetAllByIDAsync(info.ID, info.CreateTime);
  57. var flinfo = new FlightInfo(info.SimulationName, info.SimulationSpeed);
  58. points.ForEach(m => flinfo.flights.Add(new FlightData(m.SimulationLon, m.SimulationLat)));
  59. mapControl.SetFlightLine(new FlightInfo[1] { flinfo });
  60. layoutControlItem12.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  61. layoutControlItem2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
  62. }
  63. }
  64. private void TxtSpeed_EditValueChanged(object sender, EventArgs e)
  65. {
  66. double speed;
  67. if (!double.TryParse(txtSpeed.Text, out speed))
  68. {
  69. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(m/s)");
  70. return;
  71. }
  72. if (speed <= 0)
  73. {
  74. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0(m/s)");
  75. return;
  76. }
  77. dxErrorProvider.SetError(txtSpeed, string.Empty);
  78. }
  79. private void TxtFlightName_EditValueChanged(object sender, EventArgs e)
  80. {
  81. string Name = txtFlightName.Text.Trim();
  82. if (string.IsNullOrEmpty(Name))
  83. {
  84. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  85. return;
  86. }
  87. if (simulationInfos.Any(m => m.SimulationName == Name))
  88. {
  89. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  90. return;
  91. }
  92. dxErrorProvider.SetError(txtFlightName, string.Empty);
  93. }
  94. private bool Validators()
  95. {
  96. dxErrorProvider.ClearErrors();
  97. string Name = txtFlightName.Text.Trim();
  98. if (string.IsNullOrEmpty(Name))
  99. {
  100. dxErrorProvider.SetError(txtFlightName, "请设置航迹名称");
  101. return false;
  102. }
  103. if (simulationInfos.Any(m => m.SimulationName == Name))
  104. {
  105. dxErrorProvider.SetError(txtFlightName, $"航迹[{Name}]已存在");
  106. return false;
  107. }
  108. double speed;
  109. if (!double.TryParse(txtSpeed.Text, out speed))
  110. {
  111. dxErrorProvider.SetError(txtSpeed, "请设置正确的航迹速度(m/s)");
  112. return false;
  113. }
  114. if (speed <= 0)
  115. {
  116. dxErrorProvider.SetError(txtSpeed, "航迹速度范围不能小于等于0(m/s)");
  117. return false;
  118. }
  119. return true;
  120. }
  121. private async void btnSave_Click(object sender, EventArgs e)
  122. {
  123. if (!Validators())
  124. {
  125. return;
  126. }
  127. try
  128. {
  129. var fligths = mapControl.GetFlightLine<FlightInfo>();
  130. var fligth = fligths.First();
  131. info = new SimulationInfo();
  132. info.SimulationName = fligth.FlightName;
  133. info.SimulationSpeed = fligth.Speed;
  134. using (SimulationPartContext db = SimulationPartContext.GetContext(info.CreateTime))
  135. {
  136. var sinfo = db.SimulationInfos.Add(info);
  137. await db.SaveChangesAsync();
  138. var points = fligth.flights.Select(f => new SimulationPonit()
  139. {
  140. SimulationId = sinfo.ID,
  141. SimulationLon = f.FlightLon,
  142. SimulationLat = f.FlightLat,
  143. });
  144. db.SimulationPonits.AddRange(points);
  145. await db.SaveChangesAsync();
  146. }
  147. this.DialogResult = DialogResult.OK;
  148. }
  149. catch (Exception ex)
  150. {
  151. Serilog.Log.Error(ex, $"{this.Text}异常");
  152. DxHelper.MsgBoxHelper.ShowError($"{this.Text}异常");
  153. }
  154. }
  155. private void btnFlight_Click(object sender, EventArgs e)
  156. {
  157. if (!Validators())
  158. {
  159. return;
  160. }
  161. try
  162. {
  163. string Name = txtFlightName.Text.Trim();
  164. double speed = Convert.ToDouble(txtSpeed.Text);
  165. mapControl.DrawFlightLine(Name, speed);
  166. }
  167. catch (Exception ex)
  168. {
  169. Serilog.Log.Error(ex, $"绘制航迹异常");
  170. DxHelper.MsgBoxHelper.ShowError($"绘制航迹异常");
  171. }
  172. }
  173. }
  174. }