CtrlXl.cs 6.4 KB

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