RefEditor.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 RefEditor : DevExpress.XtraEditors.XtraForm
  18. {
  19. public RefInfo info;
  20. public RefEditor()
  21. {
  22. InitializeComponent();
  23. this.layoutControl1.UseDefault();
  24. this.Text = "添加参考站";
  25. info = new RefInfo();
  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. txtRefLon.EditValueChanged += TxtLon_EditValueChanged;
  32. txtRefLat.EditValueChanged += TxtLat_EditValueChanged;
  33. }
  34. private void TxtLat_EditValueChanged(object sender, EventArgs e)
  35. {
  36. txtRefLat.CheckLat(dxErrorProvider, "参考站");
  37. }
  38. private void TxtLon_EditValueChanged(object sender, EventArgs e)
  39. {
  40. txtRefLon.CheckLon(dxErrorProvider, "参考站");
  41. }
  42. public RefEditor(RefInfo info)
  43. : this()
  44. {
  45. this.Text = "编辑参考站";
  46. this.info = info;
  47. }
  48. private void Editor_Load(object sender, EventArgs e)
  49. {
  50. if (this.Text == "编辑参考站" && info != null)
  51. {
  52. this.txtRefName.Text = info.RefName;
  53. this.txtRefLon.Text = $"{info.RefLon}";
  54. this.txtRefLat.Text = $"{info.RefLat}";
  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 (!txtRefLon.CheckLon(dxErrorProvider, "参考站") || !txtRefLat.CheckLat(dxErrorProvider, "参考站"))
  64. {
  65. return;
  66. }
  67. try
  68. {
  69. info.RefName = txtRefName.Text.Trim();
  70. info.RefLon = Convert.ToDouble(txtRefLon.Text);
  71. info.RefLat = Convert.ToDouble(txtRefLat.Text);
  72. if (this.Text == "添加参考站")
  73. {
  74. using (SimulationContext db = new SimulationContext())
  75. {
  76. bool isAny = db.RefInfos.Any(s => s.RefName == info.RefName && s.RefLon == info.RefLon && s.RefLat == info.RefLat);
  77. if (isAny)
  78. {
  79. DxHelper.MsgBoxHelper.ShowError($"添加参考站信息失败,已经存在参考站:{info.Ref}");
  80. return;
  81. }
  82. db.RefInfos.Add(info);
  83. await db.SaveChangesAsync();
  84. }
  85. }
  86. else
  87. {
  88. using (SimulationContext db = new SimulationContext())
  89. {
  90. bool isAny = db.RefInfos.Any(s => s.ID != info.ID && s.RefName == info.RefName && s.RefLon == info.RefLon && s.RefLat == info.RefLat);
  91. if (isAny)
  92. {
  93. DxHelper.MsgBoxHelper.ShowError($"编辑参考站信息失败,已经存在参考站:{info.Ref}");
  94. return;
  95. }
  96. var find = await db.RefInfos.Where(p => p.ID == info.ID).FirstOrDefaultAsync();
  97. find.RefName = info.RefName;
  98. find.RefLon = info.RefLon;
  99. find.RefLat = info.RefLat;
  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. }