using FastReport; using HandyControl.Controls; using JSMachine.WMS.App.Dto; using JSMachine.WMS.App.Dto.Enum; using JSMachine.WMS.App.IService; using JSMachine.WMS.Common; using JSMachine.WMS.Common.Globalization; using JSMachine.WMS.Common.Mvvm; using JSMachine.WMS.Domain.Entity; using JSMachine.WMS.Infrastructure.Extensions; using JSMachine.WMS.Infrastructure.Helper; using JSMachine.WMS.Storage.BusinessModules.Views; using Prism.Commands; using Prism.Events; using SqlSugar; using System.Windows; using System.Windows.Input; using Unity; using static JSMachine.WMS.Common.EventBus.GlobalEventBus; using static JSMachine.WMS.Common.EventBus.JobEvent; namespace JSMachine.WMS.Storage.BusinessModules.ViewModels { public class PaperStorageViewModel : ViewModelBase, IViewLoadedAndUnloadedAware { #region Command /// /// 自动打印标签 /// public ICommand IAtuoPrintCmd { get; set; } /// /// 手动打印标签 /// public ICommand IHandPrintCmd { get; set; } #endregion Command #region Search Condition private DateTime? _startTime; /// /// 订单开始时间起点 /// public DateTime? StartTime { get => _startTime; set => SetProperty(ref _startTime, value); } private DateTime? _endTime; /// /// 订单结束时间终点 /// public DateTime? EndTime { get => _endTime; set => SetProperty(ref _endTime, value); } private string _paperLabel; /// /// 原纸卷标 /// public string PaperLabel { get => _paperLabel; set => SetProperty(ref _paperLabel, value); } private string _supplier; /// /// 供应商 /// public string Supplier { get => _supplier; set => SetProperty(ref _supplier, value); } private string _paperCode; /// /// 纸质 /// public string PaperCode { get => _paperCode; set => SetProperty(ref _paperCode, value); } private string _paperWidth; /// /// 门幅 /// public string PaperWidth { get => _paperWidth; set => SetProperty(ref _paperWidth, value); } #endregion Search Condition #region DataSource private List _paperNewStockIns; /// /// 新卷入库数据集合 /// public List PaperNewStockIns { get { return _paperNewStockIns; } set { SetProperty(ref _paperNewStockIns, value); } } private PaperNewStockInDto _selectedPaperNewStockIn; /// /// 当前选中新卷入库信息 /// public PaperNewStockInDto SelectedPaperNewStockIn { get { return _selectedPaperNewStockIn; } set { SetProperty(ref _selectedPaperNewStockIn, value); } } #endregion DataSource #region Page Search Condition /// /// 分页查询条件 /// private PageQueryConditionDto _pageQueryCondtion = new() { CurrentPage = 1, PageSize = 11, OrderByFiled = nameof(PaperNewStockInDto.CreationTime), OrderByType = OrderByType.Asc, Filters = new() }; #endregion Page Search Condition #region Structure Function private IPaperNewStockInService _paperNewStockInService; private IEventAggregator _eventAggregator; public PaperStorageViewModel(IUnityContainer container, IPaperNewStockInService paperNewStockInService, IEventAggregator eventAggregator) : base(container) { IAtuoPrintCmd = new DelegateCommand(OnAutoPrint); IHandPrintCmd = new DelegateCommand(OnHandPrint); _paperNewStockInService = paperNewStockInService; _eventAggregator = eventAggregator; StartTime = DateTime.Now.GetTodayBegin(); EndTime = DateTime.Now.GetTodayEnd(); _eventAggregator.GetEvent().Subscribe(OnSearchClick); //自动写入新卷入库之后,自动刷新页面 } #endregion Structure Function /// /// 自动打印 /// private async void OnAutoPrint() { try { //未打印的记录 PaperNewStockIns = await _paperNewStockInService.GetListByExpression(p => p.PrintState == 0); if (!PaperNewStockIns.Any()) { Growl.Error("没有需要自动打印的标签", PaperMesConst.MainWindow); return; } else { Report fPrewControlReport = new Report();//报表预览 foreach (var item in PaperNewStockIns) { LoadFrxAndData(fPrewControlReport, Environment.CurrentDirectory + Global.AppSettings.LabelReportFilePathStr); fPrewControlReport.SetParameterValue("companyname", "京山轻机"); fPrewControlReport.SetParameterValue("papercode", item.PaperCode); fPrewControlReport.SetParameterValue("supplier", item.Supplier); fPrewControlReport.SetParameterValue("paperwidth", item.PaperWidth); fPrewControlReport.SetParameterValue("weight", Convert.ToInt32(item.InWeight)); fPrewControlReport.SetParameterValue("paperlabel", item.PaperLabel); fPrewControlReport.SetParameterValue("paperlength", Convert.ToInt32(item.MeterLength)); fPrewControlReport.SetParameterValue("unitweight", item.PaperUnitWeight); FastReport.Utils.Config.ReportSettings.ShowProgress = false; fPrewControlReport.PrintSettings.Printer = "Doro PDF Writer"; fPrewControlReport.PrintSettings.ShowDialog = false; fPrewControlReport.Print(); } await GetPageData(1); //更新自动打印时间 await Task.Delay(500).ContinueWith(async task => { foreach (var item in PaperNewStockIns) { item.PrintTime = DateTime.Now; item.PrintState = 1; } await _paperNewStockInService.EditBatch(PaperNewStockIns); }); } } catch (Exception ex) { LogHelper.Error($"新卷入库:自动打印标签出错: {ex.Message} \n {ex.StackTrace}"); } } /// /// 载入报表路径和报表数据 /// private void LoadFrxAndData(FastReport.Report report, string filePathStr) { //1 编辑指定路径下的frx,先load frx report.Load(filePathStr); } /// /// 手动打印 /// private void OnHandPrint() { try { PrintLabelWindow printLabelWindow = new PrintLabelWindow { WindowStartupLocation = WindowStartupLocation.CenterScreen,//窗体居中 Owner = Application.Current.MainWindow, }; printLabelWindow.SelectedPaperNewStockIn = SelectedPaperNewStockIn; _eventAggregator.GetEvent().Publish(PrintType.NewStock); printLabelWindow.ShowDialog();//打开原生的xaml文件,不使用prism,report用在prism的框架下无法正常使用 } catch (Exception ex) { LogHelper.Error($"新卷入库:手动打印弹窗出错: {ex.Message} \n {ex.StackTrace}"); } } public async void OnLoaded(PaperStorageView view) { _eventAggregator.GetEvent().Subscribe(OnSearchClick); await GetPageData(1); } public void OnUnloaded(PaperStorageView view) { } private async void OnSearchClick() { await GetPageData(1); } /// /// 获取数据 /// public override async Task GetPageData(int pageIndex) { _pageQueryCondtion.Filters = new(); if (EndTime < StartTime) { Growl.Error(Localizations.GetStringValueByKey("CompletionRecordViewModel_Growl_ErrorThree"), PaperMesConst.MainWindow); return; } if (StartTime != null) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.CreationTime) , ConditionalType=ConditionalType.GreaterThanOrEqual, FieldValue=StartTime.ToString()} ) } }); } if (EndTime != null) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.CreationTime) , ConditionalType=ConditionalType.LessThanOrEqual, FieldValue=EndTime.ToString()} ) } }); } if (!string.IsNullOrEmpty(PaperLabel)) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.PaperLabel) , ConditionalType=ConditionalType.Like, FieldValue=PaperLabel} ) } }); } if (!string.IsNullOrEmpty(Supplier)) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.Supplier) , ConditionalType=ConditionalType.Like, FieldValue=Supplier} ) } }); } if (!string.IsNullOrEmpty(PaperCode)) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.PaperCode) , ConditionalType=ConditionalType.Like, FieldValue=PaperCode} ) } }); } if (!string.IsNullOrEmpty(PaperWidth)) { _pageQueryCondtion.Filters.Add(new ConditionalCollections() { ConditionalList = new List> { new KeyValuePair ( WhereType.And, new ConditionalModel { FieldName= nameof(PaperNewStockInDto.PaperWidth) , ConditionalType=ConditionalType.Like, FieldValue=PaperWidth} ) } }); } _pageQueryCondtion.CurrentPage = pageIndex; PagedResult pageData = await _paperNewStockInService.QueryByPage(_pageQueryCondtion); if (pageData != null) { base.PageIndex = pageData.PageNumber; base.PageCount = pageData.TotalPages; base.PageSize = pageData.PageSize; base.TotalCount = pageData.TotalRecords; PaperNewStockIns = pageData.PageData; } } } }