HistoryDataPage.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using CCDCount.DLL;
  2. using CCDCount.DLL.SqlDataClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using CCDCount.MODEL.ShuLiModel;
  5. using CCDCountWpf.Language;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.Drawing;
  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.Markup;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace CCDCountWpf.WpfPage
  25. {
  26. /// <summary>
  27. /// HistoryDataPage.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class HistoryDataPage : Page
  30. {
  31. private static ActionMesSqliteDataClass actionMesSqliteDataClass = null;
  32. int DataStartLine = 0;
  33. private int PageHeight = 2000;
  34. public HistoryDataPage()
  35. {
  36. InitializeComponent();
  37. DataContext = ShowMessageBus.ShowBinding;
  38. InitBatchItems();
  39. //UpDateShowBinding();
  40. }
  41. private void PreviousBtn_Click(object sender, RoutedEventArgs e)
  42. {
  43. if (ShowMessageBus.ShowBinding.HistoryImageNum <= 1)
  44. {
  45. MessageBox.Show("已经是第一页");
  46. return;
  47. }
  48. ShowMessageBus.ShowBinding.HistoryImageNum -= 1;
  49. UpDateShowBinding();
  50. }
  51. private void NextBtn_Click(object sender, RoutedEventArgs e)
  52. {
  53. if (ShowMessageBus.ShowBinding.HistoryImageNum >= ShowMessageBus.ShowBinding.HistoryImageCount)
  54. {
  55. MessageBox.Show("已经是最后一页");
  56. return;
  57. }
  58. ShowMessageBus.ShowBinding.HistoryImageNum += 1;
  59. UpDateShowBinding();
  60. }
  61. private void UpDateShowBinding()
  62. {
  63. if (actionMesSqliteDataClass == null)
  64. {
  65. return;
  66. }
  67. Stopwatch stopwatch = Stopwatch.StartNew();
  68. BitmapImage ShowBitMap = null;
  69. Bitmap BitmapImage = null;
  70. List<ActiveObjectClass> ShowResult = actionMesSqliteDataClass.GetActiveObjectForPage((ShowMessageBus.ShowBinding.HistoryImageNum - 1) * PageHeight + DataStartLine, ShowMessageBus.ShowBinding.HistoryImageNum * PageHeight + DataStartLine);
  71. if (ShowResult.Count > 0)
  72. {
  73. int ThisImageStartLine = (int)ShowResult.Min(o => o.StartLine);
  74. int ImageHeight = (int)(ShowResult.Max(o => o.LastSeenLine) - ShowResult.Min(o => o.StartLine))+50;
  75. int ImageWidth = ShowResult.Max(o => o.ImageWidth);
  76. BitmapImage = new Bitmap(ImageWidth, ImageHeight <= PageHeight ? PageHeight : ImageHeight);
  77. using (Graphics g = Graphics.FromImage(BitmapImage))
  78. {
  79. g.Clear(System.Drawing.Color.White);
  80. var redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
  81. var bluePen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 146, 255), 1);
  82. var GreenPen = new System.Drawing.Pen(System.Drawing.Color.SeaGreen, 5);
  83. foreach (var item in ShowResult)
  84. {
  85. int roix = item.MinStartCol - 5;
  86. int roiy = (int)(item.StartLine - ThisImageStartLine) + 15;
  87. int roiheight = (int)(item.LastSeenLine - (long)item.StartLine) + 10;
  88. int roiwidth = item.MaxEndCol - item.MinStartCol + 10;
  89. g.DrawRectangle(GreenPen, new System.Drawing.Rectangle(roix, roiy, roiwidth, roiheight));
  90. Font font = new Font("Arial", 20);
  91. g.DrawString(item.Num.ToString(), font, System.Drawing.Brushes.Black, new System.Drawing.Point(roix - 20 * item.Num.ToString().Length, roiy - 20));
  92. foreach (var item1 in item.RowsData)
  93. {
  94. int yPos = (int)(item1.RowsCol - ThisImageStartLine + 20);
  95. g.DrawLine(item.StateCode == 0 ? bluePen : redPen, new System.Drawing.Point(item1.StartCol, yPos), new System.Drawing.Point(item1.EndCol, yPos));
  96. }
  97. }
  98. }
  99. }
  100. else
  101. {
  102. BitmapImage = new Bitmap(4096, PageHeight);
  103. using (Graphics g = Graphics.FromImage(BitmapImage))
  104. {
  105. g.Clear(System.Drawing.Color.White);
  106. }
  107. }
  108. Application.Current.Dispatcher.InvokeAsync(() =>
  109. {
  110. ShowBitMap = ConvertToBitmapImage(BitmapImage);
  111. ShowMessageBus.ShowBinding.HistoryImage = ShowBitMap;
  112. });
  113. stopwatch.Stop();
  114. Console.WriteLine($"{stopwatch.ElapsedMilliseconds}ms");
  115. }
  116. // Bitmap 转 BitmapImage 的辅助方法
  117. private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  118. {
  119. using (MemoryStream memory = new MemoryStream())
  120. {
  121. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  122. memory.Position = 0;
  123. var bitmapImage = new BitmapImage();
  124. bitmapImage.BeginInit();
  125. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  126. bitmapImage.StreamSource = memory;
  127. bitmapImage.EndInit();
  128. return bitmapImage;
  129. }
  130. }
  131. private void SkipBtn_Click(object sender, RoutedEventArgs e)
  132. {
  133. UpDateShowBinding();
  134. }
  135. /// <summary>
  136. /// 初始化批次列表
  137. /// </summary>
  138. private void InitBatchItems()
  139. {
  140. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.NowSettingLoadMainThread.cameraConfig.CamerNo}";
  141. if (!Directory.Exists(folderPath))
  142. {
  143. // 创建文件夹
  144. Directory.CreateDirectory(folderPath);
  145. }
  146. try
  147. {
  148. // 使用 DirectoryInfo 获取文件并按修改时间排序
  149. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  150. FileInfo[] files = dirInfo.GetFiles("*.db", SearchOption.AllDirectories);
  151. // 按照修改时间排序(最新的在前)
  152. var sortedFiles = files.OrderByDescending(f => f.LastWriteTime).ToArray();
  153. ShowMessageBus.ShowBinding.BatchItems.Clear();
  154. foreach (FileInfo file in sortedFiles)
  155. {
  156. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  157. }
  158. BatchNumComBox.SelectedIndex = 0;
  159. }
  160. catch
  161. { }
  162. }
  163. /// <summary>
  164. /// 初始化批号列表
  165. /// </summary>
  166. private void InitBatchItems(DateTime Mintime, DateTime MaxTime)
  167. {
  168. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  169. if (!Directory.Exists(folderPath))
  170. {
  171. // 创建文件夹
  172. Directory.CreateDirectory(folderPath);
  173. }
  174. try
  175. {
  176. // 使用 DirectoryInfo 获取文件并按修改时间排序
  177. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  178. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  179. // 按照修改时间排序(最新的在前)
  180. var sortedFiles = files.Where(f => f.CreationTime > Mintime && f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
  181. ShowMessageBus.ShowBinding.BatchItems.Clear();
  182. foreach (FileInfo file in sortedFiles)
  183. {
  184. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  185. }
  186. BatchNumComBox.SelectedIndex = 0;
  187. }
  188. catch
  189. { }
  190. }
  191. /// <summary>
  192. /// 初始化批号列表
  193. /// </summary>
  194. private void InitBatchItemsByMinTime(DateTime Mintime)
  195. {
  196. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  197. if (!Directory.Exists(folderPath))
  198. {
  199. // 创建文件夹
  200. Directory.CreateDirectory(folderPath);
  201. }
  202. try
  203. {
  204. // 使用 DirectoryInfo 获取文件并按修改时间排序
  205. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  206. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  207. // 按照修改时间排序(最新的在前)
  208. var sortedFiles = files.Where(f => f.CreationTime > Mintime).OrderByDescending(f => f.LastWriteTime).ToArray();
  209. ShowMessageBus.ShowBinding.BatchItems.Clear();
  210. foreach (FileInfo file in sortedFiles)
  211. {
  212. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  213. }
  214. BatchNumComBox.SelectedIndex = 0;
  215. }
  216. catch
  217. { }
  218. }
  219. /// <summary>
  220. /// 初始化批号列表
  221. /// </summary>
  222. private void InitBatchItemsByMaxTime(DateTime MaxTime)
  223. {
  224. string folderPath = $"{AppDomain.CurrentDomain.BaseDirectory}DATA\\BatchData";
  225. if (!Directory.Exists(folderPath))
  226. {
  227. // 创建文件夹
  228. Directory.CreateDirectory(folderPath);
  229. }
  230. try
  231. {
  232. // 使用 DirectoryInfo 获取文件并按修改时间排序
  233. DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
  234. FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
  235. // 按照修改时间排序(最新的在前)
  236. var sortedFiles = files.Where(f => f.CreationTime < MaxTime.AddDays(1)).OrderByDescending(f => f.LastWriteTime).ToArray();
  237. ShowMessageBus.ShowBinding.BatchItems.Clear();
  238. foreach (FileInfo file in sortedFiles)
  239. {
  240. ShowMessageBus.ShowBinding.BatchItems.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name).Split('_')[1]);
  241. }
  242. BatchNumComBox.SelectedIndex = 0;
  243. }
  244. catch
  245. { }
  246. }
  247. private void BatchNumRecordMinTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  248. {
  249. if (BatchNumRecordMinTime.SelectedDate == null)
  250. {
  251. return;
  252. }
  253. else
  254. {
  255. if (BatchNumRecordMaxTime.SelectedDate == null)
  256. {
  257. InitBatchItemsByMinTime((DateTime)BatchNumRecordMinTime.SelectedDate);
  258. }
  259. else
  260. {
  261. InitBatchItems((DateTime)BatchNumRecordMinTime.SelectedDate, (DateTime)BatchNumRecordMaxTime.SelectedDate);
  262. }
  263. }
  264. }
  265. private void BatchNumRecordMaxTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  266. {
  267. if (BatchNumRecordMaxTime.SelectedDate == null)
  268. {
  269. return;
  270. }
  271. else
  272. {
  273. if (BatchNumRecordMinTime.SelectedDate == null)
  274. {
  275. InitBatchItemsByMaxTime((DateTime)BatchNumRecordMaxTime.SelectedDate);
  276. }
  277. else
  278. {
  279. InitBatchItems((DateTime)BatchNumRecordMinTime.SelectedDate, (DateTime)BatchNumRecordMaxTime.SelectedDate);
  280. }
  281. }
  282. }
  283. private void BatchNumRecordComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  284. {
  285. if (BatchNumComBox.SelectedItem != null && BatchNumComBox.SelectedItem.ToString() != string.Empty)
  286. {
  287. string BatchNumber = BatchNumComBox.SelectedItem.ToString();
  288. actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.NowSettingLoadMainThread.cameraConfig.CamerNo}\\ActiveObjectData_{BatchNumber}.db");
  289. //actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}\\ActiveObjectData_20250908.db");
  290. actionMesSqliteDataClass.GetAllActionMinStartMaxEndLine(out int num, out int StartLine, out int EndLine);
  291. ShowMessageBus.ShowBinding.HistoryImageCount = (EndLine - StartLine) / PageHeight;
  292. DataStartLine = StartLine;
  293. ShowMessageBus.ShowBinding.HistoryImageNum = 1;
  294. UpDateShowBinding();
  295. }
  296. }
  297. private void Page_Loaded(object sender, RoutedEventArgs e)
  298. {
  299. BatchNumRecordMinTime.SelectedDate = DateTime.Today;
  300. BatchNumRecordMaxTime.SelectedDate = DateTime.Today;
  301. }
  302. }
  303. }