AntEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using DevExpress.XtraEditors;
  2. using ExtensionsDev;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Data.Entity;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Windows.Forms;
  11. using DataSimulation.Repostory;
  12. using DataSimulation.Repostory.EFContext;
  13. using DataSimulation.Repostory.Model;
  14. using DxHelper;
  15. namespace DataSimulation.Forms.EditForms
  16. {
  17. public partial class AntEditor : DevExpress.XtraEditors.XtraForm
  18. {
  19. public AntInfo info;
  20. public AntEditor()
  21. {
  22. InitializeComponent();
  23. this.layoutControl1.UseDefault();
  24. this.Text = "添加天线";
  25. info = new AntInfo();
  26. this.StartPosition = FormStartPosition.CenterParent;
  27. btnOk.ImageOptions.SvgImage = SvgHelper.LoadFromFile("Images\\Ok.svg");
  28. btnOk.ImageOptions.SvgImageSize = new Size(20, 20);
  29. btnCancel.ImageOptions.SvgImage = SvgHelper.LoadFromFile("Images\\Cancel.svg");
  30. btnCancel.ImageOptions.SvgImageSize = new Size(20, 20);
  31. txtAntLon.EditValueChanged += TxtAntLon_EditValueChanged;
  32. txtAntLat.EditValueChanged += TxtAntLat_EditValueChanged;
  33. }
  34. private void TxtAntLat_EditValueChanged(object sender, EventArgs e)
  35. {
  36. txtAntLat.CheckLat(dxErrorProvider, "天线");
  37. }
  38. private void TxtAntLon_EditValueChanged(object sender, EventArgs e)
  39. {
  40. txtAntLon.CheckLon(dxErrorProvider, "天线");
  41. }
  42. public AntEditor(AntInfo info)
  43. : this()
  44. {
  45. this.Text = "编辑天线";
  46. this.info = info;
  47. }
  48. private void AntEditor_Load(object sender, EventArgs e)
  49. {
  50. if (this.Text == "编辑天线" && info != null)
  51. {
  52. this.txtAntName.Text = info.AntName;
  53. this.txtAntLon.Text = $"{info.AntLon}";
  54. this.txtAntLat.Text = $"{info.AntLat}";
  55. }
  56. }
  57. private void btnCancel_Click(object sender, EventArgs e)
  58. {
  59. this.DialogResult = DialogResult.Cancel;
  60. }
  61. private async void btnOk_Click(object sender, EventArgs e)
  62. {
  63. if (!txtAntLon.CheckLon(dxErrorProvider, "天线") || !txtAntLat.CheckLat(dxErrorProvider, "天线"))
  64. {
  65. return;
  66. }
  67. try
  68. {
  69. info.AntName = txtAntName.Text.Trim();
  70. info.AntLon = Convert.ToDouble(txtAntLon.Text);
  71. info.AntLat = Convert.ToDouble(txtAntLat.Text);
  72. if (this.Text == "添加天线")
  73. {
  74. using (SimulationContext db = new SimulationContext())
  75. {
  76. bool isAny = db.AntInfos.Any(s => s.AntName == info.AntName && s.AntLon == info.AntLon && s.AntLat == info.AntLat);
  77. if (isAny)
  78. {
  79. DxHelper.MsgBoxHelper.ShowError($"添加天线信息失败,已经存在卫星:{info.Ant}");
  80. return;
  81. }
  82. db.AntInfos.Add(info);
  83. await db.SaveChangesAsync();
  84. }
  85. }
  86. else
  87. {
  88. using (SimulationContext db = new SimulationContext())
  89. {
  90. bool isAny = db.AntInfos.Any(s => s.ID != info.ID && s.AntName == info.AntName && s.AntLon == info.AntLon && s.AntLat == info.AntLat);
  91. if (isAny)
  92. {
  93. DxHelper.MsgBoxHelper.ShowError($"编辑卫星信息失败,已经存在卫星:{info.Ant}");
  94. return;
  95. }
  96. var find = await db.AntInfos.Where(p => p.ID == info.ID).FirstOrDefaultAsync();
  97. find.AntName = info.AntName;
  98. find.AntLon = info.AntLon;
  99. find.AntLat = info.AntLat;
  100. await db.SaveChangesAsync();
  101. }
  102. }
  103. this.DialogResult = DialogResult.OK;
  104. }
  105. catch (Exception ex)
  106. {
  107. Serilog.Log.Error(ex, $"{this.Text}信息出错");
  108. DxHelper.MsgBoxHelper.ShowError($"{this.Text}信息出错");
  109. }
  110. }
  111. }
  112. }