diff --git a/RIZO_Application.Models/Model/FinishedProductDto.cs b/RIZO_Application.Models/Model/FinishedProductDto.cs index 54e7155..e1d0eec 100644 --- a/RIZO_Application.Models/Model/FinishedProductDto.cs +++ b/RIZO_Application.Models/Model/FinishedProductDto.cs @@ -193,7 +193,7 @@ namespace RIZO_Application.Models.Model public string Remark { get; set; } = string.Empty; } - public class FinishedProductQueryDto + public class FinishedProductQueryDto: PagerInfo { public DateTime? StartTime { get; set; } public DateTime? EndTime { get; set; } diff --git a/RIZO_Application.Repository/BaseRepository.cs b/RIZO_Application.Repository/BaseRepository.cs index ad4c098..c03f9b7 100644 --- a/RIZO_Application.Repository/BaseRepository.cs +++ b/RIZO_Application.Repository/BaseRepository.cs @@ -1,4 +1,5 @@ -using SqlSugar; +using RIZO_Application.Models.Model; +using SqlSugar; using SqlSugar.IOC; using System; using System.Collections.Generic; @@ -58,4 +59,30 @@ namespace RIZO_Application.Repository return await Context.Deleteable().In(id).ExecuteCommandAsync() > 0; } } + + /// + /// 分页查询扩展 + /// + public static class QueryableExtension + { + /// + /// 读取列表 + /// + /// + /// 查询表单式 + /// 分页参数 + /// + public static PagedInfo ToPage(this ISugarQueryable source, PagerInfo parm) + { + var page = new PagedInfo(); + var total = 0; + page.PageSize = parm.PageSize; + page.PageIndex = parm.PageNum; + page.Result = source + //.OrderByIF(parm.Sort.IsNotEmpty(), $"{parm.Sort.ToSqlFilter()} {(!string.IsNullOrWhiteSpace(parm.SortType) && parm.SortType.Contains("desc") ? "desc" : "asc")}") + .ToPageList(parm.PageNum, parm.PageSize, ref total); + page.TotalNum = total; + return page; + } + } } diff --git a/RIZO_Application.Repository/RIZO_Application.Repository.csproj b/RIZO_Application.Repository/RIZO_Application.Repository.csproj index e08b68f..3084704 100644 --- a/RIZO_Application.Repository/RIZO_Application.Repository.csproj +++ b/RIZO_Application.Repository/RIZO_Application.Repository.csproj @@ -10,6 +10,7 @@ + diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/FinishedProductService.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/FinishedProductService.cs index 15bd993..e61fcf8 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/FinishedProductService.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/FinishedProductService.cs @@ -1,95 +1,78 @@ using System; using System.Collections.Generic; -using System.Drawing.Printing; -using System.Net.Http; -using System.Text; +using System.Linq; using System.Threading.Tasks; -using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json; using Prism.Events; using RIZO_Application.Core; -using RIZO_Application.Infrastructure.CustomAttribute; -using RIZO_Application.Infrastructure.Model; using RIZO_Application.Models; using RIZO_Application.Models.Model; using RIZO_Application.Modules.ModuleName.Services.IServices; using RIZO_Application.Repository; using SqlSugar; +using SqlSugar.IOC; +using RIZO_Application.Infrastructure.CustomAttribute; +using RIZO_Application.Infrastructure.Model; namespace RIZO_Application.Modules.ModuleName.Services { - [AppService( - ServiceType = typeof(IFinishedProductService), - Lifetime = ServiceLifetime.Singleton - )] - public class FinishedProductService - : BaseRepository, - IFinishedProductService + [AppService(ServiceType = typeof(IFinishedProductService), Lifetime = ServiceLifetime.Transient)] + public class FinishedProductService : BaseRepository, IFinishedProductService { - private readonly HttpClient _httpClient; private readonly IEventAggregator _eventAggregator; - private readonly IPrintService _printService; + private readonly SqlSugarClient _db; + private readonly bool _useMockData = false; + private readonly string _labelFrom; // 改为在构造函数中初始化 private readonly List _mockData; - private readonly bool _useMockData = false; // 控制是否使用模拟数据 - private readonly string _labelFrom = PrintConfigs.Current.LabelFrom; - - public FinishedProductService(IEventAggregator eventAggregator, IPrintService printService) + public FinishedProductService(IEventAggregator eventAggregator) { - _httpClient = new HttpClient(); - _httpClient.BaseAddress = new Uri(ServerConfigs.Current.ServerPath); // 设置默认请求地址前缀 _eventAggregator = eventAggregator; - _printService = printService; + _mockData = GenerateMockData(); + // 安全地获取标签来源配置 + _labelFrom = PrintConfigs.Current?.LabelFrom ?? "全部"; + } - // 初始化模拟数据 - _mockData = new List + private List GenerateMockData() + { + var mockData = new List(); + var random = new Random(); + + // 生成30条测试数据,分布在不同的日期 + for (int i = 1; i <= 30; i++) { - new ProFinishedProductReceipt + var daysOffset = random.Next(-30, 1); // 最近30天内的数据 + var createdTime = DateTime.Now.AddDays(daysOffset); + var receiptDate = createdTime.Date; + + var statuses = new[] { "SUBMITTED", "APPROVED", "COMPLETED" }; + var printStatuses = new[] { "全部", "未打印", "已打印" }; + var receiptTypes = new[] { "全部", "正常入库", "返工入库", "其他入库" }; + var labelFroms = new[] { "全部", "GP12", "后道", "产线" }; + + mockData.Add(new ProFinishedProductReceipt { - ReceiptNo = "RK202511140001", - WorkOrder = "W20251114001", - Description = "T1EJ左后门把手-新卡其白", - PartNumber = "220707", - PackageCount = 8, - PackageNum = 465, - BatchCode = "20240501001", - WarehouseCode = "A1-01-01", - CreatedBy = "admin", - ProductionTime = DateTime.Now.AddDays(-1), - LabelPrintStatus = "未打印", + ReceiptNo = $"RK{receiptDate:yyyyMMdd}{i:D4}", + ReceiptDate = receiptDate, + ReceiptType = receiptTypes[random.Next(receiptTypes.Length)], + WorkOrder = $"WO{receiptDate:yyyyMMdd}{random.Next(100, 999)}", + Description = $"产品{random.Next(1, 10)}入库", + PartNumber = $"PART{random.Next(100, 999):D3}", + PackageCount = random.Next(5, 50), + PackageNum = 1, + BatchCode = $"BATCH{random.Next(1, 20):D3}", + WarehouseCode = $"WH{random.Next(1, 5):D3}", + CreatedBy = new[] { "张三", "李四", "王五", "赵六" }[random.Next(4)], + CreatedTime = createdTime, + ProductionTime = receiptDate.AddHours(random.Next(8, 18)), + Status = statuses[random.Next(statuses.Length)], + LabelPrintStatus = printStatuses[random.Next(printStatuses.Length)], + LabelFrom = labelFroms[random.Next(labelFroms.Length)], Remark = "正常入库" - }, - new ProFinishedProductReceipt - { - ReceiptNo = "RK202511140002", - WorkOrder = "W20251114002", - Description = "T1EJ右前门把手-右舵-新卡其白", - PartNumber = "221353", - PackageCount = 7, - PackageNum = 458, - BatchCode = "20240502001", - WarehouseCode = "A1-02-01", - CreatedBy = "admin", - ProductionTime = DateTime.Now, - LabelPrintStatus = "未打印", - Remark = "正常入库" - }, - new ProFinishedProductReceipt - { - ReceiptNo = "RK202511140003", - WorkOrder = "W20251114003", - Description = "T1EJ右前门把手-左舵-新卡其白", - PartNumber = "220701", - PackageCount = 7, - PackageNum = 475, - BatchCode = "20240502002", - WarehouseCode = "A1-02-02", - CreatedBy = "admin", - ProductionTime = DateTime.Now, - LabelPrintStatus = "未打印", - Remark = "正常入库" - } - }; + }); + } + + return mockData; } public async Task> GetFinishedProductsAsync( @@ -143,6 +126,86 @@ namespace RIZO_Application.Modules.ModuleName.Services } } + public async Task> GetFinishedProductsPagedAsync(FinishedProductQueryDto query) + { + try + { + if (_useMockData) + { + _eventAggregator.GetEvent().Publish("使用模拟数据获取分页成品入库单"); + + // 模拟分页逻辑 + var queryable = _mockData.AsQueryable(); + + // 应用查询条件 + if (!string.IsNullOrEmpty(query.LabelFrom) && query.LabelFrom != "全部") + { + queryable = queryable.Where(x => x.LabelFrom == query.LabelFrom); + } + + if (!string.IsNullOrEmpty(query.LabelPrintStatus) && query.LabelPrintStatus != "全部") + { + queryable = queryable.Where(x => x.LabelPrintStatus == query.LabelPrintStatus); + } + + if (query.StartTime.HasValue && query.StartTime.Value > DateTime.MinValue) + { + queryable = queryable.Where(x => x.CreatedTime >= query.StartTime.Value); + } + + if (query.EndTime.HasValue && query.EndTime.Value > DateTime.MinValue) + { + var endDateInclusive = query.EndTime.Value.AddDays(1); + queryable = queryable.Where(x => x.CreatedTime <= endDateInclusive); + } + + var totalCount = queryable.Count(); + var items = queryable + .OrderByDescending(x => x.ReceiptNo) + .Skip((query.PageNum - 1) * query.PageSize) + .Take(query.PageSize) + .ToList(); + + return new PagedInfo { + PageIndex = query.PageNum, + PageSize = query.PageSize, + TotalNum = totalCount, + Result = items + }; + } + else + { + DateTime? endDateInclusive = null; + if (query.EndTime.HasValue) + { + endDateInclusive = query.EndTime.Value.AddDays(1); + } + var total = 0; + var result = Context + .Queryable() + .WhereIF(!string.IsNullOrEmpty(query.LabelFrom) && query.LabelFrom != "全部", it => it.LabelFrom == query.LabelFrom) + .WhereIF(!string.IsNullOrEmpty(query.LabelPrintStatus) && query.LabelPrintStatus != "全部", it => it.LabelPrintStatus == query.LabelPrintStatus) + .WhereIF(query.StartTime.HasValue && query.StartTime.Value > DateTime.MinValue, it => it.CreatedTime >= query.StartTime.Value) + .WhereIF(query.EndTime.HasValue && query.EndTime.Value > DateTime.MinValue, it => it.CreatedTime <= endDateInclusive) + .OrderByDescending(it => it.ReceiptNo) + .ToPage(query); + + return result; + } + } + catch (Exception ex) + { + _eventAggregator.GetEvent().Publish($"获取分页成品入库单失败: {ex.Message}"); + return new PagedInfo + { + PageIndex = query.PageNum, + PageSize = query.PageSize, + TotalNum = 0, + Result = new List() + }; + } + } + public async Task GetFinishedProductByReceiptNoAsync(string receiptNo) { try @@ -201,7 +264,7 @@ namespace RIZO_Application.Modules.ModuleName.Services } else { - if (product.ReceiptNo.IsNullOrEmpty()) + if (string.IsNullOrEmpty(product.ReceiptNo)) { _eventAggregator.GetEvent().Publish($"添加成品入库单失败: 未填写入库单号"); return false; @@ -306,13 +369,13 @@ namespace RIZO_Application.Modules.ModuleName.Services { if (_useMockData) { - var product = _mockData.Find(p => p.ReceiptNo == receiptNo); - if (product != null) + var index = _mockData.FindIndex(p => p.ReceiptNo == receiptNo); + if (index != -1) { - _mockData.Remove(product); + _mockData.RemoveAt(index); _eventAggregator .GetEvent() - .Publish($"模拟删除成品入库单成功: {product.WorkOrder}"); + .Publish($"模拟删除成品入库单成功: {receiptNo}"); return await Task.FromResult(true); } return await Task.FromResult(false); @@ -343,62 +406,34 @@ namespace RIZO_Application.Modules.ModuleName.Services { try { - var product = await GetFinishedProductByReceiptNoAsync(receiptNo); - if (product == null) + if (_useMockData) { - _eventAggregator.GetEvent().Publish("未找到要打印的成品入库单"); - return false; - } - - // 创建打印参数 - var printDto = new PrintDto - { - Path = PrintConfigs.Current.BaseLabelPath, - SiteNo = product.SiteNo, - Name = $"成品入库单_{product.WorkOrder}", - LabelFrom = product.LabelFrom, - PartNumber = product.PartNumber, - Description = product.Description, - Color = product.Color, - Specification = product.Specification, - WorkOrder = product.WorkOrder, - PackageCode = product.PackageCode, - Team = product.Team, - Sort = 1, // 默认流水号 - ProductionTime = product.ProductionTime?.ToString("yyyy-MM-dd HH:mm:ss") ?? "", - BatchCode = product.BatchCode, - PackageCout = product.PackageCount ?? 0, - PackageNum = product.PackageNum ?? 0, - LabelCode = product.LabelCode, - LabelType = 1, // 默认标签类型 - CreatedTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") - }; - - // 执行打印 - bool success = await _printService.PrintLabelAsync(printDto); - - // 如果打印成功,更新状态为已打印 - if (success) - { - product.LabelPrintStatus = "已打印"; - int recordUpdate = Context.Updateable(product).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand(); - ProFinishedProductReceiptLog finishedProductReceiptLog = new ProFinishedProductReceiptLog + var product = _mockData.Find(p => p.ReceiptNo == receiptNo); + if (product != null) { - ReceiptNo = product.ReceiptNo, - OperatedBy = product.LabelFrom, - OperatedTime = DateTime.Now, - OperationType = "PRINT", - OperationContent = recordUpdate > 0 ? "入库单打印成功" : "入库单打印失败", - OperationResult = recordUpdate > 0 ? "SUCCESS" : "FAIL", - Remark = "" - }; - Context.Insertable(finishedProductReceiptLog).ExecuteCommand(); - _eventAggregator - .GetEvent() - .Publish($"更新成品入库单状态为已打印: {product.ReceiptNo}"); + product.LabelPrintStatus = "已打印"; + _eventAggregator + .GetEvent() + .Publish($"模拟打印成品入库单成功: {receiptNo}"); + return await Task.FromResult(true); + } + return await Task.FromResult(false); + } + else + { + int result = Context + .Updateable() + .SetColumns(it => new ProFinishedProductReceipt { LabelPrintStatus = "已打印" }) + .Where(it => it.ReceiptNo == receiptNo) + .ExecuteCommand(); + if (result > 0) + { + _eventAggregator + .GetEvent() + .Publish($"打印成品入库单成功: {receiptNo}"); + } + return result > 0; } - - return success; } catch (Exception ex) { diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/IServices/IFinishedProductService.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/IServices/IFinishedProductService.cs index 661479d..dfcfcb2 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/IServices/IFinishedProductService.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Services/IServices/IFinishedProductService.cs @@ -7,6 +7,7 @@ namespace RIZO_Application.Modules.ModuleName.Services.IServices public interface IFinishedProductService { Task> GetFinishedProductsAsync(FinishedProductQueryDto query); + Task> GetFinishedProductsPagedAsync(FinishedProductQueryDto query); Task GetFinishedProductByReceiptNoAsync(string receiptNo); Task AddFinishedProductAsync(ProFinishedProductReceipt product); Task UpdateFinishedProductAsync(ProFinishedProductReceipt product); diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/DeleteConfirmDialogViewModel.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/DeleteConfirmDialogViewModel.cs index b9c04aa..932496a 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/DeleteConfirmDialogViewModel.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/DeleteConfirmDialogViewModel.cs @@ -34,13 +34,13 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels public DeleteConfirmDialogViewModel(IFinishedProductService finishedProductService) { _finishedProductService = finishedProductService; - ConfirmCommand = new DelegateCommand(ConfirmDelete); + ConfirmCommand = new DelegateCommand(async () => await ConfirmDeleteAsync()); CancelCommand = new DelegateCommand(Cancel); } - private void ConfirmDelete() + private async System.Threading.Tasks.Task ConfirmDeleteAsync() { - var success = _finishedProductService.DeleteFinishedProductAsync(Product.ReceiptNo).Result; + var success = await _finishedProductService.DeleteFinishedProductAsync(Product.ReceiptNo); if (success) { var result = new DialogResult(ButtonResult.OK, new DialogParameters { { "Product", Product } }); RequestClose?.Invoke(result); diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/MainControlViewModel.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/MainControlViewModel.cs index 15a0af4..5d9ef96 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/MainControlViewModel.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/MainControlViewModel.cs @@ -1,232 +1,408 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics; using System.Linq; +using System.Threading.Tasks; using System.Windows.Input; using Prism.Commands; using Prism.Events; +using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; +using RIZO_Application.Core; using RIZO_Application.Core.Mvvm; using RIZO_Application.Infrastructure.Model; using RIZO_Application.Models.Model; using RIZO_Application.Modules.ModuleName.Services.IServices; -using RIZO_Application.Services.Interfaces; namespace RIZO_Application.Modules.ModuleName.ViewModels { public class MainControlViewModel : RegionViewModelBase { - private readonly IPrintService _printService; private readonly IFinishedProductService _finishedProductService; private readonly IEventAggregator _eventAggregator; private readonly IDialogService _dialogService; - private readonly string _labelFrom = PrintConfigs.Current.LabelFrom; - private string _message; - public string Message + + #region 属性 + private ObservableCollection _finishedProducts; + public ObservableCollection FinishedProducts { - get { return _message; } - set { SetProperty(ref _message, value); } + get => _finishedProducts; + set => SetProperty(ref _finishedProducts, value); } - private List _finishedProducts; - public List FinishedProducts + private FinishedProductQueryDto _queryDto; + public FinishedProductQueryDto QueryDto { - get { return _finishedProducts; } - set { SetProperty(ref _finishedProducts, value); } + get => _queryDto; + set => SetProperty(ref _queryDto, value); } - private ProFinishedProductReceipt _selectedProduct; - public ProFinishedProductReceipt SelectedProduct + private DateTime _startDate; + private bool _isInitialized = false; // 添加初始化标志 + public DateTime StartDate { - get { return _selectedProduct; } - set { SetProperty(ref _selectedProduct, value); } + get => _startDate; + set + { + SetProperty(ref _startDate, value); + if (_isInitialized) // 只有在初始化完成后才触发搜索 + { + Task.Run(async () => await SearchProductsAsync()); + } + } } - // 搜索条件属性 - private DateTime? _startDate; - public DateTime? StartDate + private DateTime _endDate; + public DateTime EndDate { - get { return _startDate; } - set { SetProperty(ref _startDate, value); } - } - - private DateTime? _endDate; - public DateTime? EndDate - { - get { return _endDate; } - set { SetProperty(ref _endDate, value); } + get => _endDate; + set + { + SetProperty(ref _endDate, value); + if (_isInitialized) // 只有在初始化完成后才触发搜索 + { + Task.Run(async () => await SearchProductsAsync()); + } + } } private string _selectedCategory; public string SelectedCategory { - get { return _selectedCategory; } - set { SetProperty(ref _selectedCategory, value); } - } - - private ObservableCollection _categories; - public ObservableCollection Categories - { - get { return _categories; } - set { SetProperty(ref _categories, value); } - } - - private ObservableCollection _statusList; - public ObservableCollection StatusList - { - get { return _statusList; } - set { SetProperty(ref _statusList, value); } + get => _selectedCategory; + set + { + SetProperty(ref _selectedCategory, value); + if (_isInitialized) // 只有在初始化完成后才触发搜索 + { + Task.Run(async () => await SearchProductsAsync()); + } + } } private string _selectedStatus; public string SelectedStatus { - get { return _selectedStatus; } - set { SetProperty(ref _selectedStatus, value); } + get => _selectedStatus; + set + { + SetProperty(ref _selectedStatus, value); + if (_isInitialized) // 只有在初始化完成后才触发搜索 + { + Task.Run(async () => await SearchProductsAsync()); + } + } } - // 命令定义 - public ICommand LoadFinishedProductsCommand { get; private set; } + private string _searchText; + public string SearchText + { + get => _searchText; + set => SetProperty(ref _searchText, value); + } + + private bool _isLoading; + public bool IsLoading + { + get => _isLoading; + set => SetProperty(ref _isLoading, value); + } + + #region 下拉列表数据 + private ObservableCollection _categories; + public ObservableCollection Categories + { + get => _categories; + set => SetProperty(ref _categories, value); + } + + private ObservableCollection _statusList; + public ObservableCollection StatusList + { + get => _statusList; + set => SetProperty(ref _statusList, value); + } + #endregion + + #region 分页属性 + private int _currentPage = 1; + public int CurrentPage + { + get => _currentPage; + set + { + if (SetProperty(ref _currentPage, value)) + { + // 当页码改变时,通知相关属性更新 + RaisePropertyChanged(nameof(HasPreviousPage)); + RaisePropertyChanged(nameof(HasNextPage)); + } + } + } + + private int _pageSize = 20; + public int PageSize + { + get => _pageSize; + set => SetProperty(ref _pageSize, value); + } + + private int _totalItems; + public int TotalItems + { + get => _totalItems; + set + { + if (SetProperty(ref _totalItems, value)) + { + // 当总记录数改变时,重新计算总页数并通知相关属性更新 + TotalPages = (int)Math.Ceiling((double)_totalItems / _pageSize); + } + } + } + + private int _totalPages; + public int TotalPages + { + get => _totalPages; + set + { + if (SetProperty(ref _totalPages, value)) + { + // 当总页数改变时,通知相关属性更新 + RaisePropertyChanged(nameof(HasPreviousPage)); + RaisePropertyChanged(nameof(HasNextPage)); + } + } + } + + public bool HasPreviousPage => CurrentPage > 1; + public bool HasNextPage => CurrentPage < TotalPages; + #endregion + + #endregion + + #region 命令 + public ICommand SearchCommand { get; private set; } + public ICommand ResetCommand { get; private set; } + public ICommand RefreshCommand { get; private set; } + public ICommand PreviousPageCommand { get; private set; } + public ICommand NextPageCommand { get; private set; } + public ICommand GoToPageCommand { get; private set; } public ICommand AddProductCommand { get; private set; } public ICommand EditProductCommand { get; private set; } public ICommand DeleteProductCommand { get; private set; } public ICommand PrintProductCommand { get; private set; } - public ICommand SearchCommand { get; private set; } + #endregion public MainControlViewModel( IRegionManager regionManager, - IEventAggregator eventAggregator, - IMessageService messageService, - IPrintService printService, IFinishedProductService finishedProductService, + IEventAggregator eventAggregator, IDialogService dialogService ) : base(regionManager) { - _printService = printService; - _eventAggregator = eventAggregator; _finishedProductService = finishedProductService; + _eventAggregator = eventAggregator; _dialogService = dialogService; - Message = messageService.GetMessage(); - // 初始化搜索条件 - StatusList = new ObservableCollection { "全部", "已打印", "未打印" }; - Categories = new ObservableCollection { "全部", "GP12", "后道", "产线" }; - SelectedStatus = "全部"; // 默认选中全部 - SelectedCategory = _labelFrom; // 默认选中配置 + InitializeCommands(); + InitializeData(); + + // 标记初始化完成 + _isInitialized = true; + + // 进入系统时默认搜索(延迟执行,确保所有依赖项已初始化) + Task.Run(async () => + { + await Task.Delay(100); // 短暂延迟,确保UI完全加载 + await LoadFinishedProductsAsync(); + }); + } - // 初始化命令 - LoadFinishedProductsCommand = new DelegateCommand(async () => await LoadFinishedProductsAsync()); - AddProductCommand = new DelegateCommand(AddProduct); - EditProductCommand = new DelegateCommand(EditProduct, CanExecuteEditDeletePrint).ObservesProperty(() => SelectedProduct); - DeleteProductCommand = new DelegateCommand(async () => await DeleteProductAsync(), CanExecuteEditDeletePrint).ObservesProperty(() => SelectedProduct); - PrintProductCommand = new DelegateCommand(async () => await PrintProductAsync(), CanExecuteEditDeletePrint).ObservesProperty(() => SelectedProduct); + private void InitializeCommands() + { SearchCommand = new DelegateCommand(async () => await SearchProductsAsync()); + ResetCommand = new DelegateCommand(ResetSearchCriteria); + RefreshCommand = new DelegateCommand(async () => await LoadFinishedProductsAsync()); + PreviousPageCommand = new DelegateCommand(async () => await GoToPageAsync(CurrentPage - 1), () => HasPreviousPage); + NextPageCommand = new DelegateCommand(async () => await GoToPageAsync(CurrentPage + 1), () => HasNextPage); + GoToPageCommand = new DelegateCommand(async () => await GoToPageAsync(CurrentPage)); + AddProductCommand = new DelegateCommand(AddProduct); + EditProductCommand = new DelegateCommand(EditProduct); + DeleteProductCommand = new DelegateCommand(DeleteProduct); + PrintProductCommand = new DelegateCommand(PrintProduct); + + // 安全地设置默认值,避免PrintConfigs.Current为null时的空引用异常 + SelectedCategory = PrintConfigs.Current?.LabelFrom ?? "全部"; + SelectedStatus = "未打印"; } - public override void OnNavigatedTo(NavigationContext navigationContext) + private void InitializeData() { - // 页面加载时获取数据 - LoadFinishedProductsCommand.Execute(null); - } - - private async System.Threading.Tasks.Task LoadFinishedProductsAsync() - { - FinishedProductQueryDto query = new FinishedProductQueryDto + StartDate = DateTime.Today; + EndDate = DateTime.Today; + QueryDto = new FinishedProductQueryDto { - StartTime = _startDate, - EndTime = _endDate, - LabelFrom = SelectedCategory, - LabelPrintStatus = SelectedStatus + StartTime = StartDate, + EndTime = EndDate, + PageNum = CurrentPage, + PageSize = PageSize + }; + + // 初始化类别列表 + Categories = new ObservableCollection + { + "全部", + "GP12", + "后道", + "产线" + }; + + // 初始化状态列表 + StatusList = new ObservableCollection + { + "全部", + "未打印", + "已打印" }; - FinishedProducts = (List)await _finishedProductService.GetFinishedProductsAsync(query); } - private async System.Threading.Tasks.Task SearchProductsAsync() + private async Task LoadFinishedProductsAsync() { - FinishedProductQueryDto query = new FinishedProductQueryDto + IsLoading = true; + try { - StartTime = _startDate, - EndTime = _endDate, - LabelFrom = SelectedCategory, - LabelPrintStatus = SelectedStatus - }; - // 获取所有产品 - var allProducts = (List)await _finishedProductService.GetFinishedProductsAsync(query); + // 更新查询条件 + QueryDto.StartTime = StartDate; + QueryDto.EndTime = EndDate; + QueryDto.LabelFrom = SelectedCategory; + QueryDto.LabelPrintStatus = SelectedStatus; + QueryDto.PageNum = CurrentPage; + QueryDto.PageSize = PageSize; + + var pagedResult = await _finishedProductService.GetFinishedProductsPagedAsync(QueryDto); + + FinishedProducts = new ObservableCollection(pagedResult.Result); + TotalItems = pagedResult.TotalNum; + // 确保当前页码在有效范围内 + if (CurrentPage > TotalPages && TotalPages > 0) + { + CurrentPage = TotalPages; + await LoadFinishedProductsAsync(); // 重新加载正确页码的数据 + return; + } + + // 显式刷新命令的可执行状态 - 确保按钮状态正确更新 + ((DelegateCommand)PreviousPageCommand).RaiseCanExecuteChanged(); + ((DelegateCommand)NextPageCommand).RaiseCanExecuteChanged(); + } + catch (Exception ex) + { + _eventAggregator.GetEvent().Publish($"加载成品入库单失败: {ex.Message}"); + FinishedProducts = new ObservableCollection(); + TotalItems = 0; + // TotalPages 现在会在 TotalItems 设置为 0 时自动计算为 0 + CurrentPage = 1; + + // 异常情况下也要刷新命令状态 + ((DelegateCommand)PreviousPageCommand).RaiseCanExecuteChanged(); + ((DelegateCommand)NextPageCommand).RaiseCanExecuteChanged(); + } + finally + { + IsLoading = false; + } + } + + private async Task SearchProductsAsync() + { + CurrentPage = 1; // 搜索时重置到第一页 - // 应用搜索条件 - var filteredProducts = allProducts.AsEnumerable(); + // 更新查询条件 + QueryDto.StartTime = StartDate; + QueryDto.EndTime = EndDate; + QueryDto.LabelFrom = SelectedCategory; + QueryDto.LabelPrintStatus = SelectedStatus; + QueryDto.PageNum = CurrentPage; + QueryDto.PageSize = PageSize; - // 状态过滤 - //if (!string.IsNullOrEmpty(SelectedStatus) && SelectedStatus != "全部") - //{ - //filteredProducts = filteredProducts.Where(p => p.LabelPrintStatus == SelectedStatus); - //} - - FinishedProducts = filteredProducts.ToList(); + await LoadFinishedProductsAsync(); + } + + private void ResetSearchCriteria() + { + StartDate = DateTime.Today; + EndDate = DateTime.Today; + SelectedCategory = null; + SelectedStatus = null; + SearchText = string.Empty; + CurrentPage = 1; + } + + private async Task GoToPageAsync(int page) + { + if (page >= 1 && page <= TotalPages) + { + CurrentPage = page; + await LoadFinishedProductsAsync(); + } } private void AddProduct() { - var parameters = new DialogParameters(); - _dialogService.ShowDialog("AddProductDialog", parameters, async r => + _dialogService.ShowDialog("AddProductDialog", new DialogParameters(), async result => { - if (r.Result == ButtonResult.OK) + if (result.Result == ButtonResult.OK) { - // 重新加载产品列表 await LoadFinishedProductsAsync(); } }); } - private void EditProduct() + private void EditProduct(ProFinishedProductReceipt product) { - var parameters = new DialogParameters(); - parameters.Add("Product", SelectedProduct); - _dialogService.ShowDialog("EditProductDialog", parameters, async r => + if (product == null) return; + + var parameters = new DialogParameters { { "Product", product } }; + _dialogService.ShowDialog("EditProductDialog", parameters, async result => { - if (r.Result == ButtonResult.OK) + if (result.Result == ButtonResult.OK) { - // 重新加载产品列表 await LoadFinishedProductsAsync(); } }); } - private async System.Threading.Tasks.Task DeleteProductAsync() + private void DeleteProduct(ProFinishedProductReceipt product) { - var parameters = new DialogParameters(); - parameters.Add("Product", SelectedProduct); - parameters.Add("Message", "确定要删除选中的产品吗?"); - _dialogService.ShowDialog("DeleteConfirmDialog", parameters, async r => + if (product == null) return; + + var parameters = new DialogParameters { { "Product", product } }; + _dialogService.ShowDialog("DeleteConfirmDialog", parameters, async result => { - if (r.Result == ButtonResult.OK) + if (result.Result == ButtonResult.OK) { - // 重新加载产品列表 await LoadFinishedProductsAsync(); } }); } - private async System.Threading.Tasks.Task PrintProductAsync() + private void PrintProduct(ProFinishedProductReceipt product) { - var parameters = new DialogParameters(); - parameters.Add("Product", SelectedProduct); - _dialogService.ShowDialog("PrintPreviewDialog", parameters, async r => + if (product == null) return; + + var parameters = new DialogParameters { { "Product", product } }; + _dialogService.ShowDialog("PrintPreviewDialog", parameters, async result => { - if (r.Result == ButtonResult.OK) + if (result.Result == ButtonResult.OK) { - // 打印逻辑已在弹窗中处理 - // 重新加载产品列表 await LoadFinishedProductsAsync(); } }); } - - private bool CanExecuteEditDeletePrint() - { - return SelectedProduct != null; - } } } diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintPreviewDialogViewModel.cs b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintPreviewDialogViewModel.cs index 25fd27d..4f8ac7f 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintPreviewDialogViewModel.cs +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/ViewModels/PrintPreviewDialogViewModel.cs @@ -18,6 +18,13 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels set { SetProperty(ref _product, value); } } + private string _printStatus; + public string PrintStatus + { + get { return _printStatus; } + set { SetProperty(ref _printStatus, value); } + } + public string Title { get; private set; } = "打印预览"; public event System.Action RequestClose; @@ -28,23 +35,24 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels public PrintPreviewDialogViewModel(IFinishedProductService finishedProductService) { _finishedProductService = finishedProductService; - PrintCommand = new DelegateCommand(async () => await PrintProductAsync()); - CloseCommand = new DelegateCommand(CloseDialog); + PrintCommand = new DelegateCommand(async () => await PrintAsync()); + CloseCommand = new DelegateCommand(Close); } - private async System.Threading.Tasks.Task PrintProductAsync() + private async System.Threading.Tasks.Task PrintAsync() { - if (Product != null) + PrintStatus = "正在打印..."; + var success = await _finishedProductService.PrintFinishedProductAsync(Product.ReceiptNo); + PrintStatus = success ? "打印成功" : "打印失败"; + + if (success) { - var success = await _finishedProductService.PrintFinishedProductAsync(Product.ReceiptNo); - if (success) - { - RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); - } + var result = new DialogResult(ButtonResult.OK, new DialogParameters { { "Product", Product } }); + RequestClose?.Invoke(result); } } - private void CloseDialog() + private void Close() { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); } @@ -63,6 +71,7 @@ namespace RIZO_Application.Modules.ModuleName.ViewModels if (parameters.ContainsKey("Product")) { Product = parameters.GetValue("Product"); + PrintStatus = "准备打印"; } } } diff --git a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Views/MainControl.xaml b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Views/MainControl.xaml index cf52ce5..ebdcce8 100644 --- a/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Views/MainControl.xaml +++ b/RIZO_Application/Modules/RIZO_Application.Modules.ModuleName/Views/MainControl.xaml @@ -17,96 +17,132 @@ - - - - - - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + +