StateChange.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Microsoft.Win32;
  2. using NEIntelligentControl2.Models;
  3. using NEIntelligentControl2.Models.Alarm;
  4. using NEIntelligentControl2.Models.Messages;
  5. using NEIntelligentControl2.Models.Windturbine;
  6. using NEIntelligentControl2.Service.WebSocket;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Security.Policy;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace NEIntelligentControl2.Views.Infos
  24. {
  25. /// <summary>
  26. /// 状态变化查询
  27. /// </summary>
  28. public partial class StateChange : UserControl
  29. {
  30. public WindturbineInfo WindturbineInfo { get; internal set; }
  31. private WEBHelper _WEBHelper;
  32. private UrlManager _UrlManager;
  33. private string _Url;
  34. private List<StatusTimeInfo> _CurrentData;
  35. public StateChange()
  36. {
  37. InitializeComponent();
  38. _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;
  39. _UrlManager = App.ServiceProvider.GetService(typeof(UrlManager)) as UrlManager;
  40. _Url = _UrlManager.ServicePath;
  41. Init();
  42. }
  43. private void Init()
  44. {
  45. var st = DateTime.Now.AddDays(-1);
  46. _tpstart.SelectedDateTime = st.AddSeconds(-st.Second);
  47. }
  48. private void Button_Click(object sender, RoutedEventArgs e)
  49. {
  50. switch (((Control)sender).Tag)
  51. {
  52. case "search":
  53. Search();
  54. break;
  55. case "output":
  56. Output();
  57. break;
  58. default:return;
  59. }
  60. }
  61. /// <summary>
  62. /// 导出数据
  63. /// </summary>
  64. private void Output()
  65. {
  66. SaveFileDialog dialog = new SaveFileDialog
  67. {
  68. Title = "保存csv文件",
  69. Filter = "csv文件(*.csv) |*.csv |所有文件(*.*) |*.*",
  70. FilterIndex = 1
  71. };
  72. var v = dialog.ShowDialog();
  73. if (v != true) return;
  74. var path = dialog.FileName;
  75. Task.Run(output);
  76. void output()
  77. {
  78. try
  79. {
  80. _IBMain.Inof("导出数据...");
  81. if (File.Exists(path))
  82. {
  83. File.Delete(path);
  84. }
  85. using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
  86. {
  87. sw.WriteLine("风机,状态,开始时间,结束时间,持续时间(小时)");
  88. foreach (var dt in _CurrentData)
  89. {
  90. sw.WriteLine(string.Format("{0},{1},{2},{3},{4}", dt.Code, dt.StatusName, dt.StartTime, dt.EndTime, dt.Duration));
  91. }
  92. }
  93. _IBMain.Success();
  94. Dispatcher.Invoke(() => MessageWindow.ShowMessage("数据导出成功!"));
  95. }
  96. catch (Exception ex)
  97. {
  98. _IBMain.Warning($"导出数据出现错误:{ex.Message}");
  99. }
  100. }
  101. }
  102. private void Search()
  103. {
  104. var ur = $"{_Url}/api/windturbine/status-nopage?startTs={_tpstart.SelectedDateTime.GetTimeSpan()}&endTs={_tpend.SelectedDateTime.GetTimeSpan()}&windturbineId={this.WindturbineInfo.WindturbineId}";
  105. Task.Run(search);
  106. void search()
  107. {
  108. try
  109. {
  110. _IBMain.Inof("查询数据...");
  111. var sts = _WEBHelper.HttpGetJSON<List<StatusTimeInfo>>(ur);
  112. Dispatcher.Invoke(() =>
  113. {
  114. _DGMain.ItemsSource = sts;
  115. _si.ItemsSource = sts;
  116. _CurrentData = sts;
  117. });
  118. _IBMain.Success();
  119. }
  120. catch (Exception ex)
  121. {
  122. _IBMain.Warning($"查询数据出现错误:{ex.Message}");
  123. }
  124. }
  125. }
  126. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  127. {
  128. Search();
  129. }
  130. }
  131. }