using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.ObjectModel; using System.Windows.Input; using System.Data; using System.Windows.Controls; using System.ComponentModel; using System.Runtime.CompilerServices; using AnQing.Models; using RGD.DBUtility; using AnQing.Commons; using Microsoft.Win32; using Microsoft.WindowsAPICodePack.Dialogs; using System.Windows.Media.Imaging; namespace RFIDReaderJilin.ViewModel { public class WindowQueryViewModel : INotifyPropertyChanged { private ObservableCollection _dgScanMain = new ObservableCollection(); public ObservableCollection DgScanMain { get => _dgScanMain; set { if (_dgScanMain != value) { _dgScanMain = value; OnPropertyChanged(); OnPropertyChanged(nameof(MaxPageCount)); // 数据加载后更新 MaxPageCount DgScanMain_Pagination = new ObservableCollection(DgScanMain.Skip((PageIndex - 1) * DataCountPerPage).Take(DataCountPerPage)); } } } private ObservableCollection _dgScanMain_Pagination = new ObservableCollection(); public ObservableCollection DgScanMain_Pagination { get => _dgScanMain_Pagination; set { _dgScanMain_Pagination = value; OnPropertyChanged(); } } private ICommand buttonQuit2Command; public ICommand ButtonQuit2Command => buttonQuit2Command ??= new RelayCommand(ButtonQuit2); private ICommand buttonQuitCommand; public ICommand ButtonQuitCommand => buttonQuitCommand ??= new RelayCommand(ButtonQuit); private ICommand buttonQueryCommand; public ICommand ButtonQueryCommand => buttonQueryCommand ??= new RelayCommand(ButtonQuery); private ICommand buttonQueryClearCommand; public ICommand ButtonQueryClearCommand => buttonQueryClearCommand ??= new RelayCommand(ButtonQueryClear); private ICommand buttonExportCommand; public ICommand ButtonExportCommand => buttonExportCommand ??= new RelayCommand(ButtonExport); public WindowQueryViewModel() { LoadScanMain(); } /// /// 初始化获取所有历史记录 /// private void LoadScanMain() { Task.Run(() => { DataView dv = DbHelperSQL.Query("select * from SCAN_MIAN").Tables[0].DefaultView; DgScanMain = Common.ConvertDataViewToObservableCollection(dv); }); } /// /// 退出页面 /// private void ButtonQuit() => Common.MenuViewQuit(); /// /// 退出页面 /// private void ButtonQuit2() { SelectMain = null; Common.historycontrol.List_DeTail.Visibility = System.Windows.Visibility.Collapsed; } #region 查询条件 private string _barcodes; private string _startTime; private string _endTime; public string Barcodes { get => _barcodes; set { if (_barcodes != value) { _barcodes = value; OnPropertyChanged(nameof(Barcodes)); } } } public string StartTime { get => _startTime; set { if (_startTime != value) { _startTime = value; OnPropertyChanged(nameof(StartTime)); } } } public string EndTime { get => _endTime; set { if (_endTime != value) { _endTime = value; OnPropertyChanged(nameof(EndTime)); } } } /// /// 查询事件 /// private void ButtonQuery() { try { string sql = "select * from SCAN_MIAN"; if (!string.IsNullOrEmpty(Barcodes)) { sql += $" where goodcode='{Barcodes}'"; } else { sql += " where 1=1"; } if (!string.IsNullOrEmpty(StartTime)) { sql += $" AND time >= '{DateTime.Parse(StartTime).ToString("yyyy-MM-dd HH:mm:ss")}'"; } if (!string.IsNullOrEmpty(EndTime)) { sql += $" AND time <= '{DateTime.Parse(EndTime).ToString("yyyy-MM-dd HH:mm:ss")}'"; } sql += " order by time desc"; DataView dv = DbHelperSQL.Query(sql).Tables[0].DefaultView; DgScanMain = Common.ConvertDataViewToObservableCollection(dv); } catch (Exception ex) { throw; } } /// /// 清空查询条件 /// private void ButtonQueryClear() { try { Barcodes = ""; StartTime = ""; EndTime = ""; DataView dv = DbHelperSQL.Query("select * from SCAN_MIAN").Tables[0].DefaultView; DgScanMain = Common.ConvertDataViewToObservableCollection(dv); } catch (Exception ex) { } } private void ButtonExport() { try { var dialog = new CommonOpenFileDialog(); dialog.IsFolderPicker = true; CommonFileDialogResult result = dialog.ShowDialog(); if (result == CommonFileDialogResult.Ok) { string path = dialog.FileName + $"\\{DateTime.Now.ToString("yyyyMMddHHmmss")}导出.xlsx"; Task.Run(() => { Common.SaveToExcel(DgScanMain, path); }); } } catch (Exception ex) { } } #endregion #region 分页相关 /// /// 页码数 /// public int MaxPageCount => (int)Math.Ceiling((double)DgScanMain.Count / (double)DataCountPerPage); /// /// 当前选中页码 /// private int _pageIndex = 1; public int PageIndex { get => _pageIndex; set { if (_pageIndex != value) { _pageIndex = value; OnPropertyChanged(nameof(PageIndex)); UpdatePagination(1); } } } /// /// 每页显示最多数 --当前选中的 /// private int _dataCountPerPage = 10; public int DataCountPerPage { get => _dataCountPerPage; set { if (_dataCountPerPage != value) { _dataCountPerPage = value; OnPropertyChanged(nameof(DataCountPerPage)); OnPropertyChanged(nameof(MaxPageCount)); UpdatePagination(1); } } } private void UpdatePagination(int type) { try { if (type == 1) { DgScanMain_Pagination = new ObservableCollection( DgScanMain.Skip((PageIndex - 1) * DataCountPerPage).Take(DataCountPerPage) ); } else if (type == 2) { string sql = "select * from SCAN_MIAN"; switch (DataCountPageTimeSelect) { case "当天": sql += " WHERE time >= CAST(GETDATE() AS DATE)"; break; case "近三天": sql += " WHERE time >= DATEADD(DAY, -3, GETDATE())"; break; case "近三十天": sql += " WHERE time >= DATEADD(DAY, -30, GETDATE())"; break; } sql += " order by time desc"; DataView dv = DbHelperSQL.Query(sql).Tables[0].DefaultView; DgScanMain = Common.ConvertDataViewToObservableCollection(dv); } } catch (Exception ex) { } } /// /// 页面切换 /// /// /// public void Pagination_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs e) { try { PageIndex = e.Info; } catch (Exception ex) { } } /// /// 每页显示多少数据 /// private int[] _dataCountPageNumber = { 10, 30, 60, 100, 200, 300 }; public int[] DataCountPageNumber { get => _dataCountPageNumber; set { if (_dataCountPageNumber != value) { _dataCountPageNumber = value; OnPropertyChanged(nameof(DataCountPageNumber)); } } } /// /// 查找近几天的 /// private string[] _dataCountPageTime = { "全部时间","当天", "近三天", "近三十天" }; public string[] DataCountPageTime { get => _dataCountPageTime; set { if (_dataCountPageTime != value) { _dataCountPageTime = value; OnPropertyChanged(nameof(DataCountPageTime)); } } } /// /// 查找近几天的 --当前选中的 /// private string _dataCountPageTimeSelect = "全部时间"; public string DataCountPageTimeSelect { get => _dataCountPageTimeSelect; set { if (_dataCountPageTimeSelect != value) { _dataCountPageTimeSelect = value; OnPropertyChanged(nameof(DataCountPageTimeSelect)); UpdatePagination(2); } } } /// /// 查找近几天的 /// private SCAN_MAIN_IMG _selectMain; public SCAN_MAIN_IMG SelectMain { get => _selectMain; set { if (_selectMain != value) { _selectMain = value; OnPropertyChanged(nameof(SelectMain)); } } } public void UpdateSelectMain(SCAN_MAIN mAIN) { if (SelectMain != null) { // 释放旧的 BitmapImage 资源 DisposeBitmapImage(SelectMain.img_after); DisposeBitmapImage(SelectMain.img); } var newMain = new SCAN_MAIN_IMG { id = mAIN.id, goodcode = mAIN.goodcode, time = mAIN.time, ex = mAIN.ex, img_after = CreateBitmapImage(mAIN.img_after), img = CreateBitmapImage(mAIN.img) }; SelectMain = newMain; // 直接替换对象 } /// /// 释放 BitmapImage 资源 /// /// private void DisposeBitmapImage(BitmapImage bitmapImage) { if (bitmapImage != null) { bitmapImage.StreamSource?.Dispose(); bitmapImage.StreamSource = null; bitmapImage = null; // 彻底释放对象 } } /// /// 创建 BitmapImage /// /// 图片路径 /// 返回新的 BitmapImage private BitmapImage CreateBitmapImage(string imagePath) { var newBitmap = new BitmapImage(); newBitmap.BeginInit(); newBitmap.CacheOption = BitmapCacheOption.OnLoad; // 立刻加载到内存,释放文件句柄 newBitmap.DecodePixelWidth = 800; // 限制宽度 newBitmap.DecodePixelHeight = 620; // 限制高度 newBitmap.UriSource = new Uri(imagePath, UriKind.Absolute); newBitmap.EndInit(); newBitmap.Freeze(); // 让其在 UI 线程外使用 return newBitmap; } #endregion public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }