ReadCheckRecordWindowModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.IO;
  7. using AGV_WPF_Global;
  8. using System.Data;
  9. using DataServices;
  10. using System.Data.OleDb;
  11. using AGV_WPF.Commands;
  12. namespace AGV_WPF.ExtraUI
  13. {
  14. public class ReadCheckRecordWindowModel
  15. {
  16. public ReadCheckRecordWindowModel(Window w)
  17. {
  18. recordWindow = w as ReadCheckRecordWindow;
  19. recordWindow.DateChanged += new RoutedEventHandler(recordWindow_DateChanged);
  20. recordWindow.Closed += new EventHandler(recordWindow_Closed);
  21. CheckChangedCommand = new DelegateCommand();
  22. CheckChangedCommand.ExecuteAction = new Action<object>(this.CheckChanged);
  23. ClearRecordCommand = new DelegateCommand();
  24. ClearRecordCommand.ExecuteAction = new Action<object>(this.ClearRecord);
  25. for (int i = 0; i < MainWindow.mainWindow.AGVNUM_MAX;i++ )
  26. {
  27. recordWindow.comboBoxCarID.Items.Add(string.Format("{0}", i + 1));
  28. }
  29. }
  30. //用于删除临时文件
  31. void recordWindow_Closed(object sender, EventArgs e)
  32. {
  33. try
  34. {
  35. if (!string.IsNullOrEmpty(tempFile))//如果临时文件路径不为空则执行删除
  36. {
  37. File.Delete(tempFile);
  38. }
  39. }
  40. catch (System.Exception ex)
  41. {
  42. MessageBox.Show(ex.Message);
  43. }
  44. }
  45. ExcelDataBase excelData;
  46. string tempFile;//临时文件路径
  47. public Visibility IsVisible
  48. {
  49. get
  50. {
  51. if (GlobalPara.IsManager)
  52. return Visibility.Visible;
  53. else
  54. return Visibility.Hidden;
  55. }
  56. }
  57. /// <summary>
  58. /// 加载排行数据
  59. /// </summary>
  60. /// <param name="date">待排行文件</param>
  61. /// <param name="type">排行类型,AGV和站点</param>
  62. /// <param name="kind">筛选类型,0为无效,1为漏卡,2为掉线,3为漏卡掉线</param>
  63. public void LoadRankData(string date,string type,int kind)
  64. {
  65. string dateFile = string.Format(@"{0}\{1}.xls", GlobalPara.linkDirectory, date);//获取对应日期源文件
  66. string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
  67. if (date.Equals(currentDate))//如果查询的是当天,则先复制到临时文件中
  68. {
  69. if (File.Exists(dateFile))
  70. {
  71. tempFile = string.Format(@"{0}\{1}.xls", System.IO.Path.GetTempPath(), date);
  72. if (!File.Exists(tempFile))
  73. {
  74. System.IO.File.Copy(dateFile, tempFile);
  75. }
  76. excelData = new ExcelDataBase(tempFile);
  77. try
  78. {
  79. string sql="";
  80. if (GlobalPara.IsManager)//管理员可查看所有信息
  81. {
  82. if (type == "AGV")
  83. {
  84. if (kind == 1)
  85. {
  86. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读");
  87. }
  88. else if (kind == 2)
  89. {
  90. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "掉线");
  91. }
  92. else if (kind == 3)
  93. {
  94. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' or [记录信息] like '%{4}%'group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读","掉线");
  95. }
  96. else
  97. return;
  98. }
  99. else if(type == "站点")
  100. {
  101. if (kind == 1)
  102. {
  103. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "漏读");
  104. }
  105. else if (kind == 2)
  106. {
  107. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "掉线");
  108. }
  109. else if (kind == 3)
  110. {
  111. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' or [记录信息] like '%{4}%'group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "掉线","漏读");
  112. }
  113. else
  114. return;
  115. }
  116. else
  117. {
  118. return;
  119. }
  120. }
  121. else
  122. {
  123. if (type == "AGV")
  124. {
  125. if (kind == 1)
  126. { sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读"); }
  127. else
  128. return;
  129. }
  130. else if (type == "站点")
  131. {
  132. if (kind == 1)
  133. { sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "漏读"); }
  134. else
  135. return;
  136. }
  137. else
  138. {
  139. return;
  140. }
  141. }
  142. DataTable table = (DataTable)excelData.Execute(sql);
  143. recordWindow.listBox1.ItemsSource = table.DefaultView;
  144. }
  145. catch (System.Exception ex)
  146. {
  147. MessageBox.Show(ex.Message);
  148. }
  149. }
  150. else
  151. {
  152. recordWindow.listBox1.ItemsSource = null;
  153. return;
  154. }
  155. }
  156. else//否则直接读取
  157. {
  158. if (File.Exists(dateFile))
  159. {
  160. excelData = new ExcelDataBase(dateFile);
  161. try
  162. {
  163. string sql = "";
  164. if (GlobalPara.IsManager)//管理员可查看所有信息
  165. {
  166. if (type == "AGV")
  167. {
  168. if (kind == 1)
  169. {
  170. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读");
  171. }
  172. else if (kind == 2)
  173. {
  174. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "掉线");
  175. }
  176. else if (kind == 3)
  177. {
  178. sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' or [记录信息] like '%{4}%'group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读", "掉线");
  179. }
  180. else
  181. return;
  182. }
  183. else if (type == "站点")
  184. {
  185. if (kind == 1)
  186. {
  187. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "漏读");
  188. }
  189. else if (kind == 2)
  190. {
  191. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "掉线");
  192. }
  193. else if (kind == 3)
  194. {
  195. sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' or [记录信息] like '%{4}%'group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "掉线", "漏读");
  196. }
  197. else
  198. return;
  199. }
  200. else
  201. {
  202. return;
  203. }
  204. }
  205. else
  206. {
  207. if (type == "AGV")
  208. {
  209. if (kind == 1)
  210. { sql = string.Format("select [AGV编号] as {0},count([AGV编号]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [AGV编号] order by count([AGV编号]) desc", "Name", "Counts", GlobalPara.readCheckRecordName, "漏读"); }
  211. else
  212. return;
  213. }
  214. else if (type == "站点")
  215. {
  216. if (kind == 1)
  217. { sql = string.Format("select [应读站点] as {0},count([应读站点]) as {1} from [{2}$] where [记录信息] like '%{3}%' group by [应读站点] order by count([应读站点]) desc", "Name1", "Counts", GlobalPara.readCheckRecordName, "漏读"); }
  218. else
  219. return;
  220. }
  221. else
  222. {
  223. return;
  224. }
  225. }
  226. DataTable table = (DataTable)excelData.Execute(sql);
  227. recordWindow.listBox1.ItemsSource = table.DefaultView;
  228. }
  229. catch (System.Exception ex)
  230. {
  231. MessageBox.Show(ex.Message);
  232. }
  233. }
  234. else
  235. {
  236. recordWindow.listBox1.ItemsSource = null;
  237. return;
  238. }
  239. }
  240. }
  241. /// <summary>
  242. /// 加载指定日期记录
  243. /// </summary>
  244. /// <param name="date">指定日期</param>
  245. /// <param name="carId">待筛选的AGV编号,默认0为筛选全部</param>
  246. public void LoadExcelData(string date, int carId=0)
  247. {
  248. string dateFile = string.Format(@"{0}\{1}.xls", GlobalPara.linkDirectory, date);//获取对应日期源文件
  249. string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
  250. if (date.Equals(currentDate))//如果查询的是当天,则先复制到临时文件中
  251. {
  252. if (File.Exists(dateFile))
  253. {
  254. tempFile = string.Format(@"{0}\{1}.xls", System.IO.Path.GetTempPath(), date);
  255. if (!File.Exists(tempFile))
  256. {
  257. System.IO.File.Copy(dateFile, tempFile);
  258. }
  259. excelData = new ExcelDataBase(tempFile);
  260. try
  261. {
  262. string sql;
  263. if (GlobalPara.IsManager)//管理员可查看所有信息
  264. {
  265. sql = string.Format("select * from [{0}$]", GlobalPara.readCheckRecordName);
  266. if (carId > 0)
  267. sql = string.Format("{0} where [AGV编号]={1}", sql, carId);
  268. }
  269. else
  270. {
  271. sql = string.Format("select * from [{0}$] where [记录信息] not like '%掉线%' and [记录信息] not like '%连接%'", GlobalPara.readCheckRecordName);
  272. if (carId > 0)
  273. sql = string.Format("{0} and [AGV编号]={1}", sql, carId);
  274. }
  275. DataTable table = (DataTable)excelData.Execute(sql);
  276. recordWindow.dataGrid1.ItemsSource = table.DefaultView;
  277. }
  278. catch (System.Exception ex)
  279. {
  280. MessageBox.Show(ex.Message);
  281. }
  282. }
  283. else
  284. {
  285. //MessageBox.Show("无对应记录");
  286. recordWindow.dataGrid1.ItemsSource = null;
  287. return;
  288. }
  289. }
  290. else//否则直接读取
  291. {
  292. if (File.Exists(dateFile))
  293. {
  294. excelData = new ExcelDataBase(dateFile);
  295. try
  296. {
  297. string sql;
  298. if (GlobalPara.IsManager)//管理员可查看所有信息
  299. {
  300. sql = string.Format("select * from [{0}$]", GlobalPara.readCheckRecordName);
  301. if (carId > 0)
  302. sql = string.Format("{0} where [AGV编号]={1}", sql, carId);
  303. }
  304. else
  305. {
  306. sql = string.Format("select * from [{0}$] where [记录信息] not like '%掉线%' and [记录信息] not like '%连接%'", GlobalPara.readCheckRecordName);
  307. if (carId > 0)
  308. sql = string.Format("{0} and [AGV编号]={1}", sql, carId);
  309. }
  310. DataTable table = (DataTable)excelData.Execute(sql);
  311. recordWindow.dataGrid1.ItemsSource = table.DefaultView;
  312. }
  313. catch (System.Exception ex)
  314. {
  315. MessageBox.Show(ex.Message);
  316. }
  317. }
  318. else
  319. {
  320. //MessageBox.Show("无对应记录");
  321. recordWindow.dataGrid1.ItemsSource = null;
  322. return;
  323. }
  324. }
  325. }
  326. void recordWindow_DateChanged(object sender, RoutedEventArgs e)
  327. {
  328. string date = recordWindow.calendar1.SelectedDate.Value.ToString("yyyy-MM-dd");//获取指定格式日期字符串
  329. LoadExcelData(date);
  330. }
  331. /// <summary>
  332. /// 清空记录
  333. /// </summary>
  334. /// <param name="parameter"></param>
  335. public void ClearRecord(object parameter)
  336. {
  337. if (MessageBox.Show("确定要清除之前所有记录?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
  338. return;
  339. string currentDateFile = string.Format(@"{0}.xls",DateTime.Now.ToString("yyyy-MM-dd"));
  340. try
  341. {
  342. var files = from file in Directory.EnumerateFiles(string.Format(@"{0}\", GlobalPara.linkDirectory), "*.xls") where file.ToLower().Contains(currentDateFile) != true select file;
  343. foreach (string f in files)
  344. {
  345. File.Delete(f);
  346. //Console.WriteLine(f);
  347. }
  348. }
  349. catch (System.Exception ex)
  350. {
  351. MessageBox.Show(ex.Message);
  352. }
  353. }
  354. /// <summary>
  355. /// 窗口Check动作处理
  356. /// </summary>
  357. /// <param name="parameter"></param>
  358. public void CheckChanged(object parameter)
  359. {
  360. int kind = 0;
  361. if (recordWindow.checkBox1.IsChecked.Value && recordWindow.checkBox2.IsChecked.Value)
  362. {
  363. kind = 3;
  364. }
  365. else if (recordWindow.checkBox1.IsChecked.Value)
  366. {
  367. kind = 1;
  368. }
  369. else if (recordWindow.checkBox2.IsChecked.Value)
  370. {
  371. kind = 2;
  372. }
  373. if (recordWindow.calendar1.SelectedDate != null)
  374. {
  375. LoadRankData(recordWindow.calendar1.SelectedDate.Value.ToString("yyyy-MM-dd"), recordWindow.comboBoxType.Text, kind);
  376. }
  377. else
  378. {
  379. LoadRankData(DateTime.Now.ToString("yyyy-MM-dd"), recordWindow.comboBoxType.Text, kind);
  380. }
  381. }
  382. /// <summary>
  383. /// 关联窗口
  384. /// </summary>
  385. ReadCheckRecordWindow recordWindow;
  386. List<TestClass> list = new List<TestClass>();
  387. public DelegateCommand CheckChangedCommand { get; set; }
  388. public DelegateCommand ClearRecordCommand { get; set; }
  389. }
  390. public class TestClass
  391. {
  392. public int Number { get; set; }
  393. public string Name { get; set; }
  394. public int Counts { get; set; }
  395. }
  396. }