CtrlSat.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using DevExpress.XtraBars.Customization;
  8. using DW5S.App.EditForms;
  9. using DW5S.Entity;
  10. using DW5S.Repostory;
  11. using DW5S.Service;
  12. using Serilog;
  13. using XdCxRhDW5S.Entity;
  14. namespace DW5S.App.UserControl
  15. {
  16. public partial class CtrlSat : DevExpress.XtraEditors.XtraUserControl
  17. {
  18. readonly List<SatViewModel> list = new List<SatViewModel>();
  19. public CtrlSat()
  20. {
  21. InitializeComponent();
  22. }
  23. private async void CtrlSat_Load(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. gridSat.UseDefault(list).UseMultiSelect().UseRowNumber()
  28. .UseAddAsync(Add)
  29. .UseEditAsync<SatViewModel>(Edit)
  30. .UseDeleteAsync<SatViewModel>(Delete);
  31. var unitOfWork = IocContainer.UnitOfWork;
  32. var repsSat = unitOfWork.Of<SatInfo>();
  33. var items = await repsSat.GetAllAsync(p => p.SatName);
  34. list.AddRange(items.To<List<SatViewModel>>());
  35. }
  36. catch (Exception ex)
  37. {
  38. string msg = "查询卫星信息异常";
  39. IocContainer.Logger.Error(ex, msg);
  40. DxHelper.MsgBoxHelper.ShowError(msg);
  41. }
  42. }
  43. private async Task<SatViewModel> Add()
  44. {
  45. try
  46. {
  47. SatEditor frm = new SatEditor();
  48. if (frm.ShowDialog() != DialogResult.OK) return null;
  49. var addItem = frm.info;
  50. var unitOfWork = IocContainer.UnitOfWork;
  51. var repsSat = unitOfWork.Of<SatInfo>();
  52. var satInfo = await repsSat.AddOrUpdateAsync(addItem.To<SatInfo>());
  53. await unitOfWork.SaveAsync();
  54. addItem.Id = satInfo.Id;
  55. return addItem;
  56. }
  57. catch (Exception ex)
  58. {
  59. IocContainer.Logger.Error(ex, "添加卫星信息异常");
  60. DxHelper.MsgBoxHelper.ShowError("添加卫星信息异常");
  61. return null;
  62. }
  63. }
  64. private async Task<SatViewModel> Edit(SatViewModel editItem)
  65. {
  66. try
  67. {
  68. SatEditor frm = new SatEditor(editItem);
  69. if (frm.ShowDialog() != DialogResult.OK) return null;
  70. editItem = frm.info;
  71. var unitOfWork = IocContainer.UnitOfWork;
  72. var repsSat = unitOfWork.Of<SatInfo>();
  73. await repsSat.AddOrUpdateAsync(editItem.To<SatInfo>());
  74. await unitOfWork.SaveAsync();
  75. return editItem;
  76. }
  77. catch (Exception ex)
  78. {
  79. IocContainer.Logger.Error(ex, "保存卫星信息异常");
  80. DxHelper.MsgBoxHelper.ShowError("保存卫星信息异常");
  81. return null;
  82. }
  83. }
  84. private async Task<bool> Delete(List<SatViewModel> list)
  85. {
  86. try
  87. {
  88. var ids = list.Select(p => p.Id);
  89. var unitOfWork = IocContainer.UnitOfWork;
  90. var repsSat = unitOfWork.Of<SatInfo>();
  91. await repsSat.DeleteAsync(list.To<List<SatInfo>>());
  92. await unitOfWork.SaveAsync();
  93. return true;
  94. }
  95. catch (Exception ex)
  96. {
  97. IocContainer.Logger.Error("删除卫星信息异常", ex);
  98. DxHelper.MsgBoxHelper.ShowError("删除卫星信息异常");
  99. return false;
  100. }
  101. }
  102. private async void gridView1_DoubleClick(object sender, EventArgs e)
  103. {
  104. var editItem = gridView1.GetFocusedRow() as SatViewModel;
  105. if (editItem == null) return;
  106. await Edit(editItem);
  107. }
  108. }
  109. }