| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- @page "/setting/nozzlesetting"
- @using EasyTemplate.Service
- @using EasyTemplate.Tool.Entity.App
- @inject NozzleService nozzleService
- @inject IJSRuntime JSRuntime
- @inject NavigationManager navigationManager
- <h3>油枪管理</h3>
- <div class="row mb-3">
- <div class="col-md-6">
- <button class="btn btn-primary" @onclick="ShowCreateModal">添加油枪</button>
- <a class="btn btn-secondary ms-2" href="/setting/boardsetting">返回主板列表</a>
- </div>
- <div class="col-md-6">
- <div class="input-group">
- <span class="input-group-text">筛选主板ID</span>
- <select class="form-select" @bind="filterBoardId">
- <option value="">全部</option>
- @if (boards != null)
- {
- @foreach (var board in boards)
- {
- <option value="@board.BoardId">@board.BoardId</option>
- }
- }
- </select>
- <button class="btn btn-outline-secondary" @onclick="ApplyFilter">筛选</button>
- <button class="btn btn-outline-danger" @onclick="ClearFilter">清除</button>
- </div>
- </div>
- </div>
- <table class="table table-striped">
- <thead>
- <tr>
- <th>ID</th>
- <th>所属油机ID</th>
- <th>所属主板ID</th>
- <th>节点</th>
- <th>产品</th>
- <th>禁用状态</th>
- <th>MN码</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody>
- @if (filteredNozzles != null)
- {
- @foreach (var nozzle in filteredNozzles)
- {
- <tr>
- <td>@nozzle.NozzleId</td>
- <td>@GetEngineIdByBoardId(nozzle.BoardId)</td>
- <td>@nozzle.BoardId</td>
- <td>@nozzle.node</td>
- <td>@nozzle.product</td>
- <td>@(nozzle.disable ? "是" : "否")</td>
- <td>@nozzle.mncode</td>
- <td>
- <button class="btn btn-sm btn-info me-1" @onclick="() => ShowEditModal(nozzle)">编辑</button>
- <button class="btn btn-sm btn-danger" @onclick="() => DeleteNozzle(nozzle.NozzleId)">删除</button>
- </td>
- </tr>
- }
- }
- </tbody>
- </table>
- <!-- 创建/编辑模态框 -->
- <div class="modal @(showModal ? "show" : "")" tabindex="-1" style="display: @(showModal ? "block" : "none"); background-color: rgba(0,0,0,0.5);">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <h5 class="modal-title">@modalTitle</h5>
- <button type="button" class="btn-close" @onclick="CloseModal"></button>
- </div>
- <div class="modal-body">
- <div class="mb-3">
- <label class="form-label">油枪号</label>
- <input type="number" class="form-control" @bind="currentNozzle.NozzleId" />
- </div>
- <div class="mb-3">
- <label class="form-label">所属主板ID</label>
- <select class="form-select" @bind="currentNozzle.BoardId">
- <option value="">请选择主板</option>
- @if (boards != null)
- {
- @foreach (var board in boards)
- {
- <option value="@board.BoardId">@board.BoardId (油机:@board.EngineId)</option>
- }
- }
- </select>
- </div>
- <div class="mb-3">
- <label class="form-label">节点</label>
- <input type="number" class="form-control" @bind="currentNozzle.node" />
- </div>
- <div class="mb-3">
- <label class="form-label">产品</label>
- <input type="number" class="form-control" @bind="currentNozzle.product" />
- </div>
- <div class="mb-3 form-check">
- <input type="checkbox" class="form-check-input" @bind="currentNozzle.disable" />
- <label class="form-check-label">禁用</label>
- </div>
- <div class="mb-3">
- <label class="form-label">MN码</label>
- <input class="form-control" @bind="currentNozzle.mncode" />
- </div>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-secondary" @onclick="CloseModal">取消</button>
- <button type="button" class="btn btn-primary" @onclick="SaveNozzle">保存</button>
- </div>
- </div>
- </div>
- </div>
- @code {
- private List<TNozzle> allNozzles = new();
- private List<TNozzle> filteredNozzles = new();
- private List<TBoard> boards = new();
- private List<TEngine> engines = new();
- private Dictionary<int, int> boardToEngineMap = new(); // 主板ID到油机ID的映射
- private TNozzle currentNozzle = new();
- private string modalTitle = "";
- private bool isEditMode = false;
- private bool showModal = false;
- private int? filterBoardId;
- protected override async Task OnInitializedAsync()
- {
- await LoadData();
- // 检查URL参数
- var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
- if (Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query).TryGetValue("boardId", out var boardId))
- {
- if (int.TryParse(boardId, out int id))
- {
- filterBoardId = id;
- ApplyFilter();
- }
- }
- }
- private async Task LoadData()
- {
- engines = await nozzleService.GetEnginesAsync();
- boards = await nozzleService.GetBoardsAsync();
- allNozzles = await nozzleService.GetNozzlesAsync();
- // 构建主板到油机的映射关系
- BuildBoardToEngineMap();
- ApplyFilter();
- }
- private void BuildBoardToEngineMap()
- {
- boardToEngineMap.Clear();
- foreach (var board in boards)
- {
- boardToEngineMap[board.BoardId] = board.EngineId;
- }
- }
- private int GetEngineIdByBoardId(int boardId)
- {
- return boardToEngineMap.TryGetValue(boardId, out int engineId) ? engineId : 0;
- }
- private void ApplyFilter()
- {
- if (filterBoardId.HasValue)
- {
- filteredNozzles = allNozzles.Where(n => n.BoardId == filterBoardId.Value).ToList();
- }
- else
- {
- filteredNozzles = new List<TNozzle>(allNozzles);
- }
- StateHasChanged();
- }
- private void ClearFilter()
- {
- filterBoardId = null;
- ApplyFilter();
- }
- private void ShowCreateModal()
- {
- currentNozzle = new TNozzle();
- modalTitle = "添加油枪";
- isEditMode = false;
- showModal = true;
- StateHasChanged();
- }
- private void ShowEditModal(TNozzle nozzle)
- {
- currentNozzle = new TNozzle
- {
- NozzleId = nozzle.NozzleId,
- BoardId = nozzle.BoardId,
- node = nozzle.node,
- product = nozzle.product,
- disable = nozzle.disable,
- mncode = nozzle.mncode
- };
- modalTitle = "编辑油枪";
- isEditMode = true;
- showModal = true;
- StateHasChanged();
- }
- private void CloseModal()
- {
- showModal = false;
- StateHasChanged();
- }
- private async Task SaveNozzle()
- {
- if (currentNozzle.BoardId <= 0)
- {
- await JSRuntime.InvokeVoidAsync("alert", "请选择有效的主板ID");
- return;
- }
- try
- {
- if (isEditMode)
- {
- await nozzleService.UpdateNozzleAsync(currentNozzle);
- }
- else
- {
- await nozzleService.CreateNozzleAsync(currentNozzle);
- }
- CloseModal();
- await LoadData();
- }
- catch (Exception ex)
- {
- await JSRuntime.InvokeVoidAsync("alert", $"操作失败: {ex.Message}");
- }
- }
- private async Task DeleteNozzle(int id)
- {
- var confirmed = await JSRuntime.InvokeAsync<bool>("confirm", "确定要删除这个油枪吗?");
- if (confirmed)
- {
- try
- {
- await nozzleService.DeleteNozzleAsync(id);
- await LoadData();
- }
- catch (Exception ex)
- {
- await JSRuntime.InvokeVoidAsync("alert", $"删除失败: {ex.Message}");
- }
- }
- }
- }
|