CtrlFixedStation.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. using XdCxRhDW.App.EditForms;
  9. using XdCxRhDW.Entity;
  10. using XdCxRhDW.Repostory;
  11. namespace XdCxRhDW.App.UserControl
  12. {
  13. public partial class CtrlFixedStation : DevExpress.XtraEditors.XtraUserControl
  14. {
  15. readonly List<FixedStation> list = new List<FixedStation>();
  16. public CtrlFixedStation()
  17. {
  18. InitializeComponent();
  19. }
  20. private async void CtrlSat_Load(object sender, EventArgs e)
  21. {
  22. try
  23. {
  24. gridFixedStation.UseDefault(list).UseMultiSelect().UseRowNumber()
  25. .UseAddAsync(Add)
  26. .UseEditAsync<FixedStation>(Edit)
  27. .UseDeleteAsync<FixedStation>(Delete).SetDisplayText(nameof(FixedStation.Value), val => $"{val}秒");
  28. using (var db = new RHDWContext())
  29. {
  30. var items = await db.FixedStation.OrderBy(p => p.StationName).ToListAsync();
  31. list.AddRange(items);
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Serilog.Log.Error(ex, "查询固定站信息异常");
  37. DxHelper.MsgBoxHelper.ShowError("查询固定站信息异常");
  38. }
  39. }
  40. private async Task<FixedStation> Add()
  41. {
  42. try
  43. {
  44. FixedStationEditor frm = new FixedStationEditor();
  45. if (frm.ShowDialog() != DialogResult.OK) return null;
  46. var addItem = frm.info;
  47. using (RHDWContext db = new RHDWContext())
  48. {
  49. db.FixedStation.Add(addItem);
  50. await db.SaveChangesAsync();
  51. }
  52. return addItem;
  53. }
  54. catch (Exception ex)
  55. {
  56. Serilog.Log.Error(ex, "添加卫星信息异常");
  57. DxHelper.MsgBoxHelper.ShowError("添加卫星信息异常");
  58. return null;
  59. }
  60. }
  61. private async Task<FixedStation> Edit(FixedStation editItem)
  62. {
  63. try
  64. {
  65. FixedStationEditor frm = new FixedStationEditor(editItem);
  66. if (frm.ShowDialog() != DialogResult.OK) return null;
  67. editItem = frm.info;
  68. using (RHDWContext db = new RHDWContext())
  69. {
  70. var find = await db.FixedStation.Where(p => p.ID == editItem.ID).FirstOrDefaultAsync();
  71. find.StationName = editItem.StationName;
  72. find.FreqUpHz = editItem.FreqUpHz;
  73. find.Lon = editItem.Lon;
  74. find.Lat = editItem.Lat;
  75. find.Value = editItem.Value;
  76. find.Enable = editItem.Enable;
  77. find.UpdateTime = DateTime.Now;
  78. await db.SaveChangesAsync();
  79. }
  80. return editItem;
  81. }
  82. catch (Exception ex)
  83. {
  84. Serilog.Log.Error(ex, "保存固定站信息异常");
  85. DxHelper.MsgBoxHelper.ShowError("保存固定站信息异常");
  86. return null;
  87. }
  88. }
  89. private async Task<bool> Delete(List<FixedStation> list)
  90. {
  91. try
  92. {
  93. var ids = list.Select(p => p.ID);
  94. using (RHDWContext db = new RHDWContext())
  95. {
  96. var delItems = await db.SatInfos.Where(p => ids.Contains(p.ID)).ToListAsync();
  97. db.SatInfos.RemoveRange(delItems);
  98. await db.SaveChangesAsync();
  99. }
  100. return true;
  101. }
  102. catch (Exception ex)
  103. {
  104. Serilog.Log.Error(ex, "删除卫星信息异常");
  105. DxHelper.MsgBoxHelper.ShowError("删除卫星信息异常");
  106. return false;
  107. }
  108. }
  109. private async void gridView1_DoubleClick(object sender, EventArgs e)
  110. {
  111. var editItem = gridView1.GetFocusedRow() as FixedStation;
  112. if (editItem == null) return;
  113. await Edit(editItem);
  114. }
  115. }
  116. }