123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- using MeterVision.Config;
- using MeterVision.db;
- using MeterVision.Dlg;
- using MeterVision.model;
- using MeterVision.Patch;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- namespace MeterVision.Stand
- {
- /// <summary>
- /// UCStandGrid.xaml 的交互逻辑
- /// </summary>
- public partial class UCStandGrid : UserControl, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- public event EventHandler<StandItemCountChangedEventArgs> OnStandItemCountChanged;
- //定义数据源
- public ObservableCollection<StandDetailItem> StandDetailList { get; set; }
-
- //定义事件,用于通知外部组件选中项已更改
- public event EventHandler<StandDetailItemChangedEventArgs> OnSelectedStandDetailItemChanged;
- public event Action<string> OnDeleteStandDetailItem;
- private StandDetailItem _selectedStandDetailsItem;
- //定义SelectedDataItem属性,并在值变化时触发事件
- public StandDetailItem SelectedStandDetailItem
- {
- get => _selectedStandDetailsItem;
- set
- {
- if(_selectedStandDetailsItem != value)
- {
- _selectedStandDetailsItem = value;
- OnPropertyChanged(nameof(SelectedStandDetailItem));
- OnSelectedStandDetailItemChanged?.Invoke(this, new StandDetailItemChangedEventArgs(value));
- }
- }
- }
- private StandItem _curStandItem;
- public StandItem CurStandItem
- {
- get => _curStandItem;
- set
- {
- if (_curStandItem != value)
- {
- _curStandItem = value;
- StandDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
- LoadStandDetailItemList(value);
- }
- }
- }
- //详情分页信息
- public PageModel StandDetailPage { get; set; }
- private int _totalRecord;
- public int TotalRecords
- {
- get => _totalRecord;
- set
- {
- if(_totalRecord != value)
- {
- _totalRecord = value;
- OnPropertyChanged(nameof(TotalRecords));
- }
- }
- }
- public CfginiItem mConfigItem { get; set; }
- //异步方法
- private async void LoadStandDetailItemList(StandItem standItem)
- {
- StandDetailList.Clear();
- if (CurStandItem != null)
- {
- try
- {
- var result = await Task.Run(() =>
- {
- return DBStand.GetPagedStandDetails(
- StandDetailPage.PageNumber, StandDetailPage.PageSize, CurStandItem.StandId);
- });
- TotalRecords = result.Item1;
- StandDetailPage.PageCount = result.Item2;
- List<TStandDetail> standDetails = result.Item3;
- int index = 0;
- foreach (TStandDetail standDetail in standDetails)
- {
- StandDetailList.Add(new StandDetailItem(standDetail));
- index++;
- //更新索引号
- StandDetailList[StandDetailList.Count - 1].Index =
- index + ((StandDetailPage.PageNumber - 1) * StandDetailPage.PageSize);
- }
- if(StandDetailList.Count > 0)
- {
- dgStandDetails.ScrollIntoView(dgStandDetails.Items[0]);
- }
- }catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow,
- $"加载数据时发生错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- SelectedStandDetailItem = null;
- }
- public UCStandGrid()
- {
- InitializeComponent();
- StandDetailList = new ObservableCollection<StandDetailItem>();
- dgStandDetails.ItemsSource = StandDetailList;
-
- CurStandItem = null;
- mConfigItem = CfginiItem.GetConfigItem();
- StandDetailPage = new PageModel()
- {
- PageSize = mConfigItem.StandPageSize,
- PageNumber = 1,
- };
- //mConfigItem.OnStandPageSizeChanged += MConfigItem_OnStandPageSizeChanged;
- mConfigItem.OnStandPageSizeChanged += MConfigItem_OnStandPageSizeChanged1;
- UCPatchGrid.OnDeleteStandDetail += UCPatchGrid_OnDeleteStandDetail;
- UCPatchGrid.OnUpdateStandValue += UCPatchGrid_OnUpdateStandValue;
- this.DataContext = this;
- }
- private void UCPatchGrid_OnUpdateStandValue(string arg1, string arg2)
- {
- //throw new NotImplementedException();
- StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == arg1);
- if(standDetailItem != null)
- {
- standDetailItem.StandValue = arg2;
- }
- }
- private void UCPatchGrid_OnDeleteStandDetail(string standDetailId)
- {
- //Application.Current.Dispatcher.Invoke(() =>
- //{
- // // 从数据源中移除该条目
- // StandDetailList.Remove(selectedItem);
- // SelectedStandDetailItem = null;
- // OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
- //});
- StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == standDetailId);
- if (standDetailItem != null)
- {
- StandDetailList.Remove(standDetailItem);
- OnDeleteStandDetailItem?.Invoke(standDetailItem.StandId);
- }
- }
- private void MConfigItem_OnStandPageSizeChanged(object sender, PageSizeChangedEventArgs e)
- {
- //throw new NotImplementedException();
- StandDetailPage.InitDefaulValue(e.PageSize);
- LoadStandDetailItemList(CurStandItem);
- }
- private void MConfigItem_OnStandPageSizeChanged1(int pageSize)
- {
- StandDetailPage.InitDefaulValue(pageSize);
- LoadStandDetailItemList(CurStandItem);
- }
- private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var image = sender as System.Windows.Controls.Image;
- if (image == null) return;
- var dataContext = image.DataContext as StandDetailItem;
- if (dataContext == null) return;
- //var dialog = new ImageViewerWindow(dataContext.SourceImagePath)
- var dialog = new imgWindow(dataContext.SrcImage)
- {
- Owner = Application.Current.MainWindow
- };
- dialog.Show();
- }
- private void StandValue_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ClickCount == 2)
- {
- // 获取当前行的数据上下文
- var textBlock = sender as TextBlock;
- if (textBlock == null) return;
- var dataContext = textBlock.DataContext as StandDetailItem; // 替换为你的数据类型
- if (dataContext == null) return;
- SelectedStandDetailItem = dataContext;
- UpdateStandvalue(dataContext);
- }//e.clickCount
- }
- private void UpdateStandvalue(StandDetailItem detailItem)
- {
- StandValueModel standValueModel = new StandValueModel(detailItem);
- var dialog = new EditStandValueDlg(standValueModel)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- //if (dialog.ShowDialog() == true)
- //{
- // UpdateStandValue(detailItem, standValueModel);
- //}//if showDialog
- dialog.OnEditStandValue += (value) =>
- {
- UpdateStandValue(detailItem, standValueModel);
- };
- dialog.Show();
- }
- private void UpdateStandValue(StandDetailItem detailItem,StandValueModel standValueModel)
- {
- try
- {
- //修改数据库
- bool updateStandValue2 = DBStand.UpdateStandDetailStandValue(detailItem.SrcImage, standValueModel.StandValue);
- if (updateStandValue2)
- {
- detailItem.StandValue = standValueModel.StandValue;
- //MessageBox.Show(Application.Current.MainWindow, "修改标准值完成。", "提示", MessageBoxButton.OK,
- // MessageBoxImage.Information);
- }
- else
- {
- MessageBox.Show(Application.Current.MainWindow, "修改标准值失败。", "警告", MessageBoxButton.OK,
- MessageBoxImage.Warning);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"修改标准值错误:{ex.Message}!", "警告", MessageBoxButton.OK,
- MessageBoxImage.Warning);
- }
- }
- private void UserControl_DragEnter(object sender, DragEventArgs e)
- {
- e.Handled = true;
- // Check if the dragged item is a folder
- if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
- {
- var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
- if (paths != null && paths.Length > 0 && Directory.Exists(paths[0]))
- {
- e.Effects = DragDropEffects.Copy;
- }
- else
- {
- e.Effects = DragDropEffects.None;
- }
- }
- else
- {
- e.Effects = DragDropEffects.None;
- }
- }
- private async void UserControl_Drop(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
- {
- var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
- if (paths != null && paths.Length > 0)
- {
- var folderPath = paths[0];
- if (Directory.Exists(folderPath))
- {
- // Process folder
- //await LoadJpgFilesFromFolder(folderPath);
- ////StandDetailPage.PageNumber = 1;
- //StandDetailPage.InitDefaulValue();
- //LoadStandDetailItemList(CurStandItem);
- await ImportStandImageFloder(folderPath);
- }
- }
- }//if
- }
- public async Task<bool> ImportStandImageFloder(string folderPath)
- {
- try
- {
- bool blLoad = await LoadJpgFilesFromFolder(folderPath);
- if (blLoad)
- {
- StandDetailPage.InitDefaulValue();
- LoadStandDetailItemList(CurStandItem);
- }
- return true;
- }
- catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow,
- $"导入模板图像错误:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- }
- private async Task<bool> LoadJpgFilesFromFolder(string folderPath)
- {
- //var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories);
- var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories)
- .Concat(Directory.GetFiles(folderPath, "*.bmp", SearchOption.AllDirectories))
- .ToArray();
- List<string> fileList = new List<string>();
- foreach (var filePath in jpgFiles)
- {
- /*_fileItems.Add(new FileItem
- {
- FileName = Path.GetFileName(filePath),
- FilePath = filePath,
- FileSize = new FileInfo(filePath).Length
- });*/
- if( (ThisApp.IsJpgFile(filePath) || ThisApp.IsBmpFile(filePath) ) &&
- ThisApp.IsFileSizeValid(filePath) &&
- ThisApp.IsImageDimensionsValid(filePath))
- {
- fileList.Add(filePath);
- }
- }//foreach
- if(fileList.Count == 0)
- {
- MessageBox.Show(Application.Current.MainWindow,
- "没有找到符号要求的图片文件。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
- return false;
- }
- //加载数据到数据库中
- return await InsertFilesWithProgress(fileList);
- }
- public async Task<bool> InsertFilesWithProgress(List<string> filePaths)
- {
- // 弹出一个非模式对话框来显示进度
- ProgressDialog progressDialog = new ProgressDialog()
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- progressDialog.Show();
- try
- {
- // 任务来异步执行文件插入
- bool blInsert = await InsertFilesToDatabase(filePaths, progressDialog);
- //Task.Run(() => InsertFilesToDatabase(filePaths, progressDialog));
- //刷新CurItem
- if (blInsert)
- {
- OnStandItemCountChanged?.Invoke(this, new StandItemCountChangedEventArgs(CurStandItem));
- }
- return blInsert;
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"操作数据库失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- finally
- {
- // 关闭进度对话框
- progressDialog.Close();
- }
- }
- public async Task<bool> InsertFilesToDatabase(List<string> filePaths, ProgressDialog progressDialog)
- {
- int totalFiles = filePaths.Count;
- int currentFile = 0;
- int insertCount = 0;
- try
- {
- await Task.Run(() =>
- {
- foreach (var filePath in filePaths)
- {
- // 检查文件是否已经存在于数据库
- if (!DBStand.IsSrcImageExitInStand(filePath, CurStandItem.StandId))
- {
- // 插入文件路径到数据库
- //InsertFileToDatabase(filePath);
- TStandDetail standDetail = new TStandDetail()
- {
- StandDetailId = Guid.NewGuid().ToString(),
- CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
- StandId = CurStandItem.StandId,
- SrcImage = filePath,
- StandValue = ""
- };
- bool blInsert = DBStand.InsertStandDetail(standDetail);
- if(blInsert)
- {
- insertCount++;
- }
- }
- // 更新进度条
- currentFile++;
- Application.Current.Dispatcher.Invoke(() =>
- {
- UpdateProgress(progressDialog, currentFile, totalFiles);
- });
- }
- });
- }
- catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"插入数据失败:{ex.Message}。", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- return insertCount > 0;
- }
- public void UpdateProgress(ProgressDialog progressDialog, int currentFile, int totalFiles)
- {
- // 更新进度条的进度
- double progress = (double)currentFile / totalFiles * 100;
- progressDialog.UpdateProgress(progress);
- }
-
- public void NextPage()
- {
- if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
- {
- StandDetailPage.PageNumber += 1;
- LoadStandDetailItemList(CurStandItem);
- }
- }
- public void PrePage()
- {
- if (StandDetailPage.PageNumber > 1)
- {
- StandDetailPage.PageNumber -= 1;
- LoadStandDetailItemList(CurStandItem);
- }
- }
- public void FirstPage()
- {
- if (StandDetailPage.PageNumber > 1)
- {
- StandDetailPage.PageNumber = 1;
- LoadStandDetailItemList(CurStandItem);
- }
- }
- public void LastPage()
- {
- if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
- {
- StandDetailPage.PageNumber = StandDetailPage.PageCount;
- LoadStandDetailItemList(CurStandItem);
- }
- }
- public void SpeciPage(int pageNumber)
- {
- if(pageNumber != StandDetailPage.PageNumber &&
- pageNumber>0 && pageNumber <= StandDetailPage.PageCount)
- {
- StandDetailPage.PageNumber = pageNumber;
- LoadStandDetailItemList(CurStandItem);
- }
- }
- private void BtnDelDetailItem_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button == null) return;
- DataGridRow row = FindAncestor<DataGridRow>(button);
- if (row == null) return;
- //获取当前行绑定的数据项
- var selectedItem = row.Item as StandDetailItem;
- DeleteSelectedItem(selectedItem);
- }
- private async void DeleteSelectedItem(StandDetailItem selectedItem)
- {
- if (selectedItem == null) return;
- MessageBoxResult result = MessageBox.Show("确定要删除此条目吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
- if (result != MessageBoxResult.Yes) return;
- string titleInfo = "正在删除,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- bool deleteSuccess = false;
- await Task.Run(() =>
- {
- deleteSuccess = DBStand.DeleteTStandDetailById(selectedItem.StandDetailId);
- });
- if (deleteSuccess)
- {
- //LoadStandDetailItemList(CurStandItem);
- Application.Current.Dispatcher.Invoke(() =>
- {
- // 从数据源中移除该条目
- StandDetailList.Remove(selectedItem);
- SelectedStandDetailItem = null;
- OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
- //重新加载数据
- //LoadPatchDetailItemList();
- });
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"删除失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- waitWindow.Close();
- }
- }
- public static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
- {
- var parent = VisualTreeHelper.GetParent(dependencyObject);
- if (parent == null) return null;
- var parentT = parent as T;
- return parentT ?? FindAncestor<T>(parent);
- }
- private void BtnUpdateStandvalue_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button == null) return;
- DataGridRow row = FindAncestor<DataGridRow>(button);
- if (row == null) return;
- //获取当前行绑定的数据项
- var selectedItem = row.Item as StandDetailItem;
- UpdateStandvalue(selectedItem);
- }
- private void MiDelteRow_Click(object sender, RoutedEventArgs e)
- {
- if(SelectedStandDetailItem != null)
- {
- DeleteSelectedItem(SelectedStandDetailItem);
- }
- }
- private void MiUpdateStandvalue_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedStandDetailItem == null) return;
- UpdateStandvalue(SelectedStandDetailItem);
- }
- ///////////////////////////////////////////////////////////////////////////
- }
- public class StandDetailItemChangedEventArgs : EventArgs
- {
- public StandDetailItem SelectedDataItem { get; }
- public StandDetailItemChangedEventArgs(StandDetailItem selectedDataItem)
- {
- SelectedDataItem = selectedDataItem;
- }
- }
- //目录的条目数发生了变化
- public class StandItemCountChangedEventArgs : EventArgs
- {
- public StandItem mStandItem { get; }
- public StandItemCountChangedEventArgs(StandItem standItem)
- {
- mStandItem = standItem;
- }
- }
- ///////////////////////////////////////////////////////////////////
- }
|