ExceptionManage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using Microsoft.Win32;
  14. using System.IO;
  15. using System.Reflection;
  16. using System.Data;
  17. using AGV_WPF.Models;
  18. using System.Text.RegularExpressions;
  19. namespace AGV_WPF
  20. {
  21. /// <summary>
  22. /// ExceptionManage.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class ExceptionManage : Window
  25. {
  26. public int carid = -1;
  27. public string strdate;
  28. public ExceptionManage()
  29. {
  30. InitializeComponent();
  31. this.dataGrid1.LoadingRow += new EventHandler<DataGridRowEventArgs>(this.DataGridSoftware_LoadingRow);
  32. BindCombox();
  33. this.DataContext = new ExceptionManageModel(this);
  34. }
  35. /// <summary>
  36. /// 表格增加行号
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. private void DataGridSoftware_LoadingRow(object sender, DataGridRowEventArgs e)
  41. {
  42. e.Row.Header = e.Row.GetIndex() + 1;
  43. }
  44. /// <summary>
  45. /// 下拉框绑定
  46. /// </summary>
  47. private void BindCombox()
  48. {
  49. //绑定AGV小车编号
  50. DAL.ZSql sql1 = new DAL.ZSql();
  51. sql1.Open("select DISTINCT CarID from T_Ex order by CarID");
  52. /*cbCarid.ItemsSource = sql1.m_table.DefaultView;
  53. cbCarid.DisplayMemberPath = "CarID";
  54. cbCarid.SelectedValuePath = "CarID";*/
  55. for (int i = 0; i < sql1.m_table.Rows.Count;i++ )
  56. {
  57. cbCarid.Items.Add(sql1.m_table.Rows[i]["CarID"].ToString());
  58. }
  59. cbCarid.Items.Add("所有AGV");
  60. sql1.Close();
  61. //日期绑定到今天的
  62. cbYear.Text = DateTime.Now.Year.ToString(); //获取年份
  63. }
  64. /// <summary>
  65. /// 全部删除
  66. /// </summary>
  67. /// <param name="sender"></param>
  68. /// <param name="e"></param>
  69. private void btnDeleteAll_Click(object sender, RoutedEventArgs e)
  70. {
  71. if (MessageBox.Show("确认要删除当前记录?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
  72. {
  73. DAL.ZSql sql1 = new DAL.ZSql();
  74. if (carid == 0)
  75. {
  76. sql1.Open("delete from T_Ex where ExTimer like '" + strdate + "%'");
  77. }
  78. else
  79. sql1.Open("delete from T_Ex where CarID=" + carid.ToString() + " and ExTimer like '" + strdate + "%'");
  80. sql1.Close();
  81. LoadDataGrid(carid, strdate);
  82. }
  83. }
  84. /// <summary>
  85. /// AGV编号下拉框选择响应
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. private void cbCarid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  90. {
  91. if (cbCarid.SelectedValue != null)
  92. {
  93. if (cbCarid.SelectedValue.ToString().Trim() == "所有AGV")
  94. {
  95. carid = 0;
  96. }
  97. else
  98. carid = Convert.ToUInt16(cbCarid.SelectedValue.ToString().Trim());
  99. LoadDataGrid(carid,strdate);
  100. }
  101. }
  102. /// <summary>
  103. /// 加载数据表格
  104. /// </summary>
  105. /// <param name="paracarid"></param>
  106. /// <param name="strdate"></param>
  107. private void LoadDataGrid(int paracarid,string strdate)
  108. {
  109. if (paracarid >= 0)
  110. {
  111. DAL.ZSql sql2 = new DAL.ZSql();
  112. if (paracarid==0)
  113. {
  114. sql2.Open("select * from T_Ex where ExTimer like '" + strdate + "%' order by ExTimer");
  115. }
  116. else
  117. sql2.Open("select * from T_Ex where CarID=" + paracarid.ToString() + " and ExTimer like '" + strdate + "%' order by ExTimer");
  118. dataGrid1.ItemsSource = sql2.m_table.DefaultView;
  119. sql2.Close();
  120. }
  121. }
  122. /// <summary>
  123. /// 日期下拉框选择响应
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void cbDate_SelectionChanged(object sender, SelectionChangedEventArgs e)
  128. {
  129. if (cbYear.SelectedValue != null && cbMonth.SelectedValue != null && cbDay.SelectedValue !=null)
  130. {
  131. ComboBoxItem cbi = (ComboBoxItem)cbYear.SelectedItem;
  132. string stryear =cbi.Content.ToString();
  133. cbi = (ComboBoxItem)cbMonth.SelectedItem;
  134. string strmonth = cbi.Content.ToString();
  135. cbi = (ComboBoxItem)cbDay.SelectedItem;
  136. string strday = cbi.Content.ToString();
  137. if (strmonth == "-选择月份-")
  138. {
  139. strmonth = "%";
  140. }
  141. if (strday == "-选择日期-")
  142. {
  143. strday = "";
  144. }
  145. strdate=stryear + "-" + strmonth + "-" + strday;
  146. LoadDataGrid(carid, strdate);
  147. }
  148. }
  149. /// <summary>
  150. /// 导出到excel
  151. /// </summary>
  152. /// <param name="sender"></param>
  153. /// <param name="e"></param>
  154. private void btnExcel_Click(object sender, RoutedEventArgs e)
  155. {
  156. MyExportDataGrid(dataGrid1);
  157. }
  158. #region DataGrid数据导出Excel
  159. private void MyExportDataGrid(DataGrid dg)
  160. {
  161. if (dg.HasItems)//判断datagrid中是否有数据
  162. {
  163. try
  164. {
  165. string strPath = Environment.CurrentDirectory;
  166. Microsoft.Win32.SaveFileDialog dialogOpenFile = new Microsoft.Win32.SaveFileDialog();
  167. dialogOpenFile.DefaultExt = "csv";//默认扩展名
  168. dialogOpenFile.AddExtension = true;//是否自动添加扩展名
  169. dialogOpenFile.Filter = "*.csv|.csv";
  170. dialogOpenFile.OverwritePrompt = true;//文件已存在是否提示覆盖
  171. dialogOpenFile.FileName = "文件名";//默认文件名
  172. dialogOpenFile.CheckPathExists = true;//提示输入的文件名无效
  173. dialogOpenFile.Title = "对话框标题";
  174. //显示对话框
  175. bool? b = dialogOpenFile.ShowDialog();
  176. if (b == true)//点击保存
  177. {
  178. using (Stream stream = dialogOpenFile.OpenFile())
  179. {
  180. string c = "";
  181. StreamWriter writer = new StreamWriter(stream, Encoding.GetEncoding("gb2312"));
  182. //DataTable dt = (DataTable)dg.DataContext;
  183. DataTable dt = ((DataView)dg.ItemsSource).Table;
  184. for (int k = 0; k < dt.Columns.Count; k++)
  185. {
  186. string strcolumns;
  187. switch (dt.Columns[k].Caption)
  188. {
  189. case "CarID":
  190. strcolumns = "小车编号";
  191. break;
  192. case "ExTimer":
  193. strcolumns = "报警时间";
  194. break;
  195. case "ExType":
  196. strcolumns = "报警类别";
  197. break;
  198. case "ExWorkLine":
  199. strcolumns = "报警生产区";
  200. break;
  201. case "ExRouteNum":
  202. strcolumns = "报警路线";
  203. break;
  204. case "ExMarkNum":
  205. strcolumns = "报警地标";
  206. break;
  207. default:
  208. strcolumns = dt.Columns[k].Caption;
  209. break;
  210. }
  211. c += strcolumns + ",";
  212. }
  213. c = c.Substring(0, c.Length - 1) + "\r\n";
  214. writer.Write(c);
  215. for (int i = 0; i < dt.Rows.Count; i++)
  216. {
  217. c = "";
  218. for (int j = 0; j < dt.Columns.Count; j++)
  219. {
  220. c += dt.Rows[i][j].ToString() + ",";
  221. }
  222. c = c.Substring(0, c.Length - 1) + "\r\n";
  223. writer.Write(c);
  224. }
  225. /*Byte[] fileContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(c);
  226. stream.Write(fileContent, 0, fileContent.Leth);*/
  227. writer.Close();
  228. stream.Close();
  229. }
  230. }
  231. //恢复系统路径-涉及不到的可以去掉
  232. Environment.CurrentDirectory = strPath;
  233. }
  234. catch (Exception msg)
  235. {
  236. MessageBox.Show(msg.ToString());
  237. }
  238. finally
  239. { MessageBox.Show("导出成功!", "系统提示"); }
  240. }
  241. else
  242. {
  243. MessageBox.Show("没有可以输出的数据!", "系统提示");
  244. }
  245. }
  246. #endregion
  247. private void comBox_DropDownClosed(object sender, EventArgs e)
  248. {
  249. if (comboBoxRankType.Text == "站点")
  250. {
  251. comboBoxWorkLine.Visibility = Visibility.Visible;
  252. }
  253. else
  254. comboBoxWorkLine.Visibility = Visibility.Hidden;
  255. LoadRank();
  256. }
  257. void LoadRank()
  258. {
  259. string date = "";
  260. if (!Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
  261. {
  262. date = string.Format("{0}-", cbYear.Text);
  263. }
  264. else if (!Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$"))
  265. {
  266. date = string.Format("{0}-{1}-", cbYear.Text,cbMonth.Text);
  267. }
  268. else if (!Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$") && !Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
  269. {
  270. date = string.Format("{0}-", cbYear.Text);
  271. }
  272. else if (Regex.IsMatch(cbDay.Text, @"^[0-9][0-9]*$") && Regex.IsMatch(cbMonth.Text, @"^[0-9][0-9]*$"))
  273. {
  274. date = string.Format("{0}-{1}-{2}", cbYear.Text, cbMonth.Text,cbDay.Text);
  275. }
  276. if (this.DataContext == null)
  277. return;
  278. string carID = cbCarid.Text;
  279. if (checkBoxCurrentDate.IsChecked.Value)
  280. {
  281. ((ExceptionManageModel)this.DataContext).LoadRankData(date, comboBoxExType.Text, comboBoxRankType.Text, comboBoxWorkLine.Text, carID);
  282. }
  283. else
  284. {
  285. ((ExceptionManageModel)this.DataContext).LoadRankData("", comboBoxExType.Text, comboBoxRankType.Text, comboBoxWorkLine.Text, carID);
  286. }
  287. }
  288. }
  289. }