Nozzles.razor 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. @page "/setting/nozzlesetting"
  2. @using EasyTemplate.Service
  3. @using EasyTemplate.Tool.Entity.App
  4. @inject NozzleService nozzleService
  5. @inject IJSRuntime JSRuntime
  6. @inject NavigationManager navigationManager
  7. <h3>油枪管理</h3>
  8. <div class="row mb-3">
  9. <div class="col-md-6">
  10. <button class="btn btn-primary" @onclick="ShowCreateModal">添加油枪</button>
  11. <a class="btn btn-secondary ms-2" href="/setting/boardsetting">返回主板列表</a>
  12. </div>
  13. <div class="col-md-6">
  14. <div class="input-group">
  15. <span class="input-group-text">筛选主板ID</span>
  16. <select class="form-select" @bind="filterBoardId">
  17. <option value="">全部</option>
  18. @if (boards != null)
  19. {
  20. @foreach (var board in boards)
  21. {
  22. <option value="@board.BoardId">@board.BoardId</option>
  23. }
  24. }
  25. </select>
  26. <button class="btn btn-outline-secondary" @onclick="ApplyFilter">筛选</button>
  27. <button class="btn btn-outline-danger" @onclick="ClearFilter">清除</button>
  28. </div>
  29. </div>
  30. </div>
  31. <table class="table table-striped">
  32. <thead>
  33. <tr>
  34. <th>ID</th>
  35. <th>所属油机ID</th>
  36. <th>所属主板ID</th>
  37. <th>节点</th>
  38. <th>产品</th>
  39. <th>禁用状态</th>
  40. <th>MN码</th>
  41. <th>操作</th>
  42. </tr>
  43. </thead>
  44. <tbody>
  45. @if (filteredNozzles != null)
  46. {
  47. @foreach (var nozzle in filteredNozzles)
  48. {
  49. <tr>
  50. <td>@nozzle.NozzleId</td>
  51. <td>@GetEngineIdByBoardId(nozzle.BoardId)</td>
  52. <td>@nozzle.BoardId</td>
  53. <td>@nozzle.node</td>
  54. <td>@nozzle.product</td>
  55. <td>@(nozzle.disable ? "是" : "否")</td>
  56. <td>@nozzle.mncode</td>
  57. <td>
  58. <button class="btn btn-sm btn-info me-1" @onclick="() => ShowEditModal(nozzle)">编辑</button>
  59. <button class="btn btn-sm btn-danger" @onclick="() => DeleteNozzle(nozzle.NozzleId)">删除</button>
  60. </td>
  61. </tr>
  62. }
  63. }
  64. </tbody>
  65. </table>
  66. <!-- 创建/编辑模态框 -->
  67. <div class="modal @(showModal ? "show" : "")" tabindex="-1" style="display: @(showModal ? "block" : "none"); background-color: rgba(0,0,0,0.5);">
  68. <div class="modal-dialog">
  69. <div class="modal-content">
  70. <div class="modal-header">
  71. <h5 class="modal-title">@modalTitle</h5>
  72. <button type="button" class="btn-close" @onclick="CloseModal"></button>
  73. </div>
  74. <div class="modal-body">
  75. <div class="mb-3">
  76. <label class="form-label">油枪号</label>
  77. <input type="number" class="form-control" @bind="currentNozzle.NozzleId" />
  78. </div>
  79. <div class="mb-3">
  80. <label class="form-label">所属主板ID</label>
  81. <select class="form-select" @bind="currentNozzle.BoardId">
  82. <option value="">请选择主板</option>
  83. @if (boards != null)
  84. {
  85. @foreach (var board in boards)
  86. {
  87. <option value="@board.BoardId">@board.BoardId (油机:@board.EngineId)</option>
  88. }
  89. }
  90. </select>
  91. </div>
  92. <div class="mb-3">
  93. <label class="form-label">节点</label>
  94. <input type="number" class="form-control" @bind="currentNozzle.node" />
  95. </div>
  96. <div class="mb-3">
  97. <label class="form-label">产品</label>
  98. <input type="number" class="form-control" @bind="currentNozzle.product" />
  99. </div>
  100. <div class="mb-3 form-check">
  101. <input type="checkbox" class="form-check-input" @bind="currentNozzle.disable" />
  102. <label class="form-check-label">禁用</label>
  103. </div>
  104. <div class="mb-3">
  105. <label class="form-label">MN码</label>
  106. <input class="form-control" @bind="currentNozzle.mncode" />
  107. </div>
  108. </div>
  109. <div class="modal-footer">
  110. <button type="button" class="btn btn-secondary" @onclick="CloseModal">取消</button>
  111. <button type="button" class="btn btn-primary" @onclick="SaveNozzle">保存</button>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. @code {
  117. private List<TNozzle> allNozzles = new();
  118. private List<TNozzle> filteredNozzles = new();
  119. private List<TBoard> boards = new();
  120. private List<TEngine> engines = new();
  121. private Dictionary<int, int> boardToEngineMap = new(); // 主板ID到油机ID的映射
  122. private TNozzle currentNozzle = new();
  123. private string modalTitle = "";
  124. private bool isEditMode = false;
  125. private bool showModal = false;
  126. private int? filterBoardId;
  127. protected override async Task OnInitializedAsync()
  128. {
  129. await LoadData();
  130. // 检查URL参数
  131. var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
  132. if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("boardId", out var boardId))
  133. {
  134. if (int.TryParse(boardId, out int id))
  135. {
  136. filterBoardId = id;
  137. ApplyFilter();
  138. }
  139. }
  140. }
  141. private async Task LoadData()
  142. {
  143. engines = await nozzleService.GetEnginesAsync();
  144. boards = await nozzleService.GetBoardsAsync();
  145. allNozzles = await nozzleService.GetNozzlesAsync();
  146. // 构建主板到油机的映射关系
  147. BuildBoardToEngineMap();
  148. ApplyFilter();
  149. }
  150. private void BuildBoardToEngineMap()
  151. {
  152. boardToEngineMap.Clear();
  153. foreach (var board in boards)
  154. {
  155. boardToEngineMap[board.BoardId] = board.EngineId;
  156. }
  157. }
  158. private int GetEngineIdByBoardId(int boardId)
  159. {
  160. return boardToEngineMap.TryGetValue(boardId, out int engineId) ? engineId : 0;
  161. }
  162. private void ApplyFilter()
  163. {
  164. if (filterBoardId.HasValue)
  165. {
  166. filteredNozzles = allNozzles.Where(n => n.BoardId == filterBoardId.Value).ToList();
  167. }
  168. else
  169. {
  170. filteredNozzles = new List<TNozzle>(allNozzles);
  171. }
  172. StateHasChanged();
  173. }
  174. private void ClearFilter()
  175. {
  176. filterBoardId = null;
  177. ApplyFilter();
  178. }
  179. private void ShowCreateModal()
  180. {
  181. currentNozzle = new TNozzle();
  182. modalTitle = "添加油枪";
  183. isEditMode = false;
  184. showModal = true;
  185. StateHasChanged();
  186. }
  187. private void ShowEditModal(TNozzle nozzle)
  188. {
  189. currentNozzle = new TNozzle
  190. {
  191. NozzleId = nozzle.NozzleId,
  192. BoardId = nozzle.BoardId,
  193. node = nozzle.node,
  194. product = nozzle.product,
  195. disable = nozzle.disable,
  196. mncode = nozzle.mncode
  197. };
  198. modalTitle = "编辑油枪";
  199. isEditMode = true;
  200. showModal = true;
  201. StateHasChanged();
  202. }
  203. private void CloseModal()
  204. {
  205. showModal = false;
  206. StateHasChanged();
  207. }
  208. private async Task SaveNozzle()
  209. {
  210. if (currentNozzle.BoardId <= 0)
  211. {
  212. await JSRuntime.InvokeVoidAsync("alert", "请选择有效的主板ID");
  213. return;
  214. }
  215. try
  216. {
  217. if (isEditMode)
  218. {
  219. await nozzleService.UpdateNozzleAsync(currentNozzle);
  220. }
  221. else
  222. {
  223. await nozzleService.CreateNozzleAsync(currentNozzle);
  224. }
  225. CloseModal();
  226. await LoadData();
  227. }
  228. catch (Exception ex)
  229. {
  230. await JSRuntime.InvokeVoidAsync("alert", $"操作失败: {ex.Message}");
  231. }
  232. }
  233. private async Task DeleteNozzle(int id)
  234. {
  235. var confirmed = await JSRuntime.InvokeAsync<bool>("confirm", "确定要删除这个油枪吗?");
  236. if (confirmed)
  237. {
  238. try
  239. {
  240. await nozzleService.DeleteNozzleAsync(id);
  241. await LoadData();
  242. }
  243. catch (Exception ex)
  244. {
  245. await JSRuntime.InvokeVoidAsync("alert", $"删除失败: {ex.Message}");
  246. }
  247. }
  248. }
  249. }