CtrlXl.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.Data.Entity.Migrations;
  10. using System.Drawing;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace XdCxRhDW.App.UserControl
  17. {
  18. public partial class CtrlXl : DevExpress.XtraEditors.XtraUserControl
  19. {
  20. List<XlInfo> list = new List<XlInfo>();
  21. public CtrlXl()
  22. {
  23. InitializeComponent();
  24. }
  25. private async void CtrlXl_Load(object sender, EventArgs e)
  26. {
  27. gridXl.Init().UseSort().UseFilter().UseMultiSelect().UseRowNumber();
  28. gridXl.DataSource = list;
  29. await LoadData();
  30. }
  31. private void btnOpen_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  32. {
  33. if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
  34. {
  35. btnOpen.Text = xtraOpenFileDialog1.FileName;
  36. }
  37. }
  38. public async Task LoadData()
  39. {
  40. try
  41. {
  42. list.Clear();
  43. using (RHDWContext db = new RHDWContext())
  44. {
  45. var res = db.XlInfos.OrderBy(p => p.SatName).OrderByDescending(p => p.TimeBJ);
  46. list.AddRange(await res.ToListAsync());
  47. }
  48. gridView1.RefreshData();
  49. }
  50. catch (Exception ex)
  51. {
  52. Serilog.Log.Error(ex,"加载星历信息异常");
  53. DxHelper.MsgBoxHelper.ShowError("加载星历信息异常");
  54. }
  55. }
  56. private async void btnImp_Click(object sender, EventArgs e)
  57. {
  58. //https://www.space-track.org/documentation#tle网站上有星历文件格式说明
  59. string line = null;
  60. try
  61. {
  62. this.Enabled = false;
  63. await Task.Run(async () =>
  64. {
  65. var lines = File.ReadAllLines(btnOpen.Text).ToList();
  66. lines.RemoveAll(p => string.IsNullOrWhiteSpace(p));
  67. List<XlInfo> tmp = new List<XlInfo>();
  68. for (int i = 0; i < lines.Count; i += 3)
  69. {
  70. line = lines[i];
  71. var satName = lines[i].Trim();
  72. if (satName.StartsWith("0 "))
  73. satName = satName.Substring(2).Trim();
  74. if (satName.StartsWith("TBA"))//待发布的卫星
  75. continue;
  76. XlInfo xl = new XlInfo()
  77. {
  78. SatName = satName,
  79. Line1 = lines[i + 1],
  80. Line2 = lines[i + 2],
  81. SatCode = Convert.ToInt32(lines[i + 1].Substring(2, 5))
  82. };
  83. var timeStr = lines[i + 1].Substring(18, 14).Replace(" ","");//https://www.space-track.org/documentation#tle星历接口中说这里面可以接受空格
  84. var yearStr = timeStr.Substring(0, 2);
  85. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  86. var day = Convert.ToDouble(dayStr);
  87. var year = 2000 + Convert.ToInt32(yearStr);
  88. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  89. dt = dt.AddDays(day);
  90. xl.TimeBJ = dt.AddHours(8);
  91. tmp.Add(xl);
  92. }
  93. using (RHDWContext db = new RHDWContext())
  94. {
  95. foreach (var item in db.XlInfos)
  96. {
  97. var findItem = tmp.Find(p => p.SatCode == item.SatCode && p.TimeBJ == item.TimeBJ);
  98. if (findItem != null)
  99. {
  100. item.Line1 = findItem.Line1;
  101. item.Line2 = findItem.Line2;
  102. item.UpdateTime = DateTime.Now;
  103. }
  104. tmp.Remove(findItem);
  105. }
  106. db.XlInfos.AddRange(tmp);
  107. await db.SaveChangesAsync();
  108. }
  109. await LoadData();
  110. });
  111. Serilog.Log.Information("星历导入成功");
  112. DxHelper.MsgBoxHelper.ShowInfo("星历导入成功");
  113. }
  114. catch (Exception ex)
  115. {
  116. Serilog.Log.Error(ex, $"{line}星历导入异常");
  117. DxHelper.MsgBoxHelper.ShowError("星历导入异常");
  118. }
  119. finally
  120. {
  121. this.Enabled = true;
  122. }
  123. }
  124. private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  125. {
  126. try
  127. {
  128. this.Enabled = false;
  129. await Task.Run(async () =>
  130. {
  131. var ids = gridView1.GetSelectedRows();
  132. List<int> listID = new List<int>();
  133. foreach (var idx in ids)
  134. {
  135. var id = (gridView1.GetRow(idx) as XlInfo).ID;
  136. listID.Add(id);
  137. }
  138. using (RHDWContext db = new RHDWContext())
  139. {
  140. var delItems = await db.XlInfos.Where(p => listID.Contains(p.ID)).ToListAsync();
  141. db.XlInfos.RemoveRange(delItems);
  142. await db.SaveChangesAsync();
  143. }
  144. });
  145. gridView1.DeleteSelectedRows();
  146. }
  147. catch (Exception ex)
  148. {
  149. Serilog.Log.Error(ex, "删除选中的星历时异常");
  150. DxHelper.MsgBoxHelper.ShowError("删除选中的星历时异常");
  151. }
  152. finally
  153. {
  154. this.Enabled = true;
  155. }
  156. }
  157. private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
  158. {
  159. if (gridView1.FocusedRowObject != null)
  160. popupMenu1.ShowPopup(MousePosition);
  161. }
  162. }
  163. }