using DevExpress.XtraEditors; using XzXdDw.App.EFContext; using XzXdDw.App.Model; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace XzXdDw.App.UserControl { public partial class CtrlXl : DevExpress.XtraEditors.XtraUserControl { List list = new List(); public CtrlXl() { InitializeComponent(); } private async void CtrlXl_Load(object sender, EventArgs e) { gridXl.Init().UseFilter().UseMultiSelect().UseRowNumber(); gridXl.DataSource = list; await LoadData(); } private void btnOpen_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK) { btnOpen.Text = xtraOpenFileDialog1.FileName; } } public async Task LoadData() { try { list.Clear(); using (RHDWContext db = new RHDWContext()) { var res = db.XlInfos.OrderBy(p => p.SatName).OrderByDescending(p => p.TimeBJ); list.AddRange(await res.ToListAsync()); } gridView1.RefreshData(); } catch (Exception ex) { Serilog.Log.Error("加载星历信息异常", ex); XtraMessageBox.Show("加载星历信息异常"); } } private async void btnImp_Click(object sender, EventArgs e) { try { var lines = File.ReadAllLines(btnOpen.Text); List tmp = new List(); for (int i = 0; i < lines.Length; i += 3) { if (string.IsNullOrWhiteSpace(lines[i])) { i -= 2; continue; } XlInfo xl = new XlInfo() { SatName = lines[i].Trim(), Line1 = lines[i + 1], Line2 = lines[i + 2], SatCode = Convert.ToInt32(lines[i + 2].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]), }; //23356.37277044 var timeStr = lines[i + 1].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[3]; var yearStr = timeStr.Substring(0, 2); var dayStr = timeStr.Substring(2, timeStr.Length - 2); var day = Convert.ToDouble(dayStr); var year = 2000 + Convert.ToInt32(yearStr); DateTime dt = new DateTime(year, 1, 1, 0, 0, 0); dt = dt.AddDays(day); xl.TimeBJ = dt.AddHours(8); tmp.Add(xl); } using (RHDWContext db = new RHDWContext()) { foreach (var item in db.XlInfos) { var findItem = tmp.Find(p => p.SatCode == item.SatCode && p.TimeBJ == item.TimeBJ); if (findItem != null) { item.Line1 = findItem.Line1; item.Line2 = findItem.Line2; item.UpdateTime = DateTime.Now; } tmp.Remove(findItem); } db.XlInfos.AddRange(tmp); await db.SaveChangesAsync(); } await LoadData(); } catch (Exception ex) { Serilog.Log.Error("星历导入异常", ex); XtraMessageBox.Show("星历导入异常"); } } private async void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { try { var ids = gridView1.GetSelectedRows(); using (RHDWContext db = new RHDWContext()) { foreach (var id in ids) { var item = gridView1.GetRow(id) as XlInfo; var delItem = await db.XlInfos.Where(p => p.ID == item.ID).FirstOrDefaultAsync(); if (delItem != null) db.XlInfos.Remove(delItem); } await db.SaveChangesAsync(); } gridView1.DeleteSelectedRows(); } catch (Exception ex) { Serilog.Log.Error("删除选中的星历时出错", ex); XtraMessageBox.Show("删除选中的星历时出错"); } } private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e) { if (gridView1.FocusedRowObject != null) popupMenu1.ShowPopup(MousePosition); } } }