InboundProductionViewModel.cs 2.8 KB
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using IndustrialControl.Services;
using System.Threading.Tasks;

namespace IndustrialControl.ViewModels
{
    public partial class InboundProductionViewModel : ObservableObject
    {
        private readonly IWarehouseDataService _warehouseSvc;

        public InboundProductionViewModel(IWarehouseDataService warehouseSvc)
        {
            _warehouseSvc = warehouseSvc;

            ConfirmCommand = new AsyncRelayCommand(ConfirmInboundAsync);

            // 测试数据
            Lines = new ObservableCollection<ProductionScanItem>
            {
                new ProductionScanItem { IsSelected = false, Barcode="FSC2025060300001", Bin="CK1_A201", Qty=1 },
                new ProductionScanItem { IsSelected = false, Barcode="FSC2025060300002", Bin="CK1_A201", Qty=1 }
            };

            AvailableBins = new ObservableCollection<string>
            {
                "CK1_A201", "CK1_A202", "CK1_A203"
            };
        }

        // 基础信息
        [ObservableProperty] private string orderNo;
        [ObservableProperty] private string workOrderNo;
        [ObservableProperty] private string productName;
        [ObservableProperty] private int pendingQty;

        public ObservableCollection<ProductionScanItem> Lines { get; set; }
        public ObservableCollection<string> AvailableBins { get; set; }

        public IAsyncRelayCommand ConfirmCommand { get; }

        public void ClearScan()
        {
            Lines.Clear();
        }

        public void ClearAll()
        {
            OrderNo = string.Empty;
            WorkOrderNo = string.Empty;
            ProductName = string.Empty;
            PendingQty = 0;
            Lines.Clear();
        }

        public void PassSelectedScan()
        {
            foreach (var item in Lines)
            {
                if (item.IsSelected)
                {
                    // 扫描通过处理
                }
            }
        }

        public void CancelSelectedScan()
        {
            for (int i = Lines.Count - 1; i >= 0; i--)
            {
                if (Lines[i].IsSelected)
                    Lines.RemoveAt(i);
            }
        }

        public async Task<bool> ConfirmInboundAsync()
        {
            if (string.IsNullOrWhiteSpace(OrderNo))
                return false;

            var result = await _warehouseSvc.ConfirmInboundProductionAsync(OrderNo,
                Lines.Select(x => new ScanItem(0, x.Barcode, x.Bin, x.Qty)));

            return result.Succeeded;
        }
    }

    public class ProductionScanItem
    {
        public bool IsSelected { get; set; }
        public string Barcode { get; set; }
        public string Bin { get; set; }
        public int Qty { get; set; }
    }
}