12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- using MeterVision.Config;
- using MeterVision.db;
- using MeterVision.Dlg;
- using MeterVision.FreeAi;
- using MeterVision.model;
- using MeterVision.Util;
- 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;
- 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;
- using System.Windows.Shapes;
- namespace MeterVision.Patch
- {
- /// <summary>
- /// UCPatchGrid.xaml 的交互逻辑
- /// </summary>
- public partial class UCPatchGrid : UserControl, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- //定义任务详情数据源
- public ObservableCollection<PatchDetailItem> PatchDetailItemList { get; set; }
- // 定义事件,用于通知外部组件选中项已更改
- public event EventHandler<PatchDetailItemChangedEventArgs> OnSelectedPatchDetailItemChanged;
- public event Action<string> OnRefreshPatchItem;
- //删除标准模板明细事件
- public static event Action<string> OnDeleteStandDetail;
- //修改标准模板值事件
- public static event Action<string,string> OnUpdateStandValue;
-
- private PatchDetailItem _selectedPatchDetailItem;
- //定义SelectedDataItem属性,并在值变化时触发事件
- public PatchDetailItem SelectedPatchDetailItem
- {
- get => _selectedPatchDetailItem;
- set
- {
- if (_selectedPatchDetailItem != value)
- {
- _selectedPatchDetailItem = value;
- OnPropertyChanged(nameof(SelectedPatchDetailItem));
- OnSelectedPatchDetailItemChanged?.Invoke(this, new PatchDetailItemChangedEventArgs(value));
- }
- }
- }
- private PatchItem _curPatchItem;
- private PatchItem CurPatchItem
- {
- get => _curPatchItem;
- set
- {
- if (_curPatchItem != value)
- {
- _curPatchItem = value;
- //PatchDetailPage.InitDefaulValue(mConfigItem.PatchPageSize);
- //PatchDetailPage.InitDefaulValue();
- //LoadPatchDetailItemList();
- }
- }
- }
- //此属性只在函数内部调用
- private VPatchStation _curStationItem;
- private VPatchStation CurStationItem
- {
- get => _curStationItem;
- set
- {
- if (_curStationItem != value)
- {
- _curStationItem = value;
- //PatchDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
- //LoadStandDetailItemList();
- }
- }
- }
- private PatchFindType _selectedPatchFindType;
- private PatchFindType SelectedPatchFindType
- {
- get => _selectedPatchFindType;
- set
- {
- if (_selectedPatchFindType != value)
- {
- _selectedPatchFindType = value;
- //OnPropertyChanged(nameof(SelectedPatchFindType));
- //PatchDetailPage.InitDefaulValue();
- }
- }
- }
- //改变PatchItem或PatchFinType
- public async void ChangePatchItemOrFinType(PatchItem curPatchItem,VPatchStation curStationItem,PatchFindType patchFindType)
- {
- bool blPatchItemChanged = false;
- bool blPatchFindTypeChanged = false;
- bool blStationChanged = false;
- //判断是否数据发生变化
- if(curPatchItem != _curPatchItem)
- {
- CurPatchItem = curPatchItem;
- blPatchItemChanged = true;
- }
-
- if(curStationItem != _curStationItem)
- {
- CurStationItem = curStationItem;
- blStationChanged = true;
- }
- if(patchFindType != _selectedPatchFindType)
- {
- SelectedPatchFindType = patchFindType;
- blPatchFindTypeChanged = true;
- }
-
- //if(blPatchItemChanged || blPatchFindTypeChanged || blStationChanged)
- {
- PatchDetailPage.InitDefaulValue();
- await LoadPatchDetailItemList(true);
- }
- }
- public CfginiItem mConfigItem { get; set; }
- public PageModel PatchDetailPage { get; set; }
- private int _totalRecord;
- public int TotalRecords
- {
- get => _totalRecord;
- set
- {
- if (_totalRecord != value)
- {
- _totalRecord = value;
- OnPropertyChanged(nameof(TotalRecords));
- }
- }
- }
- public UCPatchGrid()
- {
- InitializeComponent();
- PatchDetailItemList = new ObservableCollection<PatchDetailItem>();
- dgPatchDetail.ItemsSource = PatchDetailItemList;
- mConfigItem = CfginiItem.GetConfigItem();
- PatchDetailPage = new PageModel
- {
- PageSize = mConfigItem.PageSize3, //mConfigItem.PatchPageSize,
- PageNumber = 1
- };
- //mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
- //mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
- mConfigItem.OnPageSize3Changed += MConfigItem_OnPageSize3Changed;
- this.DataContext = this;
- }
- private async void MConfigItem_OnPageSize3Changed(int pageSize3)
- {
- //throw new NotImplementedException();
- PatchDetailPage.InitDefaulValue(pageSize3);
- await LoadPatchDetailItemList(true);
- }
- //private void MConfigItem_OnPatchPageSizeChanged(int pageSize)
- //{
- // PatchDetailPage.InitDefaulValue(pageSize);
- // LoadPatchDetailItemList(true);
- //}
- //private void MConfigItem_OnPatchPageSizeChanged(object sender, PageSizeChangedEventArgs e)
- //{
- // PatchDetailPage.InitDefaulValue(e.PageSize);
- // LoadPatchDetailItemList(true);
- //}
- private async Task<bool> LoadPatchDetailItemList(bool scrollTop)
- //private void LoadPatchDetailItemList(bool scrollTop)
- {
- PatchDetailItemList.Clear();
- //if (CurPatchItem != null )
- if(CurStationItem != null)
- {
- try
- {
- var result = await Task.Run(() =>
- {
- //数据库查询
- //return DBPatch.GetPagedPatchDetails(
- // PatchDetailPage.PageNumber, PatchDetailPage.PageSize, CurPatchItem.PatchId);
- string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
- PatchFindModel findModel = new PatchFindModel(PatchDetailPage.PageNumber, PatchDetailPage.PageSize,
- CurPatchItem.PatchId,stationId ,SelectedPatchFindType);
- return DBPatch.GetPagedPatchDetails(findModel);
- });
- //var result = DBPatch.GetPagedPatchDetails(findModel);
- //更新分页信息
- TotalRecords = result.Item1;
- PatchDetailPage.PageCount = result.Item2;
- List<TPatchDetail> patchDetails = result.Item3;
- //更新数据需要在主线中进行
- Application.Current.Dispatcher.Invoke(() =>
- {
- int index = 0;
- foreach (TPatchDetail patchDetail in patchDetails)
- {
- index++;
- PatchDetailItem item = new PatchDetailItem(patchDetail);
- item.Index = index + ((PatchDetailPage.PageNumber - 1) * PatchDetailPage.PageSize);
- PatchDetailItemList.Add(item);
- }//foreach
- SelectedPatchDetailItem = null;
- if (PatchDetailItemList.Count > 0 && scrollTop)
- {
- dgPatchDetail.ScrollIntoView(dgPatchDetail.Items[0]);
- }
- });
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow,
- $"加载数据时发生错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- }
- if(PatchDetailItemList.Count > 0)
- {
- SelectedPatchDetailItem = PatchDetailItemList[0];
- }
- else
- {
- SelectedPatchDetailItem = null;
- }
- return true;
- }
- private void StandResultTextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- // 检查是否是双击
- if (e.ClickCount == 2)
- {
- // 获取当前行的数据上下文
- var textBlock = sender as TextBlock;
- if (textBlock == null) return;
- var dataContext = textBlock.DataContext as PatchDetailItem; // 替换为你的数据类型
- if (dataContext == null) return;
- SelectedPatchDetailItem = dataContext;
- StandValueModel standValueModel = new StandValueModel(dataContext);
- var dialog = new EditStandValueDlg(standValueModel)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- //if(dialog.ShowDialog() == true)
- //{
- // UpdatePatchDetailStandValue(dataContext, standValueModel);
- //}//if showDialog
- dialog.OnEditStandValue += (value) =>
- {
- UpdatePatchDetailStandValue(dataContext, standValueModel);
- };
- dialog.Show();
- }//e.clickCount
- }
- private void UpdateStandvalue(PatchDetailItem detailItem)
- {
- StandValueModel standValueModel = new StandValueModel(detailItem);
- var dialog = new EditStandValueDlg(standValueModel)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- //if (dialog.ShowDialog() == true)
- //{
- // UpdatePatchDetailStandValue(detailItem, standValueModel);
- //}//if showDialog
- dialog.OnEditStandValue += (value) =>
- {
- UpdatePatchDetailStandValue(detailItem, standValueModel);
- };
- dialog.Show();
- }
- private void UpdatePatchDetailStandValue(PatchDetailItem detailItem, StandValueModel standValueModel)
- {
- try
- {
- //修改数据库
- //判断detailItem中的FinalValue与标准值是否相等
- int equalFlag = 0;
- if (string.IsNullOrWhiteSpace(standValueModel.StandValue))
- {
- equalFlag = 3;
- }
- else
- {
- if (double.TryParse(standValueModel.StandValue, out double dStandValue))
- {
- equalFlag = detailItem.GetEqualFlag(dStandValue, detailItem.FinalValue);
- }
- }
-
- bool updateStandValue1 = DBPatch.UpdatePatchDetailStandValue(detailItem.PatchDetailId, standValueModel.StandValue,equalFlag);
- bool updateStandValue2 = DBStand.UpdateStandDetailStandValue(detailItem.SrcImage, standValueModel.StandValue);
- if (updateStandValue1 && updateStandValue2)
- {
- detailItem.StandValue = standValueModel.StandValue;
- detailItem.EqualFlag = equalFlag;
- //MessageBox.Show(Application.Current.MainWindow, "修改标准值完成。", "提示", MessageBoxButton.OK,
- // MessageBoxImage.Information);
- //应该更新目录内容
- OnRefreshPatchItem?.Invoke(detailItem.PatchId);
- OnUpdateStandValue?.Invoke(detailItem.StandDetailId, detailItem.StandValue);
- }
- 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 SrcImage_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var image = sender as Image;
- if (image == null) return;
- var dataContext = image.DataContext as PatchDetailItem;
- if (dataContext == null) return;
- //var dialog = new ImageViewerWindow(dataContext.SourceImagePath)
- if (!File.Exists(dataContext.SrcImage)) return;
- var dialog = new imgWindow(dataContext.SrcImage)
- {
- Owner = Application.Current.MainWindow
- };
- dialog.Show();
- }
- private void DstImage_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- var image = sender as Image;
- if (image == null) return;
- var dataContext = image.DataContext as PatchDetailItem;
- if (dataContext == null) return;
- //var dialog = new ImageViewerWindow(dataContext.SourceImagePath)
- if (!File.Exists(dataContext.DstImage)) return;
- var dialog = new imgWindow(dataContext.DstImage)
- {
- Owner = Application.Current.MainWindow
- };
- dialog.Show();
- }
- public async void NextPage()
- {
- if (PatchDetailPage.PageNumber < PatchDetailPage.PageCount)
- {
- PatchDetailPage.PageNumber += 1;
- await LoadPatchDetailItemList(true);
- }
- }
- public async void PrePage()
- {
- if (PatchDetailPage.PageNumber > 1)
- {
- PatchDetailPage.PageNumber -= 1;
- await LoadPatchDetailItemList(true);
- }
- }
- public async void FirstPage()
- {
- if (PatchDetailPage.PageNumber > 1)
- {
- PatchDetailPage.PageNumber = 1;
- await LoadPatchDetailItemList(true);
- }
- }
- public async void LastPage()
- {
- if (PatchDetailPage.PageNumber < PatchDetailPage.PageCount)
- {
- PatchDetailPage.PageNumber = PatchDetailPage.PageCount;
- await LoadPatchDetailItemList(true);
- }
- }
- public async void SpeciPage(int pageNumber)
- {
- if (pageNumber != PatchDetailPage.PageNumber &&
- pageNumber > 0 && pageNumber <= PatchDetailPage.PageCount)
- {
- PatchDetailPage.PageNumber = pageNumber;
- await LoadPatchDetailItemList(true);
- }
- }
- private bool IsProcessingTask = false;
- public async Task<bool> StartPatchTask()
- {
- if (CurPatchItem == null) return false;
- int taskCount = await GetTaskCount();
- if(taskCount == 0)
- {
- MessageBox.Show(Application.Current.MainWindow, "没有任务可执行。", "提示",
- MessageBoxButton.OK, MessageBoxImage.Information);
- return false;
- }
- //先查询是否有需要识别的任务
- PatchTaskDlg patchTaskDlg = new PatchTaskDlg()
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- patchTaskDlg.OnStopTask += PatchTaskDlg_OnStopTask;
- patchTaskDlg.Show();
- Application.Current.MainWindow.IsEnabled = false;
-
- try
- {
- IsProcessingTask = true;
- int runCount = 0;
- await Task.Run(() =>
- {
- string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
- //查询任务
- List<TPatchDetail> patchDetails = DBPatch.GetPatchDetailsWithRunFlag(CurPatchItem.PatchId,stationId,0);
- for(int i = 0; i < patchDetails.Count; i++)
- {
- if (!IsProcessingTask)
- {
- return;
- }
- FaRun faRun = new FaRun();
- ResultModel resultModel = null;
- try
- {
- resultModel = faRun.StartRecognition(patchDetails[i]);
- }
- catch(Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- if (resultModel == null) continue;
- patchDetails[i].SetResult(resultModel);
- bool blUpdate = DBPatch.UpdatePatchDetailWithResult(patchDetails[i]);
- //修改下一条记录的最近一次正确值(判断下一条数据的站点与本站点相同)
- if( i + 1 < patchDetails.Count && patchDetails[i + 1].StationId == patchDetails[i].StationId)
- {
- patchDetails[i + 1].LastCompress = resultModel.CompressIndex;
- if (patchDetails[i].IsGoodResult())
- {
- patchDetails[i+1].LatestValue = (long)resultModel.FinalValue;
- patchDetails[i+1].LatestComplete = (long)resultModel.CompleteValue;
- patchDetails[i+1].LatestTime = patchDetails[i].SampleTime;
- }
- else
- {
- patchDetails[i+1].LatestValue = patchDetails[i].LatestValue;
- patchDetails[i+1].LatestComplete = patchDetails[i].LatestComplete;
- patchDetails[i+1].LatestTime = patchDetails[i].LatestTime;
- }
- //更新改变了Latest的数据
- if (DBPatch.UpdatePatchDetails_Latest(patchDetails[i + 1]))
- {
- foreach (PatchDetailItem detailItem in PatchDetailItemList)
- {
- if (detailItem.PatchDetailId == patchDetails[i + 1].PatchDetailId)
- {
- ObjectHelper.CopyMatchingFields(patchDetails[i + 1], detailItem);
- break;
- }
- }//foreach
- }
- }//i+1
- if (blUpdate)
- {
- runCount++;
- }
- //根据结果更新表内容,暂且模拟
- //Thread.Sleep(300);
- Application.Current.Dispatcher.Invoke(() =>
- {
- patchTaskDlg.UpdateProgress(i + 1, taskCount);
- //更新背后的数据
- if (blUpdate)
- {
- //如果界面中存在本条数据,则更新。意义不大
- foreach (PatchDetailItem detailItem in PatchDetailItemList)
- {
- if(detailItem.PatchDetailId == patchDetails[i].PatchDetailId)
- {
- ObjectHelper.CopyMatchingFields(patchDetails[i], detailItem);
- break;
- }
- }//foreach
- }//if
- });
- //Thread.Sleep(3000);
- }//for i
- });
- return runCount > 0;
- }
- catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"执行批量识别任务:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- finally
- {
- // 执行完毕后恢复主界面交互
- Application.Current.MainWindow.IsEnabled = true;
- patchTaskDlg.Close();
- }
- }
- //如果是存在已执行的任务,恢复未执行任务的数据状态
- public async Task<bool> ResetPatchTask()
- {
- if (CurPatchItem == null) return false;
- string titleInfo = "正在恢复为任务未执行的状态,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- List<TPatchDetail> patchDetails = null;
- bool blUpdate = false;
- await Task.Run(() =>
- {
- string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
- //查询数据
- patchDetails = DBPatch.GetPatchDetailsWithRunFlag(CurPatchItem.PatchId, stationId,1);
- foreach(var patchDetail in patchDetails)
- {
- //删除目标图像及识别日志
- try
- {
- if (File.Exists(patchDetail.DstImage))
- {
- File.Delete(patchDetail.DstImage);
- }
- if (File.Exists(patchDetail.LogPath))
- {
- File.Delete(patchDetail.LogPath);
- }
- }
- catch
- {
- }
- patchDetail.ResetRunValue(); //恢复初始值
- }
-
- if (patchDetails.Count > 0)
- {
- blUpdate = DBPatch.UpdatePatchDetails(patchDetails);
- }
- });
- if (blUpdate && patchDetails != null)
- {
- foreach (PatchDetailItem detailItem in PatchDetailItemList)
- {
- var findItem = patchDetails.Find((a) => a.PatchDetailId == detailItem.PatchDetailId);
- if(findItem!= null)
- {
- ObjectHelper.CopyMatchingFields(findItem, detailItem);
- }
- }//foreach
- }
- return true;
- }
- catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- finally
- {
- waitWindow.Close();
- }
- }
- private void PatchTaskDlg_OnStopTask()
- {
- IsProcessingTask = false;
- //throw new NotImplementedException();
- }
- //获取任务数量
- private async Task<int> GetTaskCount()
- {
- if (CurPatchItem == null) return 0;
- string titleInfo = "正在获取任务数量,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- int taskCount = await Task.Run(() =>
- {
- string stationId = CurStationItem == null ? "" : CurStationItem.StationId;
- return DBPatch.GetPatchDetailsCountWithRunFlagZero(CurPatchItem.PatchId,stationId);
- });
- return taskCount;
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"获取任务数量:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return 0;
- }
- finally
- {
- waitWindow.Close();
- }
- }
- private void BtnLogViewer_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button == null) return;
- DataGridRow row = FindAncestor<DataGridRow>(button);
- if (row == null) return;
- //获取当前行绑定的数据项
- PatchDetailItem selectedItem = row.Item as PatchDetailItem;
- ShowLogViewer(selectedItem);
- }
- private void ShowLogViewer(PatchDetailItem selectedItem)
- {
- if (selectedItem.LogPath != null && File.Exists(selectedItem.LogPath))
- {
- //LogViewerWindow dialog = new LogViewerWindow(selectedItem.DstImage, selectedItem.LogPath)
- var dialog = new LogViewerWindow(selectedItem)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- //dialog.ShowDialog();
- dialog.Show();
- }
- }
- 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;
- //获取当前行绑定的数据项
- PatchDetailItem selectedItem = row.Item as PatchDetailItem;
- DeleteSelectedItem(selectedItem);
- }
- private async void DeleteSelectedItem(PatchDetailItem 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 = DBPatch.Delete_TPatchDetail_TStandDetail_ByStand(selectedItem.StandDetailId);
- //deleteSuccess = DBPatch.DeleteTPatchDetailById(selectedItem.PatchDetailId);
- });
- if (deleteSuccess)
- {
- //Application.Current.Dispatcher.Invoke(() =>
- //{
- // 从数据源中移除该条目
- int selectedIndex = PatchDetailItemList.IndexOf(selectedItem);
- //PatchDetailItemList.Remove(selectedItem); 改为从新加载页面
- SelectedPatchDetailItem = null;
- OnRefreshPatchItem?.Invoke(selectedItem.PatchId);
- OnDeleteStandDetail?.Invoke(selectedItem.StandDetailId);
- //重新加载数据
- await LoadPatchDetailItemList(false);
- //if (PatchDetailItemList.Count > selectedIndex)
- //{
- // dgPatchDetail.ScrollIntoView(dgPatchDetail.Items[selectedIndex]);
- //}
- //});
- }
- }
- 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 MiViewLog_Click(object sender, RoutedEventArgs e)
- {
- if(SelectedPatchDetailItem != null)
- {
- ShowLogViewer(SelectedPatchDetailItem);
- }
- }
- private void MiDelteRow_Click(object sender, RoutedEventArgs e)
- {
- //获取当前行绑定的数据项
- if(SelectedPatchDetailItem != null)
- {
- DeleteSelectedItem(SelectedPatchDetailItem);
- }
- }
- 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 PatchDetailItem;
- UpdateStandvalue(selectedItem);
- }
- private void MiUpdateStandvalue_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedPatchDetailItem == null) return;
- UpdateStandvalue(SelectedPatchDetailItem);
- }
- private void MiAddMemo_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedPatchDetailItem == null) return;
- AddMemo(SelectedPatchDetailItem);
- }
- private void AddMemo(PatchDetailItem detailItem)
- {
- if (detailItem == null) return;
- EditMemo editMemo = new EditMemo(detailItem.Memo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- if (editMemo.ShowDialog() == true)
- {
- string memo = editMemo.Memo;
- if (DBPatch.UpdatePatchDetailMemo(detailItem.PatchDetailId, memo))
- {
- detailItem.Memo = memo;
- }
- }
- }
- private void Memo_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (e.ClickCount == 2)
- {
- if (SelectedPatchDetailItem == null) return;
- AddMemo(SelectedPatchDetailItem);
- }
- }
- private void FaRun_OnAiRealLogInfo(object sender, AiRealLogEventArgs e)
- {
- Console.Write(e.mLogInfo);
- }
- private void MiRecong_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedPatchDetailItem == null) return;
- RecongPatchDetail(SelectedPatchDetailItem);
- }
- private async void RecongPatchDetail(PatchDetailItem patchDetailItem)
- {
- string jpgFile = patchDetailItem.SrcImage;
- if (!File.Exists(jpgFile))
- {
- MessageBox.Show(Application.Current.MainWindow, $"文件:{jpgFile}不存在!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- bool blRun = await RunAI(patchDetailItem);
- if (blRun)
- {
- //MessageBox.Show(Application.Current.MainWindow, "识别成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- else
- {
- MessageBox.Show(Application.Current.MainWindow, "识别失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private async Task<bool> RunAI(PatchDetailItem patchDetailItem)
- {
- FaRun faRun = new FaRun();
- faRun.OnAiRealLogInfo += FaRun_OnAiRealLogInfo;
- string titleInfo = "正在识别,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- TPatchDetail newPatchDetail = null;
- bool blRun = await Task.Run(() =>
- {
- try
- {
- ResultModel resultModel = null;
- try
- {
- resultModel = faRun.StartRecognition(patchDetailItem);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- if (resultModel == null) return false;
- newPatchDetail = new TPatchDetail(patchDetailItem, resultModel);
- //更新数据
- bool blUpdate = DBPatch.UpdatePatchDetailWithResult(newPatchDetail);
- return blUpdate;
- }
- catch (Exception ex)
- {
- Application.Current.Dispatcher.Invoke(() =>
- {
- MessageBox.Show(Application.Current.MainWindow, $"{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- });
- return false;
- }
- });
- //------
- //在主线程中更新DataGrid的Row数据
- if (blRun && newPatchDetail != null)
- {
- ObjectHelper.CopyMatchingFields(newPatchDetail, patchDetailItem);
- }
- return blRun;
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"识别[{patchDetailItem.SrcImage}]时出错:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- return false;
- }
- finally
- {
- waitWindow.Close();
- }
- }
- ////-------------以下代码被作废,暂不使用此方法-------------------
- //private PatchPrgDialog PatchPrgDialog; // 用于弹出进度对话框
- //public event Action<int,int> OnProcessingProgressChanged; // 用于通知进度
- //public event Action OnProcessingCompleted; // 用于通知完成
- //public event Action OnProcessingStopped; // 用于通知停止
- //private BackgroundWorker worker;
- ////private bool isProcessing;
- //private bool cancelRequested;
- //private int TotalImages;
- ////开始批量识别任务
- //public void StartPatchTask2()
- //{
- // worker = new BackgroundWorker();
- // worker.DoWork += Worker_DoWork;
- // worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
- // worker.ProgressChanged += Worker_ProgressChanged;
- // worker.WorkerReportsProgress = true;
- // worker.WorkerSupportsCancellation = true;
- // PatchPrgDialog = new PatchPrgDialog(this); // 传递 UserControl 实例给对话框
- // PatchPrgDialog.StartProcessing();
- //}
- //// 启动处理
- //public void StartProcessing()
- //{
- // //isProcessing = true;
- // cancelRequested = false;
- // worker.RunWorkerAsync();
- //}
- //// 停止处理
- //public void StopProcessing()
- //{
- // cancelRequested = true;
- // worker.CancelAsync();
- //}
- //// 执行后台处理任务
- //private void Worker_DoWork(object sender, DoWorkEventArgs e)
- //{
- // //int totalImages = 100; // 假设有100张图片要处理
- // TotalImages = 100;
- // for (int i = 1; i <= TotalImages; i++)
- // {
- // if (cancelRequested)
- // {
- // e.Cancel = true;
- // break;
- // }
- // // 模拟处理图片
- // Thread.Sleep(1000); // 这里模拟了图片处理的延迟
- // // 更新进度
- // //worker.ReportProgress((i * 100) / totalImages);
- // worker.ReportProgress(i);
- // }
- //}
- //// 更新进度条
- //private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
- //{
- // //progressBar.Value = e.ProgressPercentage;
- // OnProcessingProgressChanged?.Invoke(e.ProgressPercentage,TotalImages);
- //}
- //// 处理完成或被取消
- //private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
- //{
- // if (e.Cancelled)
- // {
- // OnProcessingStopped?.Invoke();
- // }
- // else
- // {
- // OnProcessingCompleted?.Invoke();
- // }
- //}
- //-----------------------------------------------------------------------------------
- }
- // 自定义事件参数类
- public class PatchDetailItemChangedEventArgs : EventArgs
- {
- public PatchDetailItem SelectedPatchDetailItem { get; }
- public PatchDetailItemChangedEventArgs(PatchDetailItem selectedData)
- {
- SelectedPatchDetailItem = selectedData;
- }
- }
- }
|