CtrlTx.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using DevExpress.XtraEditors;
  2. using XdCxRhDW.App.EFContext;
  3. using XdCxRhDW.App.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using DevExpress.XtraEditors.Controls;
  15. namespace XdCxRhDW.App.UserControl
  16. {
  17. public partial class CtrlTx : DevExpress.XtraEditors.XtraUserControl
  18. {
  19. List<TxInfo> list = new List<TxInfo>();
  20. public CtrlTx()
  21. {
  22. InitializeComponent();
  23. }
  24. private async void CtrlTx_Load(object sender, EventArgs e)
  25. {
  26. try
  27. {
  28. using (RHDWContext db = new RHDWContext())
  29. {
  30. var sats = await db.SatInfos.ToListAsync();
  31. foreach (var item in sats)
  32. {
  33. this.txtMainSat.Properties.Items.Add(new ImageComboBoxItem(item.Sat, item));
  34. this.txtAdjaSat.Properties.Items.Add(new ImageComboBoxItem(item.Sat, item));
  35. }
  36. list = await db.TxInfos.ToListAsync();
  37. var mainTx = list.Find(p => p.TxType == EnumTxType.MainSat);
  38. var adjaTx = list.Find(p => p.TxType == EnumTxType.AdjaSat);
  39. var cdbTx = list.Find(p => p.TxType == EnumTxType.Cdb);
  40. var cxTx = list.Find(p => p.TxType == EnumTxType.Cx);
  41. var refLoc = list.Find(p => p.TxType == EnumTxType.Ref);
  42. if (mainTx != null)
  43. {
  44. this.txtMainTxName.Text = mainTx.Name;
  45. this.txtMainTxLon.Text = mainTx.Lon.ToString();
  46. this.txtMainTxLat.Text = mainTx.Lat.ToString();
  47. this.txtMainSat.EditValue = mainTx.SatInfo;
  48. }
  49. if (adjaTx != null)
  50. {
  51. this.txtAdjaTxName.Text = adjaTx.Name;
  52. this.txtAdjaTxLon.Text = adjaTx.Lon.ToString();
  53. this.txtAdjaTxLat.Text = adjaTx.Lat.ToString();
  54. this.txtAdjaSat.EditValue = adjaTx.SatInfo;
  55. }
  56. if (cdbTx != null)
  57. {
  58. this.txtCdbName.Text = cdbTx.Name;
  59. this.txtCdbLon.Text = cdbTx.Lon.ToString();
  60. this.txtCdbLat.Text = cdbTx.Lat.ToString();
  61. }
  62. if (cxTx != null)
  63. {
  64. this.txtCxName.Text = cxTx.Name;
  65. this.txtCxLon.Text = cxTx.Lon.ToString();
  66. this.txtCxLat.Text = cxTx.Lat.ToString();
  67. }
  68. if (refLoc != null)
  69. {
  70. this.txtRefLon.Text = refLoc.Lon.ToString();
  71. this.txtRefLat.Text = refLoc.Lat.ToString();
  72. }
  73. }
  74. }
  75. catch (Exception ex)
  76. {
  77. Serilog.Log.Error("加载天线信息异常", ex);
  78. XtraMessageBox.Show("加载天线信息异常");
  79. }
  80. }
  81. private async void btnSave_Click(object sender, EventArgs e)
  82. {
  83. try
  84. {
  85. TxInfo txMain = list.Find(p => p.TxType == EnumTxType.MainSat);
  86. TxInfo txAdja = list.Find(p => p.TxType == EnumTxType.AdjaSat);
  87. TxInfo txCdb = list.Find(p => p.TxType == EnumTxType.Cdb);
  88. TxInfo txCx = list.Find(p => p.TxType == EnumTxType.Cx);
  89. TxInfo txRef = list.Find(p => p.TxType == EnumTxType.Ref);
  90. if (txMain == null) txMain = new TxInfo();
  91. if (txAdja == null) txAdja = new TxInfo();
  92. if (txCdb == null) txCdb = new TxInfo();
  93. if (txCx == null) txCx = new TxInfo();
  94. if (txRef == null) txRef = new TxInfo();
  95. List<TxInfo> tmp = new List<TxInfo>();
  96. txMain.Name = txtMainTxName.Text;
  97. txMain.TxType = EnumTxType.MainSat;
  98. txMain.Lon = Convert.ToDouble(txtMainTxLon.Text);
  99. txMain.Lat = Convert.ToDouble(txtMainTxLat.Text);
  100. txMain.SatInfoID = (txtMainSat.EditValue as SatInfo)?.ID;
  101. tmp.Add(txMain);
  102. txAdja.Name = txtAdjaTxName.Text;
  103. txAdja.TxType = EnumTxType.AdjaSat;
  104. txAdja.Lon = Convert.ToDouble(txtAdjaTxLon.Text);
  105. txAdja.Lat = Convert.ToDouble(txtAdjaTxLat.Text);
  106. txAdja.SatInfoID = (txtAdjaSat.EditValue as SatInfo)?.ID;
  107. tmp.Add(txAdja);
  108. txCdb.Name = txtCdbName.Text;
  109. txCdb.TxType = EnumTxType.Cdb;
  110. txCdb.Lon = Convert.ToDouble(txtCdbLon.Text);
  111. txCdb.Lat = Convert.ToDouble(txtCdbLat.Text);
  112. txCdb.SatInfoID = null;
  113. tmp.Add(txCdb);
  114. txCx.Name = txtCxName.Text;
  115. txCx.TxType = EnumTxType.Cx;
  116. txCx.Lon = Convert.ToDouble(txtCxLon.Text);
  117. txCx.Lat = Convert.ToDouble(txtCxLat.Text);
  118. txCx.SatInfoID = null;
  119. tmp.Add(txCx);
  120. txRef.TxType = EnumTxType.Ref;
  121. txRef.Lon = Convert.ToDouble(txtRefLon.Text);
  122. txRef.Lat = Convert.ToDouble(txtRefLat.Text);
  123. txRef.SatInfoID = null;
  124. tmp.Add(txRef);
  125. using (RHDWContext db = new RHDWContext())
  126. {
  127. foreach (var item in tmp)
  128. {
  129. var find = await db.TxInfos.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  130. if (find != null)
  131. {
  132. find.Name = item.Name;
  133. find.Lon = item.Lon;
  134. find.Lat = item.Lat;
  135. find.SatInfoID = item.SatInfoID;
  136. }
  137. else
  138. {
  139. db.TxInfos.Add(item);
  140. }
  141. await db.SaveChangesAsync();
  142. }
  143. }
  144. XtraMessageBox.Show("保存成功!");
  145. }
  146. catch (Exception ex)
  147. {
  148. Serilog.Log.Error("保存天线信息异常", ex);
  149. XtraMessageBox.Show("保存天线信息异常");
  150. }
  151. }
  152. }
  153. }