CtrlXl.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using DevExpress.XtraEditors;
  2. using XdDw.App.EFContext;
  3. using XzXdDw.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 XdDw.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();//.UseFilter();//UseRowNumber();//UseMultiSelect();//
  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. XtraMessageBox.Show("加载星历信息异常");
  54. }
  55. }
  56. private async void btnImp_Click(object sender, EventArgs e)
  57. {
  58. try
  59. {
  60. var lines = File.ReadAllLines(btnOpen.Text);
  61. List<XlInfo> tmp = new List<XlInfo>();
  62. for (int i = 0; i < lines.Length; i += 3)
  63. {
  64. if (string.IsNullOrWhiteSpace(lines[i]))
  65. {
  66. i -= 2;
  67. continue;
  68. }
  69. XlInfo xl = new XlInfo()
  70. {
  71. SatName = lines[i].Trim(),
  72. Line1 = lines[i + 1],
  73. Line2 = lines[i + 2],
  74. SatCode = Convert.ToInt32(lines[i + 2].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]),
  75. };
  76. //23356.37277044
  77. var timeStr = lines[i + 1].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[3];
  78. var yearStr = timeStr.Substring(0, 2);
  79. var dayStr = timeStr.Substring(2, timeStr.Length - 2);
  80. var day = Convert.ToDouble(dayStr);
  81. var year = 2000 + Convert.ToInt32(yearStr);
  82. DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);
  83. dt = dt.AddDays(day);
  84. xl.TimeBJ = dt.AddHours(8);
  85. tmp.Add(xl);
  86. }
  87. using (RHDWContext db = new RHDWContext())
  88. {
  89. foreach (var item in db.XlInfos)
  90. {
  91. var findItem = tmp.Find(p => p.SatCode == item.SatCode && p.TimeBJ == item.TimeBJ);
  92. if (findItem != null)
  93. {
  94. item.Line1 = findItem.Line1;
  95. item.Line2 = findItem.Line2;
  96. item.UpdateTime = DateTime.Now;
  97. }
  98. tmp.Remove(findItem);
  99. }
  100. db.XlInfos.AddRange(tmp);
  101. await db.SaveChangesAsync();
  102. }
  103. await LoadData();
  104. }
  105. catch (Exception ex)
  106. {
  107. Serilog.Log.Error("星历导入异常", ex);
  108. XtraMessageBox.Show("星历导入异常");
  109. }
  110. }
  111. private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
  112. {
  113. try
  114. {
  115. var ids = gridView1.GetSelectedRows();
  116. using (RHDWContext db = new RHDWContext())
  117. {
  118. foreach (var id in ids)
  119. {
  120. var item = gridView1.GetRow(id) as XlInfo;
  121. var delItem = await db.XlInfos.Where(p => p.ID == item.ID).FirstOrDefaultAsync();
  122. if (delItem != null)
  123. db.XlInfos.Remove(delItem);
  124. }
  125. await db.SaveChangesAsync();
  126. }
  127. gridView1.DeleteSelectedRows();
  128. }
  129. catch (Exception ex)
  130. {
  131. Serilog.Log.Error("删除选中的星历时出错", ex);
  132. XtraMessageBox.Show("删除选中的星历时出错");
  133. }
  134. }
  135. private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
  136. {
  137. if (gridView1.FocusedRowObject != null)
  138. popupMenu1.ShowPopup(MousePosition);
  139. }
  140. }
  141. }