CtrlXl.cs 6.9 KB

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