RemoteMappingSetting.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using Tofly.Core.Stereotype;
  4. using Tofly.CoreUI.Win.Form.Forms;
  5. using Tofly.SearchUI.Select;
  6. using Tofly.SearchUI.Common;
  7. using Tofly.Core.Context.Support;
  8. using Tofly.CoreUI.Message;
  9. using System.Xml;
  10. using System;
  11. using DevExpress.XtraGrid.Columns;
  12. namespace Tofly.SearchUI.Win
  13. {
  14. /// <summary>
  15. /// 设置查询参数
  16. /// </summary>
  17. [Component(IsSingleton = "false", Name = "RemoteMappingSetting")]
  18. public partial class RemoteMappingSetting : FormClass, IRemoteMappingSetting
  19. {
  20. private readonly string xmlFilePath = RemoteMappingHelper.xmlFilePath;
  21. private DataTable dataTable = null;
  22. private XmlDocument xmlDoc = null;
  23. private string wordDirectory = string.Empty;
  24. private string lastValue = string.Empty;
  25. public RemoteMappingSetting()
  26. {
  27. InitializeComponent();
  28. }
  29. private void buttonSelectOnMap_Click(object sender, System.EventArgs e)
  30. {
  31. }
  32. private void FormRemoteMapping_Load(object sender, EventArgs e)
  33. {
  34. //当文件名不存在的时候
  35. if (!System.IO.File.Exists(xmlFilePath))
  36. {
  37. MessageManager.Show(MessageType.Information, "请加载名为'RemoteMappingSetting.xml'的配置文件!", "提示");
  38. this.Close();
  39. return;
  40. }
  41. BindDataAndSetDisplaySchema();
  42. this.textFolderPath.Text = wordDirectory;
  43. this.calcEdit.Text = this.xmlDoc.SelectSingleNode("//MaxSheetCount").InnerText;
  44. this.comboBox.Text = this.xmlDoc.SelectSingleNode("//DataType").InnerText;
  45. //if(this)
  46. //this.calcEdit.Enabled = false;
  47. }
  48. private void buttonOK_Click(object sender, System.EventArgs e)
  49. {
  50. if (string.IsNullOrEmpty(this.textFolderPath.Text))
  51. {
  52. MessageManager.Show(MessageType.Information, "请选择文件目录!", "提示");
  53. return;
  54. }
  55. if (string.IsNullOrEmpty(this.comboBox.Text))
  56. {
  57. MessageManager.Show(MessageType.Information, "请选择数据类型!", "提示");
  58. return;
  59. }
  60. if (string.IsNullOrEmpty(this.textFolderPath.Text))
  61. {
  62. return;
  63. }
  64. if (!CheckDataIsValid())
  65. {
  66. return;
  67. }
  68. //保存编辑的值
  69. SaveEditValue(this.textFolderPath.Text);
  70. this.Close();
  71. }
  72. private bool CheckDataIsValid()
  73. {
  74. DataTable dataTable = RemoveInvalidRowsAndFillValueForEmptyCell(this.dataTable);
  75. return CheckIsValid(dataTable);
  76. }
  77. //首先对DataTable做预处理
  78. private DataTable RemoveInvalidRowsAndFillValueForEmptyCell(DataTable dataTable)
  79. {
  80. if (dataTable == null)
  81. {
  82. return null;
  83. }
  84. DataTable copyTable = dataTable.Clone();
  85. for (int i = 0; i < this.dataTable.Rows.Count; i++)
  86. {
  87. DataRow dataRow = dataTable.Rows[i];
  88. if (CheckRowIsEmpty(dataRow))
  89. {
  90. continue;
  91. }
  92. //新添加的一行
  93. DataRow newRow = copyTable.NewRow();
  94. if (CheckFirstCellIsEmpty(dataRow))
  95. {
  96. newRow[0] = dataRow[0];
  97. newRow[1] = 0;
  98. newRow[2] = dataRow[2];
  99. }
  100. else if (CheckSecondCellIsEmpty(dataRow))
  101. {
  102. newRow[0] = dataRow[0];
  103. newRow[2] = long.MaxValue;
  104. newRow[1] = dataRow[1];
  105. }
  106. else
  107. {
  108. newRow[0] = dataRow[0];
  109. newRow[1] = dataRow[1];
  110. newRow[2] = dataRow[2];
  111. }
  112. //添加一行的数据
  113. copyTable.Rows.Add(newRow);
  114. }
  115. return copyTable;
  116. }
  117. //用来坚持DataTable是否有效
  118. private bool CheckIsValid(DataTable dataTable)
  119. {
  120. foreach (DataRow item in dataTable.Rows)
  121. {
  122. if (!CheckRowIsValid(item))
  123. {
  124. return false;
  125. }
  126. }
  127. for (int i = 0; i < dataTable.Rows.Count - 1; i++)
  128. {
  129. for (int j = 0; j < dataTable.Rows.Count - i - 1; j++)
  130. {
  131. DataRow rowOne = dataTable.Rows[j];
  132. DataRow rowTwo = dataTable.Rows[j + 1];
  133. if (CheckRoleHasIntersect(rowOne, rowTwo) || CheckRoleHasIntersect(rowTwo, rowOne))
  134. {
  135. MessageManager.Show(MessageType.Information, "请检查值是否有重叠!", "提示");
  136. return false;
  137. }
  138. else
  139. {
  140. if (GetFirstCellValueL(rowOne) > GetFirstCellValueL(rowTwo))
  141. {
  142. //添加一行
  143. DataRow tempRow = dataTable.NewRow();
  144. tempRow[0] = rowOne[0];
  145. tempRow[1] = rowOne[1];
  146. tempRow[2] = rowOne[2];
  147. dataTable.Rows.InsertAt(tempRow, j + 2);
  148. dataTable.Rows.RemoveAt(j);
  149. }
  150. }
  151. }
  152. }
  153. return true;
  154. }
  155. //验证值是否有效
  156. bool CheckCellValueIsValid(object value)
  157. {
  158. if (value == null || !CanConvertToNumber(value))
  159. {
  160. return false;
  161. }
  162. if (CanConvertToNumber(value))
  163. {
  164. long myValue = long.Parse(value.ToString());
  165. if (myValue < 0)
  166. {
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. //判断两行有没有相交
  173. bool CheckRoleHasIntersect(DataRow rowOne, DataRow rowTwo)
  174. {
  175. long firstCellValue = long.MinValue;
  176. bool firstCanChange = long.TryParse(rowOne[1].ToString(), out firstCellValue);
  177. long secondCellValue = long.MinValue;
  178. bool secondCanChange = long.TryParse(rowOne[2].ToString(), out secondCellValue);
  179. long rowTwoFirstCellValue = long.Parse(rowTwo[1].ToString());
  180. if (rowTwoFirstCellValue > firstCellValue && rowTwoFirstCellValue < secondCellValue)
  181. {
  182. return true;
  183. }
  184. long rowTwoSecondCellValue = long.Parse(rowTwo[2].ToString());
  185. if (rowTwoSecondCellValue > firstCellValue && rowTwoSecondCellValue < secondCellValue)
  186. {
  187. return true;
  188. }
  189. return false;
  190. }
  191. private void gridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
  192. {
  193. }
  194. private void gridView_MouseEnter(object sender, EventArgs e)
  195. {
  196. }
  197. private bool IsFirstCell(long rowIndex, long columnIndex)
  198. {
  199. return rowIndex == 0 && columnIndex == 1;
  200. }
  201. //是否为第一列
  202. private bool IsFirstColumn(int columnCount)
  203. {
  204. if (columnCount == 1)
  205. {
  206. return true;
  207. }
  208. return false;
  209. }
  210. //是否为最后一列
  211. private bool IsLastRow(int rowCount)
  212. {
  213. if (rowCount == this.dataTable.Rows.Count - 1)
  214. {
  215. return true;
  216. }
  217. return false;
  218. }
  219. //是否为第一行
  220. private bool IsFirstRow(int rowCount)
  221. {
  222. if (rowCount == 0)
  223. {
  224. return true;
  225. }
  226. return false;
  227. }
  228. //转换为字符串
  229. private string ConvertToString(object value)
  230. {
  231. try
  232. {
  233. if (value == null) return string.Empty;
  234. return Convert.ToString(value);
  235. }
  236. catch (Exception)
  237. {
  238. return string.Empty;
  239. }
  240. }
  241. private long GetFirstCellValueL(DataRow dataRow)
  242. {
  243. object firstCell = GetFirstCellValue(dataRow);
  244. if (firstCell != null)
  245. {
  246. return long.Parse(firstCell.ToString());
  247. }
  248. return long.MinValue;
  249. }
  250. private long GetSecondCellValueL(DataRow dataRow)
  251. {
  252. object secondCell = GetSecondCellValue(dataRow);
  253. if (secondCell != null)
  254. {
  255. return long.Parse(secondCell.ToString());
  256. }
  257. return long.MinValue;
  258. }
  259. //坚持当前行是否为空
  260. private bool CheckRowIsEmpty(DataRow dataRow)
  261. {
  262. if (dataRow == null) return true;
  263. //第一个象元的值
  264. string firstCellValue = ConvertToString(GetFirstCellValue(dataRow));
  265. string secondCellValue = ConvertToString(GetSecondCellValue(dataRow));
  266. if (string.IsNullOrEmpty(firstCellValue) && string.IsNullOrEmpty(secondCellValue))
  267. {
  268. return true;
  269. }
  270. return false;
  271. }
  272. //用来检查这行是否有效的
  273. private bool CheckRowIsValid(DataRow dataRow)
  274. {
  275. long firstCellValue = long.MinValue;
  276. bool firstCanChange = long.TryParse(dataRow[1].ToString(), out firstCellValue);
  277. long secondCellValue = long.MinValue;
  278. bool secondCanChange = long.TryParse(dataRow[2].ToString(), out secondCellValue);
  279. if (!firstCanChange || !secondCanChange)
  280. {
  281. MessageManager.Show(MessageType.Information, "请检查是否含有非数值类型的值!", "提示");
  282. return false;
  283. }
  284. if (firstCellValue - secondCellValue > 0)
  285. {
  286. MessageManager.Show(MessageType.Information, "请检查值是否有重叠部分!", "提示");
  287. return false;
  288. }
  289. return true;
  290. }
  291. //当前的第一个象元值是否为空
  292. private bool CheckFirstCellIsEmpty(DataRow dataRow)
  293. {
  294. if (dataRow == null) return true;
  295. //第一个象元的值
  296. string firstCellValue = ConvertToString(GetFirstCellValue(dataRow));
  297. if (string.IsNullOrEmpty(firstCellValue))
  298. {
  299. return true;
  300. }
  301. return false;
  302. }
  303. //第二个象元值是否为空
  304. private bool CheckSecondCellIsEmpty(DataRow dataRow)
  305. {
  306. if (dataRow == null) return true;
  307. //第一个象元的值
  308. string SecondCellValue = ConvertToString(GetSecondCellValue(dataRow));
  309. if (string.IsNullOrEmpty(SecondCellValue))
  310. {
  311. return true;
  312. }
  313. return false;
  314. }
  315. //是否为最后一列
  316. private bool IsLastColumn(int columnCount)
  317. {
  318. if (columnCount == 2)
  319. {
  320. return true;
  321. }
  322. return false;
  323. }
  324. //得到行的第一列的值
  325. private object GetFirstCellValue(DataRow dataRow)
  326. {
  327. return dataRow[1];
  328. }
  329. //得到第二列的值
  330. private object GetSecondCellValue(DataRow dataRow)
  331. {
  332. return dataRow[2];
  333. }
  334. private bool IsLastCell(long rowIndex, long columnIndex)
  335. {
  336. return (rowIndex == this.dataTable.Rows.Count - 1) && (columnIndex == this.dataTable.Columns.Count - 1);
  337. }
  338. private void calcEdit_EditValueChanged(object sender, EventArgs e)
  339. {
  340. int value;
  341. if (!int.TryParse(this.calcEdit.Value.ToString(), out value))
  342. {
  343. MessageManager.Show(MessageType.Information, "必须整形的!", "提示");
  344. }
  345. }
  346. private void buttonCancel_Click(object sender, System.EventArgs e)
  347. {
  348. this.Close();
  349. }
  350. #region 内部调用函数
  351. //保存路径
  352. private void SaveEditValue(string path)
  353. {
  354. //保存编辑的值
  355. if (SaveDataTableToXmlConfig(this.dataTable, this.xmlDoc, path))
  356. MessageManager.Show(MessageType.Information, "保存成功!", "提示");
  357. else MessageManager.Show(MessageType.Information, "保存失败!", "提示");
  358. }
  359. //读取xml文件获得一个DataTable
  360. private DataTable ReadXmlConfig(string filePath)
  361. {
  362. DataTable dataTable = CreateTable();
  363. xmlDoc = Tofly.Core.Util.XmlUtils.OpenXml(filePath);
  364. //当前工作目录
  365. wordDirectory = xmlDoc.SelectSingleNode("//DirectoryPath").InnerText;
  366. XmlNodeList nodeList = xmlDoc.SelectNodes("//MatchTable/MatchItem");
  367. if (nodeList == null) return null;
  368. foreach (XmlNode item in nodeList)
  369. {
  370. DataRow dataRow = dataTable.NewRow();
  371. dataRow["ColumnScale"] = item.Attributes["scale"].Value;
  372. if (item.Attributes["minValue"].Value.Equals("MINI"))
  373. {
  374. dataRow["ColumnMiniValue"] = string.Empty;
  375. }
  376. else
  377. {
  378. dataRow["ColumnMiniValue"] = item.Attributes["minValue"].Value;
  379. }
  380. if (item.Attributes["maxValue"].Value.Equals("MAX"))
  381. {
  382. dataRow["ColumnMaxiValue"] = string.Empty;
  383. }
  384. else
  385. {
  386. dataRow["ColumnMaxiValue"] = item.Attributes["maxValue"].Value;
  387. }
  388. dataTable.Rows.Add(dataRow);
  389. }
  390. return dataTable;
  391. }
  392. //是否能够转换为long型
  393. private bool CanConvertToNumber(object value)
  394. {
  395. try
  396. {
  397. Convert.ToInt32(value);
  398. }
  399. catch
  400. {
  401. return false;
  402. }
  403. return true;
  404. }
  405. //绑定数据并且设置显示方案
  406. private void BindDataAndSetDisplaySchema()
  407. {
  408. dataTable = ReadXmlConfig(xmlFilePath);
  409. if (dataTable != null) this.gridControl.DataSource = dataTable;
  410. for (int i = 0; i < this.gridView.Columns.Count; i++)
  411. {
  412. GridColumn item = this.gridView.Columns[i];
  413. if (i == 0)
  414. {
  415. item.OptionsColumn.ReadOnly = true;
  416. }
  417. item.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
  418. }
  419. }
  420. //保存内存表到xml配置文件
  421. private bool SaveDataTableToXmlConfig(DataTable dataTable, XmlDocument xmlDoc, string dataPath)
  422. {
  423. try
  424. {
  425. if (xmlDoc == null) return false;
  426. XmlNode xmlNode = xmlDoc.SelectSingleNode("//DirectoryPath");
  427. if (xmlNode == null) return false;
  428. xmlNode.InnerText = dataPath;
  429. //图幅最大值
  430. XmlNode sheetCount = xmlDoc.SelectSingleNode("//MaxSheetCount");
  431. if (sheetCount != null)
  432. {
  433. sheetCount.InnerText = this.calcEdit.Text;
  434. }
  435. XmlNode dataType = xmlDoc.SelectSingleNode("//DataType");
  436. if (dataType != null)
  437. {
  438. dataType.InnerText = this.comboBox.Text;
  439. }
  440. //便利每一行
  441. for (int i = 0; i < dataTable.Rows.Count; i++)
  442. {
  443. DataRow dataRow = dataTable.Rows[i];
  444. if (dataRow != null)
  445. {
  446. string scale = dataRow["ColumnScale"].ToString();
  447. XmlNode nodeItem = xmlDoc.SelectSingleNode("//MatchItem[@scale='" + scale + "']");
  448. string miniValue = dataRow["ColumnMiniValue"].ToString();
  449. string maxValue = dataRow["ColumnMaxiValue"].ToString();
  450. if (i == 0 && string.IsNullOrEmpty(miniValue))
  451. {
  452. nodeItem.Attributes["minValue"].Value = string.Empty;
  453. }
  454. else
  455. {
  456. nodeItem.Attributes["minValue"].Value = miniValue;
  457. }
  458. if (i == dataTable.Rows.Count - 1 && string.IsNullOrEmpty(maxValue))
  459. {
  460. nodeItem.Attributes["maxValue"].Value = string.Empty;
  461. }
  462. else
  463. {
  464. nodeItem.Attributes["maxValue"].Value = maxValue;
  465. }
  466. }
  467. }
  468. xmlDoc.Save(this.xmlFilePath);
  469. return true;
  470. }
  471. catch (System.Exception)
  472. {
  473. return false;
  474. }
  475. }
  476. //创建一张表
  477. private DataTable CreateTable()
  478. {
  479. DataTable dataTable = new DataTable();
  480. DataColumn scaleColumn = new DataColumn();
  481. scaleColumn.ColumnName = "ColumnScale";
  482. scaleColumn.Caption = "栅格数据比例尺";
  483. DataColumn minValueColumn = new DataColumn();
  484. minValueColumn.ColumnName = "ColumnMiniValue";
  485. minValueColumn.Caption = "最小值";
  486. DataColumn maxValueColumn = new DataColumn();
  487. maxValueColumn.ColumnName = "ColumnMaxiValue";
  488. maxValueColumn.Caption = "最大值";
  489. dataTable.Columns.Add(scaleColumn);
  490. dataTable.Columns.Add(minValueColumn);
  491. dataTable.Columns.Add(maxValueColumn);
  492. return dataTable;
  493. }
  494. private bool OneIsLargerThanTwo(long one, long two)
  495. {
  496. if (one - two > 0)
  497. {
  498. return true;
  499. }
  500. return false;
  501. }
  502. #endregion
  503. #region IRemoteMappingSetting 成员
  504. public bool GridEdit
  505. {
  506. get
  507. {
  508. return this.groupControl.Enabled;
  509. }
  510. set
  511. {
  512. groupControl.Enabled = value;
  513. }
  514. }
  515. public bool MaxEdit
  516. {
  517. get
  518. {
  519. return this.calcEdit.Enabled;
  520. }
  521. set
  522. {
  523. calcEdit.Enabled = value;
  524. }
  525. }
  526. #endregion
  527. private void textFolderPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  528. {
  529. IFolderDialog folderdialog = ContextRegistry.GetContext().GetObject("IFolderDialog") as IFolderDialog;
  530. string path = folderdialog.GetFolderPath("请选择文件目录?", true);
  531. if (!string.IsNullOrEmpty(path))
  532. this.textFolderPath.Text = path;
  533. }
  534. }
  535. }