CtrlXl.cs 6.5 KB

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