using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq.Expressions; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Sln.Wcs.Model.Domain; using Sln.Wcs.Repository.service; using Sln.Wcs.UI.ViewModels.Base; using Sln.Wcs.UI.Views.Base; namespace Sln.Wcs.UI.ViewModels.Task; public partial class TaskQueueViewModel : CrudPageViewModel { private readonly ILiveTaskDetailService _detailService; private LiveTaskQueue? _currentTask; [ObservableProperty] private bool _isPanelOpen; [ObservableProperty] private ObservableCollection _detailItems = new(); [ObservableProperty] private string _slidePanelTitle = string.Empty; public TaskQueueViewModel(ILiveTaskQueueService service, ILiveTaskDetailService detailService) : base(service) { _detailService = detailService; PageTitle = "任务队列管理"; } public override List FieldConfigs => new() { new() { PropertyName = "taskCode", DisplayName = "任务编号" }, new() { PropertyName = "materialCode", DisplayName = "物料编号" }, new() { PropertyName = "palletBarcode", DisplayName = "托盘条码" }, new() { PropertyName = "materialBarcode", DisplayName = "物料条码" }, new() { PropertyName = "materialCount", DisplayName = "物料数量", FieldType = FieldType.Number }, new() { PropertyName = "taskType", DisplayName = "任务类型", FieldType = FieldType.Number }, new() { PropertyName = "taskCategory", DisplayName = "任务类别", FieldType = FieldType.Number }, new() { PropertyName = "startPoint", DisplayName = "起始位置" }, new() { PropertyName = "endPoint", DisplayName = "结束位置" }, new() { PropertyName = "pathCode", DisplayName = "路径编号" }, new() { PropertyName = "taskStatus", DisplayName = "任务状态", FieldType = FieldType.Number }, new() { PropertyName = "taskSteps", DisplayName = "任务步骤", FieldType = FieldType.Number }, new() { PropertyName = "isFlag", DisplayName = "启用", FieldType = FieldType.CheckBox }, new() { PropertyName = "remark", DisplayName = "备注" }, }; protected override Expression>? BuildSearchExpression(string search) => x => (x.taskCode != null && x.taskCode.Contains(search)) || (x.materialCode != null && x.materialCode.Contains(search)); public override Avalonia.Controls.Control CreateView() => new Sln.Wcs.UI.Views.Task.TaskQueueListView(); public void LoadDetailsData(LiveTaskQueue task) { _currentTask = task; LoadDetails(); SlidePanelTitle = $"任务明细 - {task.taskCode}"; } [RelayCommand] public void ClosePanel() { IsPanelOpen = false; } [RelayCommand] private async System.Threading.Tasks.Task AddDetail() { if (_currentTask is null) return; var entity = new LiveTaskDetail { taskCode = _currentTask.taskCode }; var editor = new EntityEditWindow(); var result = await editor.ShowDialog(entity, DetailFieldConfigs, false, GetMainWindow()); if (result) { _detailService.Insert(entity); LoadDetails(); } } public async System.Threading.Tasks.Task EditDetailAsync(LiveTaskDetail detail) { var editor = new EntityEditWindow(); var result = await editor.ShowDialog(detail, DetailFieldConfigs, true, GetMainWindow()); if (result) { _detailService.Update(detail); LoadDetails(); } } public void DeleteDetail(LiveTaskDetail detail) { _detailService.DeleteById(detail.objId); LoadDetails(); } private void LoadDetails() { if (_currentTask is null) return; var list = _detailService.Query(x => x.taskCode == _currentTask.taskCode); DetailItems = new ObservableCollection(list); } public List DetailFieldConfigs => new() { new() { PropertyName = "taskCode", DisplayName = "任务编号", IsRequired = true, IsReadOnly = true }, new() { PropertyName = "pathCode", DisplayName = "路径编号" }, new() { PropertyName = "materialCode", DisplayName = "物料编号" }, new() { PropertyName = "startPoint", DisplayName = "起始位置" }, new() { PropertyName = "endPoint", DisplayName = "结束位置" }, new() { PropertyName = "deviceType", DisplayName = "设备类型", FieldType = FieldType.Number }, new() { PropertyName = "isValidate", DisplayName = "校验物料", FieldType = FieldType.CheckBox }, new() { PropertyName = "taskStatus", DisplayName = "任务状态", FieldType = FieldType.Number }, new() { PropertyName = "isFlag", DisplayName = "启用", FieldType = FieldType.CheckBox }, new() { PropertyName = "remark", DisplayName = "备注" }, }; private Avalonia.Controls.Window GetMainWindow() { return (Avalonia.Controls.Window)Avalonia.Application.Current! .ApplicationLifetime!.GetType() .GetProperty("MainWindow")! .GetValue(Avalonia.Application.Current.ApplicationLifetime)!; } }