using System.Collections.ObjectModel; using Avalonia.Controls; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Sln.Wcs.Business; using Sln.Wcs.Repository.service; namespace Sln.Wcs.UI.ViewModels.Task; public partial class CreateTaskViewModel : ObservableObject { private readonly TaskCreateService _taskCreateService; private readonly ILiveTaskQueueService _taskQueueService; public string PageTitle => "手动创建任务"; // ---- 起点 ---- [ObservableProperty] private int _startBuilding = 13; [ObservableProperty] private int _startFloor = 1; [ObservableProperty] private string _startLocation = "01"; // ---- 终点 ---- [ObservableProperty] private int _endBuilding = 15; [ObservableProperty] private int _endFloor = 5; [ObservableProperty] private string _endLocation = "01"; // ---- 任务属性 ---- [ObservableProperty] private int _taskType = 1; [ObservableProperty] private int _taskCategory = 1; [ObservableProperty] private string _palletBarcode = string.Empty; [ObservableProperty] private string _materialCode = string.Empty; // ---- 下拉选项 ---- public ObservableCollection TaskTypeOptions { get; } = new() { new() { Value = 1, Display = "1 - 入库" }, new() { Value = 2, Display = "2 - 出库" }, }; public ObservableCollection TaskCategoryOptions { get; } = new() { new() { Value = 1, Display = "1 - 包材" }, new() { Value = 2, Display = "2 - 成品" }, new() { Value = 3, Display = "3 - 托盘" }, }; [ObservableProperty] private TaskAttributeOption _selectedTaskType; [ObservableProperty] private TaskAttributeOption _selectedTaskCategory; partial void OnSelectedTaskTypeChanged(TaskAttributeOption? value) { if (value is not null) TaskType = value.Value; } partial void OnSelectedTaskCategoryChanged(TaskAttributeOption? value) { if (value is not null) TaskCategory = value.Value; } // ---- 手动操作选项 ---- [ObservableProperty] private bool _manualPutIn; [ObservableProperty] private bool _manualTakeOut; // ---- 状态 ---- [ObservableProperty] private string _statusText = string.Empty; [ObservableProperty] private bool _isBusy; public CreateTaskViewModel(TaskCreateService taskCreateService, ILiveTaskQueueService taskQueueService) { _taskCreateService = taskCreateService; _taskQueueService = taskQueueService; _selectedTaskType = TaskTypeOptions[0]; _selectedTaskCategory = TaskCategoryOptions[0]; } public Avalonia.Controls.Control CreateView() => new Views.Task.CreateTaskView(); [RelayCommand] private async System.Threading.Tasks.Task CreateAsync() { if (string.IsNullOrWhiteSpace(PalletBarcode) || string.IsNullOrWhiteSpace(MaterialCode)) { StatusText = "托盘条码和物料编码不能为空"; return; } IsBusy = true; StatusText = "正在生成任务..."; try { var taskQueue = await System.Threading.Tasks.Task.Run(() => _taskCreateService.CreateTask( StartBuilding, StartFloor, StartLocation, EndBuilding, EndFloor, EndLocation, TaskType, TaskCategory, PalletBarcode, MaterialCode, ManualPutIn, ManualTakeOut)); StatusText = $"路由生成完成,共 {taskQueue.taskSteps} 条明细,正在保存..."; var saved = await System.Threading.Tasks.Task.Run(() => _taskQueueService.InsertTaskQueue(taskQueue)); StatusText = saved ? $"✓ 任务创建成功: {taskQueue.taskCode},共 {taskQueue.taskSteps} 条明细" : "✗ 保存失败"; } catch (Exception ex) { StatusText = $"✗ 创建失败: {ex.Message}"; } finally { IsBusy = false; } } } public class TaskAttributeOption { public int Value { get; set; } public string Display { get; set; } = string.Empty; }