CNAS取数仪器端升级
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1094 lines
47KB

  1. using CnasSynchronousCommon;
  2. using CnasSynchronusClient;
  3. using CnasSynchrousModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Windows.Forms;
  14. using System.Xml.Linq;
  15. using System.Xml.Serialization;
  16. namespace CNAS_DBSync
  17. {
  18. public delegate void ReferenceChangeSyncParamHanlder(SyncInstrumentItemInfo syncInstrumentItem);
  19. public partial class frmSyncParams : Form
  20. {
  21. public List<SyncInstrumentItemInfo> lstSyncInstrument = new List<SyncInstrumentItemInfo>(); //本地存储的仪器数据源
  22. public SyncInstrumentItemInfo currentSyncItem = new SyncInstrumentItemInfo(); //当前正在操作的仪器项
  23. public Dictionary<string, DataTable> dictInstruTables = new Dictionary<string, DataTable>(); //当前仪器项的来源表结构
  24. private SyncInstrumentItemInfo syncInstrumentItem; //其他调用传递的仪器项
  25. public ReferenceChangeSyncParamHanlder ParamsChangedDelegate; //传递仪器参数委托
  26. private string strMode = "";
  27. private string strTableInfoMode = ""; //获取所有表信息的方式;0为自动获取,1为手动输入
  28. public frmSyncParams(SyncInstrumentItemInfo syncInstrumentItem = null)
  29. {
  30. InitializeComponent();
  31. dgvInstrument.AutoGenerateColumns = false;
  32. dgvInstrument.RowHeadersVisible = false;
  33. dgvInstruDS.AutoGenerateColumns = false;
  34. dgvInstruDS.RowHeadersVisible = false;
  35. dgvCnas.AutoGenerateColumns = false;
  36. dgvCnas.RowHeadersVisible = false;
  37. dgvMapping.AutoGenerateColumns = false;
  38. dgvMapping.RowHeadersVisible = false;
  39. if (syncInstrumentItem != null)
  40. {
  41. strMode = "Reference";
  42. this.syncInstrumentItem = syncInstrumentItem;
  43. this.btnAdd.Visible = false;
  44. this.btnDel.Visible = false;
  45. }
  46. }
  47. private void Option1_Click(object sender, EventArgs e)
  48. {
  49. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  50. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  51. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  52. {
  53. lstSyncInstrument.Add(Instrumentitem);
  54. //绑定数据
  55. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  56. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  57. dgvInstrument_SelectionChanged(null, null);
  58. };
  59. frmSetting.ShowDialog();
  60. }
  61. private void Option2_Click(object sender, EventArgs e)
  62. {
  63. // 处理选项2的点击事件
  64. MessageBox.Show("选项2被点击");
  65. }
  66. private void frmSyncParams_Load(object sender, EventArgs e)
  67. {
  68. if (syncInstrumentItem == null)
  69. {
  70. lstSyncInstrument = FileOperation.GetLocalSyncInStrumentData();
  71. }
  72. else
  73. {
  74. lstSyncInstrument = new List<SyncInstrumentItemInfo>() { syncInstrumentItem };
  75. }
  76. //绑定数据源,填写相关内容
  77. if (lstSyncInstrument.Count != 0)
  78. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  79. }
  80. /// <summary>
  81. /// 保存当前设置到本地
  82. /// </summary>
  83. /// <param name="sender"></param>
  84. /// <param name="e"></param>
  85. private void btnSave_Click(object sender, EventArgs e)
  86. {
  87. //将配置好的信息存储到本地文件中
  88. try
  89. {
  90. bool bIfSaveSuccess = true;
  91. if (strMode == "Reference")
  92. {
  93. if (!CheckIfHaveDateField(new List<SyncInstrumentItemInfo>() { currentSyncItem }))
  94. {
  95. MessageBox.Show("日期字段不允许为空!");
  96. return;
  97. }
  98. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  99. {
  100. MessageBox.Show("关键字段不允许为空!");
  101. return;
  102. }
  103. //1.先加载所有数据 2.替换当前数据 3.重新保存
  104. List<SyncInstrumentItemInfo> lstDB = FileOperation.GetLocalSyncInStrumentData();
  105. var item = lstDB.Where(p => p.GUID == currentSyncItem.GUID).SingleOrDefault();
  106. if (item != null)
  107. {
  108. lstDB.Remove(item);
  109. lstDB.Add(currentSyncItem);
  110. }
  111. else
  112. {
  113. lstDB.Add(currentSyncItem);
  114. }
  115. //重新保存信息
  116. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstDB);
  117. //委托发送参数
  118. this.ParamsChangedDelegate(syncInstrumentItem);
  119. }
  120. else
  121. {
  122. if (!CheckIfHaveDateField(lstSyncInstrument))
  123. {
  124. MessageBox.Show("日期字段不允许为空!");
  125. return;
  126. }
  127. if (!CheckIfHaveKeyPrimaryField(lstSyncInstrument))
  128. {
  129. MessageBox.Show("关键字段不允许为空!");
  130. return;
  131. }
  132. bIfSaveSuccess = FileOperation.SaveLocalSyncInStrumentData(lstSyncInstrument);
  133. }
  134. if (bIfSaveSuccess)
  135. MessageBox.Show("保存成功!");
  136. else
  137. MessageBox.Show("保存失败!");
  138. }
  139. catch (Exception ex)
  140. {
  141. MessageBox.Show("保存失败!错误信息为:" + ex.Message.ToString());
  142. AppLog.Error(ex.Message);
  143. }
  144. }
  145. private bool CheckIfHaveDateField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  146. {
  147. bool bIfHave = true;
  148. foreach (var item in lstSyncInstrument)
  149. {
  150. if (item.LstSyncPramas == null) continue;
  151. if (item.LstSyncPramas.Count <= 0) continue;
  152. if (item.LstSyncPramas.Where(s => s.IfDateField == true).Count() != 1)
  153. {
  154. bIfHave = false;
  155. break;
  156. }
  157. }
  158. return bIfHave;
  159. }
  160. private bool CheckIfHaveKeyPrimaryField(List<SyncInstrumentItemInfo> lstSyncInstrument)
  161. {
  162. bool bIfHave = true;
  163. foreach (var item in lstSyncInstrument)
  164. {
  165. if (item.LstSyncPramas == null) continue;
  166. if (item.LstSyncPramas.Count <= 0) continue;
  167. if (item.LstSyncPramas.Where(s => s.IfPrimaryKey == true).Count() <= 0)
  168. {
  169. bIfHave = false;
  170. break;
  171. }
  172. }
  173. return bIfHave;
  174. }
  175. /// <summary>
  176. /// 新增仪器
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. private void btnAdd_Click(object sender, EventArgs e)
  181. {
  182. frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument);
  183. frmInstrumentCode frmCode = new frmInstrumentCode(lstSyncInstrument);
  184. frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  185. {
  186. lstSyncInstrument.Add(Instrumentitem);
  187. //绑定数据
  188. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  189. dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  190. dgvInstrument_SelectionChanged(null, null);
  191. };
  192. frmSetting.ShowDialog();
  193. }
  194. /// <summary>
  195. /// 删除仪器
  196. /// </summary>
  197. /// <param name="sender"></param>
  198. /// <param name="e"></param>
  199. private void btnDel_Click(object sender, EventArgs e)
  200. {
  201. if (dgvInstrument.Rows.Count <= 0) return;
  202. //当前选中行
  203. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  204. //找到数据源中数据,删除
  205. var lstitem = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  206. if (lstitem == null)
  207. return;
  208. else if (lstitem.Count >= 1)
  209. {
  210. foreach (var item in lstitem)
  211. {
  212. lstSyncInstrument.Remove(item);
  213. }
  214. }
  215. //绑定数据
  216. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>();
  217. dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  218. //dgvInstrument.dgvInstrument_SelectionChanged()
  219. if (lstSyncInstrument.Count > 0)
  220. dgvInstrument_SelectionChanged(null, null);
  221. else
  222. {
  223. currentSyncItem = new SyncInstrumentItemInfo();
  224. dictInstruTables.Clear();
  225. cbxCnas.DataSource = null;
  226. cbxCnas.Items.Clear();
  227. cbxInstrument.DataSource = null;
  228. dgvInstruDS.DataSource = null;
  229. dgvCnas.DataSource = null;
  230. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  231. }
  232. }
  233. private void dgvInstrument_SelectionChanged(object sender, EventArgs e)
  234. {
  235. if (dgvInstrument.Rows.Count <= 0 || dgvInstrument.Rows[dgvInstrument.CurrentRow.Index].Cells[0].Value == null)
  236. {
  237. currentSyncItem = new SyncInstrumentItemInfo();
  238. return;
  239. }
  240. //清空绑定
  241. cbxCnas.DataSource = null;
  242. cbxInstrument.DataSource = null;
  243. dgvInstruDS.DataSource = null;
  244. dgvCnas.DataSource = null;
  245. //当前选中单元格
  246. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  247. var lstInstrument = lstSyncInstrument.Where(s => s.Code == strInstrumentCode).ToList<SyncInstrumentItemInfo>();
  248. if (lstInstrument.Count == 1)
  249. {
  250. currentSyncItem = lstInstrument[0];
  251. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn))
  252. txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  253. else
  254. txtInstrumentColumn.Text = "";
  255. if (currentSyncItem.SyncInstrumentDSInfo != null && currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType != DataSourceType.None)
  256. {
  257. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  258. {
  259. case DataSourceType.MySQL:
  260. case DataSourceType.Dm:
  261. case DataSourceType.Oracle:
  262. case DataSourceType.PostgreSQL:
  263. case DataSourceType.SQL:
  264. case DataSourceType.Kingbase:
  265. if (currentSyncItem.LstSyncPramas != null)
  266. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  267. if (currentSyncItem.SyncInstrumentDSInfo.ServerName != "")
  268. btnLoadDBData_Click(sender, e);
  269. break;
  270. default:
  271. if (currentSyncItem.LstSyncPramas != null)
  272. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  273. btnLoadDBData_Click(sender, e);
  274. break;
  275. }
  276. }
  277. else
  278. {
  279. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  280. }
  281. }
  282. else
  283. {
  284. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  285. }
  286. }
  287. /// <summary>
  288. /// 切换选中表时发生
  289. /// </summary>
  290. /// <param name="sender"></param>
  291. /// <param name="e"></param>
  292. private void cbxInstrument_SelectedIndexChanged(object sender, EventArgs e)
  293. {
  294. if (cbxInstrument.Text == null) return;
  295. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  296. // 使用LINQ查找匹配的数据
  297. SyncInstrumentItemInfo matchedInstrument = lstSyncInstrument.FirstOrDefault(x => x.Code == strInstrumentCode);
  298. InstrumentData instrumentDatas = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  299. DataTable dtTableType = null;
  300. string strTableName_Instru = cbxInstrument.Text.ToString();
  301. if (matchedInstrument.SyncInstrumentDSInfo != null)
  302. {
  303. switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
  304. {
  305. case DataSourceType.MySQL:
  306. dtTableType = SelectTableType.MySqlsec(strTableName_Instru);
  307. break;
  308. case DataSourceType.Dm:
  309. dtTableType = SelectTableType.DmSql(strTableName_Instru);
  310. break;
  311. case DataSourceType.Oracle:
  312. dtTableType = SelectTableType.OrcSql(strTableName_Instru, currentSyncItem);
  313. break;
  314. case DataSourceType.PostgreSQL:
  315. dtTableType = SelectTableType.PostgreSql(strTableName_Instru);
  316. break;
  317. case DataSourceType.SQL:
  318. dtTableType = SelectTableType.Sqlserversec(strTableName_Instru, currentSyncItem);
  319. break;
  320. case DataSourceType.Kingbase:
  321. dtTableType = SelectTableType.KingSql(strTableName_Instru);
  322. break;
  323. default:
  324. strTableName_Instru = matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType.ToString();
  325. strTableName_Instru = currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType.ToString();
  326. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(matchedInstrument.SyncInstrumentDSInfo, new object[] { "", "", "" });
  327. dictInstruTables = instrumentData.GetInstrumentData();
  328. dictInstruTables = instrumentDatas.GetInstrumentData();
  329. if (dictInstruTables.ContainsKey(strTableName_Instru))
  330. dtTableType = dictInstruTables[strTableName_Instru];
  331. if (dictInstruTables.ContainsKey(cbxInstrument.Text))
  332. dtTableType = dictInstruTables[cbxInstrument.Text];
  333. break;
  334. }
  335. DataTable dtInstruShow = new DataTable();
  336. dtInstruShow.Columns.Add("InstruFieldName");
  337. dtInstruShow.Columns.Add("InstruDataType");
  338. dtInstruShow.Columns.Add("remark");
  339. if (dtTableType != null)
  340. {
  341. switch (matchedInstrument.SyncInstrumentDSInfo.InstrumentDataSourceType)
  342. {
  343. case DataSourceType.MySQL:
  344. case DataSourceType.Dm:
  345. case DataSourceType.PostgreSQL:
  346. case DataSourceType.SQL:
  347. case DataSourceType.Kingbase:
  348. for (int i = 0; i < dtTableType.Rows.Count; i++)
  349. {
  350. dtInstruShow.Rows.Add(new object[] { dtTableType.Rows[i]["ColumnName"], dtTableType.Rows[i]["DataType"], dtTableType.Rows[i]["remark"] });
  351. }
  352. break;
  353. case DataSourceType.Oracle:
  354. default:
  355. foreach (DataColumn dc in dtTableType.Columns)
  356. {
  357. dtInstruShow.Rows.Add(new object[] { dc.ColumnName, dc.DataType, "" });
  358. }
  359. break;
  360. }
  361. }
  362. dgvInstruDS.DataSource = dtInstruShow;
  363. }
  364. }
  365. /// <summary>
  366. /// 切换表时发生
  367. /// </summary>
  368. /// <param name="sender"></param>
  369. /// <param name="e"></param>
  370. private void cbxCnas_SelectedIndexChanged(object sender, EventArgs e)
  371. {
  372. if (cbxCnas.Text == null) return;
  373. string strTableName_Cnas = cbxCnas.Text.ToString();
  374. DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTableTypeLenth(strTableName_Cnas, currentSyncItem.SyncTargetDBInfo);
  375. //从数据库中加载数据表结构
  376. //DataTable dtTableStruct = CnasDataOperationFact.CnasDataOperation().GetCNASTablesStruct(strTableName_Cnas,currentSyncItem.SyncTargetDBInfo);
  377. if (dtTableStruct != null)
  378. {
  379. DataTable dtCnasShow = new DataTable();
  380. dtCnasShow.Columns.Add("CnasFieldName");
  381. dtCnasShow.Columns.Add("CnasDataType");
  382. dtCnasShow.Columns.Add("remark");
  383. for (int i = 0; i < dtTableStruct.Rows.Count; i++)
  384. {
  385. dtCnasShow.Rows.Add(new object[] { dtTableStruct.Rows[i]["ColumnName"], dtTableStruct.Rows[i]["DataType"], dtTableStruct.Rows[i]["remark"] });
  386. }
  387. dgvCnas.DataSource = null;
  388. dgvCnas.DataSource = dtCnasShow;
  389. //绑定数据
  390. cbxCNASColumn.DataSource = dtCnasShow.Copy();
  391. cbxCNASColumn.DisplayMember = "CnasFieldName";
  392. cbxCNASColumn.ValueMember = "CnasFieldName";
  393. if (dtTableStruct.Columns.Count > 0)
  394. {
  395. if (!string.IsNullOrWhiteSpace(currentSyncItem.CnasInstrumentColumn) && dtTableStruct.Columns.Contains(currentSyncItem.CnasInstrumentColumn))
  396. {
  397. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = currentSyncItem.CnasInstrumentColumn;
  398. }
  399. else
  400. {
  401. cbxCNASColumn.Text = this.txtInstrumentColumn.Text = "";
  402. }
  403. }
  404. }
  405. }
  406. //增加映射
  407. private void btnAddMapping_Click(object sender, EventArgs e)
  408. {
  409. if (cbxInstrument.Text == null) return;
  410. if (cbxCnas.Text == null) return;
  411. if (dgvInstruDS.Rows.Count <= 0) return;
  412. if (dgvCnas.Rows.Count <= 0) return;
  413. SyncParamasInfo syncParamas = new SyncParamasInfo();
  414. if (currentSyncItem.LstSyncPramas == null) currentSyncItem.LstSyncPramas = new List<SyncParamasInfo>();
  415. if (dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString().ToUpper() == "ID")
  416. {
  417. MessageBox.Show("该字段为CNAS数据库保留字段,不允许插入数据,请重新选择!");
  418. return;
  419. }
  420. //仪器表名和选中行
  421. syncParamas.SourceTable = cbxInstrument.Text.ToString();
  422. syncParamas.SourceField = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[0].Value.ToString();
  423. syncParamas.DataType = dgvInstruDS.Rows[dgvInstruDS.CurrentCell.RowIndex].Cells[1].Value.ToString().ToUpper();
  424. //CNAS表名和选中行
  425. syncParamas.TargetTable = cbxCnas.Text.ToString();
  426. syncParamas.TargetField = dgvCnas.Rows[dgvCnas.CurrentCell.RowIndex].Cells[0].Value.ToString();
  427. //验证数据合法性
  428. SyncParamsOperation paramsOperation = new SyncParamsOperation();
  429. if (paramsOperation.CheckTableIfRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.TargetTable))
  430. {
  431. MessageBox.Show("已存在不同表单映射数据,无法添加!");
  432. return;
  433. }
  434. //if (paramsOperation.CheckSourceFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.SourceTable, syncParamas.SourceField))
  435. //{
  436. // MessageBox.Show("仪器数据源字段已分配,请重新选择!");
  437. // return;
  438. //}
  439. if (paramsOperation.CheckTargetFieldRepeat(currentSyncItem.LstSyncPramas, syncParamas.TargetTable, syncParamas.TargetField))
  440. {
  441. MessageBox.Show("CNAS端数据字段已分配,请重新选择!");
  442. return;
  443. }
  444. if (paramsOperation.CheckTargetKeepField(syncParamas.TargetField))
  445. {
  446. MessageBox.Show("CNAS端数据字段为保留字段,请重新选择!");
  447. return;
  448. }
  449. //绑定数据
  450. currentSyncItem.LstSyncPramas.Add(syncParamas);
  451. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  452. //选中最后一行
  453. dgvMapping.CurrentCell = dgvMapping.Rows[dgvMapping.Rows.Count - 1].Cells[0];
  454. }
  455. /// <summary>
  456. /// 删除映射
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void btnDelMap_Click(object sender, EventArgs e)
  461. {
  462. //当前选中项
  463. if (dgvMapping.CurrentCell == null) return;
  464. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  465. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  466. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  467. if (lstDelItems.Count > 0)
  468. {
  469. foreach (var item in lstDelItems)
  470. {
  471. currentSyncItem.LstSyncPramas.Remove(item);
  472. }
  473. dgvMapping.DataSource = new BindingList<SyncParamasInfo>(currentSyncItem.LstSyncPramas);
  474. }
  475. }
  476. /// <summary>
  477. /// 配置数据库界面
  478. /// </summary>
  479. /// <param name="sender"></param>
  480. /// <param name="e"></param>
  481. private void btnDatabaseConfig_Click(object sender, EventArgs e)
  482. {
  483. frmDatabaseParams frmDatabase = new frmDatabaseParams(currentSyncItem);
  484. frmDatabase.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  485. {
  486. this.currentSyncItem = Instrumentitem;
  487. };
  488. frmDatabase.InstrumentItemData = delegate (Dictionary<string, DataTable> dict)
  489. {
  490. //this.dictInstruTables = dict;
  491. };
  492. frmDatabase.ShowDialog();
  493. //加载数据
  494. if ((currentSyncItem.SyncInstrumentDSInfo.Host != null && currentSyncItem.SyncInstrumentDSInfo.Host.Length > 0) || (currentSyncItem.SyncInstrumentDSInfo.Path != null && currentSyncItem.SyncInstrumentDSInfo.Path.Length > 0))
  495. btnLoadDBData_Click(null, null);
  496. }
  497. private void btnLoadDBData_Click(object sender, EventArgs e)
  498. {
  499. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  500. strTableInfoMode = FileOperation.GetSystemFormatConfigData(strInstrumentCode).TableInfoMode;
  501. if (strTableInfoMode == "1")
  502. {
  503. cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  504. //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  505. }
  506. int iReturn = 0;
  507. if (strTableInfoMode == "0")
  508. iReturn = LoadSourceAndTargetData(true);
  509. else
  510. iReturn = LoadSourceAndTargetData();
  511. switch (iReturn)
  512. {
  513. case -1:
  514. MessageBox.Show("未能成功获取设备数据库配置信息,请配置后重试!");
  515. break;
  516. case -2:
  517. MessageBox.Show("未能成功获取CNAS数据库配置信息,请配置后重试!");
  518. break;
  519. case -3:
  520. MessageBox.Show("未能成功获取仪器数据信息,请配置后重试!");
  521. break;
  522. case -4:
  523. MessageBox.Show("未能成功获取CNAS数据信息,请配置后重试!");
  524. break;
  525. case -5:
  526. DialogResult dr = MessageBox.Show("检测到数据连接配置已经修改,是否全部删除已经分配的字段映射?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
  527. if (dr == DialogResult.Yes)
  528. {
  529. //此时将全部删除已经分配的映射字段
  530. currentSyncItem.LstSyncPramas.Clear();
  531. currentSyncItem.lstFixedValue.Clear();
  532. dgvMapping.DataSource = new BindingList<SyncParamasInfo>();
  533. }
  534. break;
  535. case -6:
  536. MessageBox.Show("请先手动输入表名");
  537. break;
  538. case -7:
  539. MessageBox.Show("配置SQL查询没有结果,请检查SQL是否正确");
  540. break;
  541. default:
  542. break;
  543. }
  544. }
  545. /// <summary>
  546. /// 加载来源和目标数据的数据结构
  547. /// </summary>
  548. /// <param name="bIfLoading"></param>
  549. /// <returns></returns>
  550. public int LoadSourceAndTargetData(bool bIfLoading = false)
  551. {
  552. //检查配置信息
  553. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  554. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  555. bool bIfSameTable = true;
  556. //是否需要重新加载来源库
  557. if (bIfLoading)
  558. {
  559. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  560. dictInstruTables = instrumentData.GetInstrumentData();
  561. }
  562. if (dictInstruTables.Count <= 0) return -3;
  563. //绑定ComboBox
  564. List<string> lstTableName = new List<string>();
  565. foreach (var item in dictInstruTables)
  566. {
  567. lstTableName.Add(item.Key);
  568. }
  569. cbxInstrument.DataSource = lstTableName;
  570. cbxInstrument.ValueMember = "";
  571. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  572. {
  573. if (lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToUpper()) || lstTableName.Contains(currentSyncItem.LstSyncPramas[0].SourceTable.ToLower()))
  574. cbxInstrument.Text = currentSyncItem.LstSyncPramas[0].SourceTable;
  575. else
  576. bIfSameTable = false;
  577. }
  578. //获取CNAS配置的数据库连接信息
  579. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  580. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  581. {
  582. List<string> lstCnasTables = new List<string>();
  583. foreach (DataRow dr in dtCNAS.Rows)
  584. {
  585. if (dtCNAS.Columns.Contains("TABNAME"))
  586. lstCnasTables.Add(dr["TABNAME"].ToString());
  587. else if (dtCNAS.Columns.Contains("table_name"))
  588. lstCnasTables.Add(dr["table_name"].ToString());
  589. }
  590. cbxCnas.DataSource = lstCnasTables;
  591. cbxCnas.ValueMember = "";
  592. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  593. {
  594. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  595. {
  596. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  597. //cbxCnas_SelectedIndexChanged(null, null);
  598. }
  599. else
  600. bIfSameTable = false;
  601. }
  602. }
  603. else
  604. return -4;
  605. if (!bIfSameTable)
  606. {
  607. return -5;
  608. }
  609. return 1;
  610. }
  611. public AccessFormatConfig AccessFormat { get; set; }
  612. public ExcelFormatConfig ExcelFormat { get; set; }
  613. public FoxProFormatConfig FoxProFormat { get; set; }
  614. public SqliteFormatConfig SqliteFormat { get; set; }
  615. public SqlServerFormatConfig SqlServerFormat { get; set; }
  616. public OracleFormatConfig OracleFormat { get; set; }
  617. public MySqlFormatConfig MySqlFormat { get; set; }
  618. public NormalFileFormatConfig NormalFileFormat { get; set; }
  619. public PostgreSqlFormatConfig PostgreSqlFormat { get; set; }
  620. public KingbaseFormatConfig KingbaseFormat { get; set; }
  621. public DmFormatConfig DmFormat { get; set; }
  622. /// <summary>
  623. /// 手动输入表名时,获取该表的表信息
  624. /// </summary>
  625. /// <returns></returns>
  626. public int LoadSourceAndTargetData()
  627. {
  628. if (currentSyncItem.SyncInstrumentDSInfo == null) return -1;
  629. if (currentSyncItem.SyncTargetDBInfo == null) return -2;
  630. string sqlName = "";
  631. string sql = "";
  632. ExcelFormat = FileOperation.GetFormatConfigData<ExcelFormatConfig>("ExcelFormatConfig.xml");
  633. AccessFormat = FileOperation.GetFormatConfigData<AccessFormatConfig>("AccessFormatConfig.xml");
  634. FoxProFormat = FileOperation.GetFormatConfigData<FoxProFormatConfig>("FoxProFormatConfig.xml");
  635. SqliteFormat = FileOperation.GetFormatConfigData<SqliteFormatConfig>("SqliteFormatConfig.xml");
  636. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  637. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  638. NormalFileFormat = FileOperation.GetFormatConfigData<NormalFileFormatConfig>("NormalFileFormatConfig.xml");
  639. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  640. {
  641. case DataSourceType.MySQL:
  642. MySqlFormat = FileOperation.GetFormatConfigData<MySqlFormatConfig>("MySqlFormatConfig.xml");
  643. sqlName = MySqlFormat.AutoSql.MySqlViewName;
  644. sql = MySqlFormat.AutoSql.MySqlViewSql;
  645. break;
  646. case DataSourceType.Dm:
  647. DmFormat = FileOperation.GetFormatConfigData<DmFormatConfig>("DmFormatConfig.xml");
  648. sqlName = DmFormat.AutoSql.DmViewName;
  649. sql = DmFormat.AutoSql.DmViewSql;
  650. break;
  651. case DataSourceType.Oracle:
  652. OracleFormat = FileOperation.GetFormatConfigData<OracleFormatConfig>("OracleFormatConfig.xml");
  653. sqlName = OracleFormat.AutoSql.OracleViewName;
  654. sql = OracleFormat.AutoSql.OracleViewSql;
  655. break;
  656. case DataSourceType.PostgreSQL:
  657. PostgreSqlFormat = FileOperation.GetFormatConfigData<PostgreSqlFormatConfig>("PostgreSqlFormatConfig.xml");
  658. sqlName = PostgreSqlFormat.AutoSql.PostgreSqlViewName;
  659. sql = PostgreSqlFormat.AutoSql.PostgreSqlViewSql;
  660. break;
  661. case DataSourceType.SQL:
  662. SqlServerFormat = FileOperation.GetFormatConfigData<SqlServerFormatConfig>("SqlServerFormatConfig.xml");
  663. sqlName = SqlServerFormat.AutoSql.SqlServerViewName;
  664. sql = SqlServerFormat.AutoSql.SqlServerViewSql;
  665. break;
  666. case DataSourceType.Kingbase:
  667. KingbaseFormat = FileOperation.GetFormatConfigData<KingbaseFormatConfig>("KingbaseFormatConfig.xml");
  668. sqlName = KingbaseFormat.AutoSql.KingbaseViewName;
  669. sql = KingbaseFormat.AutoSql.KingbaseViewSql;
  670. break;
  671. default:
  672. break;
  673. }
  674. //cbxInstrument.Text = ExtractTableNames(sql);
  675. cbxInstrument.Text = sqlName;
  676. if (cbxInstrument.Text == "") return -6;
  677. int returnValue = 1;
  678. //根据手动输入来源库的表名加载字段
  679. InstrumentData instrumentData = InstrumentDataFact.CreateInstrumentDataSource(currentSyncItem.SyncInstrumentDSInfo, new object[] { "", "", "" });
  680. //dictInstruTables = instrumentData.GetInstrumentData();
  681. //dictInstruTables =
  682. DataTable dataTableStruct = null;
  683. string strTableName_Instru = cbxInstrument.Text.ToString();
  684. switch (currentSyncItem.SyncInstrumentDSInfo.InstrumentDataSourceType)
  685. {
  686. case DataSourceType.MySQL:
  687. dataTableStruct = SelectTableType.MySqlsecSD(sql);
  688. break;
  689. case DataSourceType.Dm:
  690. dataTableStruct = SelectTableType.DmSqlSD(sql);
  691. break;
  692. case DataSourceType.Oracle:
  693. dataTableStruct = SelectTableType.OrcSqlSD(sql, currentSyncItem);
  694. break;
  695. case DataSourceType.PostgreSQL:
  696. dataTableStruct = SelectTableType.PostgreSqlSD(sql);
  697. break;
  698. case DataSourceType.SQL:
  699. dataTableStruct = SelectTableType.SqlserversecSD(sql, currentSyncItem);
  700. break;
  701. case DataSourceType.Kingbase:
  702. if (cbxInstrument.Text.Contains('.'))
  703. cbxInstrument.Text = cbxInstrument.Text.Split('.')[1];
  704. dataTableStruct = SelectTableType.KingSql(sql);
  705. break;
  706. default:
  707. break;
  708. }
  709. DataTable dtInstruShow = new DataTable();
  710. dtInstruShow.Columns.Add("InstruFieldName");
  711. dtInstruShow.Columns.Add("InstruDataType");
  712. dtInstruShow.Columns.Add("remark");
  713. if (dataTableStruct != null)
  714. {
  715. for (int i = 0; i < dataTableStruct.Columns.Count; i++)
  716. {
  717. dtInstruShow.Rows.Add(new object[] { dataTableStruct.Columns[i].ColumnName, dataTableStruct.Columns[i].DataType });
  718. }
  719. }
  720. dgvInstruDS.DataSource = dtInstruShow;
  721. if (dataTableStruct != null && dataTableStruct.Columns.Count > 0)
  722. {
  723. dictInstruTables.Clear();
  724. dictInstruTables.Add(cbxInstrument.Text, dataTableStruct);
  725. //return 1;
  726. }
  727. else
  728. {
  729. returnValue = -7;
  730. return returnValue;
  731. }
  732. //获取CNAS配置的数据库连接信息
  733. DataTable dtCNAS = CnasDataOperationFact.CnasDataOperation().GetAllCNASTablesName(currentSyncItem.SyncTargetDBInfo);
  734. if (dtCNAS != null && dtCNAS.Rows.Count > 0)
  735. {
  736. List<string> lstCnasTables = new List<string>();
  737. foreach (DataRow dr in dtCNAS.Rows)
  738. {
  739. lstCnasTables.Add(dr["TABNAME"].ToString());
  740. }
  741. cbxCnas.DataSource = lstCnasTables;
  742. cbxCnas.ValueMember = "";
  743. if (currentSyncItem.LstSyncPramas != null && currentSyncItem.LstSyncPramas.Count > 0)
  744. {
  745. if (lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToUpper()) || lstCnasTables.Contains(currentSyncItem.LstSyncPramas[0].TargetTable.ToLower()))
  746. {
  747. cbxCnas.Text = currentSyncItem.LstSyncPramas[0].TargetTable;
  748. //cbxCnas_SelectedIndexChanged(null, null);
  749. }
  750. else
  751. returnValue = -5;
  752. }
  753. }
  754. else
  755. returnValue = -4;
  756. return returnValue;
  757. }
  758. public string ExtractTableNames(string sql)
  759. {
  760. // 预处理:去除注释、统一空格
  761. sql = Regex.Replace(sql, @"(--.*)|(\/\*[\s\S]*?\*\/)", "", RegexOptions.Multiline);
  762. sql = Regex.Replace(sql, @"\s+", " ");
  763. // 定义匹配模式
  764. var patterns = new Dictionary<string, string>
  765. {
  766. { "SELECT", @"(?:FROM|JOIN)\s+([\w\.]+)(?:\s+AS\s+\w+)?" },
  767. { "INSERT", @"INSERT\s+INTO\s+([\w\.]+)" },
  768. { "UPDATE", @"UPDATE\s+([\w\.]+)" },
  769. { "DELETE", @"DELETE\s+FROM\s+([\w\.]+)" }
  770. };
  771. var tables = new HashSet<string>();
  772. foreach (var pattern in patterns.Values)
  773. {
  774. var matches = Regex.Matches(sql, pattern, RegexOptions.IgnoreCase);
  775. foreach (Match match in matches)
  776. {
  777. if (match.Groups[1].Success)
  778. tables.Add(match.Groups[1].Value.Trim());
  779. }
  780. }
  781. if (tables.Count == 0)
  782. return "";
  783. else
  784. return tables.ToList()[0];
  785. }
  786. private void dgvMapping_CellValueChanged(object sender, DataGridViewCellEventArgs e)
  787. {
  788. if (dgvMapping.CurrentCell == null) return;
  789. if (dgvMapping.CurrentCell.ColumnIndex == 2) //此时修改的主健列
  790. {
  791. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  792. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  793. string strPrimaryKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["PrimaryKey"].Value.ToString();
  794. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  795. if (lstDelItems.Count > 0)
  796. {
  797. foreach (var item in lstDelItems)
  798. {
  799. item.IfPrimaryKey = strPrimaryKey.ToLower() == "true" ? true : false;
  800. }
  801. }
  802. }
  803. if (dgvMapping.CurrentCell.ColumnIndex == 3) //此时修改的是日期字段列
  804. {
  805. string strSourceField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["InstrumentField"].Value.ToString();
  806. string strTargetField = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["CnasField"].Value.ToString();
  807. string strDateKey = dgvMapping.Rows[dgvMapping.CurrentCell.RowIndex].Cells["DateKey"].Value.ToString();
  808. var lstDelItems = currentSyncItem.LstSyncPramas.Where(s => s.SourceField == strSourceField && s.TargetField == strTargetField).ToList<SyncParamasInfo>();
  809. if (lstDelItems.Count == 1)
  810. {
  811. if (strDateKey.ToLower() == "true")
  812. {
  813. //datagridview显示列(只能允许一条数据选择true,所以要把其他行的数据都置为false)
  814. foreach (DataGridViewRow dgvRow in dgvMapping.Rows)
  815. {
  816. if (dgvRow.Cells["DateKey"].Value.ToString().ToLower() == "true")
  817. dgvRow.Cells["DateKey"].Value = false;
  818. }
  819. //内存数据源
  820. foreach (var item in currentSyncItem.LstSyncPramas)
  821. {
  822. if (item.IfDateField)
  823. item.IfDateField = false;
  824. }
  825. lstDelItems[0].IfDateField = true;
  826. }
  827. else
  828. {
  829. lstDelItems[0].IfDateField = false;
  830. }
  831. }
  832. }
  833. }
  834. private void dgvMapping_CurrentCellDirtyStateChanged(object sender, EventArgs e)
  835. {
  836. if (dgvMapping.IsCurrentCellDirty)
  837. {
  838. dgvMapping.CommitEdit(DataGridViewDataErrorContexts.Commit);
  839. }
  840. }
  841. private void btnCNASFieldConfig_Click(object sender, EventArgs e)
  842. {
  843. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  844. {
  845. MessageBox.Show("请先指定至少一个映射字段。");
  846. return;
  847. }
  848. frmCNASValue frmCNAS = new frmCNASValue(currentSyncItem);
  849. if (frmCNAS.ShowDialog() == DialogResult.OK)
  850. {
  851. this.currentSyncItem = frmCNAS.syncInstrument;
  852. }
  853. }
  854. private void btnSourceFilter_Click(object sender, EventArgs e)
  855. {
  856. if (currentSyncItem == null || currentSyncItem.LstSyncPramas == null || currentSyncItem.LstSyncPramas.Count == 0)
  857. {
  858. MessageBox.Show("请先指定至少一个映射字段。");
  859. return;
  860. }
  861. frmSourceFilter frm = new frmSourceFilter(currentSyncItem, strTableInfoMode);
  862. frm.sourceDataFilterHandler = delegate (SourceDataFilter dataFilter)
  863. {
  864. currentSyncItem.SourceFilter = dataFilter;
  865. };
  866. frm.ShowDialog();
  867. }
  868. private void dgvInstrument_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  869. {
  870. //if (dgvInstrument.Rows.Count <= 0) return;
  871. ////当前选中单元格
  872. //string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  873. //frmSystemSetting frmSetting = new frmSystemSetting(lstSyncInstrument, strInstrumentCode);
  874. //frmSetting.InstrumentDelegate = delegate (SyncInstrumentItemInfo Instrumentitem)
  875. //{
  876. // lstSyncInstrument.Add(Instrumentitem);
  877. // //绑定数据
  878. // dgvInstrument.DataSource = new BindingList<SyncInstrumentItemInfo>(lstSyncInstrument);
  879. // dgvInstrument.CurrentCell = dgvInstrument.Rows[dgvInstrument.Rows.Count - 1].Cells[0];
  880. // dgvInstrument_SelectionChanged(null, null);
  881. //};
  882. //frmSetting.ShowDialog();
  883. }
  884. private void cbxCNASColumn_SelectedIndexChanged(object sender, EventArgs e)
  885. {
  886. if (cbxCNASColumn.Visible)
  887. {
  888. cbxCNASColumn.Visible = false;
  889. txtInstrumentColumn.Visible = true;
  890. currentSyncItem.CnasInstrumentColumn = txtInstrumentColumn.Text = cbxCNASColumn.Text;
  891. }
  892. }
  893. /// <summary>
  894. /// 当双击textBox时,显示ComboBOX用于选择
  895. /// </summary>
  896. /// <param name="sender"></param>
  897. /// <param name="e"></param>
  898. private void txtInstrumentColumn_DoubleClick(object sender, EventArgs e)
  899. {
  900. txtInstrumentColumn.Visible = false;
  901. cbxCNASColumn.Top = txtInstrumentColumn.Top;
  902. cbxCNASColumn.Height = txtInstrumentColumn.Height;
  903. cbxCNASColumn.Width = txtInstrumentColumn.Width;
  904. cbxCNASColumn.Location = txtInstrumentColumn.Location;
  905. cbxCNASColumn.Visible = true;
  906. }
  907. private void cbxCNASColumn_Leave(object sender, EventArgs e)
  908. {
  909. if (cbxCNASColumn.Visible)
  910. {
  911. txtInstrumentColumn.Visible = true;
  912. cbxCNASColumn.Visible = false;
  913. }
  914. }
  915. private void tsmServiceSetting_Click(object sender, EventArgs e)
  916. {
  917. frmServiceConfig frmServiceConfig = new frmServiceConfig();
  918. frmServiceConfig.ShowDialog();
  919. }
  920. private void tsmSystemSetting_Click(object sender, EventArgs e)
  921. {
  922. //frmSystemSetting frmSystem = new frmSystemSetting();
  923. //frmSystem.ShowDialog();
  924. //strTableInfoMode = FileOperation.GetSystemFormatConfigData().TableInfoMode;
  925. //if (strTableInfoMode == "1")
  926. //{
  927. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDown;
  928. // //cbxCnas.DropDownStyle = ComboBoxStyle.DropDown;
  929. //}
  930. //else
  931. //{
  932. // cbxInstrument.DropDownStyle = ComboBoxStyle.DropDownList;
  933. //}
  934. }
  935. private void tsmSourceSetting_Click(object sender, EventArgs e)
  936. {
  937. frmSourceSetting frmSource = new frmSourceSetting();
  938. frmSource.ShowDialog();
  939. }
  940. private void tsmHelper_Click(object sender, EventArgs e)
  941. {
  942. string strHelpFilePath = FileHelper.getBasePath() + @"\Helper.CHM";
  943. //Help.ShowHelp(null, strHelpFilePath, HelpNavigator.TopicId, "1");
  944. //Help.ShowHelpIndex(this, strHelpFilePath);
  945. System.Diagnostics.Process.Start(strHelpFilePath);
  946. }
  947. /// <summary>
  948. /// 右键事件
  949. /// </summary>
  950. /// <param name="sender"></param>
  951. /// <param name="e"></param>
  952. private void dgvInstrument_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
  953. {
  954. if (e.Button == MouseButtons.Right)
  955. {
  956. string strInstrumentCode = this.dgvInstrument.Rows[this.dgvInstrument.CurrentRow.Index].Cells[0].Value.ToString();
  957. if (dgvInstrument.Rows.Count <= 0) return;
  958. //当前选中单元格
  959. if (strInstrumentCode != "")
  960. {
  961. // 显示上下文菜单
  962. ContextMenuStrip contextMenu = new ContextMenuStrip();
  963. contextMenu.Items.Add("编辑", null, new EventHandler(Option1_Click));
  964. contextMenu.Show(dgvInstrument, new Point(e.X, e.Y));
  965. }
  966. }
  967. }
  968. }
  969. }