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