Device.razor.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using AI.Platform.Core;
  2. using Microsoft.AspNetCore.Components;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Threading.Tasks;
  9. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.Device;
  10. using ZhonTai.Admin.Contracts.Domain.VehicleTerminal.UserInfo;
  11. namespace AI.Platform.Page.Pages.SiteConfiguration
  12. {
  13. public partial class Device : ComponentBase
  14. {
  15. [Inject] private SqlSugarRepository<DeviceEntity> _DeviceRepository { get; set; }
  16. #region 模型定义
  17. public class DeviceDto
  18. {
  19. public int Id { get; set; }
  20. public string DeviceId { get; set; } = "";
  21. public string SiteBUID { get; set; } = "";
  22. public string SiteName { get; set; } = "";
  23. public string OilProductName { get; set; } = "";
  24. public decimal? TankCapacity { get; set; }
  25. public decimal? CurrentCapacity { get; set; }
  26. public DateTime? LastUpdatedTime { get; set; }
  27. public string Status { get; set; } = "";
  28. public string Location { get; set; } = "";
  29. public string Manufacturer { get; set; } = "";
  30. public string LicencePlate { get; set; } = "";
  31. public DateTime InstallationDate { get; set; }
  32. public DateTime CreateTime { get; set; }
  33. public DateTime UpdateTime { get; set; }
  34. }
  35. public class DeviceEditModel
  36. {
  37. public int Id { get; set; }
  38. public string DeviceId { get; set; } = "";
  39. public string SiteBUID { get; set; } = "";
  40. public string SiteName { get; set; } = "";
  41. public string OilProductName { get; set; } = "";
  42. public decimal? TankCapacity { get; set; }
  43. public decimal? CurrentCapacity { get; set; }
  44. public string Status { get; set; } = "正常";
  45. public string Location { get; set; } = "";
  46. public string Manufacturer { get; set; } = "";
  47. public DateTime InstallationDate { get; set; } = DateTime.Today;
  48. public string LicencePlate { get; set; }
  49. }
  50. public class SelectOption
  51. {
  52. public string? Value { get; set; }
  53. public string? Label { get; set; }
  54. public bool Disabled { get; set; }
  55. }
  56. #endregion
  57. #region 状态变量
  58. private string License = "";
  59. private string deviceNo = "";
  60. private bool _loading = false;
  61. private List<DeviceDto> _deviceList = new();
  62. private int _totalCount = 0;
  63. private int _currentPage = 1;
  64. private int _pageSize = 20;
  65. private bool _showNewDeviceDialog = false;
  66. private DeviceEditModel _deviceModel = new();
  67. #endregion
  68. #region 选项数据
  69. private readonly List<SelectOption> _oilProductOptions = new()
  70. {
  71. new SelectOption { Value = "92#汽油", Label = "92#汽油" },
  72. new SelectOption { Value = "95#汽油", Label = "95#汽油" },
  73. new SelectOption { Value = "0#柴油", Label = "0#柴油" },
  74. new SelectOption { Value = "-10#柴油", Label = "-10#柴油" }
  75. };
  76. private readonly List<SelectOption> _statusOptions = new()
  77. {
  78. new SelectOption { Value = "正常", Label = "正常" },
  79. new SelectOption { Value = "维护中", Label = "维护中" },
  80. new SelectOption { Value = "故障", Label = "故障" },
  81. new SelectOption { Value = "停用", Label = "停用" }
  82. };
  83. private readonly List<SelectOption> _manufacturerOptions = new()
  84. {
  85. new SelectOption { Value = "中石化设备", Label = "中石化设备" },
  86. new SelectOption { Value = "中石油设备", Label = "中石油设备" },
  87. new SelectOption { Value = "壳牌设备", Label = "壳牌设备" },
  88. new SelectOption { Value = "BP设备", Label = "BP设备" },
  89. new SelectOption { Value = "其他", Label = "其他" }
  90. };
  91. #endregion
  92. #region 生命周期方法
  93. protected override async Task OnInitializedAsync()
  94. {
  95. await base.OnInitializedAsync();
  96. await OnQuery();
  97. }
  98. #endregion
  99. #region 数据操作方法
  100. private async Task OnQuery()
  101. {
  102. try
  103. {
  104. _loading = true;
  105. StateHasChanged();
  106. await Task.Delay(300);
  107. _deviceList = await GenerateMockData();
  108. _totalCount = _deviceList.Count;
  109. ApplyFilter();
  110. }
  111. catch (Exception ex)
  112. {
  113. Console.WriteLine($"查询失败: {ex.Message}");
  114. }
  115. finally
  116. {
  117. _loading = false;
  118. StateHasChanged();
  119. }
  120. }
  121. private async Task<List<DeviceDto>> GenerateMockData()
  122. {
  123. var Devices = await _DeviceRepository.AsQueryable()
  124. .Select(_ => new DeviceDto {
  125. DeviceId = _.DeviceId,
  126. OilProductName = _.OilProductName,
  127. TankCapacity = _.TankCapacity,
  128. CurrentCapacity = _.RemainingCapacity,
  129. LastUpdatedTime = _.LastUpdatedTime,
  130. Status = "正常",
  131. LicencePlate = _.LicencePlate
  132. }).ToListAsync();
  133. return Devices;
  134. }
  135. private void ApplyFilter()
  136. {
  137. var query = _deviceList.AsQueryable();
  138. if (!string.IsNullOrEmpty(License))
  139. {
  140. query = query.Where(x => x.LicencePlate.Contains(License, StringComparison.OrdinalIgnoreCase));
  141. }
  142. if (!string.IsNullOrEmpty(deviceNo))
  143. {
  144. query = query.Where(x => x.DeviceId.Contains(deviceNo, StringComparison.OrdinalIgnoreCase));
  145. }
  146. _totalCount = query.Count();
  147. _deviceList = query
  148. .Skip((_currentPage - 1) * _pageSize)
  149. .Take(_pageSize)
  150. .ToList();
  151. }
  152. private int GetCapacityPercent(DeviceDto device)
  153. {
  154. if (device.TankCapacity <= 0) return 0;
  155. return (int)(device.CurrentCapacity / device.TankCapacity * 100);
  156. }
  157. private string GetProgressColor(int percent)
  158. {
  159. if (percent >= 80) return "#52c41a";
  160. if (percent >= 50) return "#faad14";
  161. return "#ff4d4f";
  162. }
  163. #endregion
  164. #region 格式化方法
  165. private string FormatNumber(decimal? value)
  166. {
  167. if (value == null) return "-";
  168. return value.Value.ToString("N2", CultureInfo.CreateSpecificCulture("zh-CN")) + " L";
  169. }
  170. private string FormatDateTime(DateTime? value)
  171. {
  172. if (value == null) return "-";
  173. return value.Value.ToString("yyyy-MM-dd HH:mm:ss");
  174. }
  175. private int GetTotalPages()
  176. {
  177. if (_pageSize <= 0) return 0;
  178. return (int)Math.Ceiling(_totalCount / (double)_pageSize);
  179. }
  180. #endregion
  181. #region 事件处理方法
  182. private void OnNewDevice()
  183. {
  184. _deviceModel = new DeviceEditModel
  185. {
  186. Id = 0,
  187. DeviceId = $"DEV{new Random().Next(1000, 9999)}",
  188. SiteBUID = $"SITE{new Random().Next(100, 200)}",
  189. SiteName = "绿城加油站",
  190. OilProductName = "92#汽油",
  191. TankCapacity = 10000,
  192. CurrentCapacity = 7500,
  193. Status = "正常",
  194. Location = "A区1号位",
  195. Manufacturer = "中石化设备",
  196. InstallationDate = DateTime.Today
  197. };
  198. _showNewDeviceDialog = true;
  199. }
  200. private void OnEditDevice(DeviceDto device)
  201. {
  202. _deviceModel = new DeviceEditModel
  203. {
  204. Id = device.Id,
  205. DeviceId = device.DeviceId,
  206. SiteBUID = device.SiteBUID,
  207. SiteName = device.SiteName,
  208. OilProductName = device.OilProductName,
  209. TankCapacity = device.TankCapacity,
  210. CurrentCapacity = device.CurrentCapacity,
  211. Status = device.Status,
  212. Location = device.Location,
  213. Manufacturer = device.Manufacturer,
  214. InstallationDate = device.InstallationDate
  215. };
  216. _showNewDeviceDialog = true;
  217. }
  218. private async Task OnDeleteDevice(DeviceDto device)
  219. {
  220. // 这里可以添加确认对话框逻辑
  221. _deviceList = _deviceList.Where(x => x.Id != device.Id).ToList();
  222. _totalCount--;
  223. await OnQuery();
  224. }
  225. private async Task OnSaveDevice()
  226. {
  227. try
  228. {
  229. if (string.IsNullOrEmpty(_deviceModel.DeviceId))
  230. {
  231. return;
  232. }
  233. if (string.IsNullOrEmpty(_deviceModel.SiteBUID))
  234. {
  235. return;
  236. }
  237. if (string.IsNullOrEmpty(_deviceModel.SiteName))
  238. {
  239. return;
  240. }
  241. if (string.IsNullOrEmpty(_deviceModel.OilProductName))
  242. {
  243. return;
  244. }
  245. if (_deviceModel.TankCapacity <= 0)
  246. {
  247. return;
  248. }
  249. _loading = true;
  250. StateHasChanged();
  251. await Task.Delay(500);
  252. if (_deviceModel.Id == 0)
  253. {
  254. var newId = _deviceList.Max(d => d.Id) + 1;
  255. var newDevice = new DeviceDto
  256. {
  257. Id = newId,
  258. DeviceId = _deviceModel.DeviceId,
  259. SiteBUID = _deviceModel.SiteBUID,
  260. SiteName = _deviceModel.SiteName,
  261. OilProductName = _deviceModel.OilProductName,
  262. TankCapacity = _deviceModel.TankCapacity,
  263. CurrentCapacity = _deviceModel.CurrentCapacity,
  264. LastUpdatedTime = DateTime.Now,
  265. Status = _deviceModel.Status,
  266. Location = _deviceModel.Location,
  267. Manufacturer = _deviceModel.Manufacturer,
  268. InstallationDate = _deviceModel.InstallationDate,
  269. CreateTime = DateTime.Now,
  270. UpdateTime = DateTime.Now
  271. };
  272. _deviceList.Add(newDevice);
  273. }
  274. else
  275. {
  276. var existingDevice = _deviceList.FirstOrDefault(d => d.Id == _deviceModel.Id);
  277. if (existingDevice != null)
  278. {
  279. existingDevice.DeviceId = _deviceModel.DeviceId;
  280. existingDevice.SiteBUID = _deviceModel.SiteBUID;
  281. existingDevice.SiteName = _deviceModel.SiteName;
  282. existingDevice.OilProductName = _deviceModel.OilProductName;
  283. existingDevice.TankCapacity = _deviceModel.TankCapacity;
  284. existingDevice.CurrentCapacity = _deviceModel.CurrentCapacity;
  285. existingDevice.Status = _deviceModel.Status;
  286. existingDevice.Location = _deviceModel.Location;
  287. existingDevice.Manufacturer = _deviceModel.Manufacturer;
  288. existingDevice.InstallationDate = _deviceModel.InstallationDate;
  289. existingDevice.UpdateTime = DateTime.Now;
  290. existingDevice.LastUpdatedTime = DateTime.Now;
  291. }
  292. }
  293. _showNewDeviceDialog = false;
  294. await OnQuery();
  295. }
  296. catch (Exception ex)
  297. {
  298. Console.WriteLine($"保存失败: {ex.Message}");
  299. }
  300. finally
  301. {
  302. _loading = false;
  303. StateHasChanged();
  304. }
  305. }
  306. private async Task OnPageChange(int page)
  307. {
  308. if (page < 1 || page > GetTotalPages()) return;
  309. _currentPage = page;
  310. await OnQuery();
  311. }
  312. private void OnPageInputChange(ChangeEventArgs e)
  313. {
  314. if (int.TryParse(e.Value?.ToString(), out int page))
  315. {
  316. _currentPage = Math.Max(1, Math.Min(page, GetTotalPages()));
  317. }
  318. }
  319. private async Task OnPageSizeChange(ChangeEventArgs e)
  320. {
  321. if (int.TryParse(e.Value?.ToString(), out int pageSize))
  322. {
  323. _pageSize = pageSize;
  324. _currentPage = 1;
  325. await OnQuery();
  326. }
  327. }
  328. private void OnOilProductChange(ChangeEventArgs e)
  329. {
  330. _deviceModel.OilProductName = e.Value?.ToString() ?? "";
  331. }
  332. private void OnStatusChange(ChangeEventArgs e)
  333. {
  334. _deviceModel.Status = e.Value?.ToString() ?? "";
  335. }
  336. private void OnManufacturerChange(ChangeEventArgs e)
  337. {
  338. _deviceModel.Manufacturer = e.Value?.ToString() ?? "";
  339. }
  340. #endregion
  341. }
  342. }