UCStandGrid.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Dlg;
  4. using MeterVision.model;
  5. using MeterVision.Patch;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. namespace MeterVision.Stand
  23. {
  24. /// <summary>
  25. /// UCStandGrid.xaml 的交互逻辑
  26. /// </summary>
  27. public partial class UCStandGrid : UserControl, INotifyPropertyChanged
  28. {
  29. public event PropertyChangedEventHandler PropertyChanged;
  30. protected void OnPropertyChanged(string propertyName)
  31. {
  32. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  33. }
  34. public event EventHandler<StandItemCountChangedEventArgs> OnStandItemCountChanged;
  35. //定义数据源
  36. public ObservableCollection<StandDetailItem> StandDetailList { get; set; }
  37. //定义事件,用于通知外部组件选中项已更改
  38. public event EventHandler<StandDetailItemChangedEventArgs> OnSelectedStandDetailItemChanged;
  39. public event Action<string> OnDeleteStandDetailItem;
  40. private StandDetailItem _selectedStandDetailsItem;
  41. //定义SelectedDataItem属性,并在值变化时触发事件
  42. public StandDetailItem SelectedStandDetailItem
  43. {
  44. get => _selectedStandDetailsItem;
  45. set
  46. {
  47. if(_selectedStandDetailsItem != value)
  48. {
  49. _selectedStandDetailsItem = value;
  50. OnPropertyChanged(nameof(SelectedStandDetailItem));
  51. OnSelectedStandDetailItemChanged?.Invoke(this, new StandDetailItemChangedEventArgs(value));
  52. }
  53. }
  54. }
  55. private StandItem _curStandItem;
  56. public StandItem CurStandItem
  57. {
  58. get => _curStandItem;
  59. set
  60. {
  61. if (_curStandItem != value)
  62. {
  63. _curStandItem = value;
  64. StandDetailPage.InitDefaulValue(mConfigItem.StandPageSize);
  65. LoadStandDetailItemList(value);
  66. }
  67. }
  68. }
  69. //详情分页信息
  70. public PageModel StandDetailPage { get; set; }
  71. private int _totalRecord;
  72. public int TotalRecords
  73. {
  74. get => _totalRecord;
  75. set
  76. {
  77. if(_totalRecord != value)
  78. {
  79. _totalRecord = value;
  80. OnPropertyChanged(nameof(TotalRecords));
  81. }
  82. }
  83. }
  84. public CfginiItem mConfigItem { get; set; }
  85. //异步方法
  86. private async void LoadStandDetailItemList(StandItem standItem)
  87. {
  88. StandDetailList.Clear();
  89. if (CurStandItem != null)
  90. {
  91. try
  92. {
  93. var result = await Task.Run(() =>
  94. {
  95. return DBStand.GetPagedStandDetails(
  96. StandDetailPage.PageNumber, StandDetailPage.PageSize, CurStandItem.StandId);
  97. });
  98. TotalRecords = result.Item1;
  99. StandDetailPage.PageCount = result.Item2;
  100. List<TStandDetail> standDetails = result.Item3;
  101. int index = 0;
  102. foreach (TStandDetail standDetail in standDetails)
  103. {
  104. StandDetailList.Add(new StandDetailItem(standDetail));
  105. index++;
  106. //更新索引号
  107. StandDetailList[StandDetailList.Count - 1].Index =
  108. index + ((StandDetailPage.PageNumber - 1) * StandDetailPage.PageSize);
  109. }
  110. if(StandDetailList.Count > 0)
  111. {
  112. dgStandDetails.ScrollIntoView(dgStandDetails.Items[0]);
  113. }
  114. }catch(Exception ex)
  115. {
  116. MessageBox.Show(Application.Current.MainWindow,
  117. $"加载数据时发生错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  118. }
  119. }
  120. SelectedStandDetailItem = null;
  121. }
  122. public UCStandGrid()
  123. {
  124. InitializeComponent();
  125. StandDetailList = new ObservableCollection<StandDetailItem>();
  126. dgStandDetails.ItemsSource = StandDetailList;
  127. CurStandItem = null;
  128. mConfigItem = CfginiItem.GetConfigItem();
  129. StandDetailPage = new PageModel()
  130. {
  131. PageSize = mConfigItem.StandPageSize,
  132. PageNumber = 1,
  133. };
  134. //mConfigItem.OnStandPageSizeChanged += MConfigItem_OnStandPageSizeChanged;
  135. mConfigItem.OnStandPageSizeChanged += MConfigItem_OnStandPageSizeChanged1;
  136. UCPatchGrid.OnDeleteStandDetail += UCPatchGrid_OnDeleteStandDetail;
  137. UCPatchGrid.OnUpdateStandValue += UCPatchGrid_OnUpdateStandValue;
  138. this.DataContext = this;
  139. }
  140. private void UCPatchGrid_OnUpdateStandValue(string arg1, string arg2)
  141. {
  142. //throw new NotImplementedException();
  143. StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == arg1);
  144. if(standDetailItem != null)
  145. {
  146. standDetailItem.StandValue = arg2;
  147. }
  148. }
  149. private void UCPatchGrid_OnDeleteStandDetail(string standDetailId)
  150. {
  151. //Application.Current.Dispatcher.Invoke(() =>
  152. //{
  153. // // 从数据源中移除该条目
  154. // StandDetailList.Remove(selectedItem);
  155. // SelectedStandDetailItem = null;
  156. // OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
  157. //});
  158. StandDetailItem standDetailItem = StandDetailList.FirstOrDefault(a => a.StandDetailId == standDetailId);
  159. if (standDetailItem != null)
  160. {
  161. StandDetailList.Remove(standDetailItem);
  162. OnDeleteStandDetailItem?.Invoke(standDetailItem.StandId);
  163. }
  164. }
  165. private void MConfigItem_OnStandPageSizeChanged(object sender, PageSizeChangedEventArgs e)
  166. {
  167. //throw new NotImplementedException();
  168. StandDetailPage.InitDefaulValue(e.PageSize);
  169. LoadStandDetailItemList(CurStandItem);
  170. }
  171. private void MConfigItem_OnStandPageSizeChanged1(int pageSize)
  172. {
  173. StandDetailPage.InitDefaulValue(pageSize);
  174. LoadStandDetailItemList(CurStandItem);
  175. }
  176. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  177. {
  178. var image = sender as System.Windows.Controls.Image;
  179. if (image == null) return;
  180. var dataContext = image.DataContext as StandDetailItem;
  181. if (dataContext == null) return;
  182. //var dialog = new ImageViewerWindow(dataContext.SourceImagePath)
  183. var dialog = new imgWindow(dataContext.SrcImage)
  184. {
  185. Owner = Application.Current.MainWindow
  186. };
  187. dialog.Show();
  188. }
  189. private void StandValue_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  190. {
  191. if (e.ClickCount == 2)
  192. {
  193. // 获取当前行的数据上下文
  194. var textBlock = sender as TextBlock;
  195. if (textBlock == null) return;
  196. var dataContext = textBlock.DataContext as StandDetailItem; // 替换为你的数据类型
  197. if (dataContext == null) return;
  198. SelectedStandDetailItem = dataContext;
  199. UpdateStandvalue(dataContext);
  200. }//e.clickCount
  201. }
  202. private void UpdateStandvalue(StandDetailItem detailItem)
  203. {
  204. StandValueModel standValueModel = new StandValueModel(detailItem);
  205. var dialog = new EditStandValueDlg(standValueModel)
  206. {
  207. Owner = Application.Current.MainWindow,
  208. WindowStartupLocation = WindowStartupLocation.CenterOwner
  209. };
  210. //if (dialog.ShowDialog() == true)
  211. //{
  212. // UpdateStandValue(detailItem, standValueModel);
  213. //}//if showDialog
  214. dialog.OnEditStandValue += (value) =>
  215. {
  216. UpdateStandValue(detailItem, standValueModel);
  217. };
  218. dialog.Show();
  219. }
  220. private void UpdateStandValue(StandDetailItem detailItem,StandValueModel standValueModel)
  221. {
  222. try
  223. {
  224. //修改数据库
  225. bool updateStandValue2 = DBStand.UpdateStandDetailStandValue(detailItem.SrcImage, standValueModel.StandValue);
  226. if (updateStandValue2)
  227. {
  228. detailItem.StandValue = standValueModel.StandValue;
  229. //MessageBox.Show(Application.Current.MainWindow, "修改标准值完成。", "提示", MessageBoxButton.OK,
  230. // MessageBoxImage.Information);
  231. }
  232. else
  233. {
  234. MessageBox.Show(Application.Current.MainWindow, "修改标准值失败。", "警告", MessageBoxButton.OK,
  235. MessageBoxImage.Warning);
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. MessageBox.Show(Application.Current.MainWindow, $"修改标准值错误:{ex.Message}!", "警告", MessageBoxButton.OK,
  241. MessageBoxImage.Warning);
  242. }
  243. }
  244. private void UserControl_DragEnter(object sender, DragEventArgs e)
  245. {
  246. e.Handled = true;
  247. // Check if the dragged item is a folder
  248. if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
  249. {
  250. var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
  251. if (paths != null && paths.Length > 0 && Directory.Exists(paths[0]))
  252. {
  253. e.Effects = DragDropEffects.Copy;
  254. }
  255. else
  256. {
  257. e.Effects = DragDropEffects.None;
  258. }
  259. }
  260. else
  261. {
  262. e.Effects = DragDropEffects.None;
  263. }
  264. }
  265. private async void UserControl_Drop(object sender, DragEventArgs e)
  266. {
  267. if (e.Data.GetDataPresent(DataFormats.FileDrop) && CurStandItem != null)
  268. {
  269. var paths = (string[])e.Data.GetData(DataFormats.FileDrop);
  270. if (paths != null && paths.Length > 0)
  271. {
  272. var folderPath = paths[0];
  273. if (Directory.Exists(folderPath))
  274. {
  275. // Process folder
  276. //await LoadJpgFilesFromFolder(folderPath);
  277. ////StandDetailPage.PageNumber = 1;
  278. //StandDetailPage.InitDefaulValue();
  279. //LoadStandDetailItemList(CurStandItem);
  280. await ImportStandImageFloder(folderPath);
  281. }
  282. }
  283. }//if
  284. }
  285. public async Task<bool> ImportStandImageFloder(string folderPath)
  286. {
  287. try
  288. {
  289. bool blLoad = await LoadJpgFilesFromFolder(folderPath);
  290. if (blLoad)
  291. {
  292. StandDetailPage.InitDefaulValue();
  293. LoadStandDetailItemList(CurStandItem);
  294. }
  295. return true;
  296. }
  297. catch(Exception ex)
  298. {
  299. MessageBox.Show(Application.Current.MainWindow,
  300. $"导入模板图像错误:{ex.Message}", "错误",
  301. MessageBoxButton.OK, MessageBoxImage.Error);
  302. return false;
  303. }
  304. }
  305. private async Task<bool> LoadJpgFilesFromFolder(string folderPath)
  306. {
  307. //var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories);
  308. var jpgFiles = Directory.GetFiles(folderPath, "*.jpg", SearchOption.AllDirectories)
  309. .Concat(Directory.GetFiles(folderPath, "*.bmp", SearchOption.AllDirectories))
  310. .ToArray();
  311. List<string> fileList = new List<string>();
  312. foreach (var filePath in jpgFiles)
  313. {
  314. /*_fileItems.Add(new FileItem
  315. {
  316. FileName = Path.GetFileName(filePath),
  317. FilePath = filePath,
  318. FileSize = new FileInfo(filePath).Length
  319. });*/
  320. if( (ThisApp.IsJpgFile(filePath) || ThisApp.IsBmpFile(filePath) ) &&
  321. ThisApp.IsFileSizeValid(filePath) &&
  322. ThisApp.IsImageDimensionsValid(filePath))
  323. {
  324. fileList.Add(filePath);
  325. }
  326. }//foreach
  327. if(fileList.Count == 0)
  328. {
  329. MessageBox.Show(Application.Current.MainWindow,
  330. "没有找到符号要求的图片文件。", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  331. return false;
  332. }
  333. //加载数据到数据库中
  334. return await InsertFilesWithProgress(fileList);
  335. }
  336. public async Task<bool> InsertFilesWithProgress(List<string> filePaths)
  337. {
  338. // 弹出一个非模式对话框来显示进度
  339. ProgressDialog progressDialog = new ProgressDialog()
  340. {
  341. Owner = Application.Current.MainWindow,
  342. WindowStartupLocation = WindowStartupLocation.CenterOwner
  343. };
  344. progressDialog.Show();
  345. try
  346. {
  347. // 任务来异步执行文件插入
  348. bool blInsert = await InsertFilesToDatabase(filePaths, progressDialog);
  349. //Task.Run(() => InsertFilesToDatabase(filePaths, progressDialog));
  350. //刷新CurItem
  351. if (blInsert)
  352. {
  353. OnStandItemCountChanged?.Invoke(this, new StandItemCountChangedEventArgs(CurStandItem));
  354. }
  355. return blInsert;
  356. }
  357. catch (Exception ex)
  358. {
  359. MessageBox.Show(Application.Current.MainWindow, $"操作数据库失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  360. return false;
  361. }
  362. finally
  363. {
  364. // 关闭进度对话框
  365. progressDialog.Close();
  366. }
  367. }
  368. public async Task<bool> InsertFilesToDatabase(List<string> filePaths, ProgressDialog progressDialog)
  369. {
  370. int totalFiles = filePaths.Count;
  371. int currentFile = 0;
  372. int insertCount = 0;
  373. try
  374. {
  375. await Task.Run(() =>
  376. {
  377. foreach (var filePath in filePaths)
  378. {
  379. // 检查文件是否已经存在于数据库
  380. if (!DBStand.IsSrcImageExitInStand(filePath, CurStandItem.StandId))
  381. {
  382. // 插入文件路径到数据库
  383. //InsertFileToDatabase(filePath);
  384. TStandDetail standDetail = new TStandDetail()
  385. {
  386. StandDetailId = Guid.NewGuid().ToString(),
  387. CreateTime = ThisApp.GetNowTime_yyyyMMddHHmmss(),
  388. StandId = CurStandItem.StandId,
  389. SrcImage = filePath,
  390. StandValue = ""
  391. };
  392. bool blInsert = DBStand.InsertStandDetail(standDetail);
  393. if(blInsert)
  394. {
  395. insertCount++;
  396. }
  397. }
  398. // 更新进度条
  399. currentFile++;
  400. Application.Current.Dispatcher.Invoke(() =>
  401. {
  402. UpdateProgress(progressDialog, currentFile, totalFiles);
  403. });
  404. }
  405. });
  406. }
  407. catch(Exception ex)
  408. {
  409. MessageBox.Show(Application.Current.MainWindow, $"插入数据失败:{ex.Message}。", "错误",
  410. MessageBoxButton.OK, MessageBoxImage.Error);
  411. }
  412. return insertCount > 0;
  413. }
  414. public void UpdateProgress(ProgressDialog progressDialog, int currentFile, int totalFiles)
  415. {
  416. // 更新进度条的进度
  417. double progress = (double)currentFile / totalFiles * 100;
  418. progressDialog.UpdateProgress(progress);
  419. }
  420. public void NextPage()
  421. {
  422. if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
  423. {
  424. StandDetailPage.PageNumber += 1;
  425. LoadStandDetailItemList(CurStandItem);
  426. }
  427. }
  428. public void PrePage()
  429. {
  430. if (StandDetailPage.PageNumber > 1)
  431. {
  432. StandDetailPage.PageNumber -= 1;
  433. LoadStandDetailItemList(CurStandItem);
  434. }
  435. }
  436. public void FirstPage()
  437. {
  438. if (StandDetailPage.PageNumber > 1)
  439. {
  440. StandDetailPage.PageNumber = 1;
  441. LoadStandDetailItemList(CurStandItem);
  442. }
  443. }
  444. public void LastPage()
  445. {
  446. if (StandDetailPage.PageNumber < StandDetailPage.PageCount)
  447. {
  448. StandDetailPage.PageNumber = StandDetailPage.PageCount;
  449. LoadStandDetailItemList(CurStandItem);
  450. }
  451. }
  452. public void SpeciPage(int pageNumber)
  453. {
  454. if(pageNumber != StandDetailPage.PageNumber &&
  455. pageNumber>0 && pageNumber <= StandDetailPage.PageCount)
  456. {
  457. StandDetailPage.PageNumber = pageNumber;
  458. LoadStandDetailItemList(CurStandItem);
  459. }
  460. }
  461. private void BtnDelDetailItem_Click(object sender, RoutedEventArgs e)
  462. {
  463. Button button = sender as Button;
  464. if (button == null) return;
  465. DataGridRow row = FindAncestor<DataGridRow>(button);
  466. if (row == null) return;
  467. //获取当前行绑定的数据项
  468. var selectedItem = row.Item as StandDetailItem;
  469. DeleteSelectedItem(selectedItem);
  470. }
  471. private async void DeleteSelectedItem(StandDetailItem selectedItem)
  472. {
  473. if (selectedItem == null) return;
  474. MessageBoxResult result = MessageBox.Show("确定要删除此条目吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
  475. if (result != MessageBoxResult.Yes) return;
  476. string titleInfo = "正在删除,请稍后...";
  477. WaitWindow waitWindow = new WaitWindow(titleInfo)
  478. {
  479. Owner = Application.Current.MainWindow,
  480. WindowStartupLocation = WindowStartupLocation.CenterOwner
  481. };
  482. waitWindow.Show();
  483. try
  484. {
  485. bool deleteSuccess = false;
  486. await Task.Run(() =>
  487. {
  488. deleteSuccess = DBStand.DeleteTStandDetailById(selectedItem.StandDetailId);
  489. });
  490. if (deleteSuccess)
  491. {
  492. //LoadStandDetailItemList(CurStandItem);
  493. Application.Current.Dispatcher.Invoke(() =>
  494. {
  495. // 从数据源中移除该条目
  496. StandDetailList.Remove(selectedItem);
  497. SelectedStandDetailItem = null;
  498. OnDeleteStandDetailItem?.Invoke(selectedItem.StandId);
  499. //重新加载数据
  500. //LoadPatchDetailItemList();
  501. });
  502. }
  503. }
  504. catch (Exception ex)
  505. {
  506. MessageBox.Show(Application.Current.MainWindow, $"删除失败:{ex.Message}", "错误",
  507. MessageBoxButton.OK, MessageBoxImage.Error);
  508. }
  509. finally
  510. {
  511. waitWindow.Close();
  512. }
  513. }
  514. public static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
  515. {
  516. var parent = VisualTreeHelper.GetParent(dependencyObject);
  517. if (parent == null) return null;
  518. var parentT = parent as T;
  519. return parentT ?? FindAncestor<T>(parent);
  520. }
  521. private void BtnUpdateStandvalue_Click(object sender, RoutedEventArgs e)
  522. {
  523. Button button = sender as Button;
  524. if (button == null) return;
  525. DataGridRow row = FindAncestor<DataGridRow>(button);
  526. if (row == null) return;
  527. //获取当前行绑定的数据项
  528. var selectedItem = row.Item as StandDetailItem;
  529. UpdateStandvalue(selectedItem);
  530. }
  531. private void MiDelteRow_Click(object sender, RoutedEventArgs e)
  532. {
  533. if(SelectedStandDetailItem != null)
  534. {
  535. DeleteSelectedItem(SelectedStandDetailItem);
  536. }
  537. }
  538. private void MiUpdateStandvalue_Click(object sender, RoutedEventArgs e)
  539. {
  540. if (SelectedStandDetailItem == null) return;
  541. UpdateStandvalue(SelectedStandDetailItem);
  542. }
  543. ///////////////////////////////////////////////////////////////////////////
  544. }
  545. public class StandDetailItemChangedEventArgs : EventArgs
  546. {
  547. public StandDetailItem SelectedDataItem { get; }
  548. public StandDetailItemChangedEventArgs(StandDetailItem selectedDataItem)
  549. {
  550. SelectedDataItem = selectedDataItem;
  551. }
  552. }
  553. //目录的条目数发生了变化
  554. public class StandItemCountChangedEventArgs : EventArgs
  555. {
  556. public StandItem mStandItem { get; }
  557. public StandItemCountChangedEventArgs(StandItem standItem)
  558. {
  559. mStandItem = standItem;
  560. }
  561. }
  562. ///////////////////////////////////////////////////////////////////
  563. }